equivalent-exchange-3/ee3_common/com/pahimar/ee3/tileentity/TileAludel.java

207 lines
5.7 KiB
Java
Raw Normal View History

2013-02-18 03:30:11 +01:00
package com.pahimar.ee3.tileentity;
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;
2013-02-18 03:30:11 +01:00
import com.pahimar.ee3.core.util.ItemUtil;
2013-02-26 22:08:43 +01:00
import com.pahimar.ee3.lib.Strings;
import com.pahimar.ee3.network.PacketTypeHandler;
import com.pahimar.ee3.network.packet.PacketTileWithItemUpdate;
2013-02-18 03:30:11 +01:00
/**
* Equivalent-Exchange-3
*
* TileAludel
*
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*
*/
2013-02-18 03:30:11 +01:00
public class TileAludel extends TileEE implements IInventory {
/**
2013-02-19 02:13:24 +01:00
* The ItemStacks that hold the items currently being used in the Aludel
2013-02-18 03:30:11 +01:00
*/
private ItemStack[] inventory;
public static final int INVENTORY_SIZE = 4;
public static final int FUEL_INVENTORY_INDEX = 0;
public static final int INPUT_INVENTORY_INDEX = 1;
public static final int DUST_INVENTORY_INDEX = 2;
2013-02-26 22:08:43 +01:00
public static final int OUTPUT_INVENTORY_INDEX = 3;
2013-02-18 03:30:11 +01:00
public TileAludel() {
2013-02-26 22:08:43 +01:00
inventory = new ItemStack[INVENTORY_SIZE];
2013-02-18 03:30:11 +01:00
}
2013-02-26 22:08:43 +01:00
2013-02-18 03:30:11 +01:00
@Override
public int getSizeInventory() {
return inventory.length;
2013-02-18 03:30:11 +01:00
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
2013-02-18 03:30:11 +01:00
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack itemStack = getStackInSlot(slot);
if (itemStack != null) {
if (itemStack.stackSize <= amount) {
setInventorySlotContents(slot, null);
}
else {
itemStack = itemStack.splitStack(amount);
if (itemStack.stackSize == 0) {
setInventorySlotContents(slot, null);
}
}
}
2013-02-26 22:08:43 +01:00
2013-02-18 03:30:11 +01:00
return itemStack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack itemStack = getStackInSlot(slot);
if (itemStack != null) {
setInventorySlotContents(slot, null);
}
return itemStack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemStack) {
inventory[slot] = itemStack;
if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) {
itemStack.stackSize = getInventoryStackLimit();
}
}
@Override
public String getInvName() {
return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_ALUDEL_NAME;
2013-02-18 03:30:11 +01:00
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void openChest() {
2013-02-26 22:08:43 +01:00
2013-02-18 03:30:11 +01:00
}
@Override
public void closeChest() {
2013-02-26 22:08:43 +01:00
2013-02-18 03:30:11 +01:00
}
2013-02-26 22:08:43 +01:00
@Override
2013-02-18 03:30:11 +01:00
public void readFromNBT(NBTTagCompound nbtTagCompound) {
super.readFromNBT(nbtTagCompound);
// Read in the ItemStacks in the inventory from NBT
NBTTagList tagList = nbtTagCompound.getTagList("Items");
inventory = new ItemStack[this.getSizeInventory()];
2013-02-18 03:30:11 +01:00
for (int i = 0; i < tagList.tagCount(); ++i) {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(i);
byte slot = tagCompound.getByte("Slot");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
2013-02-18 03:30:11 +01:00
}
}
}
@Override
2013-02-18 03:30:11 +01:00
public void writeToNBT(NBTTagCompound nbtTagCompound) {
super.writeToNBT(nbtTagCompound);
// Write the ItemStacks in the inventory to NBT
NBTTagList tagList = new NBTTagList();
for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
if (inventory[currentIndex] != null) {
2013-02-18 03:30:11 +01:00
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("Slot", (byte) currentIndex);
inventory[currentIndex].writeToNBT(tagCompound);
2013-02-18 03:30:11 +01:00
tagList.appendTag(tagCompound);
}
}
nbtTagCompound.setTag("Items", tagList);
}
2013-03-05 04:18:15 +01:00
@Override
public boolean isInvNameLocalized() {
2013-03-05 04:18:15 +01:00
return this.hasCustomName();
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
2013-03-05 04:18:15 +01:00
return true;
}
@Override
public Packet getDescriptionPacket() {
ItemStack itemStack = getStackInSlot(INPUT_INVENTORY_INDEX);
if (itemStack != null && itemStack.stackSize > 0)
return PacketTypeHandler.populatePacket(new PacketTileWithItemUpdate(xCoord, yCoord, zCoord, orientation, state, customName, itemStack.itemID, itemStack.getItemDamage(), itemStack.stackSize, ItemUtil.getColor(itemStack)));
else
return super.getDescriptionPacket();
}
@Override
public void onInventoryChanged() {
worldObj.updateAllLightTypes(xCoord, yCoord, zCoord);
if (worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord) instanceof TileGlassBell) {
worldObj.updateAllLightTypes(xCoord, yCoord + 1, zCoord);
}
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(super.toString());
stringBuilder.append("TileAludel Data - ");
for (int i = 0; i < inventory.length; i++) {
if (i != 0) {
stringBuilder.append(", ");
}
if (inventory[i] != null) {
stringBuilder.append(String.format("inventory[%d]: %s", i, inventory[i].toString()));
}
else {
stringBuilder.append(String.format("inventory[%d]: empty", i));
}
}
stringBuilder.append("\n");
return stringBuilder.toString();
}
2013-02-18 03:30:11 +01:00
}