Advertisement
RovkirHexus

HexianOresTileEntity.java

May 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. package com.HexianMods.HexianOres.tileentity;
  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. import net.minecraft.util.ChatComponentText;
  10. import net.minecraft.util.ChatComponentTranslation;
  11. import net.minecraft.util.IChatComponent;
  12.  
  13. public class HexianOresTileEntity extends TileEntity implements IInventory{
  14.    
  15.     private ItemStack[] inventory;
  16.    
  17.     private String customName;
  18.    
  19.     public HexianOresTileEntity(){
  20.         this.inventory = new ItemStack[this.getSizeInventory()];
  21.     }
  22.    
  23.     public String getCustomName(){
  24.         return this.customName;
  25.     }
  26.    
  27.     public void setCustomName(String customName){
  28.         this.customName = customName;
  29.     }
  30.    
  31.     @Override
  32.     public String getName(){
  33.         return this.hasCustomName() ? this.customName : "container._hexian_ores_tile_entity";
  34.     }
  35.    
  36.     @Override
  37.     public boolean hasCustomName(){
  38.         return this.customName != null && this.customName.equals("");
  39.     }
  40.    
  41.     @Override
  42.     public IChatComponent getDisplayName(){
  43.         return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
  44.     }
  45.    
  46.     @Override
  47.     public int getSizeInventory(){
  48.         return 3;
  49.     }
  50.    
  51.     @Override
  52.     public ItemStack getStackInSlot(int index) {
  53.         if (index < 0 || index >= this.getSizeInventory())
  54.             return null;
  55.         return this.inventory[index];
  56.     }
  57.  
  58.     @Override
  59.     public ItemStack decrStackSize(int index, int count) {
  60.         if (this.getStackInSlot(index) != null) {
  61.             ItemStack itemstack;
  62.  
  63.             if (this.getStackInSlot(index).stackSize <= count) {
  64.                 itemstack = this.getStackInSlot(index);
  65.                 this.setInventorySlotContents(index, null);
  66.                 this.markDirty();
  67.                 return itemstack;
  68.             } else {
  69.                 itemstack = this.getStackInSlot(index).splitStack(count);
  70.  
  71.                 if (this.getStackInSlot(index).stackSize <= 0) {
  72.                     this.setInventorySlotContents(index, null);
  73.                 } else {
  74.                     //Just to show that changes happened
  75.                     this.setInventorySlotContents(index, this.getStackInSlot(index));
  76.                 }
  77.  
  78.                 this.markDirty();
  79.                 return itemstack;
  80.             }
  81.         } else {
  82.             return null;
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     public ItemStack removeStackFromSlot(int index) {
  88.         ItemStack stack = this.getStackInSlot(index);
  89.         this.setInventorySlotContents(index, null);
  90.         return stack;
  91.     }
  92.  
  93.     @Override
  94.     public void setInventorySlotContents(int index, ItemStack stack) {
  95.         if (index < 0 || index >= this.getSizeInventory())
  96.             return;
  97.  
  98.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  99.             stack.stackSize = this.getInventoryStackLimit();
  100.            
  101.         if (stack != null && stack.stackSize == 0)
  102.             stack = null;
  103.  
  104.         this.inventory[index] = stack;
  105.         this.markDirty();
  106.     }
  107.    
  108.     @Override
  109.     public int getInventoryStackLimit(){
  110.         return 64;
  111.     }
  112.    
  113.     @Override
  114.     public boolean isUseableByPlayer(EntityPlayer player) {
  115.         return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  116.     }
  117.    
  118.     @Override
  119.     public void openInventory(EntityPlayer player) {
  120.     }
  121.  
  122.     @Override
  123.     public void closeInventory(EntityPlayer player) {
  124.     }
  125.    
  126.     @Override
  127.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  128.         return true;
  129.     }
  130.    
  131.     @Override
  132.     public int getField(int id) {
  133.         return 0;
  134.     }
  135.  
  136.     @Override
  137.     public void setField(int id, int value) {
  138.     }
  139.  
  140.     @Override
  141.     public int getFieldCount() {
  142.         return 0;
  143.     }
  144.    
  145.     @Override
  146.     public void clear() {
  147.         for (int i = 0; i < this.getSizeInventory(); i++)
  148.             this.setInventorySlotContents(i, null);
  149.     }
  150.    
  151.     @Override
  152.     public void writeToNBT(NBTTagCompound compound){
  153.         super.writeToNBT(compound);
  154.        
  155.         NBTTagList list = new NBTTagList();
  156.         for (int i = 0; i < this.getSizeInventory(); ++i) {
  157.             if (this.getStackInSlot(i) != null) {
  158.                 NBTTagCompound stackTag = new NBTTagCompound();
  159.                 stackTag.setByte("Slot", (byte) i);
  160.                 this.getStackInSlot(i).writeToNBT(stackTag);
  161.                 list.appendTag(stackTag);
  162.             }
  163.         }
  164.         compound.setTag("Items", list);
  165.  
  166.         if (this.hasCustomName()) {
  167.             compound.setString("CustomName", this.getCustomName());
  168.         }
  169.     }
  170.    
  171.     @Override
  172.     public void readFromNBT(NBTTagCompound nbt) {
  173.         super.readFromNBT(nbt);
  174.  
  175.         NBTTagList list = nbt.getTagList("Items", 10);
  176.         for (int i = 0; i < list.tagCount(); ++i) {
  177.             NBTTagCompound stackTag = list.getCompoundTagAt(i);
  178.             int slot = stackTag.getByte("Slot") & 255;
  179.             this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
  180.         }
  181.  
  182.         if (nbt.hasKey("CustomName", 8)) {
  183.             this.setCustomName(nbt.getString("CustomName"));
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement