Big drawer refactoring. Be kind to the GC.

This commit is contained in:
Justin Aquadro committed 2014-09-19 23:03:16 -04:00
1 parent 228a8e7c6c
commit 2764c27196
3 files changed
+88 -89

No files matched your search

@@ -3,73 +3,118 @@ package com.jaquadro.minecraft.storagedrawers.block.tile;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.oredict.OreDictionary;
import java.util.ArrayList;
import java.util.List;
public class DrawerData
{
private static final ItemStack nullStack = new ItemStack((Item)null);
private int slot;
private Item item;
public int meta;
//private Item item;
//private int meta;
public int count;
public NBTTagCompound attrs;
//private NBTTagCompound attrs;
private IStorageProvider storageProvider;
private ItemStack protoStack;
private List<ItemStack> oreDictMatches;
public DrawerData (IStorageProvider storageProvider, int slot) {
this.storageProvider = storageProvider;
this.slot = slot;
this.protoStack = nullStack;
reset();
}
public Item getItem () {
return item;
return protoStack.getItem();
}
public void setItem (Item item) {
this.item = item;
protoStack = new ItemStack(item);
public int getMeta () {
return protoStack.getItemDamage();
}
public NBTTagCompound getAttrs () {
return protoStack.getTagCompound();
}
public void setAttrs (NBTTagCompound attrs) {
if (protoStack != nullStack)
protoStack.setTagCompound(attrs);
}
public void setItem (Item item, int meta) {
this.item = item;
this.meta = meta;
setItem(item, meta, null);
}
public void setItem (Item item, int meta, NBTTagCompound attrs) {
protoStack = new ItemStack(item, 1, meta);
protoStack.setTagCompound(attrs);
int[] oreIDs = OreDictionary.getOreIDs(protoStack);
if (oreIDs.length == 0)
oreDictMatches = null;
else if (oreIDs.length == 1)
oreDictMatches = OreDictionary.getOres(OreDictionary.getOreName(oreIDs[0]));
else {
oreDictMatches = new ArrayList<ItemStack>();
for (int id : oreIDs) {
List<ItemStack> list = OreDictionary.getOres(OreDictionary.getOreName(id));
for (ItemStack oreItem : list)
oreDictMatches.add(oreItem);
}
}
}
public void setItem (ItemStack stack) {
setItem(stack.getItem(), stack.getItemDamage(), stack.getTagCompound());
}
// Can't actually enforce write access, so take this as a severe advisory.
// Don't needlessly force me to feed the Garbage Collector, folks.
public ItemStack getReadOnlyItemStack () {
return protoStack;
}
public ItemStack getNewItemStack () {
if (protoStack == null)
return null;
return protoStack.copy();
}
public void writeToNBT (NBTTagCompound tag) {
if (item != null) {
tag.setShort("Item", (short) Item.getIdFromItem(item));
tag.setShort("Meta", (short) meta);
if (protoStack.getItem() != null) {
tag.setShort("Item", (short) Item.getIdFromItem(protoStack.getItem()));
tag.setShort("Meta", (short) protoStack.getItemDamage());
tag.setInteger("Count", count);
if (attrs != null)
tag.setTag("Tags", attrs);
if (protoStack.getTagCompound() != null)
tag.setTag("Tags", protoStack.getTagCompound());
}
}
public void readFromNBT (NBTTagCompound tag) {
protoStack = nullStack;
if (tag.hasKey("Item")) {
item = Item.getItemById(tag.getShort("Item"));
meta = tag.getShort("Meta");
setItem(Item.getItemById(tag.getShort("Item")), tag.getShort("Meta"));
count = tag.getInteger("Count");
attrs = null;
if (tag.hasKey("Tags"))
attrs = tag.getCompoundTag("Tags");
protoStack = new ItemStack(item);
protoStack.setItemDamage(meta);
protoStack.setTagCompound(attrs);
protoStack.setTagCompound(tag.getCompoundTag("Tags"));
}
}
public void reset () {
item = null;
meta = 0;
count = 0;
attrs = null;
protoStack = null;
protoStack = nullStack;
oreDictMatches = null;
}
public int stackCapacity () {
@@ -77,24 +122,21 @@ public class DrawerData
}
public int itemStackMaxSize () {
if (item == null)
if (protoStack.getItem() == null)
return 0;
return item.getItemStackLimit(protoStack);
return protoStack.getItem().getItemStackLimit(protoStack);
}
public int maxCapacity () {
if (item == null)
if (protoStack.getItem() == null)
return 0;
protoStack.setItemDamage(meta);
protoStack.setTagCompound(attrs);
return item.getItemStackLimit(protoStack) * stackCapacity();
return protoStack.getItem().getItemStackLimit(protoStack) * stackCapacity();
}
public int remainingCapacity () {
if (item == null)
if (protoStack.getItem() == null)
return 0;
return maxCapacity() - count;
@@ -104,9 +146,6 @@ public class DrawerData
if (protoStack == null || stack == null)
return false;
protoStack.setItemDamage(meta);
protoStack.setTagCompound(attrs);
return protoStack.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(protoStack, stack);
}
}
@@ -80,10 +80,7 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
if (!isSlotValid(slot))
return null;
ItemStack stack = new ItemStack(data[slot].getItem(), 1, data[slot].meta);
stack.setTagCompound(data[slot].attrs);
return stack;
return data[slot].getReadOnlyItemStack();
}
private ItemStack getItemsFromSlot (int slot, int count) {
@@ -94,9 +91,8 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
if (adjCount > pooledCount)
count = pooledCount / convRate[slot];
ItemStack stack = new ItemStack(data[slot].getItem(), 1, data[slot].meta);
ItemStack stack = data[slot].getNewItemStack();
stack.stackSize = Math.min(stack.getMaxStackSize(), count);
stack.setTagCompound(data[slot].attrs);
return stack;
}
@@ -128,7 +124,7 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
slot = 2;
}
if (!itemMatchesForSlot(slot, stack))
if (!data[slot].areItemsEqual(stack))
return 0;
count = Math.min(count, stack.stackSize);
@@ -271,20 +267,6 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
return data[slot] != null && data[slot].getItem() != null;
}
private boolean itemMatchesForSlot (int slot, ItemStack stack) {
if (!isSlotValid(slot))
return false;
if (data[slot].getItem() != stack.getItem() || data[slot].meta != stack.getItemDamage())
return false;
if ((data[slot].attrs == null || stack.getTagCompound() == null) && data[slot].attrs != stack.getTagCompound())
return false;
else if (data[slot].attrs != null && !data[slot].attrs.equals(stack.getTagCompound()))
return false;
return true;
}
private void populateSlots (ItemStack stack) {
int index = 0;
@@ -322,10 +304,7 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
}
private void populateSlot (int slot, ItemStack stack, int conversion) {
data[slot].setItem(stack.getItem());
data[slot].meta = stack.getItemDamage();
data[slot].attrs = stack.getTagCompound();
data[slot].setItem(stack);
convRate[slot] = conversion;
}
@@ -578,7 +557,7 @@ public class TileEntityCompDrawers extends TileEntityDrawersBase implements ISto
if (!isSlotValid(0))
return true;
if (!itemMatchesForSlot(slot, itemStack))
if (!data[slot].areItemsEqual(itemStack))
return false;
if (remainingCapacity(slot) < itemStack.stackSize)
@@ -66,14 +66,11 @@ public class TileEntityDrawers extends TileEntityDrawersBase implements IStorage
if (data[slot].getItem() == null)
return null;
ItemStack stack = new ItemStack(data[slot].getItem(), 1, data[slot].meta);
stack.setTagCompound(data[slot].attrs);
return stack;
return data[slot].getReadOnlyItemStack();
}
public NBTTagCompound getItemAttrs (int slot) {
return data[slot].attrs;
return data[slot].getAttrs();
}
public ItemStack takeItemsFromSlot (int slot, int count) {
@@ -115,13 +112,10 @@ public class TileEntityDrawers extends TileEntityDrawersBase implements IStorage
}
public int putItemsIntoSlot (int slot, ItemStack stack, int count) {
if (data[slot].getItem() == null) {
data[slot].setItem(stack.getItem());
data[slot].meta = stack.getItemDamage();
data[slot].attrs = stack.getTagCompound();
}
if (data[slot].getItem() == null)
data[slot].setItem(stack);
if (!itemMatchesForSlot(slot, stack))
if (!data[slot].areItemsEqual(stack))
return 0;
int countAdded = Math.min(data[slot].remainingCapacity(), stack.stackSize);
@@ -137,26 +131,13 @@ public class TileEntityDrawers extends TileEntityDrawersBase implements IStorage
if (data[slot].getItem() == null)
return null;
ItemStack stack = new ItemStack(data[slot].getItem(), 1, data[slot].meta);
ItemStack stack = data[slot].getNewItemStack();
stack.stackSize = Math.min(stack.getMaxStackSize(), count);
stack.stackSize = Math.min(stack.stackSize, data[slot].count);
stack.setTagCompound(data[slot].attrs);
return stack;
}
private boolean itemMatchesForSlot (int slot, ItemStack stack) {
if (data[slot].getItem() != stack.getItem() || data[slot].meta != stack.getItemDamage())
return false;
if ((data[slot].attrs == null || stack.getTagCompound() == null) && data[slot].attrs != stack.getTagCompound())
return false;
else if (data[slot].attrs != null && !data[slot].attrs.equals(stack.getTagCompound()))
return false;
return true;
}
@Override
public int getSlotCapacity (int slot) {
ConfigManager config = StorageDrawers.config;
@@ -337,7 +318,7 @@ public class TileEntityDrawers extends TileEntityDrawersBase implements IStorage
if (data[slot].getItem() == null)
return true;
if (!itemMatchesForSlot(slot, itemStack))
if (!data[slot].areItemsEqual(itemStack))
return false;
if (data[slot].remainingCapacity() < itemStack.stackSize)