From c8b3568a0caa09ef975bf691a12ba7245ee4a976 Mon Sep 17 00:00:00 2001 From: Ourten Date: Sun, 18 Dec 2016 22:06:07 +0100 Subject: [PATCH] Unify TechReborn chests + Fix deep chests inventory drops --- .../techreborn/blocks/BlockDigitalChest.java | 72 ++++- .../techreborn/blocks/BlockQuantumChest.java | 72 +++++ .../techreborn/tiles/TileDigitalChest.java | 222 +--------------- .../techreborn/tiles/TileQuantumChest.java | 221 +--------------- .../techreborn/tiles/TileTechStorageBase.java | 246 ++++++++++++++++++ 5 files changed, 395 insertions(+), 438 deletions(-) create mode 100644 src/main/java/techreborn/tiles/TileTechStorageBase.java diff --git a/src/main/java/techreborn/blocks/BlockDigitalChest.java b/src/main/java/techreborn/blocks/BlockDigitalChest.java index 4ad4856a4..ca0624393 100644 --- a/src/main/java/techreborn/blocks/BlockDigitalChest.java +++ b/src/main/java/techreborn/blocks/BlockDigitalChest.java @@ -1,14 +1,28 @@ package techreborn.blocks; +import net.minecraft.block.BlockDynamicLiquid; +import net.minecraft.block.BlockStaticLiquid; +import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.fluids.BlockFluidBase; + import reborncore.common.blocks.BlockMachineBase; import reborncore.common.blocks.IAdvancedRotationTexture; import techreborn.Core; import techreborn.client.GuiHandler; import techreborn.client.TechRebornCreativeTab; import techreborn.tiles.TileDigitalChest; +import techreborn.tiles.TileTechStorageBase; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; public class BlockDigitalChest extends BlockMachineBase implements IAdvancedRotationTexture { @@ -20,6 +34,63 @@ public class BlockDigitalChest extends BlockMachineBase implements IAdvancedRota setCreativeTab(TechRebornCreativeTab.instance); } + @Override + protected void dropInventory(World world, BlockPos pos) { + TileEntity tileEntity = world.getTileEntity(pos); + + if (tileEntity == null) { + return; + } + if (!(tileEntity instanceof TileTechStorageBase)) { + return; + } + + TileTechStorageBase inventory = (TileTechStorageBase) tileEntity; + + List items = new ArrayList(); + + List droppables = inventory.getContentDrops(); + for (int i = 0; i < droppables.size(); i++) { + ItemStack itemStack = droppables.get(i); + + if (itemStack == ItemStack.EMPTY) { + continue; + } + if (itemStack != ItemStack.EMPTY && itemStack.getCount() > 0) { + if (itemStack.getItem() instanceof ItemBlock) { + if (((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase + || ((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid + || ((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid) { + continue; + } + } + } + items.add(itemStack.copy()); + } + + for (ItemStack itemStack : items) { + Random rand = new Random(); + + float dX = rand.nextFloat() * 0.8F + 0.1F; + float dY = rand.nextFloat() * 0.8F + 0.1F; + float dZ = rand.nextFloat() * 0.8F + 0.1F; + + EntityItem entityItem = new EntityItem(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ, + itemStack.copy()); + + if (itemStack.hasTagCompound()) { + entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy()); + } + + float factor = 0.05F; + entityItem.motionX = rand.nextGaussian() * factor; + entityItem.motionY = rand.nextGaussian() * factor + 0.2F; + entityItem.motionZ = rand.nextGaussian() * factor; + world.spawnEntity(entityItem); + itemStack.setCount(0); + } + } + @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { return new TileDigitalChest(); @@ -52,5 +123,4 @@ public class BlockDigitalChest extends BlockMachineBase implements IAdvancedRota public String getBottom(boolean isActive) { return prefix + "machine_bottom"; } - } diff --git a/src/main/java/techreborn/blocks/BlockQuantumChest.java b/src/main/java/techreborn/blocks/BlockQuantumChest.java index 473a28d31..ca4aa6342 100644 --- a/src/main/java/techreborn/blocks/BlockQuantumChest.java +++ b/src/main/java/techreborn/blocks/BlockQuantumChest.java @@ -1,18 +1,33 @@ package techreborn.blocks; +import net.minecraft.block.BlockDynamicLiquid; +import net.minecraft.block.BlockStaticLiquid; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.fluids.BlockFluidBase; + import reborncore.common.blocks.BlockMachineBase; import reborncore.common.blocks.IAdvancedRotationTexture; + import techreborn.Core; import techreborn.client.GuiHandler; import techreborn.client.TechRebornCreativeTab; import techreborn.tiles.TileQuantumChest; +import techreborn.tiles.TileTechStorageBase; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; public class BlockQuantumChest extends BlockMachineBase implements IAdvancedRotationTexture { @@ -25,6 +40,63 @@ public class BlockQuantumChest extends BlockMachineBase implements IAdvancedRota setHardness(2.0F); } + @Override + protected void dropInventory(World world, BlockPos pos) { + TileEntity tileEntity = world.getTileEntity(pos); + + if (tileEntity == null) { + return; + } + if (!(tileEntity instanceof TileTechStorageBase)) { + return; + } + + TileTechStorageBase inventory = (TileTechStorageBase) tileEntity; + + List items = new ArrayList(); + + List droppables = inventory.getContentDrops(); + for (int i = 0; i < droppables.size(); i++) { + ItemStack itemStack = droppables.get(i); + + if (itemStack == ItemStack.EMPTY) { + continue; + } + if (itemStack != ItemStack.EMPTY && itemStack.getCount() > 0) { + if (itemStack.getItem() instanceof ItemBlock) { + if (((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase + || ((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid + || ((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid) { + continue; + } + } + } + items.add(itemStack.copy()); + } + + for (ItemStack itemStack : items) { + Random rand = new Random(); + + float dX = rand.nextFloat() * 0.8F + 0.1F; + float dY = rand.nextFloat() * 0.8F + 0.1F; + float dZ = rand.nextFloat() * 0.8F + 0.1F; + + EntityItem entityItem = new EntityItem(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ, + itemStack.copy()); + + if (itemStack.hasTagCompound()) { + entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy()); + } + + float factor = 0.05F; + entityItem.motionX = rand.nextGaussian() * factor; + entityItem.motionY = rand.nextGaussian() * factor + 0.2F; + entityItem.motionZ = rand.nextGaussian() * factor; + world.spawnEntity(entityItem); + itemStack.setCount(0); + } + } + @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { return new TileQuantumChest(); diff --git a/src/main/java/techreborn/tiles/TileDigitalChest.java b/src/main/java/techreborn/tiles/TileDigitalChest.java index b8216fc54..c5bea87bb 100644 --- a/src/main/java/techreborn/tiles/TileDigitalChest.java +++ b/src/main/java/techreborn/tiles/TileDigitalChest.java @@ -1,224 +1,8 @@ package techreborn.tiles; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.NetworkManager; -import net.minecraft.network.play.server.SPacketUpdateTileEntity; -import net.minecraft.util.EnumFacing; +public class TileDigitalChest extends TileTechStorageBase { -import powercrystals.minefactoryreloaded.api.IDeepStorageUnit; - -import reborncore.api.IListInfoProvider; -import reborncore.api.tile.IInventoryProvider; -import reborncore.common.IWrenchable; -import reborncore.common.tile.TileLegacyMachineBase; -import reborncore.common.util.Inventory; -import reborncore.common.util.ItemUtils; - -import techreborn.init.ModBlocks; - -import java.util.List; - -public class TileDigitalChest extends TileLegacyMachineBase - implements IInventoryProvider, IWrenchable, IListInfoProvider, IDeepStorageUnit { - - // Slot 0 = Input - // Slot 1 = Output - // Slot 2 = Fake Item - - public ItemStack storedItem = ItemStack.EMPTY; - - int storage = Short.MAX_VALUE; - public Inventory inventory = new Inventory(3, "TileDigitalChest", storage, this); - - @Override - public void updateEntity() { - if (!world.isRemote) { - if (this.getStackInSlot(0) != ItemStack.EMPTY) { - if (this.getStoredItemType().isEmpty() || (this.storedItem.isEmpty() - && ItemUtils.isItemEqual(this.getStackInSlot(0), this.getStackInSlot(1), true, true))) { - - this.storedItem = this.getStackInSlot(0); - this.setInventorySlotContents(0, ItemStack.EMPTY); - this.syncWithAll(); - } else if (ItemUtils.isItemEqual(this.getStoredItemType(), this.getStackInSlot(0), true, true)) { - - this.setStoredItemCount(this.getStackInSlot(0).getCount()); - this.setInventorySlotContents(0, ItemStack.EMPTY); - this.syncWithAll(); - } - } - - if (this.storedItem != ItemStack.EMPTY) { - if (this.getStackInSlot(1) == ItemStack.EMPTY) { - - ItemStack delivered = this.storedItem.copy(); - delivered.setCount(Math.min(this.storedItem.getCount(), delivered.getMaxStackSize())); - - this.storedItem.shrink(delivered.getCount()); - - if (this.storedItem.isEmpty()) - this.storedItem = ItemStack.EMPTY; - - this.setInventorySlotContents(1, delivered); - this.syncWithAll(); - } else if (ItemUtils.isItemEqual(this.storedItem, this.getStackInSlot(1), true, true) - && this.getStackInSlot(1).getCount() < this.getStackInSlot(1).getMaxStackSize()) { - - int wanted = Math.min(this.storedItem.getCount(), - this.getStackInSlot(1).getMaxStackSize() - this.getStackInSlot(1).getCount()); - - this.getStackInSlot(1).setCount(this.getStackInSlot(1).getCount() + wanted); - this.storedItem.shrink(wanted); - - if (this.storedItem.isEmpty()) - this.storedItem = ItemStack.EMPTY; - this.syncWithAll(); - } - } - - if (this.getStackInSlot(2) == ItemStack.EMPTY - && (!this.storedItem.isEmpty() || !this.getStackInSlot(1).isEmpty())) { - - ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); - fake.setCount(1); - - this.setInventorySlotContents(2, fake); - } else if (!ItemUtils.isItemEqual(this.getStackInSlot(2), this.storedItem, true, true)) { - - ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); - fake.setCount(1); - - this.setInventorySlotContents(2, fake); - } - } - } - - @Override - public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) { - world.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(), - getPos().getY(), getPos().getZ()); - readFromNBT(packet.getNbtCompound()); - } - - @Override - public void readFromNBT(NBTTagCompound tagCompound) { - super.readFromNBT(tagCompound); - readFromNBTWithoutCoords(tagCompound); - } - - public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) { - - storedItem = ItemStack.EMPTY; - - if (tagCompound.hasKey("storedStack")) { - storedItem = new ItemStack((NBTTagCompound) tagCompound.getTag("storedStack")); - } - - if (storedItem != ItemStack.EMPTY) { - storedItem.setCount(tagCompound.getInteger("storedQuantity")); - } - } - - @Override - public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) { - super.writeToNBT(tagCompound); - writeToNBTWithoutCoords(tagCompound); - return tagCompound; - } - - public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound) { - if (storedItem != ItemStack.EMPTY) { - tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound())); - tagCompound.setInteger("storedQuantity", storedItem.getCount()); - } else { - tagCompound.setInteger("storedQuantity", 0); - } - - return tagCompound; - } - - @Override - public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) { - return false; - } - - @Override - public EnumFacing getFacing() { - return getFacingEnum(); - } - - @Override - public boolean wrenchCanRemove(EntityPlayer entityPlayer) { - return entityPlayer.isSneaking(); - } - - @Override - public float getWrenchDropRate() { - return 1F; - } - - @Override - public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { - return getDropWithNBT(); - } - - public ItemStack getDropWithNBT() { - NBTTagCompound tileEntity = new NBTTagCompound(); - ItemStack dropStack = new ItemStack(ModBlocks.digitalChest, 1); - writeToNBTWithoutCoords(tileEntity); - dropStack.setTagCompound(new NBTTagCompound()); - dropStack.getTagCompound().setTag("tileEntity", tileEntity); - return dropStack; - } - - @Override - public ItemStack getStoredItemType() { - return this.storedItem.isEmpty() ? this.getStackInSlot(1) : this.storedItem; - } - - @Override - public void setStoredItemCount(int amount) { - storedItem.grow(amount); - this.markDirty(); - } - - @Override - public void setStoredItemType(ItemStack type, int amount) { - this.storedItem = type; - storedItem.setCount(amount); - this.markDirty(); - } - - @Override - public int getMaxStoredCount() { - return this.storage; - } - - public int getStoredCount() { - return this.storedItem.getCount(); - } - - @Override - public void addInfo(List info, boolean isRealTile) { - if (isRealTile) { - int size = 0; - String name = "of nothing"; - if (storedItem != ItemStack.EMPTY) { - name = storedItem.getDisplayName(); - size += storedItem.getCount(); - } - if (getStackInSlot(1) != ItemStack.EMPTY) { - name = getStackInSlot(1).getDisplayName(); - size += getStackInSlot(1).getCount(); - } - info.add(size + " " + name); - } - } - - @Override - public Inventory getInventory() { - return inventory; + public TileDigitalChest() { + super("TileDigitalChest", Short.MAX_VALUE); } } diff --git a/src/main/java/techreborn/tiles/TileQuantumChest.java b/src/main/java/techreborn/tiles/TileQuantumChest.java index 65fe6b50f..c636234d7 100644 --- a/src/main/java/techreborn/tiles/TileQuantumChest.java +++ b/src/main/java/techreborn/tiles/TileQuantumChest.java @@ -1,223 +1,8 @@ package techreborn.tiles; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.NetworkManager; -import net.minecraft.network.play.server.SPacketUpdateTileEntity; -import net.minecraft.util.EnumFacing; -import powercrystals.minefactoryreloaded.api.IDeepStorageUnit; -import reborncore.api.IListInfoProvider; -import reborncore.api.tile.IInventoryProvider; -import reborncore.common.IWrenchable; -import reborncore.common.tile.TileLegacyMachineBase; -import reborncore.common.util.Inventory; -import reborncore.common.util.ItemUtils; -import techreborn.init.ModBlocks; +public class TileQuantumChest extends TileTechStorageBase { -import javax.annotation.Nonnull; -import java.util.List; - -public class TileQuantumChest extends TileLegacyMachineBase - implements IInventoryProvider, IWrenchable, IDeepStorageUnit, IListInfoProvider { - - // Slot 0 = Input - // Slot 1 = Output - // Slot 2 = Fake Item - - public ItemStack storedItem = ItemStack.EMPTY; - // TODO use long so we can have 9,223,372,036,854,775,807 items instead of - // 2,147,483,647 - int storage = Integer.MAX_VALUE; - public Inventory inventory = new Inventory(3, "TileQuantumChest", storage, this); - - @Override - public void updateEntity() { - if (!world.isRemote) { - if (this.getStackInSlot(0) != ItemStack.EMPTY) { - if (this.getStoredItemType().isEmpty() || (this.storedItem.isEmpty() - && ItemUtils.isItemEqual(this.getStackInSlot(0), this.getStackInSlot(1), true, true))) { - - this.storedItem = this.getStackInSlot(0); - this.setInventorySlotContents(0, ItemStack.EMPTY); - this.syncWithAll(); - } else if (ItemUtils.isItemEqual(this.getStoredItemType(), this.getStackInSlot(0), true, true)) { - - this.setStoredItemCount(this.getStackInSlot(0).getCount()); - this.setInventorySlotContents(0, ItemStack.EMPTY); - this.syncWithAll(); - } - } - - if (this.storedItem != ItemStack.EMPTY) { - if (this.getStackInSlot(1) == ItemStack.EMPTY) { - - ItemStack delivered = this.storedItem.copy(); - delivered.setCount(Math.min(this.storedItem.getCount(), delivered.getMaxStackSize())); - - this.storedItem.shrink(delivered.getCount()); - - if (this.storedItem.isEmpty()) - this.storedItem = ItemStack.EMPTY; - - this.setInventorySlotContents(1, delivered); - this.syncWithAll(); - } else if (ItemUtils.isItemEqual(this.storedItem, this.getStackInSlot(1), true, true) - && this.getStackInSlot(1).getCount() < this.getStackInSlot(1).getMaxStackSize()) { - - int wanted = Math.min(this.storedItem.getCount(), - this.getStackInSlot(1).getMaxStackSize() - this.getStackInSlot(1).getCount()); - - this.getStackInSlot(1).setCount(this.getStackInSlot(1).getCount() + wanted); - this.storedItem.shrink(wanted); - - if (this.storedItem.isEmpty()) - this.storedItem = ItemStack.EMPTY; - this.syncWithAll(); - } - } - - if (this.getStackInSlot(2) == ItemStack.EMPTY - && (!this.storedItem.isEmpty() || !this.getStackInSlot(1).isEmpty())) { - - ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); - fake.setCount(1); - - this.setInventorySlotContents(2, fake); - } else if (!ItemUtils.isItemEqual(this.getStackInSlot(2), this.storedItem, true, true)) { - - ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); - fake.setCount(1); - - this.setInventorySlotContents(2, fake); - } - } - } - - @Override - public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) { - world.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(), - getPos().getY(), getPos().getZ()); - readFromNBT(packet.getNbtCompound()); - } - - @Override - public void readFromNBT(NBTTagCompound tagCompound) { - super.readFromNBT(tagCompound); - readFromNBTWithoutCoords(tagCompound); - } - - public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) { - - storedItem = ItemStack.EMPTY; - - if (tagCompound.hasKey("storedStack")) { - storedItem = new ItemStack((NBTTagCompound) tagCompound.getTag("storedStack")); - } - - if (storedItem != ItemStack.EMPTY) { - storedItem.setCount(tagCompound.getInteger("storedQuantity")); - } - } - - @Override - public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) { - super.writeToNBT(tagCompound); - writeToNBTWithoutCoords(tagCompound); - return tagCompound; - } - - public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound) { - if (storedItem != ItemStack.EMPTY) { - tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound())); - tagCompound.setInteger("storedQuantity", storedItem.getCount()); - } else { - tagCompound.setInteger("storedQuantity", 0); - } - - return tagCompound; - } - - @Override - public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) { - return false; - } - - @Override - public EnumFacing getFacing() { - return getFacingEnum(); - } - - @Override - public boolean wrenchCanRemove(EntityPlayer entityPlayer) { - return entityPlayer.isSneaking(); - } - - @Override - public float getWrenchDropRate() { - return 1F; - } - - @Override - public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { - return getDropWithNBT(); - } - - public ItemStack getDropWithNBT() { - NBTTagCompound tileEntity = new NBTTagCompound(); - ItemStack dropStack = new ItemStack(ModBlocks.quantumChest, 1); - writeToNBTWithoutCoords(tileEntity); - dropStack.setTagCompound(new NBTTagCompound()); - dropStack.getTagCompound().setTag("tileEntity", tileEntity); - return dropStack; - } - - @Override - public ItemStack getStoredItemType() { - return this.storedItem.isEmpty() ? this.getStackInSlot(1) : this.storedItem; - } - - @Override - public void setStoredItemCount(int amount) { - storedItem.grow(amount); - this.markDirty(); - } - - @Override - public void setStoredItemType(ItemStack type, int amount) { - this.storedItem = type; - storedItem.setCount(amount); - this.markDirty(); - } - - @Override - public int getMaxStoredCount() { - return this.storage; - } - - public int getStoredCount() { - return this.storedItem.getCount(); - } - - @Override - public void addInfo(List info, boolean isRealTile) { - if (isRealTile) { - int size = 0; - String name = "of nothing"; - if (storedItem != ItemStack.EMPTY) { - name = storedItem.getDisplayName(); - size += storedItem.getCount(); - } - if (getStackInSlot(1) != ItemStack.EMPTY) { - name = getStackInSlot(1).getDisplayName(); - size += getStackInSlot(1).getCount(); - } - info.add(size + " " + name); - } - } - - @Override - public Inventory getInventory() { - return inventory; + public TileQuantumChest() { + super("TileQuantumChest", Integer.MAX_VALUE); } } diff --git a/src/main/java/techreborn/tiles/TileTechStorageBase.java b/src/main/java/techreborn/tiles/TileTechStorageBase.java new file mode 100644 index 000000000..d62f44082 --- /dev/null +++ b/src/main/java/techreborn/tiles/TileTechStorageBase.java @@ -0,0 +1,246 @@ +package techreborn.tiles; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.play.server.SPacketUpdateTileEntity; +import net.minecraft.util.EnumFacing; + +import powercrystals.minefactoryreloaded.api.IDeepStorageUnit; + +import reborncore.api.IListInfoProvider; +import reborncore.api.tile.IInventoryProvider; +import reborncore.common.IWrenchable; +import reborncore.common.tile.TileLegacyMachineBase; +import reborncore.common.util.Inventory; +import reborncore.common.util.ItemUtils; + +import java.util.ArrayList; +import java.util.List; + +public class TileTechStorageBase extends TileLegacyMachineBase + implements IInventoryProvider, IWrenchable, IListInfoProvider, IDeepStorageUnit { + + public ItemStack storedItem; + + public final int maxCapacity; + public final Inventory inventory; + + public TileTechStorageBase(String name, int maxCapacity) { + this.maxCapacity = maxCapacity; + storedItem = ItemStack.EMPTY; + inventory = new Inventory(3, name, maxCapacity, this); + } + + @Override + public void updateEntity() { + if (!world.isRemote) { + if (this.getStackInSlot(0) != ItemStack.EMPTY) { + if (this.getStoredItemType().isEmpty() || (this.storedItem.isEmpty() + && ItemUtils.isItemEqual(this.getStackInSlot(0), this.getStackInSlot(1), true, true))) { + + this.storedItem = this.getStackInSlot(0); + this.setInventorySlotContents(0, ItemStack.EMPTY); + this.syncWithAll(); + } else if (ItemUtils.isItemEqual(this.getStoredItemType(), this.getStackInSlot(0), true, true)) { + + this.setStoredItemCount(this.getStackInSlot(0).getCount()); + this.setInventorySlotContents(0, ItemStack.EMPTY); + this.syncWithAll(); + } + } + + if (this.storedItem != ItemStack.EMPTY) { + if (this.getStackInSlot(1) == ItemStack.EMPTY) { + + ItemStack delivered = this.storedItem.copy(); + delivered.setCount(Math.min(this.storedItem.getCount(), delivered.getMaxStackSize())); + + this.storedItem.shrink(delivered.getCount()); + + if (this.storedItem.isEmpty()) + this.storedItem = ItemStack.EMPTY; + + this.setInventorySlotContents(1, delivered); + this.syncWithAll(); + } else if (ItemUtils.isItemEqual(this.storedItem, this.getStackInSlot(1), true, true) + && this.getStackInSlot(1).getCount() < this.getStackInSlot(1).getMaxStackSize()) { + + int wanted = Math.min(this.storedItem.getCount(), + this.getStackInSlot(1).getMaxStackSize() - this.getStackInSlot(1).getCount()); + + this.getStackInSlot(1).setCount(this.getStackInSlot(1).getCount() + wanted); + this.storedItem.shrink(wanted); + + if (this.storedItem.isEmpty()) + this.storedItem = ItemStack.EMPTY; + this.syncWithAll(); + } + } + + if (this.getStackInSlot(2) == ItemStack.EMPTY + && (!this.storedItem.isEmpty() || !this.getStackInSlot(1).isEmpty())) { + + ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); + fake.setCount(1); + + this.setInventorySlotContents(2, fake); + } else if (!ItemUtils.isItemEqual(this.getStackInSlot(2), this.storedItem, true, true)) { + + ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy(); + fake.setCount(1); + + this.setInventorySlotContents(2, fake); + } + } + } + + @Override + public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) { + world.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(), + getPos().getY(), getPos().getZ()); + readFromNBT(packet.getNbtCompound()); + } + + @Override + public void readFromNBT(NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + readFromNBTWithoutCoords(tagCompound); + } + + public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) { + + storedItem = ItemStack.EMPTY; + + if (tagCompound.hasKey("storedStack")) { + storedItem = new ItemStack((NBTTagCompound) tagCompound.getTag("storedStack")); + } + + if (storedItem != ItemStack.EMPTY) { + storedItem.setCount(tagCompound.getInteger("storedQuantity")); + } + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) { + super.writeToNBT(tagCompound); + writeToNBTWithoutCoords(tagCompound); + return tagCompound; + } + + public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound) { + if (storedItem != ItemStack.EMPTY) { + tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound())); + tagCompound.setInteger("storedQuantity", storedItem.getCount()); + } else { + tagCompound.setInteger("storedQuantity", 0); + } + + return tagCompound; + } + + @Override + public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) { + return false; + } + + @Override + public EnumFacing getFacing() { + return getFacingEnum(); + } + + @Override + public boolean wrenchCanRemove(EntityPlayer entityPlayer) { + return entityPlayer.isSneaking(); + } + + @Override + public float getWrenchDropRate() { + return 1F; + } + + @Override + public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { + return getDropWithNBT(); + } + + public ItemStack getDropWithNBT() { + NBTTagCompound tileEntity = new NBTTagCompound(); + ItemStack dropStack = new ItemStack(this.getBlockType(), 1); + writeToNBTWithoutCoords(tileEntity); + dropStack.setTagCompound(new NBTTagCompound()); + dropStack.getTagCompound().setTag("tileEntity", tileEntity); + return dropStack; + } + + public List getContentDrops() { + ArrayList stacks = new ArrayList<>(); + + if (!this.getStoredItemType().isEmpty()) { + if (!this.getStackInSlot(1).isEmpty()) + stacks.add(this.getStackInSlot(1)); + for (int i = 0; i < this.getStoredCount() / 64; i++) { + ItemStack droped = this.storedItem.copy(); + droped.setCount(64); + stacks.add(droped); + } + if (this.getStoredCount() % 64 != 0) { + ItemStack droped = this.storedItem.copy(); + droped.setCount(this.getStoredCount() % 64); + stacks.add(droped); + } + } + + return stacks; + } + + @Override + public ItemStack getStoredItemType() { + return this.storedItem.isEmpty() ? this.getStackInSlot(1) : this.storedItem; + } + + @Override + public void setStoredItemCount(int amount) { + storedItem.grow(amount); + this.markDirty(); + } + + @Override + public void setStoredItemType(ItemStack type, int amount) { + this.storedItem = type; + storedItem.setCount(amount); + this.markDirty(); + } + + @Override + public int getMaxStoredCount() { + return this.maxCapacity; + } + + public int getStoredCount() { + return this.storedItem.getCount(); + } + + @Override + public void addInfo(List info, boolean isRealTile) { + if (isRealTile) { + int size = 0; + String name = "of nothing"; + if (storedItem != ItemStack.EMPTY) { + name = storedItem.getDisplayName(); + size += storedItem.getCount(); + } + if (getStackInSlot(1) != ItemStack.EMPTY) { + name = getStackInSlot(1).getDisplayName(); + size += getStackInSlot(1).getCount(); + } + info.add(size + " " + name); + } + } + + @Override + public Inventory getInventory() { + return inventory; + } +}