Guest User

TIleEntityBarrel

a guest
Jul 29th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package com.konarjg.arcticraft.blocks.tileentities;
  2.  
  3. import com.konarjg.arcticraft.ModGuiHandler;
  4. import com.konarjg.arcticraft.blocks.tileentities.containers.ContainerBarrel;
  5.  
  6. import net.minecraft.block.state.IBlockState;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.InventoryPlayer;
  9. import net.minecraft.inventory.Container;
  10. import net.minecraft.inventory.IInventory;
  11. import net.minecraft.inventory.ItemStackHelper;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.item.ItemFood;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.tileentity.TileEntityLockableLoot;
  17. import net.minecraft.util.ITickable;
  18. import net.minecraft.util.NonNullList;
  19. import net.minecraft.util.math.BlockPos;
  20. import net.minecraft.world.World;
  21.  
  22. public class TileEntityBarrel extends TileEntityLockableLoot implements ITickable, IInventory{
  23.  
  24.     public static NonNullList<ItemStack> contents = NonNullList.<ItemStack>withSize(16, ItemStack.EMPTY);
  25.     private String customName;
  26.    
  27.     @Override
  28.     public void update() {
  29.          
  30.     }
  31.  
  32.  
  33.     public void openInventory(EntityPlayer player)
  34.     {
  35.      
  36.     }
  37.  
  38.     public void closeInventory(EntityPlayer player)
  39.     {
  40.        
  41.     }
  42.    
  43.     @Override
  44.     public String getName() {
  45.         return hasCustomName () ? getCustomName() : "container.barrel";
  46.     }
  47.    
  48.     public String getCustomName () {
  49.         return customName;
  50.     }
  51.     @Override
  52.     public boolean hasCustomName() {
  53.         return customName != null;
  54.     }
  55.  
  56.     @Override
  57.     public int getSizeInventory() {
  58.         return 16;
  59.     }
  60.    
  61.     public ItemStack decrStackSize(int slot, int amount) {
  62.         ItemStack stack = getStackInSlot(slot);
  63.         if(stack != null) {
  64.             if(stack.getCount() > amount) {
  65.                 stack = stack.splitStack(amount);
  66.                 markDirty();
  67.             } else {
  68.                 setInventorySlotContents(slot, ItemStack.EMPTY);
  69.             }
  70.         }
  71.         return stack;
  72.     }
  73.    
  74.     @Override
  75.     public ItemStack removeStackFromSlot(int index) {
  76.         ItemStack stack = getStackInSlot (index);
  77.            
  78.         contents.set(index, ItemStack.EMPTY);
  79.        
  80.         return stack;
  81.     }
  82.  
  83.     @Override
  84.     public boolean isEmpty() {
  85.         for (ItemStack i : contents) {
  86.             if (!i.isEmpty()) return false;
  87.         }
  88.         return true;
  89.     }
  90.  
  91.     @Override
  92.     public int getInventoryStackLimit() {
  93.         return 64;
  94.     }
  95.  
  96.     @Override
  97.     public boolean isUsableByPlayer(EntityPlayer player) {
  98.         return true;
  99.     }
  100.    
  101.     int numPlayerUsing;
  102.    
  103.    
  104.     @Override
  105.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  106.         if (!contents.get(index).isEmpty() || (contents.get(index).equals(stack) && contents.get(index).getCount() == contents.get(index).getMaxStackSize() || !contents.get(index).equals(stack))) return false;
  107.         if (stack.getItem() instanceof ItemFood) return true;
  108.         return false;
  109.     }
  110.    
  111.  
  112.     @Override
  113.     public int getField(int id) {
  114.         return 0;
  115.     }
  116.  
  117.     @Override
  118.     public void setField(int id, int value) {
  119.        
  120.     }
  121.  
  122.     @Override
  123.     public int getFieldCount() {
  124.         return 0;
  125.     }
  126.  
  127.     public void setCustomName(String displayName) {
  128.         customName = displayName;
  129.     }
  130.  
  131.     @Override
  132.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  133.         if (!world.isRemote)
  134.             return new ContainerBarrel (playerInventory, this);
  135.         return null;
  136.     }
  137.    
  138.     @Override
  139.     public void readFromNBT(NBTTagCompound compound) {
  140.         if (!world.isRemote) {
  141.             super.readFromNBT(compound);
  142.             this.contents = NonNullList.<ItemStack>withSize(getSizeInventory(), ItemStack.EMPTY);
  143.             if (!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, contents);
  144.             if (compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
  145.         }
  146.     }
  147.    
  148.     @Override
  149.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
  150.         return false;
  151.     }
  152.    
  153.      
  154.     @Override
  155.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  156.         if (!world.isRemote) {
  157.             super.writeToNBT(compound);
  158.            
  159.             if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, contents);
  160.             if (compound.hasKey("CustomName")) compound.setString("CustomName", customName);
  161.         }
  162.         return compound;
  163.     }
  164.    
  165.    
  166.    
  167.     @Override
  168.     public ItemStack getStackInSlot(int index) {
  169.         if (!world.isRemote) {
  170.             return contents.get(index);
  171.         }
  172.         return ItemStack.EMPTY;
  173.     }
  174.    
  175.     @Override
  176.     public void setInventorySlotContents(int index, ItemStack stack) {
  177.         if (!world.isRemote) {
  178.             contents.set(index, stack);
  179.         }
  180.     }
  181.  
  182.     @Override
  183.     public String getGuiID() {
  184.         return "" + ModGuiHandler.BARREL;
  185.     }
  186.  
  187.     @Override
  188.     protected NonNullList<ItemStack> getItems() {
  189.         return contents;
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment