2015-01-30 20:16:32 +01:00
|
|
|
package ellpeck.someprettyrandomstuff.tile;
|
2014-12-12 15:40:01 +01:00
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-02-09 17:25:05 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2014-12-12 15:40:01 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
2015-02-09 17:25:05 +01:00
|
|
|
public abstract class TileEntityInventoryBase extends TileEntityBase implements IInventory{
|
2014-12-12 15:40:01 +01:00
|
|
|
|
|
|
|
public ItemStack slots[];
|
|
|
|
|
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
super.writeToNBT(compound);
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
|
|
|
for(int currentIndex = 0; currentIndex < slots.length; ++currentIndex){
|
|
|
|
if (slots[currentIndex] != null){
|
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte)currentIndex);
|
|
|
|
slots[currentIndex].writeToNBT(tagCompound);
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
compound.setTag("Items", tagList);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
super.readFromNBT(compound);
|
|
|
|
NBTTagList tagList = compound.getTagList("Items", 10);
|
|
|
|
for (int i = 0; i < tagList.tagCount(); ++i){
|
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
|
|
|
byte slotIndex = tagCompound.getByte("Slot");
|
|
|
|
if (slotIndex >= 0 && slotIndex < slots.length){
|
|
|
|
slots[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInventoryStackLimit() {
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer player) {
|
2015-02-09 17:25:05 +01:00
|
|
|
return worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.getPos().getX() + 0.5D, this.getPos().getY() + 0.5D, this.getPos().getZ() + 0.5D) <= 64;
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getStackInSlotOnClosing(int i) {
|
|
|
|
return getStackInSlot(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setInventorySlotContents(int i, ItemStack stack){
|
|
|
|
this.slots[i] = stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSizeInventory() {
|
|
|
|
return slots.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getStackInSlot(int i) {
|
|
|
|
return slots[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack decrStackSize(int i, int j) {
|
|
|
|
if (slots[i] != null) {
|
|
|
|
ItemStack stackAt;
|
|
|
|
if (slots[i].stackSize <= j) {
|
|
|
|
stackAt = slots[i];
|
|
|
|
slots[i] = null;
|
|
|
|
return stackAt;
|
|
|
|
} else {
|
|
|
|
stackAt = slots[i].splitStack(j);
|
|
|
|
if (slots[i].stackSize == 0)
|
|
|
|
slots[i] = null;
|
|
|
|
return stackAt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|