2020-09-09 16:49:01 +02:00
|
|
|
package de.ellpeck.actuallyadditions.common.inventory.slot;
|
2016-12-04 00:10:52 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-09-09 16:49:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA;
|
2016-12-04 00:10:52 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.items.SlotItemHandler;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public class SlotItemHandlerUnconditioned extends SlotItemHandler {
|
2016-12-04 00:10:52 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private final ItemStackHandlerAA inv;
|
2016-12-04 00:10:52 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public SlotItemHandlerUnconditioned(ItemStackHandlerAA inv, int index, int xPosition, int yPosition) {
|
|
|
|
super(inv, index, xPosition, yPosition);
|
|
|
|
this.inv = inv;
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean isItemValid(ItemStack stack) {
|
2019-02-27 19:53:05 +01:00
|
|
|
if (stack.isEmpty() || !this.inv.canAccept(this.getSlotIndex(), stack, false)) return false;
|
2018-06-23 01:39:30 +02:00
|
|
|
|
|
|
|
ItemStack currentStack = this.inv.getStackInSlot(this.getSlotIndex());
|
|
|
|
this.inv.setStackInSlot(this.getSlotIndex(), ItemStack.EMPTY);
|
|
|
|
ItemStack remainder = this.inv.insertItem(this.getSlotIndex(), stack, true, false);
|
|
|
|
this.inv.setStackInSlot(this.getSlotIndex(), currentStack);
|
|
|
|
return remainder.isEmpty() || remainder.getCount() < stack.getCount();
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper fnct to get the stack in the slot.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Nonnull
|
2018-06-23 01:39:30 +02:00
|
|
|
public ItemStack getStack() {
|
|
|
|
return this.inv.getStackInSlot(this.getSlotIndex());
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public void putStack(ItemStack stack) {
|
|
|
|
this.inv.setStackInSlot(this.getSlotIndex(), stack);
|
2016-12-04 00:10:52 +01:00
|
|
|
this.onSlotChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getItemStackLimit(ItemStack stack) {
|
2016-12-04 00:10:52 +01:00
|
|
|
ItemStack maxAdd = stack.copy();
|
|
|
|
maxAdd.setCount(stack.getMaxStackSize());
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack currentStack = this.inv.getStackInSlot(this.getSlotIndex());
|
|
|
|
this.inv.setStackInSlot(this.getSlotIndex(), ItemStack.EMPTY);
|
|
|
|
ItemStack remainder = this.inv.insertItem(this.getSlotIndex(), maxAdd, true, false);
|
|
|
|
this.inv.setStackInSlot(this.getSlotIndex(), currentStack);
|
|
|
|
return stack.getMaxStackSize() - remainder.getCount();
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean canTakeStack(EntityPlayer playerIn) {
|
|
|
|
return !this.inv.extractItem(this.getSlotIndex(), 1, true, false).isEmpty();
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public ItemStack decrStackSize(int amount) {
|
|
|
|
return this.inv.extractItem(this.getSlotIndex(), amount, false, false);
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
}
|