Fix crash related to inserting items with conduits/ducts. Closes #151
This commit is contained in:
parent
d4fc2424b2
commit
379f2e92c7
|
@ -11,71 +11,43 @@ package cpw.mods.ironchest.common.lib;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import cpw.mods.ironchest.common.tileentity.chest.TileEntityIronChest;
|
import cpw.mods.ironchest.common.tileentity.chest.TileEntityIronChest;
|
||||||
|
import net.minecraft.inventory.IInventory;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||||
import net.minecraftforge.items.ItemHandlerHelper;
|
import net.minecraftforge.items.ItemHandlerHelper;
|
||||||
|
|
||||||
public class ICChestInventoryHandler implements IItemHandlerModifiable
|
public class ICChestInventoryHandler implements IItemHandlerModifiable
|
||||||
{
|
{
|
||||||
int slots;
|
|
||||||
|
|
||||||
TileEntityIronChest inv;
|
TileEntityIronChest inv;
|
||||||
|
|
||||||
int slotOffset;
|
public ICChestInventoryHandler(TileEntityIronChest inventory)
|
||||||
|
|
||||||
boolean[] canInsert;
|
|
||||||
|
|
||||||
boolean[] canExtract;
|
|
||||||
|
|
||||||
public ICChestInventoryHandler(int slots, TileEntityIronChest inventory, int slotOffset, boolean[] canInsert, boolean[] canExtract)
|
|
||||||
{
|
{
|
||||||
this.slots = slots;
|
|
||||||
this.inv = inventory;
|
this.inv = inventory;
|
||||||
this.slotOffset = slotOffset;
|
|
||||||
this.canInsert = canInsert;
|
|
||||||
this.canExtract = canExtract;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICChestInventoryHandler(int slots, TileEntityIronChest inventory)
|
|
||||||
{
|
|
||||||
this(slots, inventory, 0, new boolean[slots], new boolean[slots]);
|
|
||||||
for (int i = 0; i < slots; i++)
|
|
||||||
this.canExtract[i] = this.canInsert[i] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICChestInventoryHandler(int slots, TileEntityIronChest inventory, int slotOffset, boolean canInsert, boolean canExtract)
|
|
||||||
{
|
|
||||||
this(slots, inventory, slotOffset, new boolean[slots], new boolean[slots]);
|
|
||||||
for (int i = 0; i < slots; i++)
|
|
||||||
{
|
|
||||||
this.canInsert[i] = canInsert;
|
|
||||||
this.canExtract[i] = canExtract;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getSlots()
|
public int getSlots()
|
||||||
{
|
{
|
||||||
return slots;
|
return this.inv.getSizeInventory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getStackInSlot(int slot)
|
public ItemStack getStackInSlot(int slot)
|
||||||
{
|
{
|
||||||
return this.inv.getItems().get(this.slotOffset + slot);
|
return this.inv.getStackInSlot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
|
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
|
||||||
{
|
{
|
||||||
if (!canInsert[slot] || stack.isEmpty())
|
if (stack.isEmpty())
|
||||||
return stack;
|
return stack;
|
||||||
stack = stack.copy();
|
stack = stack.copy();
|
||||||
|
|
||||||
if (!inv.isItemValidForSlot(this.slotOffset + slot, stack))
|
if (!inv.isItemValidForSlot(slot, stack))
|
||||||
return stack;
|
return stack;
|
||||||
|
|
||||||
int offsetSlot = this.slotOffset + slot;
|
int offsetSlot = slot;
|
||||||
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
||||||
|
|
||||||
if (currentStack.isEmpty())
|
if (currentStack.isEmpty())
|
||||||
|
@ -144,10 +116,10 @@ public class ICChestInventoryHandler implements IItemHandlerModifiable
|
||||||
@Override
|
@Override
|
||||||
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
||||||
{
|
{
|
||||||
if (!canExtract[slot] || amount == 0)
|
if (amount == 0)
|
||||||
return ItemStack.EMPTY;
|
return ItemStack.EMPTY;
|
||||||
|
|
||||||
int offsetSlot = this.slotOffset + slot;
|
int offsetSlot = slot;
|
||||||
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
||||||
|
|
||||||
if (currentStack.isEmpty())
|
if (currentStack.isEmpty())
|
||||||
|
@ -172,13 +144,18 @@ public class ICChestInventoryHandler implements IItemHandlerModifiable
|
||||||
@Override
|
@Override
|
||||||
public int getSlotLimit(int slot)
|
public int getSlotLimit(int slot)
|
||||||
{
|
{
|
||||||
return 64;
|
return getInv().getInventoryStackLimit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
|
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
inv.getItems().set(this.slotOffset + slot, stack);
|
inv.getItems().set(slot, stack);
|
||||||
inv.markDirty();
|
inv.markDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IInventory getInv()
|
||||||
|
{
|
||||||
|
return inv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,71 +11,43 @@ package cpw.mods.ironchest.common.lib;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import cpw.mods.ironchest.common.tileentity.shulker.TileEntityIronShulkerBox;
|
import cpw.mods.ironchest.common.tileentity.shulker.TileEntityIronShulkerBox;
|
||||||
|
import net.minecraft.inventory.IInventory;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||||
import net.minecraftforge.items.ItemHandlerHelper;
|
import net.minecraftforge.items.ItemHandlerHelper;
|
||||||
|
|
||||||
public class ICShulkerInventoryHandler implements IItemHandlerModifiable
|
public class ICShulkerInventoryHandler implements IItemHandlerModifiable
|
||||||
{
|
{
|
||||||
int slots;
|
|
||||||
|
|
||||||
TileEntityIronShulkerBox inv;
|
TileEntityIronShulkerBox inv;
|
||||||
|
|
||||||
int slotOffset;
|
public ICShulkerInventoryHandler(TileEntityIronShulkerBox inventory)
|
||||||
|
|
||||||
boolean[] canInsert;
|
|
||||||
|
|
||||||
boolean[] canExtract;
|
|
||||||
|
|
||||||
public ICShulkerInventoryHandler(int slots, TileEntityIronShulkerBox inventory, int slotOffset, boolean[] canInsert, boolean[] canExtract)
|
|
||||||
{
|
{
|
||||||
this.slots = slots;
|
|
||||||
this.inv = inventory;
|
this.inv = inventory;
|
||||||
this.slotOffset = slotOffset;
|
|
||||||
this.canInsert = canInsert;
|
|
||||||
this.canExtract = canExtract;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICShulkerInventoryHandler(int slots, TileEntityIronShulkerBox inventory)
|
|
||||||
{
|
|
||||||
this(slots, inventory, 0, new boolean[slots], new boolean[slots]);
|
|
||||||
for (int i = 0; i < slots; i++)
|
|
||||||
this.canExtract[i] = this.canInsert[i] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICShulkerInventoryHandler(int slots, TileEntityIronShulkerBox inventory, int slotOffset, boolean canInsert, boolean canExtract)
|
|
||||||
{
|
|
||||||
this(slots, inventory, slotOffset, new boolean[slots], new boolean[slots]);
|
|
||||||
for (int i = 0; i < slots; i++)
|
|
||||||
{
|
|
||||||
this.canInsert[i] = canInsert;
|
|
||||||
this.canExtract[i] = canExtract;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getSlots()
|
public int getSlots()
|
||||||
{
|
{
|
||||||
return slots;
|
return this.inv.getSizeInventory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getStackInSlot(int slot)
|
public ItemStack getStackInSlot(int slot)
|
||||||
{
|
{
|
||||||
return this.inv.getItems().get(this.slotOffset + slot);
|
return this.inv.getStackInSlot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
|
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
|
||||||
{
|
{
|
||||||
if (!canInsert[slot] || stack.isEmpty())
|
if (stack.isEmpty())
|
||||||
return stack;
|
return stack;
|
||||||
stack = stack.copy();
|
stack = stack.copy();
|
||||||
|
|
||||||
if (!inv.isItemValidForSlot(this.slotOffset + slot, stack))
|
if (!inv.isItemValidForSlot(slot, stack))
|
||||||
return stack;
|
return stack;
|
||||||
|
|
||||||
int offsetSlot = this.slotOffset + slot;
|
int offsetSlot = slot;
|
||||||
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
||||||
|
|
||||||
if (currentStack.isEmpty())
|
if (currentStack.isEmpty())
|
||||||
|
@ -144,10 +116,10 @@ public class ICShulkerInventoryHandler implements IItemHandlerModifiable
|
||||||
@Override
|
@Override
|
||||||
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
||||||
{
|
{
|
||||||
if (!canExtract[slot] || amount == 0)
|
if (amount == 0)
|
||||||
return ItemStack.EMPTY;
|
return ItemStack.EMPTY;
|
||||||
|
|
||||||
int offsetSlot = this.slotOffset + slot;
|
int offsetSlot = slot;
|
||||||
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
ItemStack currentStack = inv.getItems().get(offsetSlot);
|
||||||
|
|
||||||
if (currentStack.isEmpty())
|
if (currentStack.isEmpty())
|
||||||
|
@ -172,13 +144,18 @@ public class ICShulkerInventoryHandler implements IItemHandlerModifiable
|
||||||
@Override
|
@Override
|
||||||
public int getSlotLimit(int slot)
|
public int getSlotLimit(int slot)
|
||||||
{
|
{
|
||||||
return 64;
|
return getInv().getInventoryStackLimit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
|
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
inv.getItems().set(this.slotOffset + slot, stack);
|
inv.getItems().set(slot, stack);
|
||||||
inv.markDirty();
|
inv.markDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IInventory getInv()
|
||||||
|
{
|
||||||
|
return inv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -653,14 +653,20 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick
|
||||||
return super.hasCapability(capability, facing);
|
return super.hasCapability(capability, facing);
|
||||||
}
|
}
|
||||||
|
|
||||||
IItemHandler insertionHandler = new ICChestInventoryHandler(this.getType().size, this);
|
private IItemHandler itemHandler;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IItemHandler createUnSidedHandler()
|
||||||
|
{
|
||||||
|
return new ICChestInventoryHandler(this);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||||
{
|
{
|
||||||
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
|
||||||
return (T) insertionHandler;
|
return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
|
||||||
return super.getCapability(capability, facing);
|
return super.getCapability(capability, facing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -844,22 +844,26 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements
|
||||||
fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntityIronShulkerBox.class, new String[] { "Items" }));
|
fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntityIronShulkerBox.class, new String[] { "Items" }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private IItemHandler itemHandler;
|
||||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
|
||||||
{
|
|
||||||
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
|
|
||||||
return true;
|
|
||||||
return super.hasCapability(capability, facing);
|
|
||||||
}
|
|
||||||
|
|
||||||
IItemHandler insertionHandler = new ICShulkerInventoryHandler(this.getType().size, this);
|
@Override
|
||||||
|
protected IItemHandler createUnSidedHandler()
|
||||||
|
{
|
||||||
|
return new ICShulkerInventoryHandler(this);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||||
{
|
{
|
||||||
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
|
||||||
return (T) insertionHandler;
|
return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
|
||||||
return super.getCapability(capability, facing);
|
return super.getCapability(capability, facing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
|
||||||
|
{
|
||||||
|
return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue