From aa5a1320d07845244ad073bc852989c24a7771b1 Mon Sep 17 00:00:00 2001 From: Gugle Date: Wed, 17 Apr 2024 22:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubhe/anvilcraft/api/power/PowerGrid.java | 55 ++++++++++++++----- .../entity/TransmissionPoleBlockEntity.java | 5 +- .../client/renderer/PowerGridRenderer.java | 2 +- 3 files changed, 45 insertions(+), 17 deletions(-) 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 6023cca63..ee4803afd 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 @@ -1,5 +1,6 @@ package dev.dubhe.anvilcraft.api.power; +import dev.dubhe.anvilcraft.AnvilCraft; import lombok.Getter; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.entity.BlockEntity; @@ -11,6 +12,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Set; /** @@ -19,7 +21,9 @@ @SuppressWarnings("unused") public class PowerGrid { public static boolean isServerClosing = false; - public static final Set GRID_SET = Collections.synchronizedSet(new HashSet<>()); + @Getter + private static Set gridSetClient = Collections.synchronizedSet(new HashSet<>()); + public static final Set GRID_SET = new HashSet<>(); public static final int GRID_TICK = 40; public static int cooldown = 0; @Getter @@ -35,6 +39,20 @@ public class PowerGrid { @Getter private BlockPos pos = null; + /** + * @return 获取电网中的元件数量 + */ + public int getComponentCount() { + return this.transmitters.size() + this.producers.size() + this.consumers.size() + this.storages.size(); + } + + /** + * @return 该电网是否为空电网 + */ + public boolean isEmpty() { + return this.getComponentCount() <= 0; + } + /** * 总电力刻 */ @@ -43,10 +61,14 @@ public static void tickGrid() { cooldown--; return; } - for (PowerGrid grid : PowerGrid.GRID_SET) { + Iterator iterator = PowerGrid.GRID_SET.iterator(); + while (iterator.hasNext()) { + PowerGrid grid = iterator.next(); + if (grid.isEmpty()) iterator.remove(); grid.tick(); } cooldown = GRID_TICK; + gridSetClient = Set.copyOf(GRID_SET); } /** @@ -154,11 +176,15 @@ private void addRange(IPowerComponent component) { * @param components 元件 */ public static void removeComponent(IPowerComponent @NotNull ... components) { - if (PowerGrid.isServerClosing) return; - for (IPowerComponent component : components) { - PowerGrid grid = component.getGrid(); - if (grid == null) return; - grid.remove(component); + try { + if (PowerGrid.isServerClosing) return; + for (IPowerComponent component : components) { + PowerGrid grid = component.getGrid(); + if (grid == null) return; + grid.remove(component); + } + } catch (Exception e) { + AnvilCraft.LOGGER.error(e.getMessage(), e); } } @@ -168,15 +194,15 @@ public static void removeComponent(IPowerComponent @NotNull ... components) { * @param components 电力元件 */ public void remove(IPowerComponent @NotNull ... components) { - Set set = new HashSet<>(); + Set set = new LinkedHashSet<>(); + this.transmitters.stream().filter(this::clearGrid).forEach(set::add); + this.transmitters.clear(); this.storages.stream().filter(this::clearGrid).forEach(set::add); this.storages.clear(); this.producers.stream().filter(this::clearGrid).forEach(set::add); this.producers.clear(); this.consumers.stream().filter(this::clearGrid).forEach(set::add); this.consumers.clear(); - this.transmitters.stream().filter(this::clearGrid).forEach(set::add); - this.transmitters.clear(); for (IPowerComponent component : components) { set.remove(component); } @@ -195,9 +221,13 @@ private boolean clearGrid(@NotNull IPowerComponent component) { * @param grid 电网 */ public void merge(@NotNull PowerGrid grid) { + grid.producers.forEach(producer -> producer.setGrid(this)); this.producers.addAll(grid.producers); + grid.consumers.forEach(producer -> producer.setGrid(this)); this.consumers.addAll(grid.consumers); + grid.storages.forEach(producer -> producer.setGrid(this)); this.storages.addAll(grid.storages); + grid.transmitters.forEach(producer -> producer.setGrid(this)); this.transmitters.addAll(grid.transmitters); } @@ -206,10 +236,9 @@ public void merge(@NotNull PowerGrid grid) { * @return 元件是否在电网范围内 */ public boolean isInRange(@NotNull IPowerComponent component) { - BlockPos vec3 = component.getPos(); - BlockPos center = this.getPos(); + BlockPos vec3 = component.getPos().subtract(this.getPos()); VoxelShape range = Shapes.join( - this.range.move(center.getX(), center.getY(), center.getZ()), + this.range, component.getRange().move(vec3.getX(), vec3.getY(), vec3.getZ()), BooleanOp.AND ); diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/TransmissionPoleBlockEntity.java b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/TransmissionPoleBlockEntity.java index 8cc4f69cb..8b631a0be 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/TransmissionPoleBlockEntity.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/TransmissionPoleBlockEntity.java @@ -42,9 +42,8 @@ private TransmissionPoleBlockEntity(BlockEntityType type, BlockPos pos, Block @Override public @NotNull PowerComponentType getComponentType() { if (this.getLevel() == null) return PowerComponentType.INVALID; - BlockState state = this.getLevel().getBlockState(this.getPos()); - if (!state.is(ModBlocks.TRANSMISSION_POLE.get())) return PowerComponentType.INVALID; - if (state.getValue(TransmissionPoleBlock.HALF) != Half.TOP) return PowerComponentType.INVALID; + if (!this.getBlockState().is(ModBlocks.TRANSMISSION_POLE.get())) return PowerComponentType.INVALID; + if (this.getBlockState().getValue(TransmissionPoleBlock.HALF) != Half.TOP) return PowerComponentType.INVALID; return PowerComponentType.TRANSMITTER; } } 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 e6f8237cc..2388545ea 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 @@ -20,7 +20,7 @@ public class PowerGridRenderer { public static void render(PoseStack poseStack, VertexConsumer consumer, double camX, double camY, double camZ) { if (Minecraft.getInstance().level == null) return; RandomSource random = Minecraft.getInstance().level.random; - for (PowerGrid grid : PowerGrid.GRID_SET) { + for (PowerGrid grid : PowerGrid.getGridSetClient()) { random.setSeed(grid.hashCode()); PowerGridRenderer.renderOutline( poseStack, consumer, camX, camY, camZ,