Skip to content

Commit

Permalink
Fix #265
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Nov 12, 2024
1 parent 3e646eb commit 024533f
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 58 deletions.
11 changes: 0 additions & 11 deletions src/main/java/mods/railcraft/Railcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import mods.railcraft.world.level.block.entity.SteamOvenBlockEntity;
import mods.railcraft.world.level.block.entity.SteamTurbineBlockEntity;
import mods.railcraft.world.level.block.entity.WaterTankSidingBlockEntity;
import mods.railcraft.world.level.block.entity.detector.FilterDetectorBlockEntity;
import mods.railcraft.world.level.block.entity.manipulator.CartDispenserBlockEntity;
import mods.railcraft.world.level.block.entity.manipulator.FluidLoaderBlockEntity;
import mods.railcraft.world.level.block.entity.manipulator.FluidUnloaderBlockEntity;
Expand Down Expand Up @@ -258,16 +257,6 @@ private void handleRegisterCapabilities(RegisterCapabilitiesEvent event) {
RailcraftBlockEntityTypes.ITEM_LOADER.get(), ItemLoaderBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.ITEM_UNLOADER.get(), ItemUnloaderBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.ADVANCED_DETECTOR.get(), FilterDetectorBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.ITEM_DETECTOR.get(), FilterDetectorBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.LOCOMOTIVE_DETECTOR.get(), FilterDetectorBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.SHEEP_DETECTOR.get(), FilterDetectorBlockEntity::getItemCap);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK,
RailcraftBlockEntityTypes.TANK_DETECTOR.get(), FilterDetectorBlockEntity::getItemCap);

event.registerItem(Capabilities.FluidHandler.ITEM,
(stack, ctx) -> new CreosoteBottleWrapper(stack), Items.GLASS_BOTTLE);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/mods/railcraft/util/container/AdvancedContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -50,6 +51,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);
Expand Down Expand Up @@ -183,4 +188,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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,36 @@
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.core.HolderLookup;
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.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.wrapper.InvWrapper;

public abstract class FilterDetectorBlockEntity extends DetectorBlockEntity
implements MenuProvider, ForwardingContainer, ContainerManipulator<ModifiableSlotAccessor> {
implements MenuProvider, ContainerManipulator<ModifiableSlotAccessor> {

private final AdvancedContainer invFilters;
private IItemHandler itemHandler = new InvWrapper(this);
private Map<Direction, IItemHandler> 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<ModifiableSlotAccessor> 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;
}

public IItemHandler getItemCap(@Nullable Direction side) {
if (side == null) {
return this.itemHandler;
}
return this.directionalItemHandlers
.computeIfAbsent(side, __ -> ItemHandlerFactory.wrap(this, side));
public Stream<ModifiableSlotAccessor> stream() {
return this.invFilters.stream();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected int testCarts(List<AbstractMinecart> minecarts) {
}

private boolean matchesFilter(ContainerManipulator<SlotAccessor> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public LocomotiveDetectorBlockEntity(BlockPos blockPos, BlockState blockState) {

@Override
protected int testCarts(List<AbstractMinecart> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SheepDetectorBlockEntity(BlockPos blockPos, BlockState blockState) {

@Override
protected int testCarts(List<AbstractMinecart> minecarts) {
var dyeItem = this.getItem(0);
var dyeItem = this.invFilters.getItem(0);
for (var cart : minecarts) {
var passengers = cart.getPassengers();
if (passengers.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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
Expand Down

0 comments on commit 024533f

Please sign in to comment.