Otho

CustomChest

Oct 6th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package com.Otho.customItems.mod.tileentitys;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.UUID;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraft.tileentity.TileEntity;
  12.  
  13. import net.minecraftforge.common.util.Constants;
  14.  
  15.  
  16. public class TileEntityCustomChest extends TileEntity implements IInventory{
  17.    
  18.         public static final String publicName = "tileEntityCustomChest";
  19.        
  20.         private String owner = "null";
  21.         private String name = "Custom Chest";
  22.         private ItemStack[] inventory;
  23.         private int width;
  24.         private int height;
  25.         private int slots;
  26.        
  27.        
  28.         public TileEntityCustomChest (int width, int height, String name) {
  29.             this.width = width;
  30.             this.height = height;
  31.             this.name = name;
  32.            
  33.             this.slots = width * height;
  34.             inventory = new ItemStack[this.slots];         
  35.         }
  36.        
  37.    
  38.        
  39.         @Override
  40.         public void readFromNBT(NBTTagCompound nbttagcompound)
  41.         {
  42.             super.readFromNBT(nbttagcompound);
  43.             NBTTagList nbttaglist = nbttagcompound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
  44.             inventory = new ItemStack[this.slots];
  45.             for (int i = 0; i < nbttaglist.tagCount(); i++)
  46.             {
  47.                 NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  48.                 int j = nbttagcompound1.getByte("Slot") & 0xff;
  49.                 if (j >= 0 && j < inventory.length)
  50.                 {
  51.                     inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         @Override
  57.         public void writeToNBT(NBTTagCompound nbttagcompound)
  58.         {
  59.             super.writeToNBT(nbttagcompound);
  60.             NBTTagList nbttaglist = new NBTTagList();
  61.             for (int i = 0; i < inventory.length; i++)
  62.             {
  63.                 if (inventory[i] != null)
  64.                 {
  65.                     NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  66.                     nbttagcompound1.setByte("Slot", (byte) i);
  67.                     inventory[i].writeToNBT(nbttagcompound1);
  68.                     nbttaglist.appendTag(nbttagcompound1);
  69.                 }
  70.             }
  71.  
  72.             nbttagcompound.setTag("Items", nbttaglist);
  73.            
  74.         }
  75.        
  76.        public void registerOwner(String owner)
  77.        {
  78.            this.owner = owner;
  79.        }
  80.        
  81.        public String getOwner()
  82.        {           
  83.            return this.owner;
  84.        }
  85.        
  86.         public int getWidth()
  87.         {
  88.             return this.width;
  89.         }
  90.        
  91.         public int getHeight()
  92.         {
  93.             return this.height;
  94.         }
  95.  
  96.         @Override
  97.         public int getSizeInventory() {
  98.             // TODO Auto-generated method stub
  99.             return this.slots;
  100.         }
  101.    
  102.         @Override
  103.         public ItemStack getStackInSlot(int slot) {
  104.             // TODO Auto-generated method stub
  105.            
  106.             return inventory[slot];
  107.            
  108.         }
  109.    
  110.         @Override
  111.         public ItemStack decrStackSize(int slot, int amount) {
  112.             // TODO Auto-generated method stub
  113.             ItemStack stack = getStackInSlot(slot);
  114.             if (stack != null) {
  115.                     if (stack.stackSize <= amount) {
  116.                             setInventorySlotContents(slot, null);
  117.                     } else {
  118.                             stack = stack.splitStack(amount);
  119.                             if (stack.stackSize == 0) {
  120.                                     setInventorySlotContents(slot, null);
  121.                             }
  122.                     }
  123.             }
  124.             return stack;
  125.         }
  126.    
  127.         @Override
  128.         public ItemStack getStackInSlotOnClosing(int slot) {
  129.             ItemStack stack = getStackInSlot(slot);
  130.             if (stack != null) {
  131.                     setInventorySlotContents(slot, null);
  132.             }
  133.             return stack;
  134.         }
  135.    
  136.         @Override
  137.         public void setInventorySlotContents(int slot, ItemStack stack) {
  138.            
  139.                 inventory[slot] = stack;
  140.            
  141.         }
  142.    
  143.         @Override
  144.         public String getInventoryName() {
  145.             // TODO Auto-generated method stub
  146.             return "Custom Chest";
  147.         }
  148.    
  149.         @Override
  150.         public boolean hasCustomInventoryName() {
  151.             // TODO Auto-generated method stub
  152.             return false;
  153.         }
  154.    
  155.         @Override
  156.         public int getInventoryStackLimit() {
  157.             // TODO Auto-generated method stub
  158.             return 64;
  159.         }
  160.    
  161.         @Override
  162.         public boolean isUseableByPlayer(EntityPlayer player) {
  163.             return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
  164.                     player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
  165.         }
  166.    
  167.         @Override
  168.         public void openInventory() {
  169.             // TODO Auto-generated method stub
  170.            
  171.         }
  172.    
  173.         @Override
  174.         public void closeInventory() {
  175.             // TODO Auto-generated method stub
  176.            
  177.         }
  178.    
  179.         @Override
  180.         public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
  181.             // TODO Auto-generated method stub
  182.             return true;
  183.         }
  184.            
  185. }
Advertisement
Add Comment
Please, Sign In to add comment