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 80b2fa6 commit 31ad06a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 55 deletions.
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.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
}
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,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<ModifiableSlotAccessor> {
implements MenuProvider, ContainerManipulator<ModifiableSlotAccessor> {

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

@Override
public <T> LazyOptional<T> getCapability(Capability<T> 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<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 @@ -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
Expand Down

0 comments on commit 31ad06a

Please sign in to comment.