Little cleanup on crate code

This commit is contained in:
Robert S 2014-04-14 05:07:29 -04:00
parent 6119f44947
commit 847c182571

View file

@ -1,6 +1,7 @@
package resonantinduction.archaic.crate; package resonantinduction.archaic.crate;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet;
@ -15,28 +16,13 @@ import calclavia.lib.utility.inventory.IExtendedStorage;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
/** /** Basic single stack inventory.
* Basic single stack inventory.
* *
* TODO: Add filter-locking feature. Put filter in, locks the crate to only use that item. * TODO: Add filter-locking feature. Put filter in, locks the crate to only use that item.
* *
* @author DarkGuardsman * @author DarkGuardsman */
*/
public class TileCrate extends TileExternalInventory implements IPacketReceiver, IExtendedStorage public class TileCrate extends TileExternalInventory implements IPacketReceiver, IExtendedStorage
{ {
/*
* TODO
* Fix issues with ItemStacks with NBT tags having issues
* Fix possible render issues with some items
* Yell at MachineMuse for her items rendering threw walls
* Add support to disable sides of crates when rendering items are unwanted
* Simplify item rendering to decrease graphic lag
* Add crafting manger to prevent crafting with full crates
* As well keep item stacks when upgrade crate threw crafting
* Add upgrade item for crate
* Add crate swapping in which an advanced can trade place with a basic while keeping inventory
* at the locaiton
*/
/** Collective total stack of all inv slots */ /** Collective total stack of all inv slots */
private ItemStack sampleStack; private ItemStack sampleStack;
@ -55,7 +41,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
return (InventoryCrate) this.inventory; return (InventoryCrate) this.inventory;
} }
/** Gets the sample stack that represent the total inv */ /** Gets the sample stack that represent the total inventory */
public ItemStack getSampleStack() public ItemStack getSampleStack()
{ {
if (this.sampleStack == null) if (this.sampleStack == null)
@ -65,85 +51,71 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
return this.sampleStack; return this.sampleStack;
} }
/** /** Builds the sample stack using the inventory as a point of reference. Assumes all items match
* Turns the inventory array into a single stack of matching items. This assumes that all items * each other, and only takes into account stack sizes */
* in the crate are the same TODO eject minority items and only keep the majority that are the
* same to prevent duplication issues
* TODO: Add Force?
* @param force - force a rebuild of the inventory from the single stack created
*/
public void buildSampleStack() public void buildSampleStack()
{ {
ItemStack stack = null; ItemStack newSampleStack = null;
boolean rebuildBase = false; boolean rebuildBase = false;
/* Creates the sample stack that is used as a collective itemstack */ /* Creates the sample stack that is used as a collective itemstack */
for (int i = 0; i < this.getInventory().getContainedItems().length; i++) for (int slot = 0; slot < this.getSizeInventory(); slot++)
{ {
ItemStack s = this.getInventory().getContainedItems()[i]; ItemStack slotStack = this.getInventory().getContainedItems()[slot];
if (s != null && s.itemID > 0 && s.stackSize > 0) if (slotStack != null && Item.itemsList[slotStack.itemID] != null && slotStack.stackSize > 0)
{ {
if (stack == null) if (newSampleStack == null)
{ newSampleStack = slotStack.copy();
stack = s.copy();
}
else else
{ newSampleStack.stackSize += slotStack.stackSize;
stack.stackSize += this.getInventory().getContainedItems()[i].stackSize;
} if (slotStack.stackSize > slotStack.getMaxStackSize())
if (this.getInventory().getContainedItems()[i].stackSize > this.getInventory().getContainedItems()[i].getMaxStackSize())
{
rebuildBase = true; rebuildBase = true;
} }
} }
} if (newSampleStack == null || newSampleStack.itemID == 0 || newSampleStack.stackSize <= 0)
if (stack == null || stack.itemID == 0 || stack.stackSize == 0)
{
this.sampleStack = null; this.sampleStack = null;
}
else else
{ this.sampleStack = newSampleStack.copy();
this.sampleStack = stack.copy();
} /* Rebuild inventory if the inventory is not valid */
/* if one stack is over sized this rebuilds the inv to redistribute the items in the slots */ if (this.sampleStack != null && (rebuildBase || this.getInventory().getContainedItems().length > this.getSizeInventory()))
if ((rebuildBase || this.getInventory().getContainedItems().length > this.getSlotCount()) && this.sampleStack != null)
{ {
this.getInventory().buildInventory(this.sampleStack); this.getInventory().buildInventory(this.sampleStack);
} }
} }
@Override
public ItemStack addStackToStorage(ItemStack stack)
{
return BlockCrate.addStackToCrate(this, stack);
}
/** Adds an item to the stack */ /** Adds an item to the stack */
public void addToStack(ItemStack stack, int amount) public void addToStack(ItemStack stack, int amount)
{ {
if (stack != null) if (stack != null)
{ {
this.addToStack(new ItemStack(stack.stackSize, amount, stack.getItemDamage())); ItemStack newStack = stack.copy();
newStack.stackSize = amount;
this.addToStack(newStack);
} }
} }
/** Adds the stack to the sample stack */ /** Adds the stack to the sample stack */
public void addToStack(ItemStack stack) public void addToStack(ItemStack stack)
{ {
if (stack != null) if (stack != null && stack.stackSize > 0)
{ {
this.buildSampleStack(); if (this.getSampleStack() == null)
boolean flag = false;
if (this.sampleStack == null)
{ {
this.sampleStack = stack; this.sampleStack = stack;
flag = true; getInventory().buildInventory(getSampleStack());
} }
else if (this.sampleStack.isItemEqual(stack) || OreDictionary.getOreID(sampleStack) == OreDictionary.getOreID(stack)) else if (this.getSampleStack().isItemEqual(stack) || OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack))
{ {
this.sampleStack.stackSize += stack.stackSize; getSampleStack().stackSize += stack.stackSize;
flag = true; getInventory().buildInventory(getSampleStack());
}
if (flag)
{
this.getInventory().buildInventory(this.sampleStack);
} }
} }
} }
@ -162,7 +134,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
@Override @Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side) public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{ {
return sampleStack == null || stack != null && (stack.isItemEqual(sampleStack) || OreDictionary.getOreID(sampleStack) == OreDictionary.getOreID(stack)); return getSampleStack() == null || stack != null && (stack.isItemEqual(getSampleStack()) || OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack));
} }
/** Gets the current slot count for the crate */ /** Gets the current slot count for the crate */
@ -194,7 +166,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
{ {
return false; return false;
} }
int ddd = 0;
@Override @Override
public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra) public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra)
{ {
@ -206,7 +178,6 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
{ {
this.sampleStack = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data)); this.sampleStack = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data));
this.sampleStack.stackSize = data.readInt(); this.sampleStack.stackSize = data.readInt();
//player.sendChatToPlayer(ChatMessageComponent.createFromText("Crate Packet " + (ddd++)));
} }
else else
{ {
@ -281,10 +252,4 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
} }
@Override
public ItemStack addStackToStorage(ItemStack stack)
{
return BlockCrate.addStackToCrate(this, stack);
}
} }