Advertisement
Guest User

Untitled

a guest
Feb 15th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. package pirate.common;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.inventory.IInventory;
  5. import net.minecraft.item.ItemStack;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraft.nbt.NBTTagList;
  8. import net.minecraft.tileentity.TileEntity;
  9.  
  10. public class TileEntityBigChest extends TileEntity implements IInventory
  11. {
  12.     private ItemStack[] inventory = new ItemStack[72];
  13.     private String customName;
  14.    
  15.     public void readFromNBT(NBTTagCompound nbttag)
  16.     {
  17.         super.readFromNBT(nbttag);
  18.         NBTTagList nbttaglist = nbttag.getTagList("Items");
  19.         this.inventory = new ItemStack[this.getSizeInventory()];
  20.  
  21.         if (nbttag.hasKey("CustomName"))
  22.         {
  23.             this.customName = nbttag.getString("CustomName");
  24.         }
  25.  
  26.         for (int i = 0; i < nbttaglist.tagCount(); )
  27.         {
  28.             NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
  29.             int j = nbttagcompound1.getByte("Slot");
  30.  
  31.             if (j >= 0 && j < this.inventory.length)
  32.             {
  33.                 this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  34.             }
  35.         }
  36.     }
  37.  
  38.     public void writeToNBT(NBTTagCompound nbttag)
  39.     {
  40.         super.writeToNBT(nbttag);
  41.         NBTTagList nbttaglist = new NBTTagList();
  42.  
  43.         for (int i = 0; i < this.inventory.length; )
  44.         {
  45.             if (this.inventory[i] != null)
  46.             {
  47.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  48.                 nbttagcompound1.setByte("Slot", (byte)i);
  49.                 this.inventory[i].writeToNBT(nbttagcompound1);
  50.                 nbttaglist.appendTag(nbttagcompound1);
  51.             }
  52.         }
  53.  
  54.         nbttag.setTag("Items", nbttaglist);
  55.  
  56.         if (this.isInvNameLocalized())
  57.         {
  58.             nbttag.setString("CustomName", this.customName);
  59.         }
  60.     }
  61.    
  62.     @Override
  63.     public int getSizeInventory()
  64.     {
  65.         return inventory.length;
  66.     }
  67.  
  68.     @Override
  69.     public ItemStack getStackInSlot(int slotId)
  70.     {
  71.         return inventory[slotId];
  72.     }
  73.    
  74.     @Override
  75.     public ItemStack decrStackSize(int slotId, int quantity)
  76.     {
  77.         if (this.inventory[slotId] != null)
  78.         {
  79.             ItemStack itemstack;
  80.  
  81.             if (this.inventory[slotId].stackSize <= quantity)
  82.             {
  83.                 itemstack = this.inventory[slotId];
  84.                 this.inventory[slotId] = null;
  85.                 this.onInventoryChanged();
  86.                 return itemstack;
  87.             }
  88.             else
  89.             {
  90.                 itemstack = this.inventory[slotId].splitStack(quantity);
  91.  
  92.                 if (this.inventory[slotId].stackSize == 0)
  93.                 {
  94.                     this.inventory[slotId] = null;
  95.                 }
  96.  
  97.                 this.onInventoryChanged();
  98.                 return itemstack;
  99.             }
  100.         }
  101.         else
  102.         {
  103.             return null;
  104.         }
  105.     }
  106.  
  107.     @Override
  108.     public ItemStack getStackInSlotOnClosing(int slotId)
  109.     {
  110.         if (this.inventory[slotId] != null)
  111.         {
  112.             ItemStack itemstack = this.inventory[slotId];
  113.             this.inventory[slotId] = null;
  114.             return itemstack;
  115.         }
  116.         else
  117.         {
  118.             return null;
  119.         }
  120.     }
  121.  
  122.     @Override
  123.     public void setInventorySlotContents(int slotId, ItemStack stack)
  124.     {
  125.         this.inventory[slotId] = stack;
  126.  
  127.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  128.         {
  129.             stack.stackSize = this.getInventoryStackLimit();
  130.         }
  131.  
  132.         this.onInventoryChanged();
  133.     }
  134.  
  135.     @Override
  136.     public String getInvName()
  137.     {
  138.         return this.isInvNameLocalized() ? this.customName : "container.bigchest";
  139.     }
  140.  
  141.     @Override
  142.     public boolean isInvNameLocalized()
  143.     {
  144.         return this.customName != null && this.customName.length() > 0;
  145.     }
  146.    
  147.     public void setCustomGuiName(String name)
  148.     {
  149.         this.customName = name;
  150.     }
  151.  
  152.     @Override
  153.     public int getInventoryStackLimit()
  154.     {
  155.         return 64;
  156.     }
  157.  
  158.     @Override
  159.     public boolean isUseableByPlayer(EntityPlayer player)
  160.     {
  161.         return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord  , yCoord  , zCoord  ) < 64;
  162.     }
  163.  
  164.     @Override
  165.     public void openChest()
  166.     {
  167.  
  168.     }
  169.  
  170.     @Override
  171.     public void closeChest()
  172.     {
  173.  
  174.     }
  175.  
  176.     @Override
  177.     public boolean isItemValidForSlot(int slotId, ItemStack stack)
  178.     {
  179.         return true;
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement