Little cleanup on crate code
This commit is contained in:
parent
6119f44947
commit
847c182571
1 changed files with 208 additions and 243 deletions
|
@ -1,6 +1,7 @@
|
|||
package resonantinduction.archaic.crate;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
|
@ -15,28 +16,13 @@ import calclavia.lib.utility.inventory.IExtendedStorage;
|
|||
|
||||
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.
|
||||
*
|
||||
* @author DarkGuardsman
|
||||
*/
|
||||
* @author DarkGuardsman */
|
||||
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 */
|
||||
private ItemStack sampleStack;
|
||||
|
||||
|
@ -55,7 +41,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
|
|||
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()
|
||||
{
|
||||
if (this.sampleStack == null)
|
||||
|
@ -65,85 +51,71 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
|
|||
return this.sampleStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the inventory array into a single stack of matching items. This assumes that all items
|
||||
* 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
|
||||
*/
|
||||
/** Builds the sample stack using the inventory as a point of reference. Assumes all items match
|
||||
* each other, and only takes into account stack sizes */
|
||||
public void buildSampleStack()
|
||||
{
|
||||
ItemStack stack = null;
|
||||
|
||||
ItemStack newSampleStack = 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++)
|
||||
for (int slot = 0; slot < this.getSizeInventory(); slot++)
|
||||
{
|
||||
ItemStack s = this.getInventory().getContainedItems()[i];
|
||||
if (s != null && s.itemID > 0 && s.stackSize > 0)
|
||||
ItemStack slotStack = this.getInventory().getContainedItems()[slot];
|
||||
if (slotStack != null && Item.itemsList[slotStack.itemID] != null && slotStack.stackSize > 0)
|
||||
{
|
||||
if (stack == null)
|
||||
{
|
||||
stack = s.copy();
|
||||
}
|
||||
if (newSampleStack == null)
|
||||
newSampleStack = slotStack.copy();
|
||||
else
|
||||
{
|
||||
stack.stackSize += this.getInventory().getContainedItems()[i].stackSize;
|
||||
}
|
||||
if (this.getInventory().getContainedItems()[i].stackSize > this.getInventory().getContainedItems()[i].getMaxStackSize())
|
||||
{
|
||||
newSampleStack.stackSize += slotStack.stackSize;
|
||||
|
||||
if (slotStack.stackSize > slotStack.getMaxStackSize())
|
||||
rebuildBase = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (stack == null || stack.itemID == 0 || stack.stackSize == 0)
|
||||
{
|
||||
if (newSampleStack == null || newSampleStack.itemID == 0 || newSampleStack.stackSize <= 0)
|
||||
this.sampleStack = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sampleStack = stack.copy();
|
||||
}
|
||||
/* if one stack is over sized this rebuilds the inv to redistribute the items in the slots */
|
||||
if ((rebuildBase || this.getInventory().getContainedItems().length > this.getSlotCount()) && this.sampleStack != null)
|
||||
this.sampleStack = newSampleStack.copy();
|
||||
|
||||
/* Rebuild inventory if the inventory is not valid */
|
||||
if (this.sampleStack != null && (rebuildBase || this.getInventory().getContainedItems().length > this.getSizeInventory()))
|
||||
{
|
||||
this.getInventory().buildInventory(this.sampleStack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack addStackToStorage(ItemStack stack)
|
||||
{
|
||||
return BlockCrate.addStackToCrate(this, stack);
|
||||
}
|
||||
|
||||
/** Adds an item to the stack */
|
||||
public void addToStack(ItemStack stack, int amount)
|
||||
{
|
||||
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 */
|
||||
public void addToStack(ItemStack stack)
|
||||
{
|
||||
if (stack != null)
|
||||
if (stack != null && stack.stackSize > 0)
|
||||
{
|
||||
this.buildSampleStack();
|
||||
boolean flag = false;
|
||||
|
||||
if (this.sampleStack == null)
|
||||
if (this.getSampleStack() == null)
|
||||
{
|
||||
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;
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag)
|
||||
{
|
||||
this.getInventory().buildInventory(this.sampleStack);
|
||||
getSampleStack().stackSize += stack.stackSize;
|
||||
getInventory().buildInventory(getSampleStack());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +134,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
|
|||
@Override
|
||||
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 */
|
||||
|
@ -194,7 +166,7 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
|
|||
{
|
||||
return false;
|
||||
}
|
||||
int ddd = 0;
|
||||
|
||||
@Override
|
||||
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.stackSize = data.readInt();
|
||||
//player.sendChatToPlayer(ChatMessageComponent.createFromText("Crate Packet " + (ddd++)));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -281,10 +252,4 @@ public class TileCrate extends TileExternalInventory implements IPacketReceiver,
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack addStackToStorage(ItemStack stack)
|
||||
{
|
||||
return BlockCrate.addStackToCrate(this, stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue