2015-11-02 20:39:44 +00:00
|
|
|
package WayofTime.bloodmagic.tile;
|
2015-10-30 03:22:14 +00:00
|
|
|
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-11-03 16:42:38 +00:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-10-30 03:22:14 +00:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.ChatComponentTranslation;
|
|
|
|
import net.minecraft.util.IChatComponent;
|
|
|
|
import net.minecraft.util.StatCollector;
|
|
|
|
|
|
|
|
public class TileInventory extends TileEntity implements IInventory {
|
|
|
|
|
|
|
|
private ItemStack[] inventory;
|
|
|
|
private int size;
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
public TileInventory(int size, String name) {
|
|
|
|
this.inventory = new ItemStack[size];
|
|
|
|
this.size = size;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:42:38 +00:00
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound tagCompound) {
|
|
|
|
super.readFromNBT(tagCompound);
|
|
|
|
NBTTagList tags = tagCompound.getTagList("Items", 10);
|
|
|
|
inventory = new ItemStack[getSizeInventory()];
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.tagCount(); i++) {
|
|
|
|
NBTTagCompound data = tags.getCompoundTagAt(i);
|
|
|
|
int j = data.getByte("Slot") & 255;
|
|
|
|
|
|
|
|
if (j >= 0 && j < inventory.length) {
|
|
|
|
inventory[j] = ItemStack.loadItemStackFromNBT(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound tagCompound) {
|
|
|
|
super.writeToNBT(tagCompound);
|
|
|
|
NBTTagList tags = new NBTTagList();
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.length; i++) {
|
|
|
|
if (inventory[i] != null) {
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
data.setByte("Slot", (byte) i);
|
|
|
|
inventory[i].writeToNBT(data);
|
|
|
|
tags.appendTag(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tagCompound.setTag("Items", tags);
|
|
|
|
}
|
|
|
|
|
2015-10-30 03:22:14 +00:00
|
|
|
public void dropItems() {
|
2015-11-03 16:42:38 +00:00
|
|
|
if (inventory != null) {
|
2015-11-03 15:34:11 +00:00
|
|
|
for (ItemStack stack : inventory)
|
|
|
|
getWorld().spawnEntityInWorld(new EntityItem(getWorld(), getPos().getX(), pos.getY(), pos.getZ(), stack));
|
|
|
|
}
|
2015-10-30 03:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IInventory
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory() {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int index) {
|
|
|
|
return inventory[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int index, int count) {
|
|
|
|
ItemStack slotStack = getStackInSlot(index);
|
|
|
|
|
|
|
|
if (slotStack.stackSize > count)
|
|
|
|
slotStack.stackSize -= count;
|
|
|
|
else if (slotStack.stackSize <= count)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return slotStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-11-03 15:34:11 +00:00
|
|
|
public ItemStack getStackInSlotOnClosing(int slot) {
|
|
|
|
ItemStack stack = getStackInSlot(slot);
|
|
|
|
if (stack != null)
|
|
|
|
setInventorySlotContents(slot, null);
|
|
|
|
return stack;
|
2015-10-30 03:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-11-03 15:34:11 +00:00
|
|
|
public void setInventorySlotContents(int slot, ItemStack stack) {
|
|
|
|
inventory[slot] = stack;
|
|
|
|
worldObj.markBlockForUpdate(pos);
|
|
|
|
if (stack != null && stack.stackSize > getInventoryStackLimit())
|
|
|
|
stack.stackSize = getInventoryStackLimit();
|
2015-10-30 03:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit() {
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer player) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openInventory(EntityPlayer player) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory(EntityPlayer player) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
|
|
|
this.inventory = new ItemStack[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
// IWorldNameable
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2015-11-02 20:39:44 +00:00
|
|
|
return StatCollector.translateToLocal("tile.BloodMagic." + name + ".name");
|
2015-10-30 03:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomName() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IChatComponent getDisplayName() {
|
2015-11-02 20:39:44 +00:00
|
|
|
return new ChatComponentTranslation("tile.BloodMagic." + name + ".name");
|
2015-10-30 03:22:14 +00:00
|
|
|
}
|
|
|
|
}
|