From d4fc2424b209cb3a54f81ffebfc88ba048f2ad6f Mon Sep 17 00:00:00 2001 From: alexbegt Date: Thu, 4 Oct 2018 23:12:34 -0400 Subject: [PATCH] Hopefully fix the disappearing issue with the chests. Closes #142 --- .../common/lib/ICChestInventoryHandler.java | 184 ++++++++++++++++++ .../common/lib/ICShulkerInventoryHandler.java | 184 ++++++++++++++++++ .../tileentity/chest/TileEntityIronChest.java | 23 +++ .../shulker/TileEntityIronShulkerBox.java | 33 +++- 4 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cpw/mods/ironchest/common/lib/ICChestInventoryHandler.java create mode 100644 src/main/java/cpw/mods/ironchest/common/lib/ICShulkerInventoryHandler.java diff --git a/src/main/java/cpw/mods/ironchest/common/lib/ICChestInventoryHandler.java b/src/main/java/cpw/mods/ironchest/common/lib/ICChestInventoryHandler.java new file mode 100644 index 0000000..4a64ad6 --- /dev/null +++ b/src/main/java/cpw/mods/ironchest/common/lib/ICChestInventoryHandler.java @@ -0,0 +1,184 @@ +/* + * BluSunrize + * Copyright (c) 2017 + * + * This code is licensed under "Blu's License of Common Sense" + * Details can be found in the license file in the root folder of this project + */ + +package cpw.mods.ironchest.common.lib; + +import javax.annotation.Nonnull; + +import cpw.mods.ironchest.common.tileentity.chest.TileEntityIronChest; +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) + { + 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; + } + + @Override + public ItemStack getStackInSlot(int slot) + { + return this.inv.getItems().get(this.slotOffset + slot); + } + + @Override + public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) + { + if (!canInsert[slot] || stack.isEmpty()) + return stack; + stack = stack.copy(); + + if (!inv.isItemValidForSlot(this.slotOffset + slot, stack)) + return stack; + + int offsetSlot = this.slotOffset + slot; + ItemStack currentStack = inv.getItems().get(offsetSlot); + + if (currentStack.isEmpty()) + { + int accepted = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()); + if (accepted < stack.getCount()) + { + if (!simulate) + { + inv.getItems().set(offsetSlot, stack.splitStack(accepted)); + inv.markDirty(); + return stack; + } + else + { + stack.shrink(accepted); + return stack; + } + } + else + { + if (!simulate) + { + inv.getItems().set(offsetSlot, stack); + inv.markDirty(); + } + return ItemStack.EMPTY; + } + } + else + { + if (!ItemHandlerHelper.canItemStacksStack(stack, currentStack)) + return stack; + + int accepted = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()) - currentStack.getCount(); + if (accepted < stack.getCount()) + { + if (!simulate) + { + ItemStack newStack = stack.splitStack(accepted); + newStack.grow(currentStack.getCount()); + inv.getItems().set(offsetSlot, newStack); + inv.markDirty(); + return stack; + } + else + { + stack.shrink(accepted); + return stack; + } + } + else + { + if (!simulate) + { + ItemStack newStack = stack.copy(); + newStack.grow(currentStack.getCount()); + inv.getItems().set(offsetSlot, newStack); + inv.markDirty(); + } + return ItemStack.EMPTY; + } + } + } + + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) + { + if (!canExtract[slot] || amount == 0) + return ItemStack.EMPTY; + + int offsetSlot = this.slotOffset + slot; + ItemStack currentStack = inv.getItems().get(offsetSlot); + + if (currentStack.isEmpty()) + return ItemStack.EMPTY; + + int extracted = Math.min(currentStack.getCount(), amount); + + ItemStack copy = currentStack.copy(); + copy.setCount(extracted); + if (!simulate) + { + if (extracted < currentStack.getCount()) + currentStack.shrink(extracted); + else + currentStack = ItemStack.EMPTY; + inv.getItems().set(offsetSlot, currentStack); + inv.markDirty(); + } + return copy; + } + + @Override + public int getSlotLimit(int slot) + { + return 64; + } + + @Override + public void setStackInSlot(int slot, @Nonnull ItemStack stack) + { + inv.getItems().set(this.slotOffset + slot, stack); + inv.markDirty(); + } +} diff --git a/src/main/java/cpw/mods/ironchest/common/lib/ICShulkerInventoryHandler.java b/src/main/java/cpw/mods/ironchest/common/lib/ICShulkerInventoryHandler.java new file mode 100644 index 0000000..2209d4c --- /dev/null +++ b/src/main/java/cpw/mods/ironchest/common/lib/ICShulkerInventoryHandler.java @@ -0,0 +1,184 @@ +/* + * BluSunrize + * Copyright (c) 2017 + * + * This code is licensed under "Blu's License of Common Sense" + * Details can be found in the license file in the root folder of this project + */ + +package cpw.mods.ironchest.common.lib; + +import javax.annotation.Nonnull; + +import cpw.mods.ironchest.common.tileentity.shulker.TileEntityIronShulkerBox; +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) + { + 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; + } + + @Override + public ItemStack getStackInSlot(int slot) + { + return this.inv.getItems().get(this.slotOffset + slot); + } + + @Override + public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) + { + if (!canInsert[slot] || stack.isEmpty()) + return stack; + stack = stack.copy(); + + if (!inv.isItemValidForSlot(this.slotOffset + slot, stack)) + return stack; + + int offsetSlot = this.slotOffset + slot; + ItemStack currentStack = inv.getItems().get(offsetSlot); + + if (currentStack.isEmpty()) + { + int accepted = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()); + if (accepted < stack.getCount()) + { + if (!simulate) + { + inv.getItems().set(offsetSlot, stack.splitStack(accepted)); + inv.markDirty(); + return stack; + } + else + { + stack.shrink(accepted); + return stack; + } + } + else + { + if (!simulate) + { + inv.getItems().set(offsetSlot, stack); + inv.markDirty(); + } + return ItemStack.EMPTY; + } + } + else + { + if (!ItemHandlerHelper.canItemStacksStack(stack, currentStack)) + return stack; + + int accepted = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()) - currentStack.getCount(); + if (accepted < stack.getCount()) + { + if (!simulate) + { + ItemStack newStack = stack.splitStack(accepted); + newStack.grow(currentStack.getCount()); + inv.getItems().set(offsetSlot, newStack); + inv.markDirty(); + return stack; + } + else + { + stack.shrink(accepted); + return stack; + } + } + else + { + if (!simulate) + { + ItemStack newStack = stack.copy(); + newStack.grow(currentStack.getCount()); + inv.getItems().set(offsetSlot, newStack); + inv.markDirty(); + } + return ItemStack.EMPTY; + } + } + } + + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) + { + if (!canExtract[slot] || amount == 0) + return ItemStack.EMPTY; + + int offsetSlot = this.slotOffset + slot; + ItemStack currentStack = inv.getItems().get(offsetSlot); + + if (currentStack.isEmpty()) + return ItemStack.EMPTY; + + int extracted = Math.min(currentStack.getCount(), amount); + + ItemStack copy = currentStack.copy(); + copy.setCount(extracted); + if (!simulate) + { + if (extracted < currentStack.getCount()) + currentStack.shrink(extracted); + else + currentStack = ItemStack.EMPTY; + inv.getItems().set(offsetSlot, currentStack); + inv.markDirty(); + } + return copy; + } + + @Override + public int getSlotLimit(int slot) + { + return 64; + } + + @Override + public void setStackInSlot(int slot, @Nonnull ItemStack stack) + { + inv.getItems().set(this.slotOffset + slot, stack); + inv.markDirty(); + } +} diff --git a/src/main/java/cpw/mods/ironchest/common/tileentity/chest/TileEntityIronChest.java b/src/main/java/cpw/mods/ironchest/common/tileentity/chest/TileEntityIronChest.java index b54013a..75e30fc 100755 --- a/src/main/java/cpw/mods/ironchest/common/tileentity/chest/TileEntityIronChest.java +++ b/src/main/java/cpw/mods/ironchest/common/tileentity/chest/TileEntityIronChest.java @@ -18,6 +18,7 @@ import cpw.mods.ironchest.common.blocks.chest.BlockIronChest; import cpw.mods.ironchest.common.blocks.chest.IronChestType; import cpw.mods.ironchest.common.core.IronChestBlocks; import cpw.mods.ironchest.common.gui.chest.ContainerIronChest; +import cpw.mods.ironchest.common.lib.ICChestInventoryHandler; import cpw.mods.ironchest.common.network.MessageCrystalChestSync; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; @@ -39,8 +40,11 @@ import net.minecraft.util.datafix.DataFixer; import net.minecraft.util.datafix.FixTypes; import net.minecraft.util.datafix.walkers.ItemStackDataLists; import net.minecraft.util.math.AxisAlignedBB; +import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.Constants; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; public class TileEntityIronChest extends TileEntityLockableLoot implements ITickable { @@ -640,4 +644,23 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick { fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntityIronChest.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); + } + + IItemHandler insertionHandler = new ICChestInventoryHandler(this.getType().size, this); + + @SuppressWarnings("unchecked") + @Override + public T getCapability(Capability capability, EnumFacing facing) + { + if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + return (T) insertionHandler; + return super.getCapability(capability, facing); + } } diff --git a/src/main/java/cpw/mods/ironchest/common/tileentity/shulker/TileEntityIronShulkerBox.java b/src/main/java/cpw/mods/ironchest/common/tileentity/shulker/TileEntityIronShulkerBox.java index 2d41ae1..622e745 100644 --- a/src/main/java/cpw/mods/ironchest/common/tileentity/shulker/TileEntityIronShulkerBox.java +++ b/src/main/java/cpw/mods/ironchest/common/tileentity/shulker/TileEntityIronShulkerBox.java @@ -20,6 +20,7 @@ import cpw.mods.ironchest.IronChest; import cpw.mods.ironchest.common.blocks.shulker.BlockIronShulkerBox; import cpw.mods.ironchest.common.blocks.shulker.IronShulkerBoxType; import cpw.mods.ironchest.common.gui.shulker.ContainerIronShulkerBox; +import cpw.mods.ironchest.common.lib.ICShulkerInventoryHandler; import cpw.mods.ironchest.common.network.MessageCrystalShulkerSync; import net.minecraft.block.Block; import net.minecraft.block.BlockShulkerBox; @@ -48,9 +49,12 @@ import net.minecraft.util.datafix.DataFixer; import net.minecraft.util.datafix.FixTypes; import net.minecraft.util.datafix.walkers.ItemStackDataLists; import net.minecraft.util.math.AxisAlignedBB; +import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements ITickable, ISidedInventory { @@ -75,12 +79,19 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements private boolean hadStuff; private boolean hasBeenCleared; + private int openCount; + private AnimationStatus animationStatus; + private float progress; + private float progressOld; + private EnumDyeColor color; + private boolean destroyedByCreativePlayer; + private boolean hasBeenUpgraded; /** The Variant of the Shulker Box (Not Color) */ @@ -783,7 +794,8 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements return this.isDestroyedByCreativePlayer() && (!this.isEmpty() || this.hasCustomName() || this.lootTable != null); } - public ItemStack getDrop(IBlockState state, boolean inBreakBlock) { + public ItemStack getDrop(IBlockState state, boolean inBreakBlock) + { BlockIronShulkerBox block = (BlockIronShulkerBox) state.getBlock(); if (!isCleared() && (!inBreakBlock || shouldDropInBreakBlock())) { @@ -831,4 +843,23 @@ 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); + } + + IItemHandler insertionHandler = new ICShulkerInventoryHandler(this.getType().size, this); + + @SuppressWarnings("unchecked") + @Override + public T getCapability(Capability capability, EnumFacing facing) + { + if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + return (T) insertionHandler; + return super.getCapability(capability, facing); + } }