From d9de0192fe20e1a8dbba575cfba933479b63ba6b Mon Sep 17 00:00:00 2001 From: JR1811 Date: Sat, 14 Sep 2024 02:02:47 +0200 Subject: [PATCH] refactored storage BlockEntities --- .../fabricatedatelier/mayor/MayorClient.java | 2 +- .../mayor/api/StorageCallback.java | 16 +++--- .../block/AbstractVillageContainerBlock.java | 36 ++++++------- .../block/custom/LumberStorageBlock.java | 41 ++------------ .../mayor/block/custom/StoneStorageBlock.java | 5 +- .../entity/LumberStorageBlockEntity.java | 29 ---------- .../block/entity/StoneStorageBlockEntity.java | 29 ---------- .../VillageContainerBlockEntity.java} | 53 +++++++++++++++---- .../LumberStorageBlockEntityRenderer.java | 4 +- .../mayor/entity/task/BuilderCollectTask.java | 8 +-- .../mayor/init/BlockEntities.java | 12 ++--- .../mayor/util/HandledInventory.java | 4 ++ .../mayor/util/InventoryUtil.java | 6 +-- 13 files changed, 94 insertions(+), 151 deletions(-) delete mode 100644 src/main/java/io/fabricatedatelier/mayor/block/entity/LumberStorageBlockEntity.java delete mode 100644 src/main/java/io/fabricatedatelier/mayor/block/entity/StoneStorageBlockEntity.java rename src/main/java/io/fabricatedatelier/mayor/block/{AbstractVillageContainerBlockEntity.java => entity/VillageContainerBlockEntity.java} (79%) diff --git a/src/main/java/io/fabricatedatelier/mayor/MayorClient.java b/src/main/java/io/fabricatedatelier/mayor/MayorClient.java index 5bd2624..c630de7 100644 --- a/src/main/java/io/fabricatedatelier/mayor/MayorClient.java +++ b/src/main/java/io/fabricatedatelier/mayor/MayorClient.java @@ -19,6 +19,6 @@ public void onInitializeClient() { Renderer.initialize(); KeyBindings.initialize(); - BlockEntityRendererFactories.register(BlockEntities.LUMBER_STORAGE, context -> new LumberStorageBlockEntityRenderer<>()); + BlockEntityRendererFactories.register(BlockEntities.VILLAGE_STORAGE, context -> new LumberStorageBlockEntityRenderer<>()); } } diff --git a/src/main/java/io/fabricatedatelier/mayor/api/StorageCallback.java b/src/main/java/io/fabricatedatelier/mayor/api/StorageCallback.java index 5309646..cce6128 100644 --- a/src/main/java/io/fabricatedatelier/mayor/api/StorageCallback.java +++ b/src/main/java/io/fabricatedatelier/mayor/api/StorageCallback.java @@ -1,32 +1,32 @@ package io.fabricatedatelier.mayor.api; -import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import net.minecraft.util.math.BlockPos; /** - * If you want to listen to the {@link AbstractVillageContainerBlockEntity StorageBlockEntity} + * If you want to listen to the {@link VillageContainerBlockEntity StorageBlockEntity} * {@link StorageCallback callbacks}, you will need to register the callback first.
* In the class, where you want to listen to those signals, implement the {@link StorageCallback} interface.
- * Then wherever you get access to the current {@link AbstractVillageContainerBlockEntity StorageBlockEntity}, - * call its {@link AbstractVillageContainerBlockEntity#registerCallback(StorageCallback) registerCallback()} method + * Then wherever you get access to the current {@link VillageContainerBlockEntity StorageBlockEntity}, + * call its {@link VillageContainerBlockEntity#registerCallback(StorageCallback) registerCallback()} method * with your current class's instance as the parameter (usually just a this call) */ public interface StorageCallback { /** - * Runs when the origin BlockPos of an {@link AbstractVillageContainerBlockEntity} has been changed. + * Runs when the origin BlockPos of an {@link VillageContainerBlockEntity} has been changed. * @param blockEntity the BlockEntity which changed its listed origin BlockPos * @param oldPos the old listed entry of the origin BlockPos * @param newPos the new listed entry of the origin BlockPos */ - default void onOriginChanged(AbstractVillageContainerBlockEntity blockEntity, BlockPos oldPos, BlockPos newPos) { + default void onOriginChanged(VillageContainerBlockEntity blockEntity, BlockPos oldPos, BlockPos newPos) { } /** - * Runs when the connected blocks BlockPos list of an {@link AbstractVillageContainerBlockEntity} has been changed. + * Runs when the connected blocks BlockPos list of an {@link VillageContainerBlockEntity} has been changed. * @param blockEntity the BlockEntity which changed its connected blocks BlockPos list */ - default void onConnectedBlocksChanged(AbstractVillageContainerBlockEntity blockEntity) { + default void onConnectedBlocksChanged(VillageContainerBlockEntity blockEntity) { } } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java index ec10f22..ab709f6 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlock.java @@ -1,6 +1,7 @@ package io.fabricatedatelier.mayor.block; import com.mojang.serialization.MapCodec; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import io.fabricatedatelier.mayor.state.VillageData; import io.fabricatedatelier.mayor.util.ConnectedBlockUtil; import io.fabricatedatelier.mayor.util.MayorStateHelper; @@ -55,7 +56,7 @@ protected void appendProperties(StateManager.Builder builder) @Override protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { BlockPos originPos = getOrigin(world, pos).orElse(pos); - if (world.getBlockEntity(originPos) instanceof AbstractVillageContainerBlockEntity blockEntity && !world.isClient()) { + if (world.getBlockEntity(originPos) instanceof VillageContainerBlockEntity blockEntity && !world.isClient()) { // extract Optional removedStack = blockEntity.extractFromOrigin(hit.getSide()); if (removedStack.isPresent() && !removedStack.get().isEmpty()) { @@ -72,7 +73,7 @@ protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, Worl if (world.isClient()) return super.onUseWithItem(stack, state, world, pos, player, hand, hit); - if (!(world.getBlockEntity(originPos) instanceof AbstractVillageContainerBlockEntity blockEntity)) + if (!(world.getBlockEntity(originPos) instanceof VillageContainerBlockEntity blockEntity)) return super.onUseWithItem(stack, state, world, pos, player, hand, hit); if (blockEntity.canInsert(player.getStackInHand(hand).copy(), hit.getSide())) { @@ -100,26 +101,25 @@ public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable Livi world.setBlockState(pos, state); } - if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity)) { + if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity)) { super.onPlaced(world, pos, state, placer, itemStack); return; } Optional neighborPos = getFirstConnectedBlock(world, pos); - if (neighborPos.isPresent()) { - if (world.getBlockEntity(neighborPos.get()) instanceof AbstractVillageContainerBlockEntity neighborBlockEntity) { - if (neighborBlockEntity.getStructureOriginPos().isPresent()) { - blockEntity.setStructureOriginPos(neighborBlockEntity.getStructureOriginPos().get()); - if (world.getBlockEntity(neighborBlockEntity.getStructureOriginPos().get()) instanceof AbstractVillageContainerBlockEntity originBlockEntity) { - var originBox = new ConnectedBlockUtil.BoundingBox(world, neighborBlockEntity.getStructureOriginPos().get(), false); - originBlockEntity.clearConnectedBlocks(); - originBlockEntity.addConnectedBlocks(new ArrayList<>(originBox.getConnectedPosList())); - } + if (neighborPos.isEmpty()) { + blockEntity.setStructureOriginPos(getOrigin(world, pos).orElse(pos)); + } else if (world.getBlockEntity(neighborPos.get()) instanceof VillageContainerBlockEntity neighborBlockEntity) { + if (neighborBlockEntity.getStructureOriginPos().isPresent()) { + blockEntity.setStructureOriginPos(neighborBlockEntity.getStructureOriginPos().get()); + if (world.getBlockEntity(neighborBlockEntity.getStructureOriginPos().get()) instanceof VillageContainerBlockEntity originBlockEntity) { + var originBox = new ConnectedBlockUtil.BoundingBox(world, neighborBlockEntity.getStructureOriginPos().get(), false); + originBlockEntity.clearConnectedBlocks(); + originBlockEntity.addConnectedBlocks(new ArrayList<>(originBox.getConnectedPosList())); } } - } else { - blockEntity.setStructureOriginPos(getOrigin(world, pos).orElse(pos)); } + super.onPlaced(world, pos, state, placer, itemStack); } @@ -144,12 +144,12 @@ protected void onStateReplaced(BlockState state, World world, BlockPos pos, Bloc super.onStateReplaced(state, world, pos, newState, moved); return; } - if (world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity) { + if (world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity) { if (blockEntity.isStructureOrigin()) { if (getFirstConnectedBlock(world, pos).isPresent()) { BlockPos firstConnectedPos = getFirstConnectedBlock(world, pos).get(); blockEntity.broadcastNewOriginPos(world, firstConnectedPos); - if (world.getBlockEntity(firstConnectedPos) instanceof AbstractVillageContainerBlockEntity newOriginBlockEntity) { + if (world.getBlockEntity(firstConnectedPos) instanceof VillageContainerBlockEntity newOriginBlockEntity) { blockEntity.moveInventory(newOriginBlockEntity); blockEntity.moveConnectedBlocks(newOriginBlockEntity); } @@ -230,12 +230,12 @@ public static HashSet getValidConnectedDirections(WorldAccess world, public static void setOrigin(WorldAccess world, BlockPos pos) { - if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity)) return; + if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity)) return; blockEntity.setStructureOriginPos(pos); } public static Optional getOrigin(BlockView world, BlockPos pos) { - if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity)) + if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity)) return Optional.empty(); return blockEntity.getStructureOriginPos(); } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/custom/LumberStorageBlock.java b/src/main/java/io/fabricatedatelier/mayor/block/custom/LumberStorageBlock.java index 7869aae..9a7b125 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/custom/LumberStorageBlock.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/custom/LumberStorageBlock.java @@ -3,7 +3,8 @@ import com.mojang.serialization.MapCodec; import io.fabricatedatelier.mayor.Mayor; import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock; -import io.fabricatedatelier.mayor.block.entity.LumberStorageBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; +import io.fabricatedatelier.mayor.datagen.TagProvider; import io.fabricatedatelier.mayor.util.ConnectedBlockUtil; import net.minecraft.block.BlockState; import net.minecraft.block.BlockWithEntity; @@ -13,15 +14,11 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.world.BlockView; import net.minecraft.world.World; -import net.minecraft.world.WorldView; import org.jetbrains.annotations.Nullable; -import java.util.List; - public class LumberStorageBlock extends AbstractVillageContainerBlock { @@ -36,15 +33,7 @@ public MapCodec getCodec() { @Override public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) { - return new LumberStorageBlockEntity(pos, state); - } - - @Override - protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { - super.onStateReplaced(state, world, pos, newState, moved); - if (!isNextToSameBlock(world, pos)) { - setOrigin(world, pos); - } + return new VillageContainerBlockEntity(pos, state, TagProvider.ItemTags.LUMBER_STORAGE_STORABLE); } @Override @@ -61,28 +50,4 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player Mayor.LOGGER.info("Has Holes: {}", boundingBox.hasHoles()); return super.onUse(state, world, pos, player, hit); } - - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - private boolean isSupported(WorldView world, BlockPos originalPos) { - BlockState stateBelow = world.getBlockState(originalPos.down()); - boolean belowIsSolid = stateBelow.isSideSolidFullSquare(world, originalPos.down(), Direction.UP); - boolean hasSameBlockBelow = stateBelow.getBlock() instanceof LumberStorageBlock; - return belowIsSolid || hasSameBlockBelow; - } - - private int identicalHorizontalNeighborBlocks(BlockView world, BlockPos placedPos) { - return (int) Direction.Type.HORIZONTAL.stream() - .filter(direction -> world.getBlockState(placedPos.offset(direction)).getBlock() instanceof LumberStorageBlock) - .count(); - } - - public boolean isNextToSameBlock(BlockView world, BlockPos placedPos) { - return identicalHorizontalNeighborBlocks(world, placedPos) > 0; - } - - public static boolean wallsAreAdjacent(List sides) { - return Direction.Type.HORIZONTAL.stream() - .allMatch(direction -> sides.stream().noneMatch(direction.getOpposite()::equals)); - } - } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/custom/StoneStorageBlock.java b/src/main/java/io/fabricatedatelier/mayor/block/custom/StoneStorageBlock.java index 6ecabd5..3b23823 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/custom/StoneStorageBlock.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/custom/StoneStorageBlock.java @@ -1,8 +1,9 @@ package io.fabricatedatelier.mayor.block.custom; import com.mojang.serialization.MapCodec; -import io.fabricatedatelier.mayor.block.entity.StoneStorageBlockEntity; import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; +import io.fabricatedatelier.mayor.datagen.TagProvider; import net.minecraft.block.BlockState; import net.minecraft.block.BlockWithEntity; import net.minecraft.block.entity.BlockEntity; @@ -32,6 +33,6 @@ public MapCodec getCodec() { @Override public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) { - return new StoneStorageBlockEntity(pos, state); + return new VillageContainerBlockEntity(pos, state, TagProvider.ItemTags.STONE_STORAGE_STORABLE); } } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/entity/LumberStorageBlockEntity.java b/src/main/java/io/fabricatedatelier/mayor/block/entity/LumberStorageBlockEntity.java deleted file mode 100644 index dcc6b5c..0000000 --- a/src/main/java/io/fabricatedatelier/mayor/block/entity/LumberStorageBlockEntity.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.fabricatedatelier.mayor.block.entity; - -import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity; -import io.fabricatedatelier.mayor.datagen.TagProvider; -import io.fabricatedatelier.mayor.init.BlockEntities; -import net.minecraft.block.BlockState; -import net.minecraft.item.ItemStack; -import net.minecraft.util.collection.DefaultedList; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import org.jetbrains.annotations.Nullable; - -public class LumberStorageBlockEntity extends AbstractVillageContainerBlockEntity { - private final DefaultedList inventory = DefaultedList.ofSize(16, ItemStack.EMPTY); - - @Override - public DefaultedList getItems() { - return inventory; - } - - @Override - public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) { - return stack.isIn(TagProvider.ItemTags.LUMBER_STORAGE_STORABLE); - } - - public LumberStorageBlockEntity(BlockPos pos, BlockState state) { - super(BlockEntities.LUMBER_STORAGE, pos, state); - } -} diff --git a/src/main/java/io/fabricatedatelier/mayor/block/entity/StoneStorageBlockEntity.java b/src/main/java/io/fabricatedatelier/mayor/block/entity/StoneStorageBlockEntity.java deleted file mode 100644 index e322e31..0000000 --- a/src/main/java/io/fabricatedatelier/mayor/block/entity/StoneStorageBlockEntity.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.fabricatedatelier.mayor.block.entity; - -import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity; -import io.fabricatedatelier.mayor.datagen.TagProvider; -import io.fabricatedatelier.mayor.init.BlockEntities; -import net.minecraft.block.BlockState; -import net.minecraft.item.ItemStack; -import net.minecraft.util.collection.DefaultedList; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import org.jetbrains.annotations.Nullable; - -public class StoneStorageBlockEntity extends AbstractVillageContainerBlockEntity { - private final DefaultedList inventory = DefaultedList.ofSize(8, ItemStack.EMPTY); - - public StoneStorageBlockEntity(BlockPos pos, BlockState state) { - super(BlockEntities.STONE_STORAGE, pos, state); - } - - @Override - public DefaultedList getItems() { - return inventory; - } - - @Override - public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) { - return stack.isIn(TagProvider.ItemTags.LUMBER_STORAGE_STORABLE); - } -} diff --git a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java b/src/main/java/io/fabricatedatelier/mayor/block/entity/VillageContainerBlockEntity.java similarity index 79% rename from src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java rename to src/main/java/io/fabricatedatelier/mayor/block/entity/VillageContainerBlockEntity.java index 8cd7da6..718cbb4 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/AbstractVillageContainerBlockEntity.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/entity/VillageContainerBlockEntity.java @@ -1,21 +1,24 @@ -package io.fabricatedatelier.mayor.block; +package io.fabricatedatelier.mayor.block.entity; import io.fabricatedatelier.mayor.api.StorageCallback; +import io.fabricatedatelier.mayor.init.BlockEntities; import io.fabricatedatelier.mayor.state.VillageData; import io.fabricatedatelier.mayor.util.HandledInventory; import io.fabricatedatelier.mayor.util.MayorStateHelper; import io.fabricatedatelier.mayor.util.NbtKeys; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; -import net.minecraft.block.entity.BlockEntityType; import net.minecraft.inventory.Inventories; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; import net.minecraft.registry.RegistryWrapper; +import net.minecraft.registry.tag.TagKey; import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.WorldAccess; @@ -23,24 +26,46 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; -public abstract class AbstractVillageContainerBlockEntity extends BlockEntity implements HandledInventory { +public class VillageContainerBlockEntity extends BlockEntity implements HandledInventory { + public static final Map MAX_STACK_COUNT_FROM_STRUCTURE_SIZE = Map.of( + 1, 3, + 2, 12, + 3, 27 + ); + + private final DefaultedList inventory = DefaultedList.ofSize(16, ItemStack.EMPTY); private BlockPos structureOriginPos; private final List connectedBlocks = new ArrayList<>(); + + private TagKey insertableItems; private StorageCallback callback = null; - public AbstractVillageContainerBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { - super(type, pos, state); + public VillageContainerBlockEntity(BlockPos pos, BlockState state) { + super(BlockEntities.VILLAGE_STORAGE, pos, state); + } + + public VillageContainerBlockEntity(BlockPos pos, BlockState state, TagKey insertableItems) { + this(pos, state); + this.insertableItems = insertableItems; } public void registerCallback(StorageCallback callback) { this.callback = callback; } + @Override + public DefaultedList getItems() { + return this.inventory; + } + @Override public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) { - if (!this.isStructureOrigin()) return false; + if (!this.isStructureOrigin() || this.getWorld() == null) return false; + if (!stack.isIn(this.insertableItems)) return false; + //TODO: only allow if valid size return HandledInventory.super.canInsert(slot, stack, dir); } @@ -90,10 +115,18 @@ public void setStructureOriginPos(BlockPos newStructureOriginPos) { markDirty(); } + public Optional getStructureOriginBlockEntity() { + if (this.world == null) return Optional.empty(); + if (this.getStructureOriginPos().isEmpty()) return Optional.empty(); + if (!(world.getBlockEntity(this.getStructureOriginPos().get()) instanceof VillageContainerBlockEntity blockEntity)) + return Optional.empty(); + return Optional.of(blockEntity); + } + public boolean insertIntoOrigin(ItemStack stack, @Nullable Direction direction) { if (this.getWorld() == null || this.getWorld().isClient()) return false; if (this.getStructureOriginPos().isEmpty()) return false; - if (!(this.getWorld().getBlockEntity(this.getStructureOriginPos().get()) instanceof AbstractVillageContainerBlockEntity blockEntity)) + if (!(this.getWorld().getBlockEntity(this.getStructureOriginPos().get()) instanceof VillageContainerBlockEntity blockEntity)) return false; boolean inserted = blockEntity.insert(stack, direction); if (inserted) blockEntity.markDirty(); @@ -103,7 +136,7 @@ public boolean insertIntoOrigin(ItemStack stack, @Nullable Direction direction) public Optional extractFromOrigin(@Nullable Direction direction) { if (this.getWorld() == null || this.getWorld().isClient()) return Optional.empty(); if (this.getStructureOriginPos().isEmpty()) return Optional.empty(); - if (!(this.getWorld().getBlockEntity(this.getStructureOriginPos().get()) instanceof AbstractVillageContainerBlockEntity blockEntity)) + if (!(this.getWorld().getBlockEntity(this.getStructureOriginPos().get()) instanceof VillageContainerBlockEntity blockEntity)) return Optional.empty(); Optional extractedStack = blockEntity.extract(direction); if (extractedStack.isPresent()) blockEntity.markDirty(); @@ -129,14 +162,14 @@ public void clearConnectedBlocks() { this.getConnectedBlocks().clear(); } - public void moveConnectedBlocks(AbstractVillageContainerBlockEntity newOriginBlockEntity) { + public void moveConnectedBlocks(VillageContainerBlockEntity 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)) + if (!(world.getBlockEntity(connectedPos) instanceof VillageContainerBlockEntity blockEntity)) continue; blockEntity.setStructureOriginPos(newOriginPos); } diff --git a/src/main/java/io/fabricatedatelier/mayor/block/entity/client/LumberStorageBlockEntityRenderer.java b/src/main/java/io/fabricatedatelier/mayor/block/entity/client/LumberStorageBlockEntityRenderer.java index 0614025..df9cf89 100644 --- a/src/main/java/io/fabricatedatelier/mayor/block/entity/client/LumberStorageBlockEntityRenderer.java +++ b/src/main/java/io/fabricatedatelier/mayor/block/entity/client/LumberStorageBlockEntityRenderer.java @@ -2,7 +2,7 @@ import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock; import io.fabricatedatelier.mayor.block.MayorProperties; -import io.fabricatedatelier.mayor.block.entity.LumberStorageBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.client.MinecraftClient; @@ -13,7 +13,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.math.RotationAxis; -public class LumberStorageBlockEntityRenderer implements BlockEntityRenderer { +public class LumberStorageBlockEntityRenderer implements BlockEntityRenderer { private int tick = 0, rotTick = 0; @Override diff --git a/src/main/java/io/fabricatedatelier/mayor/entity/task/BuilderCollectTask.java b/src/main/java/io/fabricatedatelier/mayor/entity/task/BuilderCollectTask.java index 99d26b1..8ef9d35 100644 --- a/src/main/java/io/fabricatedatelier/mayor/entity/task/BuilderCollectTask.java +++ b/src/main/java/io/fabricatedatelier/mayor/entity/task/BuilderCollectTask.java @@ -1,7 +1,7 @@ package io.fabricatedatelier.mayor.entity.task; import com.google.common.collect.ImmutableMap; -import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import io.fabricatedatelier.mayor.entity.access.Builder; import io.fabricatedatelier.mayor.state.ConstructionData; import io.fabricatedatelier.mayor.state.MayorVillageState; @@ -95,9 +95,9 @@ private BlockPos getTarget(ServerWorld serverWorld, VillageData villageData, Con Item item = StructureHelper.getMissingConstructionBlockMap(serverWorld, constructionData).values().stream().findFirst().get().getBlock().asItem(); for (int i = 0; i < villageData.getStorageOriginBlockPosList().size(); i++) { - if (serverWorld.getBlockEntity(villageData.getStorageOriginBlockPosList().get(i)) instanceof AbstractVillageContainerBlockEntity abstractVillageContainerBlockEntity) { + if (serverWorld.getBlockEntity(villageData.getStorageOriginBlockPosList().get(i)) instanceof VillageContainerBlockEntity villageContainerBlockEntity) { // Find the correct one lul - // Todo: abstractVillageContainerBlockEntity contains item + // Todo: villageContainerBlockEntity contains item return villageData.getStorageOriginBlockPosList().get(i); // Todo: Maybe get closest storage blockpos of the of the storage multiblock // break; @@ -111,7 +111,7 @@ private BlockPos getTarget(ServerWorld serverWorld, VillageData villageData, Con //// entry.getValue().getBlock(); // // Todo: Find the correct Lumber,Stone or whatever storage block // for (int i = 0; i < villageData.getStorageOriginBlockPosList().size(); i++) { -// if (serverWorld.getBlockEntity(villageData.getStorageOriginBlockPosList().get(i)) instanceof AbstractVillageContainerBlockEntity abstractVillageContainerBlockEntity) { +// if (serverWorld.getBlockEntity(villageData.getStorageOriginBlockPosList().get(i)) instanceof VillageContainerBlockEntity abstractVillageContainerBlockEntity) { // // Find the correct one lul // return villageData.getStorageOriginBlockPosList().get(i); //// break; diff --git a/src/main/java/io/fabricatedatelier/mayor/init/BlockEntities.java b/src/main/java/io/fabricatedatelier/mayor/init/BlockEntities.java index 3a5fd1a..95e4fbf 100644 --- a/src/main/java/io/fabricatedatelier/mayor/init/BlockEntities.java +++ b/src/main/java/io/fabricatedatelier/mayor/init/BlockEntities.java @@ -2,8 +2,7 @@ import io.fabricatedatelier.mayor.Mayor; import io.fabricatedatelier.mayor.block.entity.CameraDebugBlockEntity; -import io.fabricatedatelier.mayor.block.entity.LumberStorageBlockEntity; -import io.fabricatedatelier.mayor.block.entity.StoneStorageBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import io.fabricatedatelier.mayor.util.HandledInventory; import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; import net.minecraft.block.Block; @@ -16,11 +15,9 @@ public class BlockEntities { public static final BlockEntityType CAMERA_DEBUG = register("camera_debug", CameraDebugBlockEntity::new, Blocks.CAMERA_DEBUG); - public static final BlockEntityType LUMBER_STORAGE = - registerWithStorage("lumber_storage", LumberStorageBlockEntity::new, Blocks.LUMBER_STORAGE); - - public static final BlockEntityType STONE_STORAGE = - registerWithStorage("stone_storage", StoneStorageBlockEntity::new, Blocks.STONE_STORAGE); + public static final BlockEntityType VILLAGE_STORAGE = + registerWithStorage("lumber_storage", VillageContainerBlockEntity::new, + Blocks.LUMBER_STORAGE, Blocks.STONE_STORAGE); private static BlockEntityType register( @@ -29,6 +26,7 @@ private static BlockEntityType register( BlockEntityType.Builder.create(entityFactory, blocks).build(null)); } + @SuppressWarnings("SameParameterValue") private static BlockEntityType registerWithStorage( String name, BlockEntityType.BlockEntityFactory entityFactory, Block... blocks) { BlockEntityType blockEntityType = register(name, entityFactory, blocks); diff --git a/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java b/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java index bc6eb03..acdf057 100644 --- a/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java +++ b/src/main/java/io/fabricatedatelier/mayor/util/HandledInventory.java @@ -22,6 +22,10 @@ default int size() { return getItems().size(); } + default int nonEmptyStackSize() { + return (int) getItems().stream().filter(stack -> !stack.isEmpty()).count(); + } + @Override default int[] getAvailableSlots(Direction side) { List availableSlots = new ArrayList<>(); diff --git a/src/main/java/io/fabricatedatelier/mayor/util/InventoryUtil.java b/src/main/java/io/fabricatedatelier/mayor/util/InventoryUtil.java index 5c9749b..dcb15b0 100644 --- a/src/main/java/io/fabricatedatelier/mayor/util/InventoryUtil.java +++ b/src/main/java/io/fabricatedatelier/mayor/util/InventoryUtil.java @@ -1,7 +1,7 @@ package io.fabricatedatelier.mayor.util; import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock; -import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity; +import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity; import io.fabricatedatelier.mayor.state.VillageData; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; @@ -49,8 +49,8 @@ public static List getAvailableItems(VillageData villageData, World w List availableStacks = new ArrayList<>(); for (BlockPos pos : villageData.getStorageOriginBlockPosList()) { - if (world.getBlockState(pos).getBlock() instanceof AbstractVillageContainerBlock && world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity abstractVillageContainerBlockEntity) { - for (ItemStack stack : abstractVillageContainerBlockEntity.getItems()) { + if (world.getBlockState(pos).getBlock() instanceof AbstractVillageContainerBlock && world.getBlockEntity(pos) instanceof VillageContainerBlockEntity villageContainerBlockEntity) { + for (ItemStack stack : villageContainerBlockEntity.getItems()) { stack = stack.copy(); if (stack.isEmpty()) { continue;