From f1305074fb5fef9cd4857a81dbe5c8a163e8fb28 Mon Sep 17 00:00:00 2001 From: ZhuRuoLing Date: Sun, 23 Jun 2024 16:39:00 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=82=AC=E7=86=9F?= =?UTF-8?q?=E5=8D=A1=E7=88=86=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubhe/anvilcraft/api/BonemealManager.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java index 08cc5e139..782ad677d 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java @@ -56,8 +56,8 @@ private static HashSet> doRipen( BlockPos pos1 = pos.offset(i, j, k); BlockState state = level.getBlockState(pos1); if (state.getBlock() instanceof BonemealableBlock growable - && !growable.getClass().equals(GrassBlock.class) - && !growable.getClass().equals(NyliumBlock.class) + && !(growable instanceof GrassBlock) + && !(growable instanceof NyliumBlock) && !isInSet(set, new Tuple<>(pos1, level)) && growable.isValidBonemealTarget(level, pos1, state, false) && level.getBrightness(LightLayer.BLOCK, pos1) >= 10 @@ -91,16 +91,19 @@ public static void tick() { Iterator> it = lightBlocks.iterator(); while (it.hasNext()) { Tuple lightBlock = it.next(); - BlockState lightBlockState = lightBlock.getB().getBlockState(lightBlock.getA()); - if (lightBlockState.getBlock() instanceof InductionLightBlock) { - if (isLighting(lightBlockState) && canCropGrow(lightBlockState)) { - HashSet> newRipened = - doRipen(lightBlock.getB(), lightBlock.getA(), ripenedBlocks); - ripenedBlocks.addAll(newRipened); + ServerLevel level = (ServerLevel) lightBlock.getB(); + level.getServer().execute(() -> { + BlockState lightBlockState = lightBlock.getB().getBlockState(lightBlock.getA()); + if (lightBlockState.getBlock() instanceof InductionLightBlock) { + if (isLighting(lightBlockState) && canCropGrow(lightBlockState)) { + HashSet> newRipened = + doRipen(lightBlock.getB(), lightBlock.getA(), ripenedBlocks); + ripenedBlocks.addAll(newRipened); + } + } else { + it.remove(); } - } else { - it.remove(); - } + }); } } From e3bcb2a4375b29932cbff7b4734876c94625ddd7 Mon Sep 17 00:00:00 2001 From: ZhuRuoLing Date: Sun, 23 Jun 2024 16:55:31 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BC=A0=E9=80=81?= =?UTF-8?q?=E5=88=B0=E5=85=B6=E4=BB=96=E7=BB=B4=E5=BA=A6=E6=88=96=E5=8D=B8?= =?UTF-8?q?=E8=BD=BD=E5=AF=BC=E8=87=B4=E9=9B=86=E7=94=B5=E5=99=A8=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChargeCollectorManager.java | 58 ++++++++++++------- .../api/chargecollector/ThermoManager.java | 3 +- .../block/PiezoelectricCrystalBlock.java | 24 +++++--- .../event/LightningEventListener.java | 17 +++--- .../event/PistonMoveBlockListener.java | 4 +- .../ServerBlockEntityEventListener.java | 4 +- .../fabric/ServerWorldEventListener.java | 1 - .../event/forge/LevelEventListener.java | 1 - .../forge/ServerBlockEntityEventListener.java | 4 +- 9 files changed, 70 insertions(+), 46 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ChargeCollectorManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ChargeCollectorManager.java index f340881a3..182adefa3 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ChargeCollectorManager.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ChargeCollectorManager.java @@ -1,6 +1,7 @@ package dev.dubhe.anvilcraft.api.chargecollector; import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity; + import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -8,40 +9,59 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; + import lombok.Getter; import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; import org.joml.Vector3f; public class ChargeCollectorManager { - private static final Map CHARGE_COLLECTOR_MAP = new HashMap<>(); + private final Map chargeCollectors = new HashMap<>(); + private static final Map INSTANCES = new HashMap<>(); + + private final Level level; + + public ChargeCollectorManager(Level level) { + this.level = level; + } + + /** + * 获取当前维度的ChargeCollectorManager + */ + public static ChargeCollectorManager getInstance(Level level) { + if (!INSTANCES.containsKey(level)) { + INSTANCES.put(level, new ChargeCollectorManager(level)); + } + return INSTANCES.get(level); + } /** * 添加新的集电器 */ - public static void addChargeCollector(ChargeCollectorBlockEntity blockEntity) { - CHARGE_COLLECTOR_MAP.put(blockEntity.getBlockPos(), blockEntity); + public void addChargeCollector(ChargeCollectorBlockEntity blockEntity) { + chargeCollectors.put(blockEntity.getBlockPos(), blockEntity); } /** * 删除集电器 */ - public static void removeChargeCollector(ChargeCollectorBlockEntity blockEntity) { - CHARGE_COLLECTOR_MAP.remove(blockEntity.getBlockPos()); + public void removeChargeCollector(ChargeCollectorBlockEntity blockEntity) { + chargeCollectors.remove(blockEntity.getBlockPos()); } /** * 获取最近的集电器的Collection集合(以从近至远排序) */ - public static Collection getNearestChargeCollect(BlockPos blockPos) { + public Collection getNearestChargeCollect(BlockPos blockPos) { List distanceList = new ArrayList<>(); - for (Map.Entry entry : CHARGE_COLLECTOR_MAP.entrySet()) { + for (Map.Entry entry : chargeCollectors.entrySet()) { double distance = Vector3f.distance( - entry.getKey().getX(), entry.getKey().getY(), entry.getKey().getZ(), - blockPos.getX(), blockPos.getY(), blockPos.getZ()); + entry.getKey().getX(), entry.getKey().getY(), entry.getKey().getZ(), + blockPos.getX(), blockPos.getY(), blockPos.getZ()); distanceList.add(new Entry(distance, entry.getValue())); } return distanceList.stream() - .sorted(Comparator.comparing(Entry::getDistance)).collect(Collectors.toList()); + .sorted(Comparator.comparing(Entry::getDistance)).collect(Collectors.toList()); } /** @@ -51,21 +71,17 @@ public static Collection getNearestChargeCollect(BlockPos blockPos) { * @param blockPos 电荷的位置 * @return 是否能被集点器收集 */ - public static boolean canCollect(ChargeCollectorBlockEntity blockEntity, BlockPos blockPos) { + public boolean canCollect(ChargeCollectorBlockEntity blockEntity, BlockPos blockPos) { return blockEntity.getPos().getX() - 2 <= blockPos.getX() - && blockEntity.getPos().getY() - 2 <= blockPos.getY() - && blockEntity.getPos().getZ() - 2 <= blockPos.getZ() - && blockEntity.getPos().getX() + 2 >= blockPos.getX() - && blockEntity.getPos().getY() + 2 >= blockPos.getY() - && blockEntity.getPos().getZ() + 2 >= blockPos.getZ(); - } - - public static void cleanMap() { - CHARGE_COLLECTOR_MAP.clear(); + && blockEntity.getPos().getY() - 2 <= blockPos.getY() + && blockEntity.getPos().getZ() - 2 <= blockPos.getZ() + && blockEntity.getPos().getX() + 2 >= blockPos.getX() + && blockEntity.getPos().getY() + 2 >= blockPos.getY() + && blockEntity.getPos().getZ() + 2 >= blockPos.getZ(); } @Getter - public static class Entry { + public class Entry { public final double distance; public final ChargeCollectorBlockEntity blockEntity; diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ThermoManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ThermoManager.java index a5c3c78f4..20a307aef 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ThermoManager.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/chargecollector/ThermoManager.java @@ -140,11 +140,12 @@ private void tickThis() { private void charge(int chargeNum, BlockPos blockPos) { Collection chargeCollectorCollection = ChargeCollectorManager + .getInstance(level) .getNearestChargeCollect(blockPos); double surplus = chargeNum; for (ChargeCollectorManager.Entry entry : chargeCollectorCollection) { ChargeCollectorBlockEntity chargeCollectorBlockEntity = entry.getBlockEntity(); - if (!ChargeCollectorManager.canCollect(chargeCollectorBlockEntity, blockPos)) return; + if (!ChargeCollectorManager.getInstance(level).canCollect(chargeCollectorBlockEntity, blockPos)) return; surplus = chargeCollectorBlockEntity.incomingCharge(surplus); if (surplus == 0) return; } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/PiezoelectricCrystalBlock.java b/common/src/main/java/dev/dubhe/anvilcraft/block/PiezoelectricCrystalBlock.java index 0a3d97a29..53ffd6fdc 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/PiezoelectricCrystalBlock.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/PiezoelectricCrystalBlock.java @@ -4,8 +4,10 @@ import dev.dubhe.anvilcraft.api.chargecollector.ChargeCollectorManager.Entry; import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable; import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity; + import java.util.Collection; import javax.annotation.Nonnull; + import net.minecraft.core.BlockPos; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; @@ -19,9 +21,9 @@ public class PiezoelectricCrystalBlock extends Block implements IHammerRemovable { public static VoxelShape SHAPE = Shapes.or( - Block.box(0, 14, 0, 16, 16, 16), - Block.box(2, 2, 2, 14, 14, 14), - Block.box(0, 0, 0, 16, 2, 16) + Block.box(0, 14, 0, 16, 16, 16), + Block.box(2, 2, 2, 14, 14, 14), + Block.box(0, 0, 0, 16, 2, 16) ); public PiezoelectricCrystalBlock(Properties properties) { @@ -37,7 +39,10 @@ public PiezoelectricCrystalBlock(Properties properties) { @SuppressWarnings("deprecation") @Override public @NotNull VoxelShape getShape( - @NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context + @NotNull BlockState state, + @NotNull BlockGetter level, + @NotNull BlockPos pos, + @NotNull CollisionContext context ) { return SHAPE; } @@ -48,17 +53,18 @@ public PiezoelectricCrystalBlock(Properties properties) { public void onHitByAnvil(float fallDistance, Level level, BlockPos blockPos) { int distance = (int) Math.min(3, fallDistance); int chargeNum = (int) Math.pow(2, distance); - this.charge(chargeNum, blockPos); + this.charge(chargeNum, level, blockPos); pressureConduction(level, blockPos, chargeNum / 2, 4); } - private void charge(int chargeNum, BlockPos blockPos) { + private void charge(int chargeNum, Level level, BlockPos blockPos) { Collection chargeCollectorCollection = ChargeCollectorManager - .getNearestChargeCollect(blockPos); + .getInstance(level) + .getNearestChargeCollect(blockPos); double surplus = chargeNum; for (Entry entry : chargeCollectorCollection) { ChargeCollectorBlockEntity chargeCollectorBlockEntity = entry.getBlockEntity(); - if (!ChargeCollectorManager.canCollect(chargeCollectorBlockEntity, blockPos)) return; + if (!ChargeCollectorManager.getInstance(level).canCollect(chargeCollectorBlockEntity, blockPos)) return; surplus = chargeCollectorBlockEntity.incomingCharge(surplus); if (surplus == 0) return; } @@ -70,7 +76,7 @@ private void pressureConduction(Level level, BlockPos blockPos, int chargeNum, i BlockPos pos = blockPos.below(); if (level.getBlockState(pos).getBlock() instanceof PiezoelectricCrystalBlock block) { if (chargeNum == 0) return; - this.charge(chargeNum, blockPos); + this.charge(chargeNum, level, blockPos); block.pressureConduction(level, pos, chargeNum / 2, c); } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/event/LightningEventListener.java b/common/src/main/java/dev/dubhe/anvilcraft/event/LightningEventListener.java index 0036b6178..757b051ff 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/event/LightningEventListener.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/event/LightningEventListener.java @@ -7,8 +7,11 @@ import dev.dubhe.anvilcraft.api.event.entity.LightningStrikeEvent; import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity; import dev.dubhe.anvilcraft.init.ModBlocks; + import java.util.Collection; + import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; @@ -24,7 +27,7 @@ public class LightningEventListener { public void onLightingStrike(@NotNull LightningStrikeEvent event) { BlockPos pos = event.getPos(); BlockState state = event.getLevel().getBlockState(pos); - lightningCharge(pos, state); + lightningCharge(pos, event.getLevel(), state); if (state.is(Blocks.LIGHTNING_ROD)) pos = pos.below(); int depth = AnvilCraft.config.lightningStrikeDepth; int radius = AnvilCraft.config.lightningStrikeRadius; @@ -34,9 +37,9 @@ public void onLightingStrike(@NotNull LightningStrikeEvent event) { BlockPos offset = pos.offset(x, -y, z); state = event.getLevel().getBlockState(offset); if ( - !state.is(Blocks.IRON_BLOCK) - && !state.is(ModBlocks.FERRITE_CORE_MAGNET_BLOCK.get()) - && !state.is(ModBlocks.MAGNET_BLOCK.get()) + !state.is(Blocks.IRON_BLOCK) + && !state.is(ModBlocks.FERRITE_CORE_MAGNET_BLOCK.get()) + && !state.is(ModBlocks.MAGNET_BLOCK.get()) ) continue; BlockState state1 = ModBlocks.HOLLOW_MAGNET_BLOCK.get().defaultBlockState(); event.getLevel().setBlockAndUpdate(offset, state1); @@ -45,14 +48,14 @@ public void onLightingStrike(@NotNull LightningStrikeEvent event) { } } - private void lightningCharge(BlockPos pos, BlockState state) { + private void lightningCharge(BlockPos pos, Level level, BlockState state) { if (state.is(Blocks.COPPER_BLOCK) || state.is(Blocks.LIGHTNING_ROD)) { double unCharged = 32; Collection nearestChargeCollect = - ChargeCollectorManager.getNearestChargeCollect(pos); + ChargeCollectorManager.getInstance(level).getNearestChargeCollect(pos); for (var floatChargeCollectorBlockEntityEntry : nearestChargeCollect) { ChargeCollectorBlockEntity blockEntity = floatChargeCollectorBlockEntityEntry.getBlockEntity(); - if (ChargeCollectorManager.canCollect(blockEntity, pos)) { + if (ChargeCollectorManager.getInstance(level).canCollect(blockEntity, pos)) { unCharged = blockEntity.incomingCharge(unCharged); if (unCharged <= 0) break; } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java b/common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java index 1b99a63e6..4b15b0ba8 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java @@ -37,10 +37,10 @@ public static void onPistonMoveBlocks(@NotNull Level level, @NotNull List 0) { Collection nearestChargeCollect = - ChargeCollectorManager.getNearestChargeCollect(pos); + ChargeCollectorManager.getInstance(level).getNearestChargeCollect(pos); for (var floatChargeCollectorBlockEntityEntry : nearestChargeCollect) { ChargeCollectorBlockEntity blockEntity = floatChargeCollectorBlockEntityEntry.getBlockEntity(); - if (ChargeCollectorManager.canCollect(blockEntity, pos)) { + if (ChargeCollectorManager.getInstance(level).canCollect(blockEntity, pos)) { double unCharged = blockEntity.incomingCharge(n); if (unCharged == 0) { break; diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerBlockEntityEventListener.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerBlockEntityEventListener.java index c47a9bbf1..ee3ea92f3 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerBlockEntityEventListener.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerBlockEntityEventListener.java @@ -21,7 +21,7 @@ private static void onLoad(BlockEntity entity, ServerLevel level) { PowerGrid.addComponent(component); } if (entity instanceof ChargeCollectorBlockEntity chargeCollector) { - ChargeCollectorManager.addChargeCollector(chargeCollector); + ChargeCollectorManager.getInstance(level).addChargeCollector(chargeCollector); } } @@ -30,7 +30,7 @@ private static void onUnload(BlockEntity entity, ServerLevel level) { PowerGrid.removeComponent(component); } if (entity instanceof ChargeCollectorBlockEntity chargeCollector) { - ChargeCollectorManager.removeChargeCollector(chargeCollector); + ChargeCollectorManager.getInstance(level).removeChargeCollector(chargeCollector); } if (entity instanceof OverseerBlockEntity overseerBlockEntity) { LevelLoadManager.unregister(overseerBlockEntity.getBlockPos(), level); diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerWorldEventListener.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerWorldEventListener.java index 457fb098b..d67849a98 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerWorldEventListener.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerWorldEventListener.java @@ -24,7 +24,6 @@ private static void onload(MinecraftServer server, Level level) { } private static void onUnload(MinecraftServer server, Level level) { - ChargeCollectorManager.cleanMap(); if (level instanceof ServerLevel serverLevel) LevelLoadManager.removeAll(serverLevel); } } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LevelEventListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LevelEventListener.java index 1f9033792..ea66314ab 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LevelEventListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LevelEventListener.java @@ -29,7 +29,6 @@ public static void onLevelLoad(@NotNull LevelEvent.Load event) { */ @SubscribeEvent public static void onLevelUnload(@NotNull LevelEvent.Unload event) { - ChargeCollectorManager.cleanMap(); if (event.getLevel() instanceof ServerLevel serverLevel) { LevelLoadManager.removeAll(serverLevel); } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerBlockEntityEventListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerBlockEntityEventListener.java index 739cefc80..b031eb76d 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerBlockEntityEventListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerBlockEntityEventListener.java @@ -26,7 +26,7 @@ public static void onLoad(@NotNull BlockEntityEvent.ServerLoad event) { PowerGrid.addComponent(component); } if (event.getEntity() instanceof ChargeCollectorBlockEntity chargeCollector) { - ChargeCollectorManager.addChargeCollector(chargeCollector); + ChargeCollectorManager.getInstance(event.getLevel()).addChargeCollector(chargeCollector); } } @@ -39,7 +39,7 @@ public static void onUnload(@NotNull BlockEntityEvent.ServerUnload event) { PowerGrid.removeComponent(component); } if (event.getEntity() instanceof ChargeCollectorBlockEntity chargeCollector) { - ChargeCollectorManager.removeChargeCollector(chargeCollector); + ChargeCollectorManager.getInstance(event.getLevel()).removeChargeCollector(chargeCollector); } if (event.getEntity() instanceof OverseerBlockEntity overseerBlockEntity) { LevelLoadManager.unregister(overseerBlockEntity.getBlockPos(), (ServerLevel) event.getLevel()); From 38184e7164a5aa4c0ae1ed23bcf283e65d4cd063 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 23 Jun 2024 16:18:34 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BB=85=E5=AE=89=E8=A3=85JEI=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../anvilcraft/api/event/EventManager.java | 5 +++ .../api/event/entity/PlayerEvent.java | 23 ++++++++++-- .../data/generator/lang/OtherLang.java | 15 ++++++-- .../anvilcraft/event/PlayerEventListener.java | 34 ++++++++++++++++++ .../mixin/ClientPacketListenerMixin.java | 36 +++++++++++++++++++ .../java/dev/dubhe/anvilcraft/util/Utils.java | 16 +++++++++ .../resources/anvilcraft-common.mixins.json | 1 + .../assets/anvilcraft/lang/zh_cn.json | 3 +- .../assets/anvilcraft/lang/en_ud.json | 2 ++ .../assets/anvilcraft/lang/en_us.json | 2 ++ .../anvilcraft/util/fabric/UtilsImpl.java | 9 +++++ .../assets/anvilcraft/lang/en_ud.json | 2 ++ .../assets/anvilcraft/lang/en_us.json | 2 ++ .../event/forge/PlayerEventListener.java | 2 +- .../anvilcraft/util/forge/UtilsImpl.java | 9 +++++ 15 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 common/src/main/java/dev/dubhe/anvilcraft/mixin/ClientPacketListenerMixin.java create mode 100644 common/src/main/java/dev/dubhe/anvilcraft/util/Utils.java create mode 100644 fabric/src/main/java/dev/dubhe/anvilcraft/util/fabric/UtilsImpl.java create mode 100644 forge/src/main/java/dev/dubhe/anvilcraft/util/forge/UtilsImpl.java diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/EventManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/EventManager.java index e8637e944..2b8e60b07 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/EventManager.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/event/EventManager.java @@ -1,6 +1,9 @@ package dev.dubhe.anvilcraft.api.event; +import dev.architectury.platform.Platform; +import dev.architectury.utils.Env; import dev.dubhe.anvilcraft.AnvilCraft; +import net.fabricmc.api.Environment; import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; @@ -47,6 +50,8 @@ public void register(@NotNull Object object) { for (Method method : object.getClass().getMethods()) { if (method.getParameterCount() != 1) continue; SubscribeEvent annotation = method.getAnnotation(SubscribeEvent.class); + Environment env = method.getAnnotation(Environment.class); + if (env != null && Env.fromPlatform(env.value()) != Platform.getEnvironment()) continue; if (null == annotation) continue; Class event = method.getParameterTypes()[0]; Consumer trigger = (obj) -> { diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java index f810adb8b..1b6ca2d6e 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java @@ -2,6 +2,9 @@ import lombok.Getter; import lombok.Setter; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.BlockPos; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -9,7 +12,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; -public class PlayerEvent extends EntityEvent { +public class PlayerEvent extends EntityEvent { /** * 玩家事件 * @@ -17,12 +20,12 @@ public class PlayerEvent extends EntityEvent { * @param pos 位置 * @param level 世界 */ - public PlayerEvent(Player entity, BlockPos pos, Level level) { + public PlayerEvent(T entity, BlockPos pos, Level level) { super(entity, pos, level); } @Getter - public static class UseEntity extends PlayerEvent { + public static class UseEntity extends PlayerEvent { private final Entity target; private final InteractionHand hand; @Setter @@ -37,4 +40,18 @@ public UseEntity(Player entity, Entity target, InteractionHand hand, Level level this.hand = hand; } } + + @Environment(EnvType.CLIENT) + public static class ClientPlayerJoin extends PlayerEvent { + /** + * 玩家加入事件 + * + * @param entity 实体 + * @param pos 位置 + * @param level 世界 + */ + public ClientPlayerJoin(LocalPlayer entity, BlockPos pos, Level level) { + super(entity, pos, level); + } + } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/OtherLang.java b/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/OtherLang.java index 3e19259c9..b6754d46b 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/OtherLang.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/OtherLang.java @@ -8,6 +8,11 @@ public class OtherLang { * @param provider 提供器 */ public static void init(@NotNull RegistrateLangProvider provider) { + provider.add( + "modmenu.nameTranslation.anvilcraft", + "AnvilCraft" + ); + provider.add( "item.anvilcraft.default_enchantment.tooltip", "Has default enchantments:" @@ -34,8 +39,14 @@ public static void init(@NotNull RegistrateLangProvider provider) { ); provider.add( - "item.anvilcraft.disk.stored_from", - "Stored from: %s" + "item.anvilcraft.disk.stored_from", + "Stored from: %s" + ); + + provider.add( + "tooltip.anvilcraft.only_jei", + "We have detected that you have only installed JEI and will not be able to obtain a complete recipe query. " + + "We recommend installing %s for a complete gaming experience!" ); } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java b/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java index 9770dae20..6492c7ce3 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java @@ -4,6 +4,11 @@ import dev.dubhe.anvilcraft.api.event.entity.PlayerEvent; import dev.dubhe.anvilcraft.init.ModBlocks; import dev.dubhe.anvilcraft.item.ResinBlockItem; +import dev.dubhe.anvilcraft.util.Utils; +import net.minecraft.ChatFormatting; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; @@ -24,4 +29,33 @@ public void useEntity(@NotNull PlayerEvent.UseEntity event) { event.setResult(ResinBlockItem.useEntity(player, target, item)); } } + + /** + * @param event 玩家加入事件 + */ + @SubscribeEvent + public void onPlayerJoin(@NotNull PlayerEvent.ClientPlayerJoin event) { + if (Utils.onlyJei()) { + LocalPlayer entity = event.getEntity(); + entity.sendSystemMessage( + Component.literal("[").withStyle(ChatFormatting.GREEN).append( + Component.translatable("modmenu.nameTranslation.anvilcraft").withStyle(ChatFormatting.GOLD).append( + Component.literal("] ").withStyle(ChatFormatting.GREEN).append( + Component.translatable( + "tooltip.anvilcraft.only_jei", + Component.literal("EMI").withStyle(ChatFormatting.BLUE).withStyle( + style -> style.withClickEvent( + new ClickEvent( + ClickEvent.Action.OPEN_URL, + "https://modrinth.com/mod/emi" + ) + ) + ) + ).withStyle(ChatFormatting.BLACK) + ) + ) + ) + ); + } + } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/ClientPacketListenerMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/ClientPacketListenerMixin.java new file mode 100644 index 000000000..da93bdbef --- /dev/null +++ b/common/src/main/java/dev/dubhe/anvilcraft/mixin/ClientPacketListenerMixin.java @@ -0,0 +1,36 @@ +package dev.dubhe.anvilcraft.mixin; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.PlayerEvent; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.protocol.game.ClientboundLoginPacket; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ClientPacketListener.class) +public abstract class ClientPacketListenerMixin { + @Shadow + @Final + private Minecraft minecraft; + + @Shadow + public abstract ClientLevel getLevel(); + + @Inject( + method = "handleLogin", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Options;broadcastOptions()V") + ) + private void handleLogin(ClientboundLoginPacket packet, CallbackInfo ci) { + LocalPlayer player = this.minecraft.player; + if (player != null) { + AnvilCraft.EVENT_BUS.post(new PlayerEvent.ClientPlayerJoin(player, player.getOnPos(), this.getLevel())); + } + } +} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/Utils.java b/common/src/main/java/dev/dubhe/anvilcraft/util/Utils.java new file mode 100644 index 000000000..8400f3e85 --- /dev/null +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/Utils.java @@ -0,0 +1,16 @@ +package dev.dubhe.anvilcraft.util; + +import dev.architectury.injectables.annotations.ExpectPlatform; + +public abstract class Utils { + private Utils() { + } + + /** + * @return 是否只加载了 JEI + */ + @ExpectPlatform + public static boolean onlyJei() { + throw new AssertionError(); + } +} diff --git a/common/src/main/resources/anvilcraft-common.mixins.json b/common/src/main/resources/anvilcraft-common.mixins.json index ef83a97c8..c9b4b0f44 100644 --- a/common/src/main/resources/anvilcraft-common.mixins.json +++ b/common/src/main/resources/anvilcraft-common.mixins.json @@ -35,6 +35,7 @@ ], "client": [ "ClientLevelMixin", + "ClientPacketListenerMixin", "ItemInHandRendererMixin", "LevelRendererMixin", "TooltipRenderMixin", diff --git a/common/src/main/resources/assets/anvilcraft/lang/zh_cn.json b/common/src/main/resources/assets/anvilcraft/lang/zh_cn.json index 95db4c458..590ad8d51 100644 --- a/common/src/main/resources/assets/anvilcraft/lang/zh_cn.json +++ b/common/src/main/resources/assets/anvilcraft/lang/zh_cn.json @@ -423,5 +423,6 @@ "tooltip.anvilcraft.item.topaz": "蕴含闪电的力量", "tooltip.anvilcraft.item.transmission_pole": "组建电网,传输距离为8个方块", "tooltip.anvilcraft.jade.power_information": "电网:%d/%d kW", - "tooltip.anvilcraft.ruby_prism.power": "激光等级:%d" + "tooltip.anvilcraft.ruby_prism.power": "激光等级:%d", + "modmenu.nameTranslation.anvilcraft": "铁砧工艺" } diff --git a/fabric/src/generated/resources/assets/anvilcraft/lang/en_ud.json b/fabric/src/generated/resources/assets/anvilcraft/lang/en_ud.json index c91b169ea..cf3bcbbec 100644 --- a/fabric/src/generated/resources/assets/anvilcraft/lang/en_ud.json +++ b/fabric/src/generated/resources/assets/anvilcraft/lang/en_ud.json @@ -299,6 +299,7 @@ "itemGroup.anvilcraft.ingredients": "sʇuǝıpǝɹbuI :ʇɟɐɹƆןıʌuⱯ", "itemGroup.anvilcraft.tools": "sǝıʇıןıʇ∩ :ʇɟɐɹƆןıʌuⱯ", "message.anvilcraft.need_patchouli_installed": "pǝןןɐʇsuı ǝq oʇ spǝǝu ıןnoɥɔʇɐԀ", + "modmenu.nameTranslation.anvilcraft": "ʇɟɐɹƆןıʌuⱯ", "patchouli.anvilcraft.landing_text": "ʇɟɐɹƆןıʌuⱯ oʇ ǝɯoɔןǝM", "screen.anvilcraft.active_silencer.title": "ɹǝɔuǝןıS ǝʌıʇɔⱯ", "screen.anvilcraft.button.direction": "%s :uoıʇɔǝɹıᗡ ʇndʇnO", @@ -433,5 +434,6 @@ "tooltip.anvilcraft.item.topaz": "buıuʇɥbıן ɟo ɹǝʍod ǝɥʇ buıuıɐʇuoƆ", "tooltip.anvilcraft.item.transmission_pole": "8 ɟo ǝɔuɐʇsıp uoıssıɯsuɐɹʇ ɐ ɥʇıʍ pıɹb ɹǝʍod ɐ pןınᗺ", "tooltip.anvilcraft.jade.power_information": "Mʞ %d/%d :pıɹ⅁ ɹǝʍoԀ", + "tooltip.anvilcraft.only_jei": "¡ǝɔuǝıɹǝdxǝ buıɯɐb ǝʇǝןdɯoɔ ɐ ɹoɟ %s buıןןɐʇsuı puǝɯɯoɔǝɹ ǝM ˙ʎɹǝnb ǝdıɔǝɹ ǝʇǝןdɯoɔ ɐ uıɐʇqo oʇ ǝןqɐ ǝq ʇou ןןıʍ puɐ IƎſ pǝןןɐʇsuı ʎןuo ǝʌɐɥ noʎ ʇɐɥʇ pǝʇɔǝʇǝp ǝʌɐɥ ǝM", "tooltip.anvilcraft.ruby_prism.power": "%d :ןǝʌǝן ɹǝsɐꞀ" } \ No newline at end of file diff --git a/fabric/src/generated/resources/assets/anvilcraft/lang/en_us.json b/fabric/src/generated/resources/assets/anvilcraft/lang/en_us.json index 8c46bc2d6..56f747b3f 100644 --- a/fabric/src/generated/resources/assets/anvilcraft/lang/en_us.json +++ b/fabric/src/generated/resources/assets/anvilcraft/lang/en_us.json @@ -299,6 +299,7 @@ "itemGroup.anvilcraft.ingredients": "AnvilCraft: Ingredients", "itemGroup.anvilcraft.tools": "AnvilCraft: Utilities", "message.anvilcraft.need_patchouli_installed": "Patchouli needs to be installed", + "modmenu.nameTranslation.anvilcraft": "AnvilCraft", "patchouli.anvilcraft.landing_text": "Welcome to AnvilCraft", "screen.anvilcraft.active_silencer.title": "Active Silencer", "screen.anvilcraft.button.direction": "Output Direction: %s", @@ -433,5 +434,6 @@ "tooltip.anvilcraft.item.topaz": "Containing the power of lightning", "tooltip.anvilcraft.item.transmission_pole": "Build a power grid with a transmission distance of 8", "tooltip.anvilcraft.jade.power_information": "Power Grid: %d/%d kW", + "tooltip.anvilcraft.only_jei": "We have detected that you have only installed JEI and will not be able to obtain a complete recipe query. We recommend installing %s for a complete gaming experience!", "tooltip.anvilcraft.ruby_prism.power": "Laser level: %d" } \ No newline at end of file diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/util/fabric/UtilsImpl.java b/fabric/src/main/java/dev/dubhe/anvilcraft/util/fabric/UtilsImpl.java new file mode 100644 index 000000000..3a6d4ea20 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/util/fabric/UtilsImpl.java @@ -0,0 +1,9 @@ +package dev.dubhe.anvilcraft.util.fabric; + +import net.fabricmc.loader.api.FabricLoader; + +public class UtilsImpl { + public static boolean onlyJei() { + return FabricLoader.getInstance().isModLoaded("jei") && !FabricLoader.getInstance().isModLoaded("emi"); + } +} diff --git a/forge/src/generated/resources/assets/anvilcraft/lang/en_ud.json b/forge/src/generated/resources/assets/anvilcraft/lang/en_ud.json index c91b169ea..cf3bcbbec 100644 --- a/forge/src/generated/resources/assets/anvilcraft/lang/en_ud.json +++ b/forge/src/generated/resources/assets/anvilcraft/lang/en_ud.json @@ -299,6 +299,7 @@ "itemGroup.anvilcraft.ingredients": "sʇuǝıpǝɹbuI :ʇɟɐɹƆןıʌuⱯ", "itemGroup.anvilcraft.tools": "sǝıʇıןıʇ∩ :ʇɟɐɹƆןıʌuⱯ", "message.anvilcraft.need_patchouli_installed": "pǝןןɐʇsuı ǝq oʇ spǝǝu ıןnoɥɔʇɐԀ", + "modmenu.nameTranslation.anvilcraft": "ʇɟɐɹƆןıʌuⱯ", "patchouli.anvilcraft.landing_text": "ʇɟɐɹƆןıʌuⱯ oʇ ǝɯoɔןǝM", "screen.anvilcraft.active_silencer.title": "ɹǝɔuǝןıS ǝʌıʇɔⱯ", "screen.anvilcraft.button.direction": "%s :uoıʇɔǝɹıᗡ ʇndʇnO", @@ -433,5 +434,6 @@ "tooltip.anvilcraft.item.topaz": "buıuʇɥbıן ɟo ɹǝʍod ǝɥʇ buıuıɐʇuoƆ", "tooltip.anvilcraft.item.transmission_pole": "8 ɟo ǝɔuɐʇsıp uoıssıɯsuɐɹʇ ɐ ɥʇıʍ pıɹb ɹǝʍod ɐ pןınᗺ", "tooltip.anvilcraft.jade.power_information": "Mʞ %d/%d :pıɹ⅁ ɹǝʍoԀ", + "tooltip.anvilcraft.only_jei": "¡ǝɔuǝıɹǝdxǝ buıɯɐb ǝʇǝןdɯoɔ ɐ ɹoɟ %s buıןןɐʇsuı puǝɯɯoɔǝɹ ǝM ˙ʎɹǝnb ǝdıɔǝɹ ǝʇǝןdɯoɔ ɐ uıɐʇqo oʇ ǝןqɐ ǝq ʇou ןןıʍ puɐ IƎſ pǝןןɐʇsuı ʎןuo ǝʌɐɥ noʎ ʇɐɥʇ pǝʇɔǝʇǝp ǝʌɐɥ ǝM", "tooltip.anvilcraft.ruby_prism.power": "%d :ןǝʌǝן ɹǝsɐꞀ" } \ No newline at end of file diff --git a/forge/src/generated/resources/assets/anvilcraft/lang/en_us.json b/forge/src/generated/resources/assets/anvilcraft/lang/en_us.json index 8c46bc2d6..56f747b3f 100644 --- a/forge/src/generated/resources/assets/anvilcraft/lang/en_us.json +++ b/forge/src/generated/resources/assets/anvilcraft/lang/en_us.json @@ -299,6 +299,7 @@ "itemGroup.anvilcraft.ingredients": "AnvilCraft: Ingredients", "itemGroup.anvilcraft.tools": "AnvilCraft: Utilities", "message.anvilcraft.need_patchouli_installed": "Patchouli needs to be installed", + "modmenu.nameTranslation.anvilcraft": "AnvilCraft", "patchouli.anvilcraft.landing_text": "Welcome to AnvilCraft", "screen.anvilcraft.active_silencer.title": "Active Silencer", "screen.anvilcraft.button.direction": "Output Direction: %s", @@ -433,5 +434,6 @@ "tooltip.anvilcraft.item.topaz": "Containing the power of lightning", "tooltip.anvilcraft.item.transmission_pole": "Build a power grid with a transmission distance of 8", "tooltip.anvilcraft.jade.power_information": "Power Grid: %d/%d kW", + "tooltip.anvilcraft.only_jei": "We have detected that you have only installed JEI and will not be able to obtain a complete recipe query. We recommend installing %s for a complete gaming experience!", "tooltip.anvilcraft.ruby_prism.power": "Laser level: %d" } \ No newline at end of file diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/PlayerEventListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/PlayerEventListener.java index 4815dbddb..4c57ee3c6 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/PlayerEventListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/PlayerEventListener.java @@ -30,7 +30,7 @@ public static void useEntity(@NotNull PlayerInteractEvent.EntityInteract event) } @SubscribeEvent - public static void itemTooltip(ItemTooltipEvent event) { + public static void itemTooltip(@NotNull ItemTooltipEvent event) { TooltipEventListener.addTooltip(event.getItemStack(), event.getToolTip()); } } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/util/forge/UtilsImpl.java b/forge/src/main/java/dev/dubhe/anvilcraft/util/forge/UtilsImpl.java new file mode 100644 index 000000000..40fee213c --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/util/forge/UtilsImpl.java @@ -0,0 +1,9 @@ +package dev.dubhe.anvilcraft.util.forge; + +import net.minecraftforge.fml.ModList; + +public class UtilsImpl { + public static boolean onlyJei() { + return ModList.get().isLoaded("jei") && !ModList.get().isLoaded("emi"); + } +} From e6c23b34080986e41983bee6d7e9188516b3fd59 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 23 Jun 2024 16:24:52 +0800 Subject: [PATCH 4/7] Update PlayerEvent.java --- .../dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java index 1b6ca2d6e..7944347cd 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/event/entity/PlayerEvent.java @@ -2,8 +2,6 @@ import lombok.Getter; import lombok.Setter; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.BlockPos; import net.minecraft.world.InteractionHand; @@ -41,7 +39,6 @@ public UseEntity(Player entity, Entity target, InteractionHand hand, Level level } } - @Environment(EnvType.CLIENT) public static class ClientPlayerJoin extends PlayerEvent { /** * 玩家加入事件 From e0d5106a5c9eb952097eeea5b6c86b07daa6cac6 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 23 Jun 2024 16:28:00 +0800 Subject: [PATCH 5/7] Update ForgeGuiMixin.java --- .../java/dev/dubhe/anvilcraft/mixin/forge/ForgeGuiMixin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/ForgeGuiMixin.java b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/ForgeGuiMixin.java index e67ed96ec..3623122b7 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/ForgeGuiMixin.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/ForgeGuiMixin.java @@ -32,7 +32,7 @@ public ForgeGuiMixin(Minecraft minecraft, ItemRenderer itemRenderer) { target = "Lcom/mojang/blaze3d/systems/RenderSystem;setShaderColor(FFFF)V", shift = At.Shift.AFTER ), - remap = false + remap = true ) void onHudRender(GuiGraphics guiGraphics, float partialTick, CallbackInfo ci) { if (minecraft.player == null || minecraft.isPaused()) return; From db972624df2677ac537464e83c6a7ba52f6fd486 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 23 Jun 2024 16:35:29 +0800 Subject: [PATCH 6/7] Update PlayerEventListener.java --- .../java/dev/dubhe/anvilcraft/event/PlayerEventListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java b/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java index 6492c7ce3..8803c6d85 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/event/PlayerEventListener.java @@ -51,7 +51,7 @@ public void onPlayerJoin(@NotNull PlayerEvent.ClientPlayerJoin event) { ) ) ) - ).withStyle(ChatFormatting.BLACK) + ).withStyle(ChatFormatting.WHITE) ) ) ) From 3b626b6925fcd6e5d9867351f7dbf11777246b96 Mon Sep 17 00:00:00 2001 From: ZhuRuoLing Date: Sun, 23 Jun 2024 19:16:32 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=82=AC=E7=86=9F?= =?UTF-8?q?=E7=81=AF=E7=82=B8=E7=AB=AF=E5=9D=8F=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubhe/anvilcraft/api/BonemealManager.java | 118 ---------------- .../dubhe/anvilcraft/api/RipeningManager.java | 130 ++++++++++++++++++ .../entity/InductionLightBlockEntity.java | 4 +- .../fabric/ServerLifecycleEventListener.java | 4 +- .../forge/ServerLifecycleEventListener.java | 4 +- 5 files changed, 136 insertions(+), 124 deletions(-) delete mode 100644 common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java create mode 100644 common/src/main/java/dev/dubhe/anvilcraft/api/RipeningManager.java diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java deleted file mode 100644 index 782ad677d..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java +++ /dev/null @@ -1,118 +0,0 @@ -package dev.dubhe.anvilcraft.api; - -import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.block.InductionLightBlock; -import dev.dubhe.anvilcraft.block.state.LightColor; -import net.minecraft.core.BlockPos; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.util.Tuple; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.LightLayer; -import net.minecraft.world.level.block.BonemealableBlock; -import net.minecraft.world.level.block.GrassBlock; -import net.minecraft.world.level.block.NyliumBlock; -import net.minecraft.world.level.block.state.BlockState; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - - -public class BonemealManager { - private static int cooldown = 0; - private static final Set> lightBlocks = Collections.synchronizedSet(new HashSet<>()); - - private static boolean isLighting(@NotNull BlockState state) { - return !(state.getValue(InductionLightBlock.POWERED) - || state.getValue(InductionLightBlock.OVERLOAD)); - } - - private static boolean canCropGrow(@NotNull BlockState state) { - return state.getValue(InductionLightBlock.COLOR).equals(LightColor.PINK); - } - - private static boolean isInSet(HashSet> set, Tuple item) { - for (Tuple cur : set) { - if (cur.getA().getX() == item.getA().getX() - && cur.getA().getY() == item.getA().getY() - && cur.getA().getZ() == item.getA().getZ() - && cur.getB().dimension() == item.getB().dimension()) { - return true; - } - } - return false; - } - - private static HashSet> doRipen( - @NotNull Level level, @NotNull BlockPos pos, @NotNull HashSet> set) { - int rangeSize = AnvilCraft.config.inductionLightBlockRipeningRange; - HashSet> ripened = new HashSet<>(); - for (int i = -rangeSize / 2; i <= rangeSize / 2; i++) { - for (int j = -rangeSize / 2; j <= rangeSize / 2; j++) { - for (int k = -rangeSize / 2; k <= rangeSize / 2; k++) { - BlockPos pos1 = pos.offset(i, j, k); - BlockState state = level.getBlockState(pos1); - if (state.getBlock() instanceof BonemealableBlock growable - && !(growable instanceof GrassBlock) - && !(growable instanceof NyliumBlock) - && !isInSet(set, new Tuple<>(pos1, level)) - && growable.isValidBonemealTarget(level, pos1, state, false) - && level.getBrightness(LightLayer.BLOCK, pos1) >= 10 - ) { - growable.performBonemeal( - (ServerLevel) level, - level.getRandom(), pos1, state); - level.addParticle( - ParticleTypes.HAPPY_VILLAGER, - pos1.getX() + 0.5, - pos1.getY() + 0.5, - pos1.getZ() + 0.5, - 0.0, 0.0, 0.0); - ripened.add(new Tuple<>(pos1, level)); - } - } - } - } - return ripened; - } - - /** - * - */ - public static void tick() { - cooldown--; - - if (cooldown <= 0 && !lightBlocks.isEmpty()) { - cooldown = AnvilCraft.config.inductionLightBlockRipeningCooldown; - HashSet> ripenedBlocks = new HashSet<>(); - Iterator> it = lightBlocks.iterator(); - while (it.hasNext()) { - Tuple lightBlock = it.next(); - ServerLevel level = (ServerLevel) lightBlock.getB(); - level.getServer().execute(() -> { - BlockState lightBlockState = lightBlock.getB().getBlockState(lightBlock.getA()); - if (lightBlockState.getBlock() instanceof InductionLightBlock) { - if (isLighting(lightBlockState) && canCropGrow(lightBlockState)) { - HashSet> newRipened = - doRipen(lightBlock.getB(), lightBlock.getA(), ripenedBlocks); - ripenedBlocks.addAll(newRipened); - } - } else { - it.remove(); - } - }); - } - } - - } - - /** - * - */ - public static void addLightBlock(BlockPos pos, Level level) { - lightBlocks.add(new Tuple<>(pos, level)); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/RipeningManager.java b/common/src/main/java/dev/dubhe/anvilcraft/api/RipeningManager.java new file mode 100644 index 000000000..2ae7170bb --- /dev/null +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/RipeningManager.java @@ -0,0 +1,130 @@ +package dev.dubhe.anvilcraft.api; + +import com.mojang.logging.LogUtils; +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.block.InductionLightBlock; +import dev.dubhe.anvilcraft.block.state.LightColor; +import net.minecraft.core.BlockPos; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.TickTask; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LightLayer; +import net.minecraft.world.level.block.BonemealableBlock; +import net.minecraft.world.level.block.GrassBlock; +import net.minecraft.world.level.block.NyliumBlock; +import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + + +public class RipeningManager { + private static final Map INSTANCES = new HashMap<>(); + + private static int cooldown = 0; + private final Level level; + private final Set lightBlocks = Collections.synchronizedSet(new HashSet<>()); + + /** + * 获取当前维度催熟实例 + */ + public static RipeningManager getInstance(Level level) { + if (!INSTANCES.containsKey(level)) { + INSTANCES.put(level, new RipeningManager(level)); + } + return INSTANCES.get(level); + } + + + public RipeningManager(Level level) { + this.level = level; + } + + public static void tickAll() { + INSTANCES.values().forEach(RipeningManager::tick); + } + + private static boolean isLit(@NotNull BlockState state) { + return !(state.getValue(InductionLightBlock.POWERED) || state.getValue(InductionLightBlock.OVERLOAD)); + } + + private static boolean canCropGrow(@NotNull BlockState state) { + return state.getValue(InductionLightBlock.COLOR).equals(LightColor.PINK); + } + + private void doRipen( + @NotNull BlockPos pos, + @NotNull HashSet ripened + ) { + int rangeSize = AnvilCraft.config.inductionLightBlockRipeningRange; + for (int dx = -rangeSize / 2; dx <= rangeSize / 2; dx++) { + for (int dy = -rangeSize / 2; dy <= rangeSize / 2; dy++) { + for (int dz = -rangeSize / 2; dz <= rangeSize / 2; dz++) { + BlockPos pos1 = pos.offset(dx, dy, dz); + if (ripened.contains(pos1)) continue; + BlockState state = level.getBlockState(pos1); + if (state.getBlock() instanceof BonemealableBlock growable + && !(growable instanceof GrassBlock) + && !(growable instanceof NyliumBlock) + && growable.isValidBonemealTarget(level, pos1, state, false) + && level.getBrightness(LightLayer.BLOCK, pos1) >= 10 + ) { + growable.performBonemeal((ServerLevel) level, level.getRandom(), pos1, state); + level.addParticle( + ParticleTypes.HAPPY_VILLAGER, + pos1.getX() + 0.5, + pos1.getY() + 0.5, + pos1.getZ() + 0.5, + 0.0, 0.0, 0.0); + ripened.add(pos1); + } + } + } + } + } + + /** + * + */ + private void tick() { + cooldown--; + + if (cooldown <= 0 && !lightBlocks.isEmpty()) { + MinecraftServer server = level.getServer(); + if (server == null) return; + server.tell(new TickTask(server.getTickCount(), () -> { + HashSet ripenedBlocks = new HashSet<>(); + Iterator it = lightBlocks.iterator(); + while (it.hasNext()) { + BlockPos pos = it.next(); + BlockState lightBlockState = level.getBlockState(pos); + if (lightBlockState.getBlock() instanceof InductionLightBlock + && isLit(lightBlockState) + && canCropGrow(lightBlockState) + ) { + doRipen(pos, ripenedBlocks); + } else { + it.remove(); + } + } + })); + cooldown = AnvilCraft.config.inductionLightBlockRipeningCooldown; + } + } + + /** + * + */ + public static void addLightBlock(BlockPos pos, Level level) { + RipeningManager inst = RipeningManager.getInstance(level); + inst.lightBlocks.add(pos); + } +} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/InductionLightBlockEntity.java b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/InductionLightBlockEntity.java index 6ab3f8c9d..e2e3a386c 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/InductionLightBlockEntity.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/InductionLightBlockEntity.java @@ -1,6 +1,6 @@ package dev.dubhe.anvilcraft.block.entity; -import dev.dubhe.anvilcraft.api.BonemealManager; +import dev.dubhe.anvilcraft.api.RipeningManager; import dev.dubhe.anvilcraft.api.power.IPowerConsumer; import dev.dubhe.anvilcraft.api.power.PowerGrid; import dev.dubhe.anvilcraft.block.InductionLightBlock; @@ -36,7 +36,7 @@ public void tick(Level level1) { flushState(level1, getBlockPos()); if (!registered) { registered = true; - BonemealManager.addLightBlock(getPos(), level); + RipeningManager.addLightBlock(getBlockPos(), level); } } diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEventListener.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEventListener.java index cb9d65ee8..63ea77daf 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEventListener.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEventListener.java @@ -1,7 +1,7 @@ package dev.dubhe.anvilcraft.event.fabric; import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.api.BonemealManager; +import dev.dubhe.anvilcraft.api.RipeningManager; import dev.dubhe.anvilcraft.api.chargecollector.ThermoManager; import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent; import dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent; @@ -39,7 +39,7 @@ private static void endDataPackReload( private static void startTick(MinecraftServer server) { PowerGrid.tickGrid(); ThermoManager.tick(); - BonemealManager.tick(); + RipeningManager.tickAll(); RandomChuckTickLoadManager.tick(); } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEventListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEventListener.java index 0849e9f1e..33525c412 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEventListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEventListener.java @@ -1,7 +1,7 @@ package dev.dubhe.anvilcraft.event.forge; import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.api.BonemealManager; +import dev.dubhe.anvilcraft.api.RipeningManager; import dev.dubhe.anvilcraft.api.chargecollector.ThermoManager; import dev.dubhe.anvilcraft.api.event.forge.DataPackReloadedEvent; import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent; @@ -44,7 +44,7 @@ public static void onDataPackReloaded(@NotNull DataPackReloadedEvent event) { public static void onTick(@NotNull TickEvent.ServerTickEvent event) { if (event.phase != TickEvent.Phase.START) return; PowerGrid.tickGrid(); - BonemealManager.tick(); + RipeningManager.tickAll(); ThermoManager.tick(); RandomChuckTickLoadManager.tick(); }