package extracells.tileentity;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import appeng.api.WorldCoord;
import appeng.api.events.GridStorageUpdateEvent;
import appeng.api.events.GridTileLoadEvent;
import appeng.api.events.GridTileUnloadEvent;
import appeng.api.me.tiles.ICellContainer;
import appeng.api.me.tiles.IGridMachine;
import appeng.api.me.util.IGridInterface;
import appeng.api.me.util.IMEInventoryHandler;
public class TileEntityHardMEDrive extends ColorableECTile implements IInventory, IGridMachine, ICellContainer
{
Boolean powerStatus = true, networkReady = true;
private IGridInterface grid;
private String customName = StatCollector.translateToLocal("tile.block.hardmedrive");
private ItemStack[] slots = new ItemStack[3];
public TileEntityHardMEDrive()
{
super();
}
@Override
public void validate()
{
super.validate();
MinecraftForge.EVENT_BUS.post(new GridTileLoadEvent(this, worldObj, getLocation()));
}
@Override
public void invalidate()
{
super.invalidate();
MinecraftForge.EVENT_BUS.post(new GridTileUnloadEvent(this, worldObj, getLocation()));
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbtTag = getColorDataForPacket();
this.writeToNBT(nbtTag);
return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.slots.length; ++i)
{
if (this.slots[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte) i);
this.slots[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
nbt.setTag("Items", nbttaglist);
if (this.isInvNameLocalized())
{
nbt.setString("CustomName", this.customName);
}
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
NBTTagList nbttaglist = nbt.getTagList("Items");
this.slots = new ItemStack[this.getSizeInventory()];
if (nbt.hasKey("CustomName"))
{
this.customName = nbt.getString("CustomName");
}
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttaglist.tagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.slots.length)
{
this.slots[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
}
@Override
public int getSizeInventory()
{
return 3;
}
@Override
public ItemStack getStackInSlot(int i)
{
if (slots[i] != null)
{
return slots[i];
} else
{
return null;
}
}
@Override
public ItemStack decrStackSize(int i, int j)
{
if (this.slots[i] != null)
{
ItemStack itemstack;
if (this.slots[i].stackSize <= j)
{
itemstack = this.slots[i];
this.slots[i] = null;
this.onInventoryChanged();
return itemstack;
} else
{
itemstack = this.slots[i].splitStack(j);
if (this.slots[i].stackSize == 0)
{
this.slots[i] = null;
}
this.onInventoryChanged();
return itemstack;
}
} else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
if (this.slots[i] != null)
{
ItemStack itemstack = this.slots[i];
this.slots[i] = null;
return itemstack;
} else
{
return null;
}
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.slots[i] = itemstack;
if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
{
itemstack.stackSize = this.getInventoryStackLimit();
}
this.onInventoryChanged();
}
@Override
public void onInventoryChanged()
{
super.onInventoryChanged();
MinecraftForge.EVENT_BUS.post(new GridStorageUpdateEvent(worldObj, new WorldCoord(this.xCoord, this.yCoord, this.zCoord), this.getGrid()));
}
@Override
public String getInvName()
{
return customName;
}
@Override
public boolean isInvNameLocalized()
{
return true;
}
@Override
public int getInventoryStackLimit()
{
return 1;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return true;
}
@Override
public void openChest()
{
// NOBODY needs this!
}
@Override
public void closeChest()
{
// NOBODY needs this!
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return appeng.api.Util.getCellRegistry().isCellHandled(itemstack);
}
@Override
public WorldCoord getLocation()
{
return new WorldCoord(xCoord, yCoord, zCoord);
}
@Override
public boolean isValid()
{
return true;
}
@Override
public void setPowerStatus(boolean hasPower)
{
this.powerStatus = hasPower;
}
@Override
public boolean isPowered()
{
return powerStatus;
}
@Override
public IGridInterface getGrid()
{
return this.grid;
}
@Override
public void setGrid(IGridInterface gi)
{
this.grid = gi;
}
@Override
public World getWorld()
{
return this.worldObj;
}
@Override
public List<IMEInventoryHandler> getCellArray()
{
if (powerStatus)
{
List<IMEInventoryHandler> cellArray = new ArrayList<IMEInventoryHandler>();
for (ItemStack current : slots)
{
if (appeng.api.Util.getCellRegistry().isCellHandled(current))
cellArray.add(appeng.api.Util.getCellRegistry().getHandlerForCell(current));
}
return cellArray;
}
return null;
}
@Override
public int getPriority()
{
return 0;
}
@Override
public float getPowerDrainPerTick()
{
return 2.0F;
}
public void setNetworkReady(boolean isReady)
{
networkReady = isReady;
}
public boolean isMachineActive()
{
return powerStatus && networkReady;
}
}