reworking crates

This commit is contained in:
DarkGuardsman 2013-08-06 11:31:23 -04:00
parent 50d03a632f
commit fb2d7c9723
4 changed files with 315 additions and 237 deletions

View file

@ -19,7 +19,7 @@ import dark.assembly.common.AssemblyLine;
/** A block that allows the placement of mass amount of a specific item within it. It will be allowed
* to go on Conveyor Belts
*
*
* @author Calclavia */
public class BlockCrate extends BlockAssembly
{
@ -208,7 +208,7 @@ public class BlockCrate extends BlockAssembly
}
/** Inserts all items of the same type this player has into the crate.
*
*
* @return True on success */
public boolean insertAllItems(TileEntityCrate tileEntity, EntityPlayer player)
{
@ -253,7 +253,7 @@ public class BlockCrate extends BlockAssembly
}
/** Ejects and item out of the crate and spawn it under the player entity.
*
*
* @param tileEntity
* @param player
* @param requestSize - The maximum stack size to take out. Default should be 64.
@ -267,9 +267,9 @@ public class BlockCrate extends BlockAssembly
int ammountEjected = 0;
if (sampleStack != null && requestSize > 0)
{
for (int slot = 0; slot < tileEntity.getSizeInventory(); slot++)
for (int slot = 0; slot < tileEntity.getInventory().getSizeInventory(); slot++)
{
ItemStack slotStack = tileEntity.getStackInSlot(slot);
ItemStack slotStack = tileEntity.getInventory().getStackInSlot(slot);
if (slotStack != null && slotStack.stackSize > 0)
{
@ -288,7 +288,7 @@ public class BlockCrate extends BlockAssembly
{
slotStack = null;
}
tileEntity.setInventorySlotContents(slot, slotStack);
tileEntity.getInventory().setInventorySlotContents(slot, slotStack);
}
if (ammountEjected >= requestSize)
@ -305,7 +305,7 @@ public class BlockCrate extends BlockAssembly
}
/** Puts an itemStack into the crate.
*
*
* @param tileEntity
* @param itemStack */
public static ItemStack addStackToCrate(TileEntityCrate tileEntity, ItemStack itemStack)
@ -319,7 +319,7 @@ public class BlockCrate extends BlockAssembly
if (containingStack == null || containingStack != null && containingStack.isItemEqual(itemStack))
{
int room = (tileEntity.getSizeInventory() * 64) - (containingStack != null ? containingStack.stackSize : 0);
int room = (tileEntity.getInventory().getSizeInventory() * 64) - (containingStack != null ? containingStack.stackSize : 0);
if (itemStack.stackSize <= room)
{
tileEntity.addToStack(itemStack);
@ -367,9 +367,9 @@ public class BlockCrate extends BlockAssembly
var13.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(var13);
for (int i = 0; i < tileEntity.getSizeInventory(); i++)
for (int i = 0; i < tileEntity.getInventory().getSizeInventory(); i++)
{
tileEntity.setInventorySlotContents(i, null);
tileEntity.getInventory().setInventorySlotContents(i, null);
}
world.setBlock(x, y, z, 0, 0, 3);
return true;

View file

@ -0,0 +1,247 @@
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 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)
{
super(crate, 1028);
this.crate = crate;
}
/** 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++)
{
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)
{
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)
{
this.items[par1] = null;
}
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)
{
return crate.getSlotCount();
}
return 1028;
}
@Override
public String getInvName()
{
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)
{
// TODO Auto-generated method stub
}
@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");
}
}
}
}

View file

@ -118,10 +118,10 @@ public class ItemBlockCrate extends ItemBlock
TileEntityCrate tileEntity = (TileEntityCrate) world.getBlockTileEntity(x, y, z);
int count = containingItem.stackSize;
for (int slot = 0; slot < tileEntity.getSizeInventory(); slot++)
for (int slot = 0; slot < tileEntity.getInventory().getSizeInventory(); slot++)
{
int stackSize = Math.min(64, count);
tileEntity.setInventorySlotContents(slot, new ItemStack(containingItem.itemID, stackSize, containingItem.getItemDamage()));
tileEntity.getInventory().setInventorySlotContents(slot, new ItemStack(containingItem.itemID, stackSize, containingItem.getItemDamage()));
count -= stackSize;
if (count <= 0)

View file

@ -1,59 +1,62 @@
package dark.assembly.common.machine;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import dark.api.IExternalInv;
import dark.api.IInvBox;
import dark.assembly.common.AssemblyLine;
import dark.core.blocks.TileEntityInv;
public class TileEntityCrate extends TileEntityAdvanced implements IInventory, IPacketReceiver, ISidedInventory
public class TileEntityCrate extends TileEntityInv implements IPacketReceiver, IExternalInv
{
/* Collective total stack of all inv slots */
/** Collective total stack of all inv slots */
private ItemStack sampleStack;
/* Slots that can be accesed threw ISidedInv */
private int[] slots;
/* Sudo inv for the crate to interact with other things */
private ItemStack[] items = new ItemStack[1028];
/** delay from last click */
public long prevClickTime = -1000;
/** Clones the single stack into an inventory format for automation interaction */
public void buildInventory()
@Override
public IInvBox getInventory()
{
ItemStack baseStack = this.sampleStack.copy();
this.items = new ItemStack[this.getSlotCount()];
for (int slot = 0; slot < this.items.length; slot++)
if (this.inventory == 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)
{
baseStack = null;
break;
}
inventory = new InventoryCrate(this);
}
return this.inventory;
}
@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;
}
/** 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
*
*
* @param force - force a rebuild of the inventory from the single stack created */
public void buildSampleStack(boolean force)
{
@ -64,18 +67,18 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
boolean rebuildBase = false;
/* Creates the sample stack that is used as a collective itemstack */
for (int i = 0; i < this.items.length; i++)
for (int i = 0; i < this.getInventory().getContainedItems().length; i++)
{
ItemStack stack = this.items[i];
ItemStack stack = this.getInventory().getContainedItems()[i];
if (stack != null && stack.itemID > 0 && stack.stackSize > 0)
{
id = this.items[i].itemID;
meta = this.items[i].getItemDamage();
int ss = this.items[i].stackSize;
id = this.getInventory().getContainedItems()[i].itemID;
meta = this.getInventory().getContainedItems()[i].getItemDamage();
int ss = this.getInventory().getContainedItems()[i].stackSize;
count += ss;
if (ss > this.items[i].getMaxStackSize())
if (ss > this.getInventory().getContainedItems()[i].getMaxStackSize())
{
rebuildBase = true;
}
@ -90,9 +93,9 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
this.sampleStack = new ItemStack(id, count, meta);
}
/* if one stack is over sized this rebuilds the inv to redistribute the items in the slots */
if ((rebuildBase || force || this.items.length > this.getSlotCount()) && this.sampleStack != null)
if ((rebuildBase || force || this.getInventory().getContainedItems().length > this.getSlotCount()) && this.sampleStack != null)
{
this.buildInventory();
this.getInventory().buildInventory(this.sampleStack);
}
}
@ -131,7 +134,7 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
}
if (flag)
{
this.buildInventory();
this.getInventory().buildInventory(this.sampleStack);
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj);
@ -140,6 +143,20 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
}
}
@Override
public void onInventoryChanged()
{
super.onInventoryChanged();
if (this.worldObj != null)
{
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj);
}
}
}
public int getSlotCount()
{
return TileEntityCrate.getSlotCount(this.getBlockMetadata());
@ -212,140 +229,20 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
}
}
@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)
{
this.items[par1] = null;
}
this.onInventoryChanged();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public void onInventoryChanged()
{
super.onInventoryChanged();
if (this.worldObj != null)
{
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj);
}
}
}
@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(this.getDescriptionPacket(), this.worldObj);
}
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
/** NBT Data */
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.items = new ItemStack[this.getSizeInventory()];
this.getInventory().loadInv(nbt);
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");
}
}
else
{
ItemStack stack = new ItemStack(nbt.getInteger("itemID"), nbt.getInteger("Count"), nbt.getInteger("itemMeta"));
this.blockMetadata = nbt.getInteger("size");
if (stack != null && stack.itemID != 0 && stack.stackSize > 0)
{
this.sampleStack = stack;
this.buildInventory();
this.getInventory().buildInventory(this.sampleStack);
}
}
@ -357,6 +254,7 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
super.writeToNBT(nbt);
this.buildSampleStack(false);
ItemStack stack = this.getSampleStack();
nbt.setInteger("size", this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
if (stack != null)
{
nbt.setInteger("itemID", stack.itemID);
@ -365,71 +263,4 @@ public class TileEntityCrate extends TileEntityAdvanced implements IInventory, I
}
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public int getSizeInventory()
{
if (worldObj != null)
{
return this.getSlotCount();
}
return this.items.length;
}
@Override
public String getInvName()
{
return "inv.Crate";
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack)
{
if (slot >= this.getSlotCount())
{
return false;
}
if (this.sampleStack == null || itemstack != null && itemstack.isItemEqual(this.sampleStack))
{
return true;
}
return false;
}
@Override
public int[] getAccessibleSlotsFromSide(int side)
{
if (slots == null || slots != null && slots.length != this.getSlotCount())
{
slots = new int[this.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);
}
@Override
public boolean canExtractItem(int slot, ItemStack itemstack, int side)
{
return true;
}
}