Advertisement
TechMage66

TileEntityEmpty

Aug 7th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. public class TileEntityEmpty extends TileEntityMageTech implements ISidedInventory
  2. {
  3.     public static final int INVENTORY_SIZE = 2;
  4.  
  5.     private static final int[] slotsTop = new int[] {0};
  6.     private static final int[] slotsBottom = new int[] {1};
  7.     private static final int[] slotsSides = new int[] {0, 1};
  8.  
  9.     private ItemStack[] inventory;
  10.  
  11.     public TileEntityEmpty ()
  12.     {
  13.         inventory = new ItemStack[INVENTORY_SIZE];
  14.     }
  15.  
  16.     @Override
  17.     public int getSizeInventory()
  18.     {
  19.         return inventory.length;
  20.     }
  21.  
  22.     @Override
  23.     public ItemStack getStackInSlot(int slotIndex)
  24.     {
  25.         return inventory[slotIndex];
  26.     }
  27.  
  28.     @Override
  29.     public ItemStack decrStackSize(int slotIndex, int decrementAmount)
  30.     {
  31.         ItemStack itemStack = getStackInSlot(slotIndex);
  32.         if (itemStack != null)
  33.         {
  34.             if (itemStack.stackSize <= decrementAmount)
  35.                 setInventorySlotContents(slotIndex, null);
  36.  
  37.             else
  38.             {
  39.                 itemStack = itemStack.splitStack(decrementAmount);
  40.                 if (itemStack.stackSize == 0)
  41.                     setInventorySlotContents(slotIndex, null);
  42.             }
  43.         }
  44.  
  45.         return itemStack;
  46.     }
  47.  
  48.     @Override
  49.     public ItemStack getStackInSlotOnClosing(int slotIndex)
  50.     {
  51.         ItemStack itemStack = getStackInSlot(slotIndex);
  52.         if (itemStack != null)
  53.             setInventorySlotContents(slotIndex, null);
  54.  
  55.         return itemStack;
  56.     }
  57.  
  58.     @Override
  59.     public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
  60.     {
  61.         inventory[slotIndex] = itemStack;
  62.         if (itemStack != null && itemStack.stackSize > getInventoryStackLimit())
  63.             itemStack.stackSize = getInventoryStackLimit();
  64.     }
  65.  
  66.     @Override
  67.     public String getInventoryName()
  68.     {
  69.         return this.hasCustomName() ? this.getCustomName() : Names.Containers.CONTAINERNAME;
  70.     }
  71.  
  72.     @Override
  73.     public boolean hasCustomInventoryName()
  74.     {
  75.         return this.hasCustomName();
  76.     }
  77.  
  78.     @Override
  79.     public int getInventoryStackLimit()
  80.     {
  81.         return 64;
  82.     }
  83.  
  84.     @Override
  85.     public boolean isUseableByPlayer(EntityPlayer entityplayer)
  86.     {
  87.         return true;
  88.     }
  89.  
  90.     @Override
  91.     public void openInventory() { }
  92.  
  93.     @Override
  94.     public void closeInventory() { }
  95.  
  96.     @Override
  97.     public void writeToNBT(NBTTagCompound nbtTagCompound)
  98.     {
  99.         super.writeToNBT(nbtTagCompound);
  100.  
  101.         // Write the ItemStacks in the inventory to NBT
  102.         NBTTagList tagList = new NBTTagList();
  103.         for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex)
  104.         {
  105.             if (inventory[currentIndex] != null)
  106.             {
  107.                 NBTTagCompound tagCompound = new NBTTagCompound();
  108.                 tagCompound.setByte("Slot", (byte) currentIndex);
  109.                 inventory[currentIndex].writeToNBT(tagCompound);
  110.                 tagList.appendTag(tagCompound);
  111.             }
  112.         }
  113.  
  114.         nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
  115.     }
  116.  
  117.     @Override
  118.     public void readFromNBT(NBTTagCompound nbtTagCompound)
  119.     {
  120.         super.readFromNBT(nbtTagCompound);
  121.  
  122.         // Read in the ItemStacks in the inventory from NBT
  123.         NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
  124.         inventory = new ItemStack[this.getSizeInventory()];
  125.         for (int i = 0; i < tagList.tagCount(); ++i)
  126.         {
  127.             NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
  128.             byte slotIndex = tagCompound.getByte("Slot");
  129.             if (slotIndex >= 0 && slotIndex < inventory.length)
  130.             {
  131.                 inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
  132.             }
  133.         }
  134.     }
  135.    
  136.     @Override
  137.     public void updateEntity()  {  }
  138.    
  139.     public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack)
  140.     {
  141.         return slotIndex != 1;
  142.     }
  143.  
  144.     public int[] getAccessibleSlotsFromSide(int side)
  145.     {
  146.         return side == 0 ? slotsBottom : (side == 1 ? slotsTop : slotsSides);
  147.     }
  148.  
  149.     public boolean canInsertItem(int slot, ItemStack itemStack, int side)
  150.     {
  151.         return this.isItemValidForSlot(side, itemStack);
  152.     }
  153.     public boolean canExtractItem(int slot, ItemStack itemStack, int side)
  154.     {
  155.         return !(side != 0 && side != 1) || (slot != 0);
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement