equivalent-exchange-3/common/com/pahimar/ee3/tileentity/TileCalcinator.java

187 lines
4.9 KiB
Java
Raw Normal View History

2013-08-23 16:59:50 +02: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 com.pahimar.ee3.lib.Strings;
/**
* Equivalent-Exchange-3
*
* TileCalcinator
*
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*
*/
public class TileCalcinator extends TileEE implements IInventory {
/**
* The ItemStacks that hold the items currently being used in the Calcinator
*/
private ItemStack[] inventory;
public static final int INVENTORY_SIZE = 3;
public static final int FUEL_INVENTORY_INDEX = 0;
public static final int INPUT_INVENTORY_INDEX = 1;
public static final int OUTPUT_INVENTORY_INDEX = 2;
public TileCalcinator() {
inventory = new ItemStack[INVENTORY_SIZE];
}
/**
* Returns the number of slots in the inventory.
*/
@Override
public int getSizeInventory() {
return inventory.length;
}
/**
* Returns the stack in slot i
*/
@Override
public ItemStack getStackInSlot(int slotIndex) {
2013-08-23 16:59:50 +02:00
return inventory[slotIndex];
2013-08-23 16:59:50 +02:00
}
@Override
public ItemStack decrStackSize(int slotIndex, int decrementAmount) {
2013-08-23 16:59:50 +02:00
ItemStack itemStack = getStackInSlot(slotIndex);
2013-08-23 16:59:50 +02:00
if (itemStack != null) {
if (itemStack.stackSize <= decrementAmount) {
setInventorySlotContents(slotIndex, null);
2013-08-23 16:59:50 +02:00
}
else {
itemStack = itemStack.splitStack(decrementAmount);
2013-08-23 16:59:50 +02:00
if (itemStack.stackSize == 0) {
setInventorySlotContents(slotIndex, null);
2013-08-23 16:59:50 +02:00
}
}
}
return itemStack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slotIndex) {
2013-08-23 16:59:50 +02:00
ItemStack itemStack = getStackInSlot(slotIndex);
2013-08-23 16:59:50 +02:00
if (itemStack != null) {
setInventorySlotContents(slotIndex, null);
2013-08-23 16:59:50 +02:00
}
return itemStack;
}
@Override
public void setInventorySlotContents(int slotIndex, ItemStack itemStack) {
2013-08-23 16:59:50 +02:00
inventory[slotIndex] = itemStack;
2013-08-23 16:59:50 +02:00
if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) {
itemStack.stackSize = getInventoryStackLimit();
}
}
@Override
public String getInvName() {
return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_CALCINATOR_NAME;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
@Override
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()];
for (int i = 0; i < tagList.tagCount(); ++i) {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(i);
byte slotIndex = tagCompound.getByte("Slot");
if (slotIndex >= 0 && slotIndex < inventory.length) {
inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
2013-08-23 16:59:50 +02:00
}
}
}
@Override
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) {
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("Slot", (byte) currentIndex);
inventory[currentIndex].writeToNBT(tagCompound);
tagList.appendTag(tagCompound);
}
}
nbtTagCompound.setTag("Items", tagList);
}
@Override
public boolean isInvNameLocalized() {
return this.hasCustomName();
}
@Override
public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack) {
2013-08-23 16:59:50 +02:00
return true;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(super.toString());
stringBuilder.append("TileCalcinator 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();
}
}