reworked crates

still need to fix the NBT saving issue for the item stack. Either its
not saving inside the crate or its being removed on eject from the
crate. Should be an easy fix just need to take the time to find the
issue spot.
This commit is contained in:
DarkGuardsman 2013-08-06 17:01:41 -04:00
parent 000a01daa2
commit c736a84c9c
4 changed files with 88 additions and 264 deletions

View file

@ -136,7 +136,7 @@ public class BlockCrate extends BlockAssembly
{
if (checkTile instanceof TileEntityCrate)
{
AssemblyLine.recipeLoader.blockCrate.tryInsert(((TileEntityCrate) checkTile), player, allMode, false);
this.tryInsert(((TileEntityCrate) checkTile), player, allMode, false);
}
}
}
@ -177,7 +177,7 @@ public class BlockCrate extends BlockAssembly
if (currentStack != null)
{
if (currentStack.getItem().itemID == AssemblyLine.recipeLoader.blockCrate.blockID)
if (currentStack.getItem().itemID == this.blockID)
{
ItemStack containedStack = ItemBlockCrate.getContainingItemStack(currentStack);
ItemStack crateStack = tileEntity.getSampleStack();
@ -319,7 +319,7 @@ public class BlockCrate extends BlockAssembly
if (containingStack == null || containingStack != null && containingStack.isItemEqual(itemStack))
{
int room = (tileEntity.getInventory().getSizeInventory() * 64) - (containingStack != null ? containingStack.stackSize : 0);
int room = Math.max((tileEntity.getInventory().getSizeInventory() * 64) - (containingStack != null ? containingStack.stackSize : 0), 0);
if (itemStack.stackSize <= room)
{
tileEntity.addToStack(itemStack);
@ -349,7 +349,7 @@ public class BlockCrate extends BlockAssembly
{
TileEntityCrate tileEntity = (TileEntityCrate) world.getBlockTileEntity(x, y, z);
ItemStack containingStack = tileEntity.getSampleStack();
tileEntity.buildSampleStack(false);
tileEntity.buildSampleStack();
if (containingStack != null)
{

View file

@ -1,150 +1,49 @@
package dark.assembly.common.machine;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.prefab.network.PacketManager;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.tileentity.TileEntity;
import dark.core.blocks.InvChest;
public class InventoryCrate extends InvChest
{
/** slots that can be accessed from the side */
private int[] slots;
/** Contained items */
private ItemStack[] items = new ItemStack[1028];
TileEntityCrate crate = null;
public InventoryCrate(TileEntityCrate crate)
public InventoryCrate(TileEntity crate)
{
super(crate, 1028);
this.crate = crate;
super(crate, 512);
}
/** Clones the single stack into an inventory format for automation interaction */
public void buildInventory(ItemStack sampleStack)
{
ItemStack baseStack = sampleStack.copy();
this.items = new ItemStack[crate.getSlotCount()];
for (int slot = 0; slot < this.items.length; slot++)
this.items = new ItemStack[this.getSizeInventory()];
if (sampleStack != null)
{
int stackL = Math.min(Math.min(baseStack.stackSize, baseStack.getMaxStackSize()), this.getInventoryStackLimit());
this.items[slot] = new ItemStack(baseStack.itemID, stackL, baseStack.getItemDamage());
baseStack.stackSize -= stackL;
if (baseStack.stackSize <= 0)
ItemStack baseStack = sampleStack.copy();
int itemsLeft = baseStack.stackSize;
for (int slot = 0; slot < this.items.length; slot++)
{
baseStack = null;
break;
}
}
}
@Override
public ItemStack getStackInSlot(int par1)
{
return this.items[par1];
}
@Override
public ItemStack decrStackSize(int par1, int par2)
{
if (this.items[par1] != null)
{
ItemStack itemstack;
if (this.items[par1].stackSize <= par2)
{
itemstack = this.items[par1];
this.items[par1] = null;
this.onInventoryChanged();
return itemstack;
}
else
{
itemstack = this.items[par1].splitStack(par2);
if (this.items[par1].stackSize == 0)
int stackL = Math.min(Math.min(itemsLeft, baseStack.getMaxStackSize()), this.getInventoryStackLimit());
this.items[slot] = baseStack.copy();
this.items[slot].stackSize = stackL;
itemsLeft -= stackL;
if (baseStack.stackSize <= 0)
{
this.items[par1] = null;
baseStack = null;
break;
}
this.onInventoryChanged();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int par1)
{
if (this.items[par1] != null)
{
ItemStack itemstack = this.items[par1];
this.items[par1] = null;
return itemstack;
}
else
{
return null;
}
}
@Override
public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
{
this.items[par1] = par2ItemStack;
if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
{
par2ItemStack.stackSize = this.getInventoryStackLimit();
}
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
PacketManager.sendPacketToClients(crate.getDescriptionPacket(), crate.worldObj);
}
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.crate.worldObj.getBlockTileEntity(this.crate.xCoord, this.crate.yCoord, this.crate.zCoord) != this.crate ? false : par1EntityPlayer.getDistanceSq(crate.xCoord + 0.5D, crate.yCoord + 0.5D, crate.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public int getSizeInventory()
{
if (crate != null)
if (this.hostTile instanceof TileEntityCrate)
{
return crate.getSlotCount();
return ((TileEntityCrate) this.hostTile).getSlotCount();
}
return 1028;
return 512;
}
@Override
@ -153,64 +52,6 @@ public class InventoryCrate extends InvChest
return "inv.Crate";
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack)
{
if (slot >= crate.getSlotCount())
{
return false;
}
return true;
}
@Override
public int[] getAccessibleSlotsFromSide(int side)
{
if (slots == null || slots != null && slots.length != crate.getSlotCount())
{
slots = new int[crate.getSlotCount()];
for (int i = 0; i < slots.length; i++)
{
slots[i] = i;
}
}
return this.slots;
}
@Override
public boolean canInsertItem(int slot, ItemStack itemstack, int side)
{
return this.isItemValidForSlot(slot, itemstack) && this.crate.canStore(itemstack, slot, ForgeDirection.getOrientation(side));
}
@Override
public boolean canExtractItem(int slot, ItemStack itemstack, int side)
{
return crate.canRemove(itemstack, slot, ForgeDirection.getOrientation(side));
}
@Override
public void onInventoryChanged()
{
crate.onInventoryChanged();
}
@Override
public ItemStack[] getContainedItems()
{
if (this.items == null)
{
this.items = new ItemStack[this.getSizeInventory()];
}
return this.items;
}
@Override
public void saveInv(NBTTagCompound nbt)
{
@ -221,26 +62,9 @@ public class InventoryCrate extends InvChest
@Override
public void loadInv(NBTTagCompound nbt)
{
this.items = new ItemStack[this.getSizeInventory()];
if (nbt.hasKey("Items"))
{
NBTTagList var2 = nbt.getTagList("Items");
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound) var2.tagAt(var3);
byte var5 = var4.getByte("Slot");
if (var5 >= 0 && var5 < this.items.length)
{
this.items[var5] = ItemStack.loadItemStackFromNBT(var4);
}
}
if (nbt.hasKey("Count") && this.items[0] != null)
{
this.items[0].stackSize = nbt.getInteger("Count");
}
super.loadInv(nbt);
}
}

View file

@ -131,7 +131,7 @@ public class ItemBlockCrate extends ItemBlock
}
}
tileEntity.buildSampleStack(false);
tileEntity.buildSampleStack();
}
}
}

View file

@ -25,6 +25,8 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
/** delay from last click */
public long prevClickTime = -1000;
/** max meta size of the crate */
public static final int maxSize = 2;
@Override
public InventoryCrate getInventory()
@ -36,20 +38,14 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
return (InventoryCrate) this.inventory;
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
/** Gets the sample stack that represent the total inv */
public ItemStack getSampleStack()
{
return this.sampleStack == null || stack != null && stack.equals(sampleStack);
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
if (slot >= this.getSlotCount())
if (this.sampleStack == null)
{
return false;
this.buildSampleStack();
}
return true;
return this.sampleStack;
}
/** Turns the inventory array into a single stack of matching items. This assumes that all items
@ -57,56 +53,48 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
* same to prevent duplication issues
*
* @param force - force a rebuild of the inventory from the single stack created */
public void buildSampleStack(boolean force)
public void buildSampleStack()
{
int count = 0;
int id = 0;
int meta = 0;
ItemStack stack = null;
boolean rebuildBase = false;
/* Creates the sample stack that is used as a collective itemstack */
for (int i = 0; i < this.getInventory().getContainedItems().length; i++)
{
ItemStack stack = this.getInventory().getContainedItems()[i];
if (stack != null && stack.itemID > 0 && stack.stackSize > 0)
ItemStack s = this.getInventory().getContainedItems()[i];
if (s != null && s.itemID > 0 && s.stackSize > 0)
{
id = this.getInventory().getContainedItems()[i].itemID;
meta = this.getInventory().getContainedItems()[i].getItemDamage();
int ss = this.getInventory().getContainedItems()[i].stackSize;
count += ss;
if (ss > this.getInventory().getContainedItems()[i].getMaxStackSize())
if (stack == null)
{
stack = s.copy();
}
else
{
stack.stackSize += this.getInventory().getContainedItems()[i].stackSize;
}
if (this.getInventory().getContainedItems()[i].stackSize > this.getInventory().getContainedItems()[i].getMaxStackSize())
{
rebuildBase = true;
}
}
}
if (id == 0 || count == 0)
if (stack == null || stack.itemID == 0 || stack.stackSize == 0)
{
this.sampleStack = null;
}
else
{
this.sampleStack = new ItemStack(id, count, meta);
this.sampleStack = stack.copy();
}
/* if one stack is over sized this rebuilds the inv to redistribute the items in the slots */
if ((rebuildBase || force || this.getInventory().getContainedItems().length > this.getSlotCount()) && this.sampleStack != null)
if ((rebuildBase || this.getInventory().getContainedItems().length > this.getSlotCount()) && this.sampleStack != null)
{
this.getInventory().buildInventory(this.sampleStack);
}
}
public ItemStack getSampleStack()
{
if (this.sampleStack == null)
{
this.buildSampleStack(false);
}
return this.sampleStack;
}
/** Adds an item to the stack */
public void addToStack(ItemStack stack, int amount)
{
if (stack != null)
@ -115,11 +103,12 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
}
}
/** Adds the stack to the sample stack */
public void addToStack(ItemStack stack)
{
if (stack != null)
{
this.buildSampleStack(false);
this.buildSampleStack();
boolean flag = false;
if (this.sampleStack == null)
{
@ -134,10 +123,7 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
if (flag)
{
this.getInventory().buildInventory(this.sampleStack);
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj);
}
this.onInventoryChanged();
}
}
}
@ -156,11 +142,33 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
}
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{
return this.sampleStack == null || stack != null && stack.equals(sampleStack);
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
if (slot >= this.getSlotCount())
{
return false;
}
return true;
}
/** Gets the current slot count for the crate */
public int getSlotCount()
{
if (this.worldObj == null)
{
return TileEntityCrate.getSlotCount(TileEntityCrate.maxSize);
}
return TileEntityCrate.getSlotCount(this.getBlockMetadata());
}
/** Gets the slot count for the crate meta */
public static int getSlotCount(int metadata)
{
if (metadata >= 2)
@ -171,7 +179,6 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
{
return 64;
}
return 32;
}
@ -190,16 +197,8 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
{
if (dataStream.readBoolean())
{
if (this.sampleStack == null)
{
this.sampleStack = new ItemStack(dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
}
else
{
this.sampleStack.itemID = dataStream.readInt();
this.sampleStack.stackSize = dataStream.readInt();
this.sampleStack.setItemDamage(dataStream.readInt());
}
this.sampleStack = ItemStack.loadItemStackFromNBT(PacketManager.readNBTTagCompound(dataStream));
this.sampleStack.stackSize = dataStream.readInt();
}
else
{
@ -216,11 +215,11 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
@Override
public Packet getDescriptionPacket()
{
this.buildSampleStack(false);
this.buildSampleStack();
ItemStack stack = this.getSampleStack();
if (stack != null)
{
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, true, stack.itemID, stack.stackSize, stack.getItemDamage());
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, true, stack.writeToNBT(new NBTTagCompound()), stack.stackSize);
}
else
{
@ -233,18 +232,19 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
/* Load inventory old data if present */
this.getInventory().loadInv(nbt);
/* Load current two inv methods */
ItemStack stack = null;
if (!nbt.hasKey("Items") && nbt.hasKey("itemID") && nbt.hasKey("itemMeta"))
int count = nbt.getInteger("Count");
if (nbt.hasKey("itemID"))
{
stack = new ItemStack(nbt.getInteger("itemID"), nbt.getInteger("Count"), nbt.getInteger("itemMeta"));
stack = new ItemStack(nbt.getInteger("itemID"), count, nbt.getInteger("itemMeta"));
}
else
{
stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack"));
stack.stackSize = count;
}
/* Only load sample stack if the read stack is valid */
if (stack != null && stack.itemID != 0 && stack.stackSize > 0)
{
@ -259,13 +259,13 @@ public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, I
{
super.writeToNBT(nbt);
/* Re-Build sample stack for saving */
this.buildSampleStack(false);
this.buildSampleStack();
ItemStack stack = this.getSampleStack();
/* Save sample stack */
if (this.getSampleStack() != null)
if (stack != null)
{
NBTTagCompound tag = new NBTTagCompound();
this.getSampleStack().writeToNBT(tag);
nbt.setCompoundTag("stack", tag);
nbt.setInteger("Count", stack.stackSize);
nbt.setCompoundTag("stack", stack.writeToNBT(new NBTTagCompound()));
}
}