PrettyPipes/src/main/java/de/ellpeck/prettypipes/misc/FilterSlot.java

55 lines
1.6 KiB
Java
Raw Normal View History

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;
2024-02-03 15:17:58 +01:00
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.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
}
public static boolean checkFilter(AbstractContainerMenu menu, int slotId) {
if (slotId >= 0 && slotId < menu.slots.size()) {
var slot = menu.getSlot(slotId);
2020-05-07 18:30:40 +02:00
if (slot instanceof FilterSlot) {
((FilterSlot) slot).slotClick(menu);
2020-04-16 04:42:42 +02:00
return true;
}
}
return false;
}
private void slotClick(AbstractContainerMenu menu) {
var heldStack = menu.getCarried();
2021-12-02 14:44:26 +01:00
var stackInSlot = this.getItem();
2020-04-16 04:42:42 +02:00
if (!stackInSlot.isEmpty() && heldStack.isEmpty()) {
this.set(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);
this.set(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 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
}