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-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2014-12-12 15:40:01 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{
|
2014-12-12 15:40:01 +01:00
|
|
|
|
|
|
|
public ItemStack slots[];
|
2015-02-17 16:15:16 +01:00
|
|
|
public String name;
|
|
|
|
|
|
|
|
public TileEntityInventoryBase(int slots, String name){
|
|
|
|
this.initializeSlots(slots);
|
|
|
|
this.name = name;
|
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2014-12-12 15:40:01 +01:00
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
super.writeToNBT(compound);
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
2015-03-07 02:23:31 +01:00
|
|
|
for(int currentIndex = 0; currentIndex < slots.length; currentIndex++){
|
2014-12-12 15:40:01 +01:00
|
|
|
if (slots[currentIndex] != null){
|
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte)currentIndex);
|
|
|
|
slots[currentIndex].writeToNBT(tagCompound);
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
compound.setTag("Items", tagList);
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2014-12-12 15:40:01 +01:00
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
super.readFromNBT(compound);
|
|
|
|
NBTTagList tagList = compound.getTagList("Items", 10);
|
2015-03-07 02:23:31 +01:00
|
|
|
for (int i = 0; i < tagList.tagCount(); i++){
|
2014-12-12 15:40:01 +01:00
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
|
|
|
byte slotIndex = tagCompound.getByte("Slot");
|
|
|
|
if (slotIndex >= 0 && slotIndex < slots.length){
|
|
|
|
slots[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public int getInventoryStackLimit(){
|
2014-12-12 15:40:01 +01:00
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer player){
|
|
|
|
return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64;
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2014-12-12 15:40:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public ItemStack getStackInSlotOnClosing(int i){
|
2014-12-12 15:40:01 +01:00
|
|
|
return getStackInSlot(i);
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2014-12-12 15:40:01 +01:00
|
|
|
public void setInventorySlotContents(int i, ItemStack stack){
|
|
|
|
this.slots[i] = stack;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public int getSizeInventory(){
|
2014-12-12 15:40:01 +01:00
|
|
|
return slots.length;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public ItemStack getStackInSlot(int i){
|
2014-12-12 15:40:01 +01:00
|
|
|
return slots[i];
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public ItemStack decrStackSize(int i, int j){
|
2014-12-12 15:40:01 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
|
|
|
|
public void initializeSlots(int itemAmount){
|
|
|
|
this.slots = new ItemStack[itemAmount];
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public String getInventoryName(){
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public boolean hasCustomInventoryName(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void openInventory(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void closeInventory(){
|
|
|
|
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getAccessibleSlotsFromSide(int side){
|
|
|
|
int[] theInt = new int[slots.length];
|
|
|
|
for(int i = 0; i < theInt.length; i++){
|
|
|
|
theInt[i] = i;
|
|
|
|
}
|
|
|
|
return theInt;
|
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|