128 lines
3.4 KiB
Java
128 lines
3.4 KiB
Java
|
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;
|
||
|
|
||
|
public class TileAlchemicalChest extends TileEE implements IInventory {
|
||
|
|
||
|
/**
|
||
|
* The ItemStacks that hold the items currently being used in the Alchemical
|
||
|
* Chest
|
||
|
*/
|
||
|
private ItemStack[] inventory;
|
||
|
|
||
|
public TileAlchemicalChest() {
|
||
|
|
||
|
inventory = new ItemStack[13 * 4];
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getSizeInventory() {
|
||
|
|
||
|
return this.inventory.length;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ItemStack getStackInSlot(int slot) {
|
||
|
|
||
|
return this.inventory[slot];
|
||
|
}
|
||
|
|
||
|
@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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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 "container." + Strings.ALCHEMICAL_CHEST_NAME;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getInventoryStackLimit() {
|
||
|
|
||
|
return 64;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void openChest() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void closeChest() {
|
||
|
|
||
|
}
|
||
|
|
||
|
public void readFromNBT(NBTTagCompound nbtTagCompound) {
|
||
|
|
||
|
super.readFromNBT(nbtTagCompound);
|
||
|
|
||
|
// Read in the ItemStacks in the inventory from NBT
|
||
|
NBTTagList tagList = nbtTagCompound.getTagList("Items");
|
||
|
this.inventory = new ItemStack[this.getSizeInventory()];
|
||
|
for (int i = 0; i < tagList.tagCount(); ++i) {
|
||
|
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(i);
|
||
|
byte slot = tagCompound.getByte("Slot");
|
||
|
if (slot >= 0 && slot < this.inventory.length) {
|
||
|
this.inventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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 < this.inventory.length; ++currentIndex) {
|
||
|
if (this.inventory[currentIndex] != null) {
|
||
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
||
|
tagCompound.setByte("Slot", (byte) currentIndex);
|
||
|
this.inventory[currentIndex].writeToNBT(tagCompound);
|
||
|
tagList.appendTag(tagCompound);
|
||
|
}
|
||
|
}
|
||
|
nbtTagCompound.setTag("Items", tagList);
|
||
|
}
|
||
|
}
|