/**
Copyright (C) <2017> <coolAlias>
This file is part of coolAlias' Zelda Sword Skills Minecraft Mod; as such,
you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package zeldaswordskills.block.tileentity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.Constants;
/**
*
* Base class for any tile entity that needs an inventory;
* disallows automation by default, re-override ISidedInventory methods if needed;
* displays empty "" translated name by default
*
*/
public abstract class TileEntityInventory extends TileEntityBase implements ISidedInventory
{
/** The tile entity's inventory slots need to be initialized during construction */
protected ItemStack[] inventory;
/** Dummy array to return for {@link ISidedInventory#getAccessibleSlotsFromSide} */
private static final int[] availableSlots = new int[]{0};
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if(stack.stackSize > amount) {
stack = stack.splitStack(amount);
markDirty();
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public ItemStack removeStackFromSlot(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
markDirty();
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); ++i) {
NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
byte slot = item.getByte("Slot");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(item);
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
NBTTagList items = new NBTTagList();
for (int i = 0; i < inventory.length; ++i) {
if (inventory[i] != null) {
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte) i);
inventory[i].writeToNBT(item);
items.appendTag(item);
}
}
compound.setTag("Items", items);
}
@Override
public void openInventory(EntityPlayer player) {}
@Override
public void closeInventory(EntityPlayer player) {}
@Override
public int getField(int id) {
return 0;
}
@Override
public void setField(int id, int value) {}
@Override
public int getFieldCount() {
return 0;
}
@Override
public void clear() {
for (int i = 0; i < inventory.length; ++i) {
inventory[i] = null;
}
}
@Override
public String getName() {
return "";
}
@Override
public boolean hasCustomName() {
return false;
}
@Override
public IChatComponent getDisplayName() {
return (IChatComponent)(hasCustomName() ? new ChatComponentText(getName()) : new ChatComponentTranslation(getName()));
}
@Override
public int[] getSlotsForFace(EnumFacing face) {
return availableSlots;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing direction) {
return false;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing direction) {
return false;
}
}