diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java index b80cd43f7..04d0eb1f0 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java @@ -13,6 +13,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -26,7 +27,7 @@ @SuppressWarnings("unused") public class PowerGrid { public static boolean isServerClosing = false; - public static final Map> GRID_MAP = new HashMap<>(); + public static final Map> GRID_MAP = Collections.synchronizedMap(new HashMap<>()); public static final int GRID_TICK = 20; @Getter public boolean remove = false; @@ -34,10 +35,10 @@ public class PowerGrid { private int generate = 0; // 发电功率 @Getter private int consume = 0; // 耗电功率 - final Set producers = new HashSet<>(); // 发电机 - final Set consumers = new HashSet<>(); // 用电器 - final Set storages = new HashSet<>(); // 储电 - final Set transmitters = new HashSet<>(); // 中继 + final Set producers = Collections.synchronizedSet(new HashSet<>()); // 发电机 + final Set consumers = Collections.synchronizedSet(new HashSet<>()); // 用电器 + final Set storages = Collections.synchronizedSet(new HashSet<>()); // 储电 + final Set transmitters = Collections.synchronizedSet(new HashSet<>()); // 中继 @Getter private VoxelShape shape = null; @Getter @@ -73,11 +74,13 @@ public boolean isEmpty() { public static void tickGrid() { for (Set grids : PowerGrid.GRID_MAP.values()) { Iterator iterator = grids.iterator(); + Set remove = Collections.synchronizedSet(new HashSet<>()); while (iterator.hasNext()) { PowerGrid grid = iterator.next(); - if (grid.isEmpty()) iterator.remove(); + if (grid.isEmpty()) remove.add(grid); grid.tick(); } + grids.removeAll(remove); } } @@ -97,7 +100,7 @@ protected void tick() { } } else { int need = this.consume - this.generate; - Set storages = new HashSet<>(); + Set storages = Collections.synchronizedSet(new HashSet<>()); for (IPowerStorage storage : this.storages) { need -= storage.getOutputPower(); storages.add(storage); @@ -116,7 +119,7 @@ protected void tick() { } private void gridTick() { - HashSet components = new HashSet<>(); + Set components = Collections.synchronizedSet(new HashSet<>()); components.addAll(this.transmitters); components.addAll(this.consumers); components.addAll(this.storages); @@ -283,16 +286,18 @@ public static void addComponent(IPowerComponent @NotNull ... components) { PowerGrid grid = null; Set grids = PowerGrid.getGridSet(component.getCurrentLevel()); Iterator iterator = grids.iterator(); + Set remove = Collections.synchronizedSet(new HashSet<>()); while (iterator.hasNext()) { PowerGrid grid1 = iterator.next(); if (!grid1.isInRange(component)) continue; if (grid == null) grid = grid1; else { grid.merge(grid1); - iterator.remove(); + remove.add(grid1); new PowerGridRemovePack(grid1).broadcast(); } } + grids.removeAll(remove); if (grid == null) grid = new PowerGrid(component.getCurrentLevel()); grid.add(component); grids.add(grid); @@ -321,5 +326,4 @@ public static Set getGridSet(Level level) { public static void clear() { PowerGrid.GRID_MAP.values().forEach(Collection::clear); } - } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/power/SimplePowerGrid.java b/common/src/main/java/dev/dubhe/anvilcraft/api/power/SimplePowerGrid.java index a87eeb3fa..f72995918 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/power/SimplePowerGrid.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/power/SimplePowerGrid.java @@ -177,7 +177,7 @@ public VoxelShape getShape() { * 寻找电网 */ public static List findPowerGrid(BlockPos pos) { - return PowerGridRenderer.getGrids() + return PowerGridRenderer.getGridMap() .values().stream() .filter(it -> it.blocks.stream().anyMatch(it1 -> it1.equals(pos))) .toList(); diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/GiantAnvilBlock.java b/common/src/main/java/dev/dubhe/anvilcraft/block/GiantAnvilBlock.java index fd0a8b6b1..f51fdc26f 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/GiantAnvilBlock.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/GiantAnvilBlock.java @@ -260,7 +260,9 @@ public void onPlace( @NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState oldState, boolean movedByPiston ) { - level.scheduleTick(pos, this, this.getDelayAfterPlace()); + if (state.hasProperty(HALF)) { + level.scheduleTick(pos.subtract(state.getValue(HALF).getOffset()), this, this.getDelayAfterPlace()); + } } @Override @@ -268,7 +270,9 @@ public void onPlace( @NotNull BlockState state, @NotNull Direction direction, @NotNull BlockState neighborState, @NotNull LevelAccessor level, @NotNull BlockPos pos, @NotNull BlockPos neighborPos ) { - level.scheduleTick(pos, this, this.getDelayAfterPlace()); + if (state.hasProperty(HALF)) { + level.scheduleTick(pos.subtract(state.getValue(HALF).getOffset()), this, this.getDelayAfterPlace()); + } return super.updateShape(state, direction, neighborState, level, pos, neighborPos); } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/client/renderer/PowerGridRenderer.java b/common/src/main/java/dev/dubhe/anvilcraft/client/renderer/PowerGridRenderer.java index 0dc3000b0..4b51ebe10 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/client/renderer/PowerGridRenderer.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/client/renderer/PowerGridRenderer.java @@ -3,7 +3,6 @@ import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import dev.dubhe.anvilcraft.api.power.SimplePowerGrid; -import lombok.Getter; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; @@ -19,8 +18,11 @@ @Environment(EnvType.CLIENT) public class PowerGridRenderer { - @Getter - private static Map grids = Collections.synchronizedMap(new HashMap<>()); + private static final Map GRID_MAP = Collections.synchronizedMap(new HashMap<>()); + + public static Map getGridMap() { + return PowerGridRenderer.GRID_MAP; + } /** * 渲染 @@ -29,7 +31,7 @@ public static void render(PoseStack poseStack, VertexConsumer consumer, double c if (Minecraft.getInstance().level == null) return; RandomSource random = Minecraft.getInstance().level.random; String level = Minecraft.getInstance().level.dimension().location().toString(); - for (SimplePowerGrid grid : PowerGridRenderer.grids.values()) { + for (SimplePowerGrid grid : PowerGridRenderer.GRID_MAP.values()) { if (!grid.getLevel().equals(level)) continue; random.setSeed(grid.getHash()); PowerGridRenderer.renderOutline( @@ -41,7 +43,7 @@ public static void render(PoseStack poseStack, VertexConsumer consumer, double c } public static void clearAllGrid() { - grids.clear(); + GRID_MAP.clear(); } @SuppressWarnings("SameParameterValue") diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/transform/TransformOptions.java b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/transform/TransformOptions.java index 0beb12006..8afef9623 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/transform/TransformOptions.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/transform/TransformOptions.java @@ -10,6 +10,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Mob; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import org.jetbrains.annotations.NotNull; @@ -36,10 +37,22 @@ public void accept(Entity oldEntity, Entity newEntity) { for (InteractionHand value : InteractionHand.values()) { ItemStack itemStack = o.getItemInHand(value); if (itemStack.is(Items.ANVIL) - || itemStack.is(Items.CHIPPED_ANVIL) - || itemStack.is(Items.DAMAGED_ANVIL)) { + || itemStack.is(Items.CHIPPED_ANVIL) + || itemStack.is(Items.DAMAGED_ANVIL)) { o.setItemInHand(value, ModBlocks.GIANT_ANVIL.asItem().getDefaultInstance()); n.setItemInHand(value, ModBlocks.GIANT_ANVIL.asItem().getDefaultInstance()); + if (n instanceof Mob mob) { + mob.setDropChance( + value == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND, + 1.0f + ); + } + if (o instanceof Mob mob) { + mob.setDropChance( + value == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND, + 1.0f + ); + } } } } @@ -65,8 +78,8 @@ public void accept(Entity oldEntity, Entity newEntity) { */ public static TransformOptions fromJson(JsonObject jsonObject) { return CODEC.decode(JsonOps.INSTANCE, jsonObject) - .getOrThrow(false, s -> { - }).getFirst(); + .getOrThrow(false, s -> { + }).getFirst(); } /** @@ -74,7 +87,7 @@ public static TransformOptions fromJson(JsonObject jsonObject) { */ public JsonElement toJson() { return CODEC.encodeStart(JsonOps.INSTANCE, this) - .getOrThrow(false, s -> { - }); + .getOrThrow(false, s -> { + }); } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridRemovePack.java b/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridRemovePack.java index 852e7c14b..654257d9d 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridRemovePack.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridRemovePack.java @@ -43,6 +43,6 @@ public void encode(@NotNull FriendlyByteBuf buf) { @Override @Environment(EnvType.CLIENT) public void handler() { - Minecraft.getInstance().execute(() -> PowerGridRenderer.getGrids().remove(this.grid)); + Minecraft.getInstance().execute(() -> PowerGridRenderer.getGridMap().remove(this.grid)); } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridSyncPack.java b/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridSyncPack.java index 090653284..ebde3d84c 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridSyncPack.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/network/PowerGridSyncPack.java @@ -50,6 +50,6 @@ public void encode(@NotNull FriendlyByteBuf buf) { @Override @Environment(EnvType.CLIENT) public void handler() { - Minecraft.getInstance().execute(() -> PowerGridRenderer.getGrids().put(this.grid.getHash(), this.grid)); + Minecraft.getInstance().execute(() -> PowerGridRenderer.getGridMap().put(this.grid.getHash(), this.grid)); } } diff --git a/gradle.properties b/gradle.properties index b582d06d7..87818e56e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ mod_description = The mod is an anvil-centric vanilla survival expansion contributors = - Phoupraw\n- DancingSnow\n- NightFish\n- BOTXue\n- wdfaESfaef\n- ZhuRuoLing\n- YocyCraft\n- Bi2Nb9O3\n- LemoMew\n- Cjsah supporters = - BOTXue\n- Fengyuan__\n- Take\n- Moe_Lobster mod_license = LGPL-3.0 license -mod_version = 1.4.0 +mod_version = 1.4.0+pre-release.2 mod_url = https://github.com/Anvil-Dev/AnvilCraft maven_group=dev.dubhe