public class ItemStackHandler extends java.lang.Object implements IItemHandler, IItemHandlerModifiable, INBTSerializable<CompoundNBT>
Modifier and Type | Field and Description |
---|---|
protected NonNullList<ItemStack> |
stacks |
Constructor and Description |
---|
ItemStackHandler() |
ItemStackHandler(int size) |
ItemStackHandler(NonNullList<ItemStack> stacks) |
Modifier and Type | Method and Description |
---|---|
void |
deserializeNBT(CompoundNBT nbt) |
ItemStack |
extractItem(int slot,
int amount,
boolean simulate)
Extracts an ItemStack from the given slot.
|
int |
getSlotLimit(int slot)
Retrieves the maximum stack size allowed to exist in the given slot.
|
int |
getSlots()
Returns the number of slots available
|
ItemStack |
getStackInSlot(int slot)
Returns the ItemStack in a given slot.
|
protected int |
getStackLimit(int slot,
ItemStack stack) |
ItemStack |
insertItem(int slot,
ItemStack stack,
boolean simulate)
Inserts an ItemStack into the given slot and return the remainder.
|
boolean |
isItemValid(int slot,
ItemStack stack)
This function re-implements the vanilla function
IInventory.isItemValidForSlot(int, ItemStack) . |
protected void |
onContentsChanged(int slot) |
protected void |
onLoad() |
CompoundNBT |
serializeNBT() |
void |
setSize(int size) |
void |
setStackInSlot(int slot,
ItemStack stack)
Overrides the stack in the given slot.
|
protected void |
validateSlotIndex(int slot) |
protected NonNullList<ItemStack> stacks
public ItemStackHandler()
public ItemStackHandler(int size)
public ItemStackHandler(NonNullList<ItemStack> stacks)
public void setSize(int size)
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
IItemHandlerModifiable
setStackInSlot
in interface IItemHandlerModifiable
slot
- Slot to modifystack
- ItemStack to set slot to (may be empty).public int getSlots()
IItemHandler
getSlots
in interface IItemHandler
@Nonnull public ItemStack getStackInSlot(int slot)
IItemHandler
IMPORTANT: This ItemStack MUST NOT be modified. This method is not for altering an inventory's contents. Any implementers who are able to detect modification through this method should throw an exception.
SERIOUSLY: DO NOT MODIFY THE RETURNED ITEMSTACK
getStackInSlot
in interface IItemHandler
slot
- Slot to query@Nonnull public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
IItemHandler
Inserts an ItemStack into the given slot and return the remainder. The ItemStack should not be modified in this function!
Note: This behaviour is subtly different fromIFluidHandler#fill(FluidStack, boolean)
insertItem
in interface IItemHandler
slot
- Slot to insert into.stack
- ItemStack to insert. This must not be modified by the item handler.simulate
- If true, the insertion is only simulated@Nonnull public ItemStack extractItem(int slot, int amount, boolean simulate)
IItemHandler
The returned value must be empty if nothing is extracted,
otherwise its stack size must be less than or equal to amount
and ItemStack.getMaxStackSize()
.
extractItem
in interface IItemHandler
slot
- Slot to extract from.amount
- Amount to extract (may be greater than the current stack's max limit)simulate
- If true, the extraction is only simulatedpublic int getSlotLimit(int slot)
IItemHandler
getSlotLimit
in interface IItemHandler
slot
- Slot to query.protected int getStackLimit(int slot, @Nonnull ItemStack stack)
public boolean isItemValid(int slot, @Nonnull ItemStack stack)
IItemHandler
This function re-implements the vanilla function IInventory.isItemValidForSlot(int, ItemStack)
.
It should be used instead of simulated insertions in cases where the contents and state of the inventory are
irrelevant, mainly for the purpose of automation and logic (for instance, testing if a minecart can wait
to deposit its items into a full inventory, or if the items in the minecart can never be placed into the
inventory and should move on).
isItemValid
in interface IItemHandler
slot
- Slot to query for validitystack
- Stack to test with for validitypublic CompoundNBT serializeNBT()
serializeNBT
in interface INBTSerializable<CompoundNBT>
public void deserializeNBT(CompoundNBT nbt)
deserializeNBT
in interface INBTSerializable<CompoundNBT>
protected void validateSlotIndex(int slot)
protected void onLoad()
protected void onContentsChanged(int slot)