diff --git a/src/main/java/dev/cafeteria/artofalchemy/block/BlockPipe.java b/src/main/java/dev/cafeteria/artofalchemy/block/BlockPipe.java index ec426b5..19bfbd5 100644 --- a/src/main/java/dev/cafeteria/artofalchemy/block/BlockPipe.java +++ b/src/main/java/dev/cafeteria/artofalchemy/block/BlockPipe.java @@ -53,6 +53,8 @@ public static void scheduleChunkRebuild(final World world, final BlockPos pos) { } } + private HashSet nodes; + public BlockPipe() { super( Settings.of(Material.ORGANIC_PRODUCT).strength(0.1f).nonOpaque().noCollision().sounds(BlockSoundGroup.NETHERITE) @@ -170,15 +172,15 @@ private Map getFaces(final World world, final BlockPos pos) { @Override public Set getNodes(final World world, final BlockPos pos) { - final HashSet nodes = new HashSet<>(); + this.nodes = new HashSet<>(); // TODO: Only reset when needed final Map faces = this.getFaces(world, pos); for (final Direction dir : faces.keySet()) { final IOFace face = faces.get(dir); if (face.isNode()) { - nodes.add(new NetworkNode(world, face.getType(), pos, dir)); + this.nodes.add(new NetworkNode(world, face.getType(), pos, dir)); } } - return nodes; + return this.nodes; } @Override @@ -232,6 +234,7 @@ public void neighborUpdate( final boolean notify ) { super.neighborUpdate(state, world, pos, block, fromPos, notify); + for (final Direction dir : Direction.values()) { if (fromPos.subtract(pos).equals(dir.getVector())) { if (this.faceOpen(world, pos, dir) && this.faceOpen(world, fromPos, dir.getOpposite())) { diff --git a/src/main/java/dev/cafeteria/artofalchemy/transport/EssentiaNetwork.java b/src/main/java/dev/cafeteria/artofalchemy/transport/EssentiaNetwork.java index 61afd99..e6ac072 100644 --- a/src/main/java/dev/cafeteria/artofalchemy/transport/EssentiaNetwork.java +++ b/src/main/java/dev/cafeteria/artofalchemy/transport/EssentiaNetwork.java @@ -155,6 +155,9 @@ public void tick() { return; } this.lastTicked = this.world.getTime(); + this.nodes.forEach(NetworkNode::checkBlockEntity); // KG: This should be run as irregularly as possible. Ideally + // node would listen for nearby updates and update on demand + // there. for (final NetworkNode pusher : this.pushers) { for (final NetworkNode puller : this.pullers) { @@ -186,20 +189,21 @@ public NbtList toTag() { public void transfer(final NetworkNode from, final NetworkNode to) { final BlockEntity fromBE = from.getBlockEntity(); final BlockEntity toBE = to.getBlockEntity(); - if ((fromBE instanceof HasEssentia) && (toBE instanceof HasEssentia)) { - for (int i = 0; i < ((HasEssentia) fromBE).getNumContainers(); i++) { + if ((fromBE instanceof HasEssentia fromEssenceBE) && (toBE instanceof HasEssentia)) { + final HasEssentia toEssenceBE = (HasEssentia) toBE; + for (int i = 0; i < fromEssenceBE.getNumContainers(); i++) { EssentiaContainer fromContainer; if (from.getDirection().isPresent()) { - fromContainer = ((HasEssentia) fromBE).getContainer(from.getDirection().get().getOpposite()); + fromContainer = fromEssenceBE.getContainer(from.getDirection().get().getOpposite()); } else { - fromContainer = ((HasEssentia) fromBE).getContainer(); + fromContainer = fromEssenceBE.getContainer(); } - for (int j = 0; j < ((HasEssentia) toBE).getNumContainers(); j++) { + for (int j = 0; j < toEssenceBE.getNumContainers(); j++) { EssentiaContainer toContainer; if (to.getDirection().isPresent()) { - toContainer = ((HasEssentia) toBE).getContainer(to.getDirection().get().getOpposite()); + toContainer = toEssenceBE.getContainer(to.getDirection().get().getOpposite()); } else { - toContainer = ((HasEssentia) toBE).getContainer(); + toContainer = toEssenceBE.getContainer(); } fromContainer.pushContents(toContainer); } diff --git a/src/main/java/dev/cafeteria/artofalchemy/transport/NetworkNode.java b/src/main/java/dev/cafeteria/artofalchemy/transport/NetworkNode.java index 0f523bb..fb64aef 100644 --- a/src/main/java/dev/cafeteria/artofalchemy/transport/NetworkNode.java +++ b/src/main/java/dev/cafeteria/artofalchemy/transport/NetworkNode.java @@ -28,6 +28,7 @@ public String asString() { private final World world; private final NetworkNode.Type type; private final BlockPos pos; + private BlockEntity blockEntity; private final Direction dir; @@ -40,16 +41,19 @@ public NetworkNode(final World world, final Type type, final BlockPos pos, final this.type = type; this.pos = pos; this.dir = dir; + this.updateBlockEntity(); } - public BlockEntity getBlockEntity() { - if (this.dir != null) { - return this.world.getBlockEntity(this.pos.offset(this.dir)); - } else { - return this.world.getBlockEntity(this.pos); + public void checkBlockEntity() { + if ((this.blockEntity == null) || this.blockEntity.isRemoved()) { + this.updateBlockEntity(); } } + public BlockEntity getBlockEntity() { + return this.blockEntity; + } + public Optional getDirection() { return Optional.of(this.dir); } @@ -62,4 +66,8 @@ public NetworkNode.Type getType() { return this.type; } + public void updateBlockEntity() { + this.blockEntity = this.world.getBlockEntity(this.dir == null ? this.pos : this.pos.offset(this.dir)); + } + }