2020-04-16 04:42:42 +02:00
|
|
|
package de.ellpeck.prettypipes.misc;
|
|
|
|
|
2021-12-02 14:44:26 +01:00
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2020-04-16 04:42:42 +02:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
import net.minecraftforge.items.SlotItemHandler;
|
2021-12-02 14:44:26 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-04-16 04:42:42 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-02 14:44:26 +01:00
|
|
|
public static boolean checkFilter(AbstractContainerMenu container, int slotId, Player player) {
|
|
|
|
if (slotId >= 0 && slotId < container.slots.size()) {
|
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:44:26 +01:00
|
|
|
private void slotClick(Player player) {
|
|
|
|
var heldStack = player.inventoryMenu.getCarried();
|
|
|
|
var stackInSlot = this.getItem();
|
2020-04-16 04:42:42 +02:00
|
|
|
|
|
|
|
if (!stackInSlot.isEmpty() && heldStack.isEmpty()) {
|
2021-12-02 14:44:26 +01:00
|
|
|
this.safeInsert(ItemStack.EMPTY);
|
2020-04-16 04:42:42 +02:00
|
|
|
} else if (!heldStack.isEmpty()) {
|
2021-12-02 14:44:26 +01:00
|
|
|
var s = heldStack.copy();
|
2020-10-14 22:04:52 +02:00
|
|
|
if (this.onlyOneItem)
|
|
|
|
s.setCount(1);
|
2021-12-02 14:44:26 +01:00
|
|
|
this.safeInsert(s);
|
2020-04-16 04:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean mayPlace(@NotNull ItemStack stack) {
|
2020-04-16 04:42:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public ItemStack safeInsert(ItemStack stack) {
|
|
|
|
return super.safeInsert(stack.copy());
|
2020-04-16 04:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean mayPickup(Player playerIn) {
|
2020-04-16 04:42:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-02 14:44:26 +01:00
|
|
|
|
2020-04-16 04:42:42 +02:00
|
|
|
}
|