NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/ItemStackHandlerNA.java

65 lines
1.7 KiB
Java
Raw Normal View History

2018-10-18 13:34:37 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.item.ItemStack;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.items.ItemStackHandler;
2018-10-18 13:34:37 +02:00
import javax.annotation.Nonnull;
public class ItemStackHandlerNA extends ItemStackHandler {
2021-12-04 15:40:09 +01:00
private final BlockEntityImpl tile;
2018-10-18 13:34:37 +02:00
private final boolean sendToClients;
public ItemStackHandlerNA(int size) {
this(size, null, false);
}
2021-12-04 15:40:09 +01:00
public ItemStackHandlerNA(int size, BlockEntityImpl tile, boolean sendToClients) {
2018-10-18 13:34:37 +02:00
super(size);
this.tile = tile;
this.sendToClients = sendToClients;
}
@Override
protected void onContentsChanged(int slot) {
2018-12-27 13:57:23 +01:00
if (this.tile != null) {
2021-12-06 14:38:12 +01:00
this.tile.setChanged();
2021-12-04 15:40:09 +01:00
if (this.sendToClients && !this.tile.getLevel().isClientSide)
2018-12-27 13:57:23 +01:00
this.tile.sendToClients();
2018-10-18 13:34:37 +02:00
}
}
protected boolean canInsert(ItemStack stack, int slot) {
return true;
}
protected boolean canExtract(ItemStack stack, int slot, int amount) {
return true;
}
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return this.canInsert(stack, slot);
}
@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (this.canInsert(stack, slot)) {
return super.insertItem(slot, stack, simulate);
} else {
return stack;
}
}
@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (this.canExtract(this.getStackInSlot(slot), slot, amount)) {
return super.extractItem(slot, amount, simulate);
} else {
return ItemStack.EMPTY;
}
}
}