From 31ad06a30f7ad8e9df9dd0d0006ed2545d87cf77 Mon Sep 17 00:00:00 2001 From: Davide Albiero Date: Tue, 12 Nov 2024 21:28:20 +0100 Subject: [PATCH] Fix #265 --- .../util/container/AdvancedContainer.java | 24 ++++++++++ .../detector/AdvancedDetectorMenu.java | 4 +- .../inventory/detector/ItemDetectorMenu.java | 4 +- .../detector/LocomotiveDetectorMenu.java | 4 +- .../inventory/detector/SheepDetectorMenu.java | 2 +- .../inventory/detector/TankDetectorMenu.java | 2 +- .../detector/FilterDetectorBlockEntity.java | 48 +++---------------- .../detector/ItemDetectorBlockEntity.java | 4 +- .../LocomotiveDetectorBlockEntity.java | 4 +- .../detector/SheepDetectorBlockEntity.java | 2 +- .../detector/TankDetectorBlockEntity.java | 2 +- 11 files changed, 45 insertions(+), 55 deletions(-) diff --git a/src/main/java/mods/railcraft/util/container/AdvancedContainer.java b/src/main/java/mods/railcraft/util/container/AdvancedContainer.java index 0c5f01d94..d77ea4c73 100644 --- a/src/main/java/mods/railcraft/util/container/AdvancedContainer.java +++ b/src/main/java/mods/railcraft/util/container/AdvancedContainer.java @@ -7,6 +7,7 @@ import mods.railcraft.api.container.manipulator.ContainerSlotAccessor; import mods.railcraft.api.container.manipulator.ModifiableSlotAccessor; import mods.railcraft.api.core.CompoundTagKeys; +import mods.railcraft.world.level.block.entity.RailcraftBlockEntity; import mods.railcraft.world.module.ModuleProvider; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; @@ -49,6 +50,10 @@ public AdvancedContainer listener(ModuleProvider moduleProvider) { return this.listener(new ModuleProviderCallback(moduleProvider)); } + public AdvancedContainer listener(RailcraftBlockEntity blockEntity) { + return this.listener(new BlockEntityCallback(blockEntity)); + } + public AdvancedContainer listener(Listener callback) { this.listener = callback; this.addListener(callback); @@ -177,4 +182,23 @@ public void containerChanged(Container container) { this.moduleProvider.save(); } } + + public static class BlockEntityCallback implements Listener { + + private final RailcraftBlockEntity blockEntity; + + public BlockEntityCallback(RailcraftBlockEntity blockEntity) { + this.blockEntity = blockEntity; + } + + @Override + public boolean stillValid(Player player) { + return this.blockEntity.isStillValid(player); + } + + @Override + public void containerChanged(Container container) { + this.blockEntity.setChanged(); + } + } } diff --git a/src/main/java/mods/railcraft/world/inventory/detector/AdvancedDetectorMenu.java b/src/main/java/mods/railcraft/world/inventory/detector/AdvancedDetectorMenu.java index 4a6587616..d233b8d62 100644 --- a/src/main/java/mods/railcraft/world/inventory/detector/AdvancedDetectorMenu.java +++ b/src/main/java/mods/railcraft/world/inventory/detector/AdvancedDetectorMenu.java @@ -11,8 +11,8 @@ public class AdvancedDetectorMenu extends RailcraftMenu { public AdvancedDetectorMenu(int id, Inventory inventory, AdvancedDetectorBlockEntity blockEntity) { super(RailcraftMenuTypes.ADVANCED_DETECTOR.get(), id, inventory.player, blockEntity::isStillValid); - for (int i = 0; i < blockEntity.getContainerSize(); i++) { - this.addSlot(new PhantomMinecartSlot(blockEntity, i, 8 + i * 18, 24)); + for (int i = 0; i < blockEntity.getInvFilters().getContainerSize(); i++) { + this.addSlot(new PhantomMinecartSlot(blockEntity.getInvFilters(), i, 8 + i * 18, 24)); } this.addInventorySlots(inventory, 140); } diff --git a/src/main/java/mods/railcraft/world/inventory/detector/ItemDetectorMenu.java b/src/main/java/mods/railcraft/world/inventory/detector/ItemDetectorMenu.java index d4d58d328..0de69c811 100644 --- a/src/main/java/mods/railcraft/world/inventory/detector/ItemDetectorMenu.java +++ b/src/main/java/mods/railcraft/world/inventory/detector/ItemDetectorMenu.java @@ -14,8 +14,8 @@ public ItemDetectorMenu(int id, Inventory inventory, ItemDetectorBlockEntity blo super(RailcraftMenuTypes.ITEM_DETECTOR.get(), id, inventory.player, blockEntity::isStillValid); this.blockEntity = blockEntity; - for (int i = 0; i < blockEntity.getContainerSize(); i++) { - this.addSlot(new RailcraftSlot(blockEntity, i, 8 + i * 18, 61) + for (int i = 0; i < blockEntity.getInvFilters().getContainerSize(); i++) { + this.addSlot(new RailcraftSlot(blockEntity.getInvFilters(), i, 8 + i * 18, 61) .setPhantom() .setEnableCheck(() -> blockEntity.getPrimaryMode() == ItemDetectorBlockEntity.PrimaryMode.FILTERED)); } diff --git a/src/main/java/mods/railcraft/world/inventory/detector/LocomotiveDetectorMenu.java b/src/main/java/mods/railcraft/world/inventory/detector/LocomotiveDetectorMenu.java index b9185e140..7b0cf2ae9 100644 --- a/src/main/java/mods/railcraft/world/inventory/detector/LocomotiveDetectorMenu.java +++ b/src/main/java/mods/railcraft/world/inventory/detector/LocomotiveDetectorMenu.java @@ -10,8 +10,8 @@ public class LocomotiveDetectorMenu extends RailcraftMenu { public LocomotiveDetectorMenu(int id, Inventory inventory, LocomotiveDetectorBlockEntity blockEntity) { super(RailcraftMenuTypes.LOCOMOTIVE_DETECTOR.get(), id, inventory.player, blockEntity::isStillValid); - this.addSlot(new ColorFilterSlot(blockEntity, 0, 35 ,26)); - this.addSlot(new ColorFilterSlot(blockEntity, 1, 35 ,52)); + this.addSlot(new ColorFilterSlot(blockEntity.getInvFilters(), 0, 35 ,26)); + this.addSlot(new ColorFilterSlot(blockEntity.getInvFilters(), 1, 35 ,52)); this.addInventorySlots(inventory, 170); } } diff --git a/src/main/java/mods/railcraft/world/inventory/detector/SheepDetectorMenu.java b/src/main/java/mods/railcraft/world/inventory/detector/SheepDetectorMenu.java index 92683a7f6..a39297c27 100644 --- a/src/main/java/mods/railcraft/world/inventory/detector/SheepDetectorMenu.java +++ b/src/main/java/mods/railcraft/world/inventory/detector/SheepDetectorMenu.java @@ -10,7 +10,7 @@ public class SheepDetectorMenu extends RailcraftMenu { public SheepDetectorMenu(int id, Inventory inventory, SheepDetectorBlockEntity blockEntity) { super(RailcraftMenuTypes.SHEEP_DETECTOR.get(), id, inventory.player, blockEntity::isStillValid); - this.addSlot(new ColorFilterSlot(blockEntity, 0, 60 ,24)); + this.addSlot(new ColorFilterSlot(blockEntity.getInvFilters(), 0, 60 ,24)); this.addInventorySlots(inventory, 140); } } diff --git a/src/main/java/mods/railcraft/world/inventory/detector/TankDetectorMenu.java b/src/main/java/mods/railcraft/world/inventory/detector/TankDetectorMenu.java index 818663521..3ad70af01 100644 --- a/src/main/java/mods/railcraft/world/inventory/detector/TankDetectorMenu.java +++ b/src/main/java/mods/railcraft/world/inventory/detector/TankDetectorMenu.java @@ -13,7 +13,7 @@ public class TankDetectorMenu extends RailcraftMenu { public TankDetectorMenu(int id, Inventory inventory, TankDetectorBlockEntity blockEntity) { super(RailcraftMenuTypes.TANK_DETECTOR.get(), id, inventory.player, blockEntity::isStillValid); this.blockEntity = blockEntity; - this.addSlot(new FluidFilterSlot(blockEntity, 0, 26 ,24)); + this.addSlot(new FluidFilterSlot(blockEntity.getInvFilters(), 0, 26 ,24)); this.addInventorySlots(inventory, 140); } diff --git a/src/main/java/mods/railcraft/world/level/block/entity/detector/FilterDetectorBlockEntity.java b/src/main/java/mods/railcraft/world/level/block/entity/detector/FilterDetectorBlockEntity.java index 09c000208..4347394c2 100644 --- a/src/main/java/mods/railcraft/world/level/block/entity/detector/FilterDetectorBlockEntity.java +++ b/src/main/java/mods/railcraft/world/level/block/entity/detector/FilterDetectorBlockEntity.java @@ -1,69 +1,35 @@ package mods.railcraft.world.level.block.entity.detector; -import java.util.EnumMap; -import java.util.Map; import java.util.stream.Stream; -import org.jetbrains.annotations.Nullable; import mods.railcraft.api.container.manipulator.ContainerManipulator; import mods.railcraft.api.container.manipulator.ModifiableSlotAccessor; import mods.railcraft.api.core.CompoundTagKeys; import mods.railcraft.util.container.AdvancedContainer; -import mods.railcraft.util.container.ForwardingContainer; -import mods.railcraft.util.container.ItemHandlerFactory; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; -import net.minecraft.world.Container; import net.minecraft.world.MenuProvider; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.ForgeCapabilities; -import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.wrapper.InvWrapper; public abstract class FilterDetectorBlockEntity extends DetectorBlockEntity - implements MenuProvider, ForwardingContainer, ContainerManipulator { + implements MenuProvider, ContainerManipulator { - private final AdvancedContainer invFilters; - private LazyOptional itemHandler = LazyOptional.of(() -> new InvWrapper(this)); - private Map> directionalItemHandlers = - new EnumMap<>(Direction.class); + protected final AdvancedContainer invFilters; protected FilterDetectorBlockEntity(BlockEntityType type, BlockPos blockPos, BlockState blockState, int inventorySize) { super(type, blockPos, blockState); - this.invFilters = new AdvancedContainer(inventorySize).listener((Container) this).phantom(); + this.invFilters = new AdvancedContainer(inventorySize).listener(this).phantom(); } - @Override - public Stream stream() { - return this.invFilters.stream(); - } - - @Override - public boolean stillValid(Player player) { - return this.isStillValid(player); + public AdvancedContainer getInvFilters() { + return invFilters; } @Override - public Container container() { - return this.invFilters; - } - - @Override - public LazyOptional getCapability(Capability capability, - @Nullable Direction direction) { - if (capability == ForgeCapabilities.ITEM_HANDLER) { - return direction == null - ? this.itemHandler.cast() - : this.directionalItemHandlers.computeIfAbsent(direction, - __ -> LazyOptional.of(() -> ItemHandlerFactory.wrap(this, direction))).cast(); - } - return super.getCapability(capability, direction); + public Stream stream() { + return this.invFilters.stream(); } @Override diff --git a/src/main/java/mods/railcraft/world/level/block/entity/detector/ItemDetectorBlockEntity.java b/src/main/java/mods/railcraft/world/level/block/entity/detector/ItemDetectorBlockEntity.java index 8f1d56e7a..984b4f621 100644 --- a/src/main/java/mods/railcraft/world/level/block/entity/detector/ItemDetectorBlockEntity.java +++ b/src/main/java/mods/railcraft/world/level/block/entity/detector/ItemDetectorBlockEntity.java @@ -70,8 +70,8 @@ protected int testCarts(List minecarts) { } private boolean matchesFilter(ContainerManipulator containerManipulator) { - for (int i = 0; i < getContainerSize(); i++) { - var filter = getItem(i); + for (int i = 0; i < this.invFilters.getContainerSize(); i++) { + var filter = this.invFilters.getItem(i); if (filter.isEmpty()) continue; var stackFilter = StackFilter.anyMatch(filter); diff --git a/src/main/java/mods/railcraft/world/level/block/entity/detector/LocomotiveDetectorBlockEntity.java b/src/main/java/mods/railcraft/world/level/block/entity/detector/LocomotiveDetectorBlockEntity.java index e6491c541..20ad621f4 100644 --- a/src/main/java/mods/railcraft/world/level/block/entity/detector/LocomotiveDetectorBlockEntity.java +++ b/src/main/java/mods/railcraft/world/level/block/entity/detector/LocomotiveDetectorBlockEntity.java @@ -22,8 +22,8 @@ public LocomotiveDetectorBlockEntity(BlockPos blockPos, BlockState blockState) { @Override protected int testCarts(List minecarts) { - var primary = getItem(0); - var secondary = getItem(1); + var primary = this.invFilters.getItem(0); + var secondary = this.invFilters.getItem(1); for (var cart : minecarts) { if (cart instanceof Locomotive locomotive) { if (primary.isEmpty() && secondary.isEmpty()) { diff --git a/src/main/java/mods/railcraft/world/level/block/entity/detector/SheepDetectorBlockEntity.java b/src/main/java/mods/railcraft/world/level/block/entity/detector/SheepDetectorBlockEntity.java index 7ccae5fc0..e853c0b2a 100644 --- a/src/main/java/mods/railcraft/world/level/block/entity/detector/SheepDetectorBlockEntity.java +++ b/src/main/java/mods/railcraft/world/level/block/entity/detector/SheepDetectorBlockEntity.java @@ -22,7 +22,7 @@ public SheepDetectorBlockEntity(BlockPos blockPos, BlockState blockState) { @Override protected int testCarts(List minecarts) { - var dyeItem = this.getItem(0); + var dyeItem = this.invFilters.getItem(0); for (var cart : minecarts) { var passengers = cart.getPassengers(); if (passengers.stream() diff --git a/src/main/java/mods/railcraft/world/level/block/entity/detector/TankDetectorBlockEntity.java b/src/main/java/mods/railcraft/world/level/block/entity/detector/TankDetectorBlockEntity.java index a2eae0574..00d543620 100644 --- a/src/main/java/mods/railcraft/world/level/block/entity/detector/TankDetectorBlockEntity.java +++ b/src/main/java/mods/railcraft/world/level/block/entity/detector/TankDetectorBlockEntity.java @@ -37,7 +37,7 @@ public TankDetectorBlockEntity(BlockPos blockPos, BlockState blockState) { } public FluidStack getFilterFluid() { - return FluidUtil.getFluidContained(this.getItem(0)).orElse(FluidStack.EMPTY); + return FluidUtil.getFluidContained(this.invFilters.getItem(0)).orElse(FluidStack.EMPTY); } @Override