Worked on inv prefabs

This commit is contained in:
DarkGuardsman 2013-08-06 11:29:56 -04:00
parent 38e98bab59
commit cec261a273
6 changed files with 574 additions and 46 deletions

View file

@ -3,58 +3,57 @@ package universalelectricity.prefab.tile;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
/**
* A TileEntity with some pre-added functionalities.
*
* @author Calclavia
*
*/
/** A TileEntity with some pre-added functionalities.
*
* @author Calclavia */
public abstract class TileEntityAdvanced extends TileEntity
{
protected long ticks = 0;
protected long ticks = 0;
@Override
public void updateEntity()
{
if (this.ticks == 0)
{
this.initiate();
}
@Override
public void updateEntity()
{
if (this.ticks == 0)
{
this.initiate();
}
if (this.ticks >= Long.MAX_VALUE)
{
this.ticks = 1;
}
if (this.ticks >= Long.MAX_VALUE)
{
this.ticks = 1;
}
this.ticks++;
}
this.ticks++;
}
/**
* Called on the TileEntity's first tick.
*/
public void initiate()
{
}
/** Called on the TileEntity's first tick. */
public void initiate()
{
}
@Override
public int getBlockMetadata()
{
if (this.blockMetadata == -1)
{
this.blockMetadata = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
}
@Override
public int getBlockMetadata()
{
if (this.worldObj == null)
{
return 0;
}
else if (this.blockMetadata == -1)
{
this.blockMetadata = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
}
return this.blockMetadata;
}
return this.blockMetadata;
}
@Override
public Block getBlockType()
{
if (this.blockType == null)
{
this.blockType = Block.blocksList[this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord)];
}
@Override
public Block getBlockType()
{
if (this.blockType == null)
{
this.blockType = Block.blocksList[this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord)];
}
return this.blockType;
}
return this.blockType;
}
}

View file

@ -0,0 +1,14 @@
package dark.api;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
public interface IExternalInv
{
public IInvBox getInventory();
public boolean canStore(ItemStack stack, int slot, ForgeDirection side);
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side);
}

18
src/dark/api/IInvBox.java Normal file
View file

@ -0,0 +1,18 @@
package dark.api;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
public interface IInvBox extends ISidedInventory
{
/** Gets the inventory array. ForgeDirection.UNKOWN must return all sides */
public ItemStack[] getContainedItems();
/** Called to save the inventory array */
public void saveInv(NBTTagCompound tag);
/** Called to load the inventory array */
public void loadInv(NBTTagCompound tag);
}

View file

@ -0,0 +1,231 @@
package dark.core.blocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import dark.api.IExternalInv;
import dark.api.IInvBox;
public class InvChest implements IInvBox
{
/** Access able slots side all */
private int[] chestSlots;
/** Items contained in this inv */
private ItemStack[] items;
/** Host tileEntity */
private TileEntity hostChest;
/** Host tileEntity as external inv */
private IExternalInv inv;
/** Default slot max count */
private final int slots;
public InvChest(TileEntity chest, IExternalInv inv, int slots)
{
this.hostChest = chest;
this.slots = slots;
this.inv = inv;
}
public InvChest(TileEntity chest, int slots)
{
this(chest, ((IExternalInv) chest), slots);
}
@Override
public int getSizeInventory()
{
return slots;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return this.items[slot];
}
@Override
public ItemStack decrStackSize(int slot, int ammount)
{
if (this.items[slot] != null)
{
ItemStack var3;
if (this.items[slot].stackSize <= ammount)
{
var3 = this.items[slot];
this.items[slot] = null;
this.onInventoryChanged();
return var3;
}
else
{
var3 = this.items[slot].splitStack(ammount);
if (this.items[slot].stackSize == 0)
{
this.items[slot] = null;
}
this.onInventoryChanged();
return var3;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int par1)
{
if (this.items[par1] != null)
{
ItemStack var2 = this.items[par1];
this.items[par1] = null;
return var2;
}
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();
}
this.onInventoryChanged();
}
@Override
public String getInvName()
{
return "container.chest";
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return true;
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
if (chestSlots == null)
{
this.chestSlots = new int[this.getSizeInventory()];
for (int i = 0; i < this.chestSlots.length; i++)
{
chestSlots[i] = i;
}
}
return this.chestSlots;
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return this.isItemValidForSlot(i, itemstack) && this.inv.canStore(itemstack, i, ForgeDirection.getOrientation(j));
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return this.inv.canRemove(itemstack, i, ForgeDirection.getOrientation(j));
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void onInventoryChanged()
{
this.hostChest.onInventoryChanged();
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.hostChest.worldObj.getBlockTileEntity(this.hostChest.xCoord, this.hostChest.yCoord, this.hostChest.zCoord) != this.hostChest ? false : par1EntityPlayer.getDistanceSq((double) this.hostChest.xCoord + 0.5D, (double) this.hostChest.yCoord + 0.5D, (double) this.hostChest.zCoord + 0.5D) <= 64.0D;
}
@Override
public ItemStack[] getContainedItems()
{
if (this.items == null)
{
this.items = new ItemStack[this.getSizeInventory()];
}
return this.items;
}
@Override
public void saveInv(NBTTagCompound nbt)
{
NBTTagList var2 = new NBTTagList();
for (int var3 = 0; var3 < this.items.length; ++var3)
{
if (this.items[var3] != null)
{
NBTTagCompound var4 = new NBTTagCompound();
var4.setByte("Slot", (byte) var3);
this.items[var3].writeToNBT(var4);
var2.appendTag(var4);
}
}
nbt.setTag("Items", var2);
}
@Override
public void loadInv(NBTTagCompound nbt)
{
// chest inv reading
NBTTagList var2 = nbt.getTagList("Items");
this.items = new ItemStack[this.getSizeInventory()];
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound) var2.tagAt(var3);
int var5 = var4.getByte("Slot") & 255;
if (var5 >= 0 && var5 < this.items.length)
{
this.items[var5] = ItemStack.loadItemStackFromNBT(var4);
}
}
}
}

