ForgePatch/src/main/java/net/minecraftforge/items/wrapper/SidedInvWrapper.java

185 lines
4.6 KiB
Java
Raw Normal View History

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.
2016-01-17 16:41:34 +00:00
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);
}
}
}