Some work done on crate

This commit is contained in:
Calclavia 2014-04-15 22:46:59 +08:00
parent 41b83362b2
commit 5a6e92ccd1
2 changed files with 309 additions and 239 deletions

View file

@ -1,7 +1,11 @@
package resonantinduction.archaic.crate; package resonantinduction.archaic.crate;
import java.util.HashSet; import calclavia.api.resonantinduction.IFilterable;
import calclavia.lib.network.IPacketReceiver;
import calclavia.lib.network.PacketHandler;
import calclavia.lib.prefab.tile.TileExternalInventory;
import calclavia.lib.utility.inventory.IExtendedStorage;
import com.google.common.io.ByteArrayDataInput;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -10,272 +14,338 @@ import net.minecraft.network.packet.Packet;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import resonantinduction.core.ResonantInduction; import resonantinduction.core.ResonantInduction;
import resonantinduction.core.prefab.imprint.ItemImprint;
import calclavia.api.resonantinduction.IFilterable;
import calclavia.lib.network.IPacketReceiver;
import calclavia.lib.network.PacketHandler;
import calclavia.lib.prefab.tile.TileExternalInventory;
import calclavia.lib.utility.inventory.IExtendedStorage;
import com.google.common.io.ByteArrayDataInput; /**
* Basic single stack inventory.
/** Basic single stack inventory. * <p/>
*
* 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, IFilterable */
public class TileCrate extends TileExternalInventory
implements IPacketReceiver, IExtendedStorage, IFilterable
{ {
/** max meta size of the crate */ /**
public static final int maxSize = 2; * max meta size of the crate
*/
public static final int maxSize = 2;
/** delay from last click */ /**
public long prevClickTime = -1000; * delay from last click
*/
public long prevClickTime = -1000;
/** Check to see if oreName items can be force stacked */ /**
public boolean oreFilterEnabled = false; * Check to see if oreName items can be force stacked
*/
public boolean oreFilterEnabled = false;
/** Collective total stack of all inv slots */ /**
private ItemStack sampleStack; * Collective total stack of all inv slots
private ItemStack filterStack; */
private ItemStack sampleStack;
private ItemStack filterStack;
@Override /**
public InventoryCrate getInventory() * Gets the slot count for the crate meta
{ */
if (this.inventory == null) public static int getSlotCount(int metadata)
{ {
inventory = new InventoryCrate(this); if (metadata >= 2)
} {
return (InventoryCrate) this.inventory; return 256;
} }
else if (metadata >= 1)
{
return 64;
}
return 32;
}
/** Gets the sample stack that represent the total inventory */ @Override
public ItemStack getSampleStack() public InventoryCrate getInventory()
{ {
if (this.sampleStack == null) if (this.inventory == null)
{ {
this.buildSampleStack(); inventory = new InventoryCrate(this);
} }
return this.sampleStack; return (InventoryCrate) this.inventory;
} }
/** 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 */ * Gets the sample stack that represent the total inventory
public void buildSampleStack() */
{ public ItemStack getSampleStack()
ItemStack newSampleStack = null; {
boolean rebuildBase = false; if (this.sampleStack == null)
{
this.buildSampleStack();
}
return this.sampleStack;
}
/**
* 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 newSampleStack = null;
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 slot = 0; slot < this.getSizeInventory(); slot++) for (int slot = 0; slot < this.getSizeInventory(); slot++)
{ {
ItemStack slotStack = this.getInventory().getContainedItems()[slot]; ItemStack slotStack = this.getInventory().getContainedItems()[slot];
if (slotStack != null && Item.itemsList[slotStack.itemID] != null && slotStack.stackSize > 0) if (slotStack != null && Item.itemsList[slotStack.itemID] != null && slotStack.stackSize > 0)
{ {
if (newSampleStack == null) if (newSampleStack == null)
newSampleStack = slotStack.copy(); {
else newSampleStack = slotStack.copy();
newSampleStack.stackSize += slotStack.stackSize; }
else
{
newSampleStack.stackSize += slotStack.stackSize;
}
if (slotStack.stackSize > slotStack.getMaxStackSize()) if (slotStack.stackSize > slotStack.getMaxStackSize())
rebuildBase = true; {
} rebuildBase = true;
} }
if (newSampleStack == null || newSampleStack.itemID == 0 || newSampleStack.stackSize <= 0) }
this.sampleStack = this.getFilter() != null ? this.getFilter().copy() : null; }
else if (newSampleStack == null || newSampleStack.itemID == 0 || newSampleStack.stackSize <= 0)
this.sampleStack = newSampleStack.copy(); {
this.sampleStack = this.getFilter() != null ? this.getFilter().copy() : null;
}
else
{
this.sampleStack = newSampleStack.copy();
}
/* Rebuild inventory if the inventory is not valid */ /* Rebuild inventory if the inventory is not valid */
if (this.sampleStack != null && (rebuildBase || this.getInventory().getContainedItems().length > this.getSizeInventory())) if (this.sampleStack != null && (rebuildBase || this.getInventory().getContainedItems().length > this.getSizeInventory()))
{ {
this.getInventory().buildInventory(this.sampleStack); this.getInventory().buildInventory(this.sampleStack);
} }
} }
@Override @Override
public ItemStack addStackToStorage(ItemStack stack) public ItemStack addStackToStorage(ItemStack stack)
{ {
return BlockCrate.addStackToCrate(this, stack); return BlockCrate.addStackToCrate(this, stack);
} }
/** Adds an item to the stack */ /**
public void addToStack(ItemStack stack, int amount) * Adds an item to the stack
{ */
if (stack != null) public void addToStack(ItemStack stack, int amount)
{ {
ItemStack newStack = stack.copy(); if (stack != null)
newStack.stackSize = amount; {
this.addToStack(newStack); ItemStack newStack = stack.copy();
} newStack.stackSize = amount;
} this.addToStack(newStack);
}
}
/** Adds the stack to the sample stack */ /**
public void addToStack(ItemStack stack) * Adds the stack to the sample stack
{ */
if (stack != null && stack.stackSize > 0) public void addToStack(ItemStack stack)
{ {
if (this.getSampleStack() == null) if (stack != null && stack.stackSize > 0)
{ {
this.sampleStack = stack; if (this.getSampleStack() == null)
getInventory().buildInventory(getSampleStack()); {
} this.sampleStack = stack;
else if (this.getSampleStack().isItemEqual(stack) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack))) getInventory().buildInventory(getSampleStack());
{ }
getSampleStack().stackSize += stack.stackSize; else if (this.getSampleStack().isItemEqual(stack) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack)))
getInventory().buildInventory(getSampleStack()); {
} getSampleStack().stackSize += stack.stackSize;
} getInventory().buildInventory(getSampleStack());
} }
}
}
@Override @Override
public void onInventoryChanged() public ItemStack decrStackSize(int slot, int amount)
{ {
super.onInventoryChanged(); if (sampleStack != null)
{
ItemStack var3;
if (worldObj != null && !worldObj.isRemote) if (sampleStack.stackSize <= amount)
{ {
PacketHandler.sendPacketToClients(getDescriptionPacket(), this.worldObj); var3 = sampleStack;
} sampleStack = null;
} this.onInventoryChanged();
getInventory().buildInventory(getSampleStack());
return var3;
}
else
{
var3 = sampleStack.splitStack(amount);
@Override if (sampleStack.stackSize == 0)
public boolean canStore(ItemStack stack, int slot, ForgeDirection side) {
{ sampleStack = null;
return getSampleStack() == null || stack != null && (stack.isItemEqual(getSampleStack()) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack))); }
}
/** Gets the current slot count for the crate */ getInventory().buildInventory(getSampleStack());
public int getSlotCount() onInventoryChanged();
{ return var3;
if (this.worldObj == null) }
{ }
return TileCrate.getSlotCount(TileCrate.maxSize); else
} {
return TileCrate.getSlotCount(this.getBlockMetadata()); return null;
} }
}
/** Gets the slot count for the crate meta */ @Override
public static int getSlotCount(int metadata) public void onInventoryChanged()
{ {
if (metadata >= 2) super.onInventoryChanged();
{
return 256;
}
else if (metadata >= 1)
{
return 64;
}
return 32;
}
@Override if (worldObj != null && !worldObj.isRemote)
public boolean canUpdate() {
{ PacketHandler.sendPacketToClients(getDescriptionPacket(), this.worldObj);
return false; }
} }
@Override @Override
public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra) public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{ {
if (this.worldObj.isRemote) return getSampleStack() == null || stack != null && (stack.isItemEqual(getSampleStack()) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack)));
{ }
try
{
if (data.readBoolean())
{
this.sampleStack = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data));
this.sampleStack.stackSize = data.readInt();
}
else
{
this.sampleStack = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
@Override /**
public Packet getDescriptionPacket() * Gets the current slot count for the crate
{ */
this.buildSampleStack(); public int getSlotCount()
ItemStack stack = this.getSampleStack(); {
if (stack != null) if (this.worldObj == null)
{ {
return ResonantInduction.PACKET_TILE.getPacket(this, true, stack.writeToNBT(new NBTTagCompound()), stack.stackSize); return TileCrate.getSlotCount(TileCrate.maxSize);
} }
else return TileCrate.getSlotCount(this.getBlockMetadata());
{ }
return ResonantInduction.PACKET_TILE.getPacket(this, false);
}
}
/** NBT Data */ @Override
@Override public boolean canUpdate()
public void readFromNBT(NBTTagCompound nbt) {
{ return false;
super.readFromNBT(nbt); }
/* Load current two inv methods */
ItemStack stack = null; @Override
int count = nbt.getInteger("Count"); public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra)
if (nbt.hasKey("itemID")) {
{ if (this.worldObj.isRemote)
stack = new ItemStack(nbt.getInteger("itemID"), count, nbt.getInteger("itemMeta")); {
} try
else {
{ if (data.readBoolean())
stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack")); {
if (stack != null) this.sampleStack = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data));
{ this.sampleStack.stackSize = data.readInt();
stack.stackSize = count; }
} else
} {
this.sampleStack = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
@Override
public Packet getDescriptionPacket()
{
this.buildSampleStack();
ItemStack stack = this.getSampleStack();
if (stack != null)
{
return ResonantInduction.PACKET_TILE.getPacket(this, true, stack.writeToNBT(new NBTTagCompound()), stack.stackSize);
}
else
{
return ResonantInduction.PACKET_TILE.getPacket(this, false);
}
}
/**
* NBT Data
*/
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
/* Load current two inv methods */
ItemStack stack = null;
int count = nbt.getInteger("Count");
if (nbt.hasKey("itemID"))
{
stack = new ItemStack(nbt.getInteger("itemID"), count, nbt.getInteger("itemMeta"));
}
else
{
stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack"));
if (stack != null)
{
stack.stackSize = count;
}
}
/* Only load sample stack if the read stack is valid */ /* Only load sample stack if the read stack is valid */
if (stack != null && stack.itemID != 0 && stack.stackSize > 0) if (stack != null && stack.itemID != 0 && stack.stackSize > 0)
{ {
this.sampleStack = stack; this.sampleStack = stack;
this.getInventory().buildInventory(this.sampleStack); this.getInventory().buildInventory(this.sampleStack);
} }
this.oreFilterEnabled = nbt.getBoolean("oreFilter"); this.oreFilterEnabled = nbt.getBoolean("oreFilter");
if(nbt.hasKey("filter")) if (nbt.hasKey("filter"))
filterStack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("filter")); {
filterStack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("filter"));
}
} }
@Override @Override
public void writeToNBT(NBTTagCompound nbt) public void writeToNBT(NBTTagCompound nbt)
{ {
super.writeToNBT(nbt); super.writeToNBT(nbt);
/* Re-Build sample stack for saving */ /* Re-Build sample stack for saving */
this.buildSampleStack(); this.buildSampleStack();
ItemStack stack = this.getSampleStack(); ItemStack stack = this.getSampleStack();
/* Save sample stack */ /* Save sample stack */
if (stack != null) if (stack != null)
{ {
nbt.setInteger("Count", stack.stackSize); nbt.setInteger("Count", stack.stackSize);
nbt.setCompoundTag("stack", stack.writeToNBT(new NBTTagCompound())); nbt.setCompoundTag("stack", stack.writeToNBT(new NBTTagCompound()));
} }
nbt.setBoolean("oreFilter", this.oreFilterEnabled); nbt.setBoolean("oreFilter", this.oreFilterEnabled);
if (this.filterStack != null) if (this.filterStack != null)
nbt.setCompoundTag("filter", filterStack.writeToNBT(new NBTTagCompound())); {
} nbt.setCompoundTag("filter", filterStack.writeToNBT(new NBTTagCompound()));
}
}
@Override @Override
public void setFilter(ItemStack filter) public ItemStack getFilter()
{ {
this.filterStack = filter; return this.filterStack;
this.onInventoryChanged(); }
}
@Override @Override
public ItemStack getFilter() public void setFilter(ItemStack filter)
{ {
return this.filterStack; this.filterStack = filter;
} this.onInventoryChanged();
}
} }

View file

@ -215,11 +215,11 @@ public class PartLevitator extends PartFace
if (retrieved != null) if (retrieved != null)
{ {
EntityItem item = getItemWithPosition(retrieved); EntityItem entityItem = getItemWithPosition(retrieved);
if (!world().isRemote) if (!world().isRemote)
{ {
world().spawnEntityInWorld(item); world().spawnEntityInWorld(entityItem);
} }
pushDelay = Settings.LEVITATOR_PUSH_DELAY; pushDelay = Settings.LEVITATOR_PUSH_DELAY;