From 1bd5800f120fbcabb3203dd4745a8dcd254e706a Mon Sep 17 00:00:00 2001 From: JR1811 Date: Wed, 11 Sep 2024 14:39:06 +0200 Subject: [PATCH] finished basic storage block origin handling --- .../block/AbstractVillageContainerBlock.java | 22 ++++++++++++------- .../AbstractVillageContainerBlockEntity.java | 13 +++++++---- .../mayor/util/HandledInventory.java | 8 +++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java index 91c22b1..2dd730f 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java @@ -142,17 +142,23 @@ protected void onStateReplaced(BlockState state, World world, BlockPos pos, Bloc return; } if (world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity) { - if (!world.isClient()) { - if (!blockEntity.isEmpty()) { - ItemScatterer.spawn(world, pos, blockEntity); - } - } if (blockEntity.isStructureOrigin()) { - getFirstConnectedBlock(world, pos).ifPresent(connectedPos -> - blockEntity.broadcastNewOriginToConnectedBlocks(world, connectedPos)); + if (getFirstConnectedBlock(world, pos).isPresent()) { + BlockPos firstConnectedPos = getFirstConnectedBlock(world, pos).get(); + blockEntity.broadcastNewOriginPos(world, firstConnectedPos); + if (world.getBlockEntity(firstConnectedPos) instanceof AbstractVillageContainerBlockEntity newOriginBlockEntity) { + blockEntity.moveInventory(newOriginBlockEntity); + blockEntity.moveConnectedBlocks(newOriginBlockEntity); + } + } else { + if (!world.isClient()) { + if (!blockEntity.isEmpty()) { + ItemScatterer.spawn(world, pos, blockEntity); + } + } + } } } - //TODO: refresh connected blocks list on structure origin super.onStateReplaced(state, world, pos, newState, moved); } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java index 422939e..8d87cc9 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java @@ -19,13 +19,13 @@ import net.minecraft.world.WorldAccess; import org.jetbrains.annotations.Nullable; -import java.util.HashSet; +import java.util.ArrayList; import java.util.List; import java.util.Optional; public abstract class AbstractVillageContainerBlockEntity extends BlockEntity implements HandledInventory { private BlockPos structureOriginPos; - private final HashSet connectedBlocks = new HashSet<>(); + private final List connectedBlocks = new ArrayList<>(); private StorageCallback callback = null; public AbstractVillageContainerBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { @@ -92,7 +92,7 @@ public Optional extractFromOrigin(@Nullable Direction direction) { return extractedStack; } - public HashSet getConnectedBlocks() { + public List getConnectedBlocks() { return this.connectedBlocks; } @@ -111,7 +111,12 @@ public void clearConnectedBlocks() { this.getConnectedBlocks().clear(); } - public void broadcastNewOriginToConnectedBlocks(WorldAccess world, BlockPos newOriginPos) { + public void moveConnectedBlocks(AbstractVillageContainerBlockEntity newOriginBlockEntity) { + newOriginBlockEntity.getConnectedBlocks().clear(); + newOriginBlockEntity.addConnectedBlocks(this.getConnectedBlocks()); + } + + public void broadcastNewOriginPos(WorldAccess world, BlockPos newOriginPos) { for (BlockPos connectedPos : getConnectedBlocks()) { if (!(world.getBlockEntity(connectedPos) instanceof AbstractVillageContainerBlockEntity blockEntity)) continue; diff --git a/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java b/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java index 88c6b6a..bc6eb03 100644 --- a/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java +++ b/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Optional; +@SuppressWarnings("unused") public interface HandledInventory extends SidedInventory { DefaultedList getItems(); @@ -134,6 +135,13 @@ default void clear() { markDirty(); } + default void moveInventory(HandledInventory external) { + for (int i = 0; i < this.getItems().size(); i++) { + external.setStack(i, this.getItems().get(i)); + } + this.clear(); + } + void markDirty(); default InventoryStorage getAsStorage(@Nullable Direction direction) {