Add IItemHandler capability

Add the actual patches that I forgot.

Add simple implementations of IStorage and the factory methods.

Add ItemStackHandler. A simple IItemHandler implementaton.

return nulls, not throw nulls.

Move the vanilla wrappers to a separate class for now.

Minor clean ups of VanillaWrapper code.

Inline static methods.

Add comments.

Minor cleanup of code.

Remove redundant size field and add a validate slot index method.

Minor formatting issues.

Break early If stacksize to insert is 0.

Remove setByte() methods.

Throw exception if IItemHandler can't be modifyed in NBT loading.

Replace event handler with patches

Add capability to mine cart inventory entities.

Change formatting and registration of capability.

Make InventoryPlayer implements IItemHandler because why not. Also added a field to allow mods that add additional player inventory space to publicly expose them.

Reduce patch sizes

Lazy initialization of the item handler for vanilla tiles.

Minor formatting changes.

Create a single vanilla chest item handler that will merge with adjacent chests when detected. Added hooks to reset the cached adjacent value when a block update is detected and when a chunk loads.

Revert "Make InventoryPlayer implements IItemHandler because why not. Also added a field to allow mods that add additional player inventory space to publicly expose them."

This reverts commit 306d4a37fd0e8c8a0754411c013b750dfe8e2c87.

Fix furnace derp

Replace double chest code with a simpler method.

Vanilla wrappers implement IItemHandlerModifiable (since they are modifiable)

Minor code cleanups

Add an onContentsChanged() and onLoad() callback methods.to the default implementation.

Add slot as a parameter in the callback method.

Change IItemHandlerModifiable.setStackInSlot() to void, and added a note about not being intended for cross-mod use.

Improve ItemStackHandler handling of errored NBT.

Make the stacks array protected.

Fix a lot of derps in SlotItemHandler.

Fix derp in ItemStackHandler

Clarify comments on IItemHandler

ItemStackHandler no longer caches the stack array in local variable.

Clean up the Chests code to make intentions clearer

Vanilla hoppers have their cooldown activated when an item is inserted. Made this behavior part of an item handler (rather than the insertion code)

Fix mistake in ItemStackHandler

More documentation of potential edge cases in getStackInSlot()

Make limit checking more resiliant.
This commit is contained in:
rwtema committed 2016-01-24 05:20:58 +00:00
1 parent b8238e7f22
commit a1e41f5464
20 files changed
+1299 -1

No files matched your search

@@ -0,0 +1,33 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
public class EmptyHandler implements IItemHandler
{
public static IItemHandler INSTANCE = new EmptyHandler();
@Override
public int getSlots()
{
return 0;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return null;
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
return stack;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
return null;
}
}
@@ -0,0 +1,166 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
public class InvWrapper implements IItemHandlerModifiable
{
public final IInventory inv;
public InvWrapper(IInventory inv)
{
this.inv = inv;
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
InvWrapper that = (InvWrapper) o;
return inv.equals(that.inv);
}
@Override
public int hashCode()
{
return inv.hashCode();
}
@Override
public int getSlots()
{
return inv.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot)
{
return inv.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (stack == null)
return null;
if (!inv.isItemValidForSlot(slot, stack))
return stack;
ItemStack stackInSlot = inv.getStackInSlot(slot);
int m;
if (stackInSlot != null)
{
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()) - stackInSlot.stackSize;
if (stack.stackSize <= m)
{
if (!simulate)
{
ItemStack copy = stack.copy();
copy.stackSize += stackInSlot.stackSize;
inv.setInventorySlotContents(slot, copy);
inv.markDirty();
}
return null;
}
else
{
if (!simulate)
{
ItemStack copy = stack.splitStack(m);
copy.stackSize += stackInSlot.stackSize;
inv.setInventorySlotContents(slot, copy);
inv.markDirty();
return stack;
}
else
{
stack.stackSize -= m;
return stack;
}
}
}
else
{
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
if (m < stack.stackSize)
{
if (!simulate)
{
inv.setInventorySlotContents(slot, stack.splitStack(m));
inv.markDirty();
return stack;
}
else
{
stack.stackSize -= m;
return stack;
}
}
else
{
if (!simulate)
{
inv.setInventorySlotContents(slot, stack);
inv.markDirty();
}
return null;
}
}
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (amount == 0)
return null;
ItemStack stackInSlot = inv.getStackInSlot(slot);
if (stackInSlot == null)
return null;
if (simulate)
{
if (stackInSlot.stackSize < amount)
{
return stackInSlot.copy();
}
else
{
ItemStack copy = stackInSlot.copy();
copy.stackSize = amount;
return copy;
}
}
else
{
int m = Math.min(stackInSlot.stackSize, amount);
ItemStack decrStackSize = inv.decrStackSize(slot, m);
inv.markDirty();
return decrStackSize;
}
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
inv.setInventorySlotContents(slot, stack);
}
}
@@ -0,0 +1,184 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
public class SidedInvWrapper implements IItemHandlerModifiable
{
protected final ISidedInventory inv;
protected final EnumFacing side;
public SidedInvWrapper(ISidedInventory inv, EnumFacing side)
{
this.inv = inv;
this.side = side;
}
public static int getSlot(ISidedInventory inv, int slot, EnumFacing side)
{
int[] slots = inv.getSlotsForFace(side);
if (slot < slots.length)
return slots[slot];
return -1;
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
SidedInvWrapper that = (SidedInvWrapper) o;
return inv.equals(that.inv) && side == that.side;
}
@Override
public int hashCode()
{
int result = inv.hashCode();
result = 31 * result + side.hashCode();
return result;
}
@Override
public int getSlots()
{
return inv.getSlotsForFace(side).length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
int i = getSlot(inv, slot, side);
return i == -1 ? null : inv.getStackInSlot(i);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (stack == null)
return null;
int slot1 = getSlot(inv, slot, side);
if (slot1 == -1)
return stack;
if (!inv.isItemValidForSlot(slot1, stack) || !inv.canInsertItem(slot1, stack, side))
return stack;
ItemStack stackInSlot = inv.getStackInSlot(slot1);
int m;
if (stackInSlot != null)
{
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()) - stackInSlot.stackSize;
if (stack.stackSize <= m)
{
if (!simulate)
{
ItemStack copy = stack.copy();
copy.stackSize += stackInSlot.stackSize;
inv.setInventorySlotContents(slot1, copy);
}
return null;
}
else
{
if (!simulate)
{
ItemStack copy = stack.splitStack(m);
copy.stackSize += stackInSlot.stackSize;
inv.setInventorySlotContents(slot1, copy);
return stack;
}
else
{
stack.stackSize -= m;
return stack;
}
}
}
else
{
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
if (m < stack.stackSize)
{
if (!simulate)
{
inv.setInventorySlotContents(slot1, stack.splitStack(m));
return stack;
}
else
{
stack.stackSize -= m;
return stack;
}
}
else
{
if (!simulate)
inv.setInventorySlotContents(slot1, stack);
return null;
}
}
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
inv.setInventorySlotContents(slot, stack);
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (amount == 0)
return null;
int slot1 = getSlot(inv, slot, side);
if (slot1 == -1)
return null;
ItemStack stackInSlot = inv.getStackInSlot(slot1);
if (stackInSlot == null)
return null;
if (!inv.canExtractItem(slot1, stackInSlot, side))
return null;
if (simulate)
{
if (stackInSlot.stackSize < amount)
{
return stackInSlot.copy();
}
else
{
ItemStack copy = stackInSlot.copy();
copy.stackSize = amount;
return copy;
}
}
else
{
int m = Math.min(stackInSlot.stackSize, amount);
return inv.decrStackSize(slot1, m);
}
}
}