Fix crash related to inserting items with conduits/ducts. Closes #151

This commit is contained in:
alexbegt 2018-10-09 08:36:29 -04:00
parent d4fc2424b2
commit 379f2e92c7
4 changed files with 53 additions and 89 deletions

View File

@ -11,71 +11,43 @@ package cpw.mods.ironchest.common.lib;
import javax.annotation.Nonnull;
import cpw.mods.ironchest.common.tileentity.chest.TileEntityIronChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
public class ICChestInventoryHandler implements IItemHandlerModifiable
{
int slots;
TileEntityIronChest inv;
int slotOffset;
boolean[] canInsert;
boolean[] canExtract;
public ICChestInventoryHandler(int slots, TileEntityIronChest inventory, int slotOffset, boolean[] canInsert, boolean[] canExtract)
public ICChestInventoryHandler(TileEntityIronChest inventory)
{
this.slots = slots;
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
public int getSlots()
{
return slots;
return this.inv.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot)
{
return this.inv.getItems().get(this.slotOffset + slot);
return this.inv.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (!canInsert[slot] || stack.isEmpty())
if (stack.isEmpty())
return stack;
stack = stack.copy();
if (!inv.isItemValidForSlot(this.slotOffset + slot, stack))
if (!inv.isItemValidForSlot(slot, stack))
return stack;
int offsetSlot = this.slotOffset + slot;
int offsetSlot = slot;
ItemStack currentStack = inv.getItems().get(offsetSlot);
if (currentStack.isEmpty())
@ -144,10 +116,10 @@ public class ICChestInventoryHandler implements IItemHandlerModifiable
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (!canExtract[slot] || amount == 0)
if (amount == 0)
return ItemStack.EMPTY;
int offsetSlot = this.slotOffset + slot;
int offsetSlot = slot;
ItemStack currentStack = inv.getItems().get(offsetSlot);
if (currentStack.isEmpty())
@ -172,13 +144,18 @@ public class ICChestInventoryHandler implements IItemHandlerModifiable
@Override
public int getSlotLimit(int slot)
{
return 64;
return getInv().getInventoryStackLimit();
}
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
{
inv.getItems().set(this.slotOffset + slot, stack);
inv.getItems().set(slot, stack);
inv.markDirty();
}
public IInventory getInv()
{
return inv;
}
}

View File

@ -11,71 +11,43 @@ package cpw.mods.ironchest.common.lib;
import javax.annotation.Nonnull;
import cpw.mods.ironchest.common.tileentity.shulker.TileEntityIronShulkerBox;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
public class ICShulkerInventoryHandler implements IItemHandlerModifiable
{
int slots;
TileEntityIronShulkerBox inv;
int slotOffset;
boolean[] canInsert;
boolean[] canExtract;
public ICShulkerInventoryHandler(int slots, TileEntityIronShulkerBox inventory, int slotOffset, boolean[] canInsert, boolean[] canExtract)
public ICShulkerInventoryHandler(TileEntityIronShulkerBox inventory)
{
this.slots = slots;
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
public int getSlots()
{
return slots;
return this.inv.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot)
{
return this.inv.getItems().get(this.slotOffset + slot);
return this.inv.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (!canInsert[slot] || stack.isEmpty())
if (stack.isEmpty())
return stack;
stack = stack.copy();
if (!inv.isItemValidForSlot(this.slotOffset + slot, stack))
if (!inv.isItemValidForSlot(slot, stack))
return stack;
int offsetSlot = this.slotOffset + slot;
int offsetSlot = slot;
ItemStack currentStack = inv.getItems().get(offsetSlot);
if (currentStack.isEmpty())
@ -144,10 +116,10 @@ public class ICShulkerInventoryHandler implements IItemHandlerModifiable
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (!canExtract[slot] || amount == 0)
if (amount == 0)
return ItemStack.EMPTY;
int offsetSlot = this.slotOffset + slot;
int offsetSlot = slot;
ItemStack currentStack = inv.getItems().get(offsetSlot);
if (currentStack.isEmpty())
@ -172,13 +144,18 @@ public class ICShulkerInventoryHandler implements IItemHandlerModifiable
@Override
public int getSlotLimit(int slot)
{
return 64;
return getInv().getInventoryStackLimit();
}
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
{
inv.getItems().set(this.slotOffset + slot, stack);
inv.getItems().set(slot, stack);
inv.markDirty();
}
public IInventory getInv()
{
return inv;
}
}

View File

@ -653,14 +653,20 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick
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")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return (T) insertionHandler;
return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
return super.getCapability(capability, facing);
}
}

View File

@ -844,22 +844,26 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements
fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntityIronShulkerBox.class, new String[] { "Items" }));
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return true;
return super.hasCapability(capability, facing);
}
private IItemHandler itemHandler;
IItemHandler insertionHandler = new ICShulkerInventoryHandler(this.getType().size, this);
@Override
protected IItemHandler createUnSidedHandler()
{
return new ICShulkerInventoryHandler(this);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return (T) insertionHandler;
return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
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);
}
}