Skip to content

Commit

Permalink
Fix crash related to inserting items with conduits/ducts. Closes #151
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbegt committed Oct 9, 2018
1 parent d4fc242 commit 379f2e9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,43 @@
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())
Expand Down Expand Up @@ -144,10 +116,10 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
@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())
Expand All @@ -172,13 +144,18 @@ public ItemStack extractItem(int slot, int amount, boolean simulate)
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,43 @@
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())
Expand Down Expand Up @@ -144,10 +116,10 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
@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())
Expand All @@ -172,13 +144,18 @@ public ItemStack extractItem(int slot, int amount, boolean simulate)
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,20 @@ public boolean hasCapability(Capability<?> capability, EnumFacing 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")
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -844,22 +844,26 @@ public static void registerFixesShulkerBox(DataFixer fixer)
fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntityIronShulkerBox.class, new String[] { "Items" }));
}

private IItemHandler itemHandler;

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
protected IItemHandler createUnSidedHandler()
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return true;
return super.hasCapability(capability, facing);
return new ICShulkerInventoryHandler(this);
}

IItemHandler insertionHandler = new ICShulkerInventoryHandler(this.getType().size, 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);
}
}

0 comments on commit 379f2e9

Please sign in to comment.