Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36 changes: 19 additions & 17 deletions src/main/java/dev/dubhe/anvilcraft/AnvilCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import dev.dubhe.anvilcraft.init.recipe.ModRecipeTypes;
import dev.dubhe.anvilcraft.init.recipe.ModResultModifierTypes;
import dev.dubhe.anvilcraft.init.storage.ModCategoryTypes;
import dev.dubhe.anvilcraft.item.utility.DiskItem;
import dev.dubhe.anvilcraft.mixin.invoker.BaseMappedRegistryInvoker;
import lombok.Getter;
import net.minecraft.core.registries.BuiltInRegistries;
Expand All @@ -65,26 +66,27 @@
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Mod(AnvilCraft.MOD_ID)
public class AnvilCraft {
public static final String MOD_ID = "anvilcraft";
public static final String MOD_NAME = "AnvilCraft";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);
public static final Logger LOGGER = LoggerFactory.getLogger(AnvilCraft.MOD_NAME);
public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
public static IEventBus MOD_BUS = null;
public static @Nullable IEventBus MOD_BUS = null;
public static final AnvilCraftServerConfig CONFIG = ConfigManager.register(AnvilCraft.MOD_ID, AnvilCraftServerConfig::new);
public static final AnvilCraftClientConfig CLIENT_CONFIG = ConfigManager.register(AnvilCraft.MOD_ID, AnvilCraftClientConfig::new);

@Getter
private static final IntegrationManager INTEGRATION_MANAGER = new IntegrationManager(AnvilCraft.MOD_ID);

public static final Registrum REGISTRUM = Registrum.create(MOD_ID).defaultCreativeTab((ResourceKey<CreativeModeTab>) null);
public static final Registrum REGISTRUM = Registrum.create(AnvilCraft.MOD_ID).defaultCreativeTab((ResourceKey<CreativeModeTab>) null);

public AnvilCraft(IEventBus modEventBus, ModContainer modContainer) {
MOD_BUS = modEventBus;
AnvilCraft.MOD_BUS = modEventBus;
NeoForgeMod.enableMilkFluid();
ModAttachments.register(modEventBus);
ModItemGroups.register(modEventBus);
Expand Down Expand Up @@ -123,16 +125,16 @@ public AnvilCraft(IEventBus modEventBus, ModContainer modContainer) {
// datagen
AnvilCraftDatagen.init();

registerEvents(modEventBus);
AnvilCraft.registerEvents(modEventBus);
StartupNotificationManager.addModMessage("[AnvilCraft] Loading Integrations");
IntegrationHook.setModEventBus(modEventBus);
IntegrationHook.setModContainer(modContainer);
INTEGRATION_MANAGER.compileContent();
INTEGRATION_MANAGER.loadAllIntegrations();
AnvilCraft.INTEGRATION_MANAGER.compileContent();
AnvilCraft.INTEGRATION_MANAGER.loadAllIntegrations();
StartupNotificationManager.addModMessage("[AnvilCraft] Ciallo~");
AnvilCraftDfu.constructAndOptimize();
LOGGER.info("Ciallo~(∠・ω< )⌒★");
LOGGER.info("let's 0721");
AnvilCraft.LOGGER.info("Ciallo~(∠・ω< )⌒★");
AnvilCraft.LOGGER.info("let's 0721");

ModRecipeInits.init(modEventBus);

Expand All @@ -142,7 +144,7 @@ public AnvilCraft(IEventBus modEventBus, ModContainer modContainer) {

private static void registerEvents(IEventBus eventBus) {
NeoForge.EVENT_BUS.addListener(AnvilCraft::registerCommand);
NeoForge.EVENT_BUS.addListener(dev.dubhe.anvilcraft.item.utility.DiskItem::onBlockPlaced);
NeoForge.EVENT_BUS.addListener(DiskItem::onBlockPlaced);

eventBus.addListener(AnvilCraft::registerPayload);
eventBus.addListener(AnvilCraft::loadComplete);
Expand All @@ -151,15 +153,15 @@ private static void registerEvents(IEventBus eventBus) {
}

public static Identifier of(String path) {
return Identifier.fromNamespaceAndPath(MOD_ID, path);
return Identifier.fromNamespaceAndPath(AnvilCraft.MOD_ID, path);
}

public static Identifier advancement(String path) {
return of("anvilcraft/" + path);
return AnvilCraft.of("anvilcraft/" + path);
}

public static String recipe(String path) {
return MOD_ID + ':' + path;
return AnvilCraft.MOD_ID + ':' + path;
}

public static void registerCommand(RegisterCommandsEvent event) {
Expand All @@ -175,13 +177,13 @@ public static void loadComplete(FMLLoadCompleteEvent event) {
event.enqueueWork(() -> {
ModDispenserBehavior.register();
if (Util.isLoaded("apothic_enchanting")) {
LOGGER.info(
AnvilCraft.LOGGER.info(
"Apothic Enchanting found. Set royalAnvilBeyondMaxLevel, "
+ "emberAnvilBeyondMaxLevel and transcendenceAnvilBeyondMaxLevel to true."
);
CONFIG.royalAnvilBeyondMaxLevel = true;
CONFIG.emberAnvilBeyondMaxLevel = true;
CONFIG.transcendenceAnvilBeyondMaxLevel = true;
AnvilCraft.CONFIG.royalAnvilBeyondMaxLevel = true;
AnvilCraft.CONFIG.emberAnvilBeyondMaxLevel = true;
AnvilCraft.CONFIG.transcendenceAnvilBeyondMaxLevel = true;
}
Util.<BaseMappedRegistryInvoker>cast(BuiltInRegistries.DATA_COMPONENT_PREDICATE_TYPE).invokeSetSync(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Criterion<TriggerInstance> hurtEntity(float damage) {
}

public boolean matches(Float damage) {
if (damage().isPresent()) {
if (this.damage().isPresent()) {
return damage >= this.damage.get();
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public record TriggerInstance(Optional<ContextAwarePredicate> player, Optional<B
).apply(instance, TriggerInstance::new));

public static Criterion<TriggerInstance> devourBlock(Block block) {
return devourBlock(BlockPredicate.Builder.block().of(block));
return TriggerInstance.devourBlock(BlockPredicate.Builder.block().of(block));
}

public static Criterion<TriggerInstance> devourBlock(BlockPredicate.Builder block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record TriggerInstance(
).apply(instance, TriggerInstance::new));

public static Criterion<TriggerInstance> placeBlock(Block block) {
return placeBlock(BlockPredicate.Builder.block().of(block));
return TriggerInstance.placeBlock(BlockPredicate.Builder.block().of(block));
}

public static Criterion<TriggerInstance> placeBlock(BlockPredicate.Builder block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
public class BeaconConversionBehavior implements IAnvilBehavior {
private static final Int2DoubleOpenHashMap map = new Int2DoubleOpenHashMap() {
{
put(1, 0.02F);
put(2, 0.05F);
put(3, 0.2F);
put(4, 1F);
this.put(1, 0.02F);
this.put(2, 0.05F);
this.put(3, 0.2F);
this.put(4, 1F);
}
};

Expand All @@ -50,7 +50,7 @@ public boolean handle(ServerLevel level, BlockPos hitBlockPos, BlockState hitBlo
itemEntity.setItem(stack);
for (int i = 1; i <= 4; i++) {
if (beaconLevel == i) {
if (level.getRandom().nextDouble() < map.get(i)) {
if (level.getRandom().nextDouble() < BeaconConversionBehavior.map.get(i)) {
level.setBlockAndUpdate(hitBlockPos, ModBlocks.CORRUPTED_BEACON.getDefaultState());
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void spawnEntities(
BaseSpawnerAccessor accessor
) {
for (int c = 0; c < accessor.getSpawnCount(); c++) {
try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this::toString, log)) {
try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this::toString, HitSpawnerBehavior.log)) {
ValueInput input = TagValueInput.create(reporter, level.registryAccess(), spawnData.getEntityToSpawn());
Optional<EntityType<?>> entityType = EntityType.by(input);
if (entityType.isEmpty()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.dubhe.anvilcraft.init.block.ModBlocks;
import dev.dubhe.anvilcraft.init.entity.ModDamageTypes;
import dev.dubhe.anvilcraft.util.CauldronUtil;
import dev.dubhe.anvilcraft.util.EntityUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -25,8 +26,7 @@ public boolean handle(ServerLevel level, BlockPos hitBlockPos, BlockState hitBlo
);
if (players.isEmpty()) return false;
for (ServerPlayer player : players) {
// noinspection deprecation
player.hurtOrSimulate(ModDamageTypes.lostInTime(level), Float.MAX_VALUE);
EntityUtil.hurtOrSimulate(player, ModDamageTypes.lostInTime(level), Float.MAX_VALUE);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static InteractionResult tryPlace(
if (player.isShiftKeyDown() || !player.mayBuild()) return InteractionResult.PASS;
ItemStack itemInHand = player.getItemInHand(hand);
if (itemInHand.is(blockItem)) {
for (Direction direction : orderDirectionByDistance(
for (Direction direction : BlockPlaceAssist.orderDirectionByDistance(
pos,
hit.getLocation(),
dir -> dir.getAxis() == state.getValue(propertyDef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RipeningManager {

/// 获取或新建一个当前维度催熟实例。
public static RipeningManager from(Level level) {
return INSTANCES.computeIfAbsent(level, RipeningManager::new);
return RipeningManager.INSTANCES.computeIfAbsent(level, RipeningManager::new);
}

public RipeningManager(Level level) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/dev/dubhe/anvilcraft/api/SpawningManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class SpawningManager {
/// @return 与指定世界关联的 SpawningManager 实例
/// @see Map#computeIfAbsent(Object, java.util.function.Function)
public static SpawningManager getInstance(Level level) {
return INSTANCES.computeIfAbsent(level, SpawningManager::new);
return SpawningManager.INSTANCES.computeIfAbsent(level, SpawningManager::new);
}

private SpawningManager(Level level) {
Expand Down Expand Up @@ -169,10 +169,10 @@ private static void blockEntitySummon(MobSpawnEvent.PositionCheck event) {
}
Entity entity = event.getEntity();
Level level = entity.level();
SpawningManager spawningManager = getInstance(level);
SpawningManager spawningManager = SpawningManager.getInstance(level);

if (!ignoreSummonMob(level, event, spawningManager.animalLightBlockSet, true)) {
ignoreSummonMob(level, event, spawningManager.nonAnimalLightBlockSet, false);
if (!SpawningManager.ignoreSummonMob(level, event, spawningManager.animalLightBlockSet, true)) {
SpawningManager.ignoreSummonMob(level, event, spawningManager.nonAnimalLightBlockSet, false);
}
}
}
26 changes: 17 additions & 9 deletions src/main/java/dev/dubhe/anvilcraft/api/amulet/AmuletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ public class AmuletManager {
private static @Nullable SoftReference<AmuletManager> INSTANCE;

public static AmuletManager get(HolderLookup.Provider registries) {
if (AmuletManager.INSTANCE == null || AmuletManager.INSTANCE.get() == null) {
AmuletManager.INSTANCE = new SoftReference<>(new AmuletManager(AmuletManager.extractDefinitions(registries)));
SoftReference<AmuletManager> reference = AmuletManager.INSTANCE;
AmuletManager manager = reference == null ? null : reference.get();
if (manager == null) {
manager = new AmuletManager(AmuletManager.extractDefinitions(registries));
AmuletManager.INSTANCE = new SoftReference<>(manager);
}
return AmuletManager.INSTANCE.get();
return manager;
}

public static List<Holder.Reference<IAmuletDefinition>> extractDefinitions(HolderLookup.Provider registries) {
Expand Down Expand Up @@ -144,7 +147,10 @@ public static int getStoredRaffleProbability(Player player, Holder<IAmuletDefini

public boolean hasAmuletInInventory(Player player, IAmulet amulet) {
List<ItemStack> amulets = this.getAmuletsFromInventory(player);
return CollectionUtil.anyMatch(amulets, stack -> stack.get(ModComponents.AMULET).canActAs(amulet));
return CollectionUtil.anyMatch(
amulets,
stack -> stack.getOrDefault(ModComponents.AMULET, DoNothingAmulet.INSTANCE).canActAs(amulet)
);
}

public boolean hasAmuletInInventory(Player player, Holder<IAmuletDefinition> def) {
Expand All @@ -169,12 +175,14 @@ public void inventoryTick(ServerPlayer player) {
}
List<ItemStack> now = this.getAmuletsFromInventory(player);
for (ItemStack stack : now) {
IAmulet amulet = stack.get(ModComponents.AMULET);
all.removeIf(other -> amulet.canActAs(other.get(ModComponents.AMULET)));
stack.get(ModComponents.AMULET).inventoryTick(player, stack, true);
IAmulet amulet = stack.getOrDefault(ModComponents.AMULET, DoNothingAmulet.INSTANCE);
all.removeIf(other -> amulet.canActAs(
other.getOrDefault(ModComponents.AMULET, DoNothingAmulet.INSTANCE)
));
amulet.inventoryTick(player, stack, true);
}
for (ItemStack stack : all) {
IAmulet amulet = stack.get(ModComponents.AMULET);
IAmulet amulet = stack.getOrDefault(ModComponents.AMULET, DoNothingAmulet.INSTANCE);
if (amulet instanceof WrappedOthersAmulet) return;
amulet.inventoryTick(player, stack, false);
}
Expand All @@ -183,7 +191,7 @@ public void inventoryTick(ServerPlayer player) {
public boolean shouldImmune(ServerPlayer player, DamageSource source) {
return CollectionUtil.anyMatch(
this.getAmuletsFromInventory(player),
stack -> stack.get(ModComponents.AMULET).shouldImmune(player, source)
stack -> stack.getOrDefault(ModComponents.AMULET, DoNothingAmulet.INSTANCE).shouldImmune(player, source)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IAmuletDefinition {
.byNameCodec()
.dispatch(IAmuletDefinition::getType, Type::codec);
Codec<Holder<IAmuletDefinition>> CODEC = RegistryFileCodec.create(ModRegistryKeys.AMULET_DEF, IAmuletDefinition.DIRECT_CODEC);
Codec<IAmuletDefinition> HOLDER_HELPER_CODEC = CODEC.xmap(
Codec<IAmuletDefinition> HOLDER_HELPER_CODEC = IAmuletDefinition.CODEC.xmap(
HolderHolder::new,
value -> value instanceof HolderHolder(Holder<IAmuletDefinition> def) ? def : Holder.direct(value)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ default int priority() {
}

static void registerBehavior(Block matchingBlock, IAnvilBehavior behavior) {
BEHAVIORS.put(it -> it.is(matchingBlock), behavior);
IAnvilBehavior.BEHAVIORS.put(it -> it.is(matchingBlock), behavior);
}

static void registerBehavior(Predicate<BlockState> pred, IAnvilBehavior behavior) {
BEHAVIORS.put(pred, behavior);
IAnvilBehavior.BEHAVIORS.put(pred, behavior);
}

static @Unmodifiable List<IAnvilBehavior> findMatching(BlockState state) {
return BEHAVIORS.keySet().stream()
return IAnvilBehavior.BEHAVIORS.keySet().stream()
.filter(it -> it.test(state))
.map(BEHAVIORS::get)
.map(IAnvilBehavior.BEHAVIORS::get)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import org.joml.Vector3f;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -29,10 +30,10 @@ public ChargeCollectorManager(Level level) {

/// 获取当前维度的ChargeCollectorManager
public static ChargeCollectorManager getInstance(Level level) {
if (!INSTANCES.containsKey(level)) {
INSTANCES.put(level, new ChargeCollectorManager(level));
if (!ChargeCollectorManager.INSTANCES.containsKey(level)) {
ChargeCollectorManager.INSTANCES.put(level, new ChargeCollectorManager(level));
}
return INSTANCES.get(level);
return ChargeCollectorManager.INSTANCES.get(level);
}

/// 充电
Expand All @@ -54,12 +55,12 @@ public void charge(double chargeNum, BlockPos blockPos) {
double surplus = chargeNum;
for (Entry entry : chargeCollectorCollection) {
if (entry.isInfinite()) {
InfiniteCollectorBlockEntity ic = entry.getInfiniteCollector();
if (!this.canCollect(ic, blockPos)) continue;
InfiniteCollectorBlockEntity ic = entry.infiniteCollector();
if (ic == null || !this.canCollect(ic, blockPos)) continue;
surplus = ic.incomingCharge(surplus, blockPos);
} else {
ChargeCollectorBlockEntity cc = entry.getChargeCollector();
if (!this.canCollect(cc, blockPos)) continue;
ChargeCollectorBlockEntity cc = entry.chargeCollector();
if (cc == null || !this.canCollect(cc, blockPos)) continue;
surplus = cc.incomingCharge(surplus, blockPos);
}
if (surplus == 0) return;
Expand Down Expand Up @@ -112,7 +113,7 @@ public List<Entry> getNearestChargeCollect(BlockPos blockPos) {
distanceList.add(new Entry(distance, null, entry.getValue()));
}
return distanceList.stream()
.sorted(Comparator.comparing(Entry::getDistance))
.sorted(Comparator.comparing(Entry::distance))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -145,17 +146,11 @@ public boolean canCollect(InfiniteCollectorBlockEntity blockEntity, BlockPos blo
&& blockEntity.getPos().getZ() + range >= blockPos.getZ();
}

@Getter
public static class Entry {
public final double distance;
public final ChargeCollectorBlockEntity chargeCollector;
public final InfiniteCollectorBlockEntity infiniteCollector;

public Entry(double distance, ChargeCollectorBlockEntity chargeCollector, InfiniteCollectorBlockEntity infiniteCollector) {
this.distance = distance;
this.chargeCollector = chargeCollector;
this.infiniteCollector = infiniteCollector;
}
public record Entry(
double distance,
@Nullable ChargeCollectorBlockEntity chargeCollector,
@Nullable InfiniteCollectorBlockEntity infiniteCollector
) {

public boolean isInfinite() {
return this.infiniteCollector != null;
Expand Down
Loading
Loading