Skip to content

Commit

Permalink
feat: Make the filter slot in the vacuum chest work
Browse files Browse the repository at this point in the history
Closes: GH-772
  • Loading branch information
Rover656 committed Sep 26, 2024
1 parent befdbca commit e124c0d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.enderio.machines.common.blockentity;

import com.enderio.api.filter.ItemStackFilter;
import com.enderio.api.filter.ResourceFilter;
import com.enderio.base.common.init.EIOCapabilities;
import com.enderio.base.common.item.filter.ItemFilter;
import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity;
import com.enderio.machines.common.config.MachinesConfig;
import com.enderio.machines.common.io.item.MachineInventoryLayout;
import com.enderio.machines.common.io.item.MachineInventoryLayout.Builder;
import com.enderio.machines.common.io.item.SingleSlotAccess;
import com.enderio.machines.common.menu.VacuumChestMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -13,11 +18,14 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;

import java.util.function.Predicate;

public class VacuumChestBlockEntity extends VacuumMachineBlockEntity<ItemEntity> {

public static final SingleSlotAccess FILTER_SLOT = new SingleSlotAccess();

public VacuumChestBlockEntity(BlockEntityType<?> pType, BlockPos pWorldPosition, BlockState pBlockState) {
super(pType, pWorldPosition, pBlockState, ItemEntity.class);
}
Expand All @@ -30,8 +38,13 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl
@Override
public MachineInventoryLayout getInventoryLayout() {
return extractableGUISlot(MachineInventoryLayout.builder(), 27)
.slot(slot -> slot.guiInsert().guiExtract().filter((i, s) -> false))
.build(); //TODO add proper filter slot and predicate
.slot(slot -> slot.guiInsert().guiExtract().filter(this::acceptFilter))
.slotAccess(FILTER_SLOT)
.build();
}

private boolean acceptFilter(int slot, ItemStack itemStack) {
return itemStack.getCapability(EIOCapabilities.ITEM_FILTER).isPresent();
}

@Override
Expand All @@ -52,12 +65,16 @@ public String getColor() {
return MachinesConfig.CLIENT.BLOCKS.VACUUM_CHEST_RANGE_COLOR.get();
}

//TODO filter
@Override
public Predicate<ItemEntity> getFilter() {
// get filter slot -> get filter item -> filter
// maybe cache on item insert
return super.getFilter();
ItemStack filterItemStack = FILTER_SLOT.getItemStack(this);
LazyOptional<ItemStackFilter> itemStackFilterOptional = filterItemStack.getCapability(EIOCapabilities.ITEM_FILTER);

boolean t = itemStackFilterOptional.isPresent();

return itemStackFilterOptional
.map(filter -> (Predicate<ItemEntity>)(ItemEntity itemEntity) -> filter.test(itemEntity.getItem()))
.orElse(ITEM_ENTITY_FILTER_TRUE);
}

// Slot config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.enderio.machines.common.io.FixedIOConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -27,6 +28,8 @@ public abstract class VacuumMachineBlockEntity<T extends Entity> extends Machine
private List<WeakReference<T>> entities = new ArrayList<>();
private Class<T> targetClass;

protected static final Predicate<ItemEntity> ITEM_ENTITY_FILTER_TRUE = itemEntity -> true;

public VacuumMachineBlockEntity(BlockEntityType<?> pType, BlockPos pWorldPosition, BlockState pBlockState, Class<T> targetClass) {
super(pType, pWorldPosition, pBlockState);
this.targetClass = targetClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public class VacuumChestMenu extends MachineMenu<VacuumChestBlockEntity> {

public VacuumChestMenu(VacuumChestBlockEntity blockEntity, Inventory inventory, int pContainerId) {
super(blockEntity, inventory, MachineMenus.VACUUM_CHEST.get(), pContainerId);

if (blockEntity != null) {
// Filter slot first, means shift+right click targets it first
this.addSlot(new MachineSlot(blockEntity.getInventory(), VacuumChestBlockEntity.FILTER_SLOT, 8, 86));

for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 9; ++k) {
this.addSlot(new MachineSlot(blockEntity.getInventory(), k + j * 9, 8 + k * 18, 18 + j * 18));
}
}
this.addSlot(new MachineSlot(blockEntity.getInventory(), 27, 8, 86));
}
addInventorySlots(8, 124);
}
Expand All @@ -33,4 +36,4 @@ public static VacuumChestMenu factory(@Nullable MenuType<VacuumChestMenu> pMenuT
LogManager.getLogger().warn("couldn't find BlockEntity");
return new VacuumChestMenu(null, inventory, pContainerId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.enderio.api.capability.ISideConfig;
import com.enderio.api.capability.IToggled;
import com.enderio.api.capacitor.ICapacitorData;
import com.enderio.api.filter.FluidStackFilter;
import com.enderio.api.filter.ItemStackFilter;
import com.enderio.api.filter.ResourceFilter;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
Expand All @@ -33,6 +35,8 @@ public class EIOCapabilities {
public static final Capability<ISideConfig> SIDE_CONFIG = CapabilityManager.get(new CapabilityToken<>() {});

public static final Capability<ResourceFilter> FILTER = CapabilityManager.get(new CapabilityToken<>() {});
public static final Capability<ItemStackFilter> ITEM_FILTER = CapabilityManager.get(new CapabilityToken<>() {});
public static final Capability<FluidStackFilter> FLUID_FILTER = CapabilityManager.get(new CapabilityToken<>() {});

@SubscribeEvent
public static void register(RegisterCapabilitiesEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public FluidFilter(Properties pProperties, int size) {
return new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction direction) {
if (capability == EIOCapabilities.FILTER) {
if (capability == EIOCapabilities.FILTER || capability == EIOCapabilities.FLUID_FILTER) {
return LazyOptional.of(() -> new FluidFilterCapability(stack, size)).cast();
}
return LazyOptional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ItemFilter(Properties pProperties, int size) {
return new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction direction) {
if (capability == EIOCapabilities.FILTER) {
if (capability == EIOCapabilities.FILTER || capability == EIOCapabilities.ITEM_FILTER) {
return LazyOptional.of(() -> new ItemFilterCapability(stack, size)).cast();
}
return LazyOptional.empty();
Expand Down

0 comments on commit e124c0d

Please sign in to comment.