ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/inventory/slot/SlotFilter.java

75 lines
2.3 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.inventory.slot;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.items.ItemFilter;
import de.ellpeck.actuallyadditions.common.tile.FilterSettings;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
2019-05-02 09:10:29 +02:00
public class SlotFilter extends SlotItemHandlerUnconditioned {
2019-05-02 09:10:29 +02:00
public SlotFilter(ItemStackHandlerAA inv, int slot, int x, int y) {
super(inv, slot, x, y);
}
2019-05-02 09:10:29 +02:00
public SlotFilter(FilterSettings inv, int slot, int x, int y) {
this(inv.filterInventory, slot, x, y);
}
2019-05-02 09:10:29 +02:00
public static boolean checkFilter(Container container, int slotId, EntityPlayer player) {
if (slotId >= 0 && slotId < container.inventorySlots.size()) {
Slot slot = container.getSlot(slotId);
2019-05-02 09:10:29 +02:00
if (slot instanceof SlotFilter) {
((SlotFilter) slot).slotClick(player);
return true;
}
}
return false;
}
2019-05-02 09:10:29 +02:00
public static boolean isFilter(ItemStack stack) {
2016-12-18 17:50:31 +01:00
return StackUtil.isValid(stack) && stack.getItem() instanceof ItemFilter;
}
2019-05-02 09:10:29 +02:00
private void slotClick(EntityPlayer player) {
ItemStack heldStack = player.inventory.getItemStack();
ItemStack stackInSlot = this.getStack();
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stackInSlot) && !StackUtil.isValid(heldStack)) {
if (isFilter(stackInSlot)) {
player.inventory.setItemStack(stackInSlot);
}
2017-11-02 22:49:53 +01:00
this.putStack(StackUtil.getEmpty());
2019-05-02 09:10:29 +02:00
} else if (StackUtil.isValid(heldStack)) {
if (!isFilter(stackInSlot)) {
2018-06-23 19:27:08 +02:00
ItemStack s = heldStack.copy();
s.setCount(1);
this.putStack(s);
2019-05-02 09:10:29 +02:00
if (isFilter(heldStack)) {
2018-06-23 19:27:08 +02:00
heldStack.shrink(1);
}
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public boolean isItemValid(ItemStack stack) {
2015-10-10 02:52:53 +02:00
return false;
}
2015-10-10 02:51:06 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void putStack(ItemStack stack) {
super.putStack(stack.copy());
2015-10-10 02:51:06 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public boolean canTakeStack(EntityPlayer player) {
2015-10-10 02:51:06 +02:00
return false;
}
}