Guest User

Tile Entity

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