2020-04-16 04:42:42 +02:00
|
|
|
package de.ellpeck.prettypipes.misc;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.Slot;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
import net.minecraftforge.items.SlotItemHandler;
|
|
|
|
|
2020-05-07 18:30:40 +02:00
|
|
|
public class FilterSlot extends SlotItemHandler {
|
2020-10-14 22:04:52 +02:00
|
|
|
|
|
|
|
private final boolean onlyOneItem;
|
|
|
|
|
|
|
|
public FilterSlot(IItemHandler itemHandler, int index, int xPosition, int yPosition, boolean onlyOneItem) {
|
2020-04-16 04:42:42 +02:00
|
|
|
super(itemHandler, index, xPosition, yPosition);
|
2020-10-14 22:04:52 +02:00
|
|
|
this.onlyOneItem = onlyOneItem;
|
2020-04-16 04:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean checkFilter(Container container, int slotId, PlayerEntity player) {
|
|
|
|
if (slotId >= 0 && slotId < container.inventorySlots.size()) {
|
|
|
|
Slot slot = container.getSlot(slotId);
|
2020-05-07 18:30:40 +02:00
|
|
|
if (slot instanceof FilterSlot) {
|
|
|
|
((FilterSlot) slot).slotClick(player);
|
2020-04-16 04:42:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void slotClick(PlayerEntity player) {
|
|
|
|
ItemStack heldStack = player.inventory.getItemStack();
|
|
|
|
ItemStack stackInSlot = this.getStack();
|
|
|
|
|
|
|
|
if (!stackInSlot.isEmpty() && heldStack.isEmpty()) {
|
|
|
|
this.putStack(ItemStack.EMPTY);
|
|
|
|
} else if (!heldStack.isEmpty()) {
|
|
|
|
ItemStack s = heldStack.copy();
|
2020-10-14 22:04:52 +02:00
|
|
|
if (this.onlyOneItem)
|
|
|
|
s.setCount(1);
|
2020-04-16 04:42:42 +02:00
|
|
|
this.putStack(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValid(ItemStack stack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void putStack(ItemStack stack) {
|
|
|
|
super.putStack(stack.copy());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canTakeStack(PlayerEntity playerIn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|