View file

@ -0,0 +1,146 @@
package dark.core.blocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import dark.api.IExternalInv;
import dark.api.IInvBox;
public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, ISidedInventory
{
protected IInvBox inventory;
@Override
public IInvBox getInventory()
{
if (inventory == null)
{
inventory = new InvChest(this, 1);
}
return inventory;
}
@Override
public int getSizeInventory()
{
return this.getInventory().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i)
{
return this.getInventory().getStackInSlot(i);
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return this.getInventory().decrStackSize(i, j);
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return this.getInventory().getStackInSlotOnClosing(i);
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.getInventory().setInventorySlotContents(i, itemstack);
}
@Override
public String getInvName()
{
return this.getInventory().getInvName();
}
@Override
public boolean isInvNameLocalized()
{
return this.getInventory().isInvNameLocalized();
}
@Override
public int getInventoryStackLimit()
{
return this.getInventory().getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.getInventory().isUseableByPlayer(entityplayer);
}
@Override
public void openChest()
{
this.getInventory().openChest();
}
@Override
public void closeChest()
{
this.getInventory().closeChest();
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return this.getInventory().isItemValidForSlot(i, itemstack);
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
return this.getInventory().getAccessibleSlotsFromSide(var1);
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canInsertItem(i, itemstack, j);
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canExtractItem(i, itemstack, j);
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.getInventory().loadInv(nbt);
}
/**
* Writes a tile entity to NBT.
*/
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
this.getInventory().saveInv(nbt);
}
}

View file

@ -3,6 +3,7 @@ package dark.core.blocks;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
@ -21,23 +22,27 @@ import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.api.IDisableable;
import dark.api.IExternalInv;
import dark.api.IInvBox;
import dark.api.IPowerLess;
import dark.api.PowerSystems;
import dark.core.DarkMain;
public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements IDisableable, IPacketReceiver, IPowerLess
public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IPowerLess
{
/** Forge Ore Directory name of the item to toggle power */
public static String powerToggleItemID = "battery";
protected Random random = new Random();
/** ticks to act dead or disabled */
protected int ticksDisabled = 0;
protected float WATTS_PER_TICK, MAX_WATTS;
protected boolean unpowered, running;
/** Inventory used by this machine */
protected IInvBox inventory;
public TileEntityMachine()
{
@ -243,4 +248,119 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
nbt.setInteger("disabledTicks", this.ticksDisabled);
nbt.setBoolean("shouldPower", this.unpowered);
}
@Override
public IInvBox getInventory()
{
if (inventory == null)
{
inventory = new InvChest(this, 1);
}
return inventory;
}
@Override
public int getSizeInventory()
{
return this.getInventory().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i)
{
return this.getInventory().getStackInSlot(i);
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return this.getInventory().decrStackSize(i, j);
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return this.getInventory().getStackInSlotOnClosing(i);
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.getInventory().setInventorySlotContents(i, itemstack);
}
@Override
public String getInvName()
{
return this.getInventory().getInvName();
}
@Override
public boolean isInvNameLocalized()
{
return this.getInventory().isInvNameLocalized();
}
@Override
public int getInventoryStackLimit()
{
return this.getInventory().getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.getInventory().isUseableByPlayer(entityplayer);
}
@Override
public void openChest()
{
this.getInventory().openChest();
}
@Override
public void closeChest()
{
this.getInventory().closeChest();
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return this.getInventory().isItemValidForSlot(i, itemstack);
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
return this.getInventory().getAccessibleSlotsFromSide(var1);
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canInsertItem(i, itemstack, j);
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canExtractItem(i, itemstack, j);
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
}