Guest User

tile entity

a guest
Feb 5th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.96 KB | None | 0 0
  1. package anagkai.biodiversity.advancedblocks.Beehive;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import anagkai.biodiversity.items.ItemsBiodiversity;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.ISidedInventory;
  10. import net.minecraft.inventory.ItemStackHelper;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraft.util.ITickable;
  17. import net.minecraft.util.datafix.DataFixer;
  18. import net.minecraft.util.datafix.FixTypes;
  19. import net.minecraft.util.datafix.walkers.ItemStackDataLists;
  20.  
  21. public class TileEntityBeehive extends TileEntity implements ITickable, ISidedInventory
  22. {
  23.     private static final int[] SLOTS_LEFT = new int[] {0};
  24.     private static final int[] SLOTS_RIGHT = new int[] {1};
  25.     private ItemStack[] beehiveItemStacks = new ItemStack[2];
  26.     int prodTimePassed;
  27.     private int prodTimeNeeded = 320;
  28.     private String beehiveName;
  29.  
  30.     /**
  31.      * Returns the number of slots in the inventory.
  32.      */
  33.     public int getSizeInventory()
  34.     {
  35.         return this.beehiveItemStacks.length;
  36.     }
  37.  
  38.     /**
  39.      * Returns the stack in the given slot.
  40.      */
  41.     @Nullable
  42.     public ItemStack getStackInSlot(int index)
  43.     {
  44.         return this.beehiveItemStacks[index];
  45.     }
  46.  
  47.     /**
  48.      * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
  49.      */
  50.     @Nullable
  51.     public ItemStack decrStackSize(int index, int count)
  52.     {
  53.         return ItemStackHelper.getAndSplit(this.beehiveItemStacks, index, count);
  54.     }
  55.  
  56.     /**
  57.      * Removes a stack from the given slot and returns it.
  58.      */
  59.     @Nullable
  60.     public ItemStack removeStackFromSlot(int index)
  61.     {
  62.         return ItemStackHelper.getAndRemove(this.beehiveItemStacks, index);
  63.     }
  64.  
  65.     /**
  66.      * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  67.      */
  68.     public void setInventorySlotContents(int index, @Nullable ItemStack stack)
  69.     {
  70.         boolean flag = stack != null && stack.isItemEqual(this.beehiveItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.beehiveItemStacks[index]);
  71.         this.beehiveItemStacks[index] = stack;
  72.  
  73.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  74.         {
  75.             stack.stackSize = this.getInventoryStackLimit();
  76.         }
  77.  
  78.         if (index == 0 && !flag)
  79.         {
  80.             this.prodTimeNeeded = 320;
  81.             this.prodTimePassed = 0;
  82.             this.markDirty();
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Get the name of this object. For players this returns their username
  88.      */
  89.     public String getName()
  90.     {
  91.         return this.hasCustomName() ? this.beehiveName : "container.beehive";
  92.     }
  93.  
  94.     /**
  95.      * Returns true if this thing is named
  96.      */
  97.     public boolean hasCustomName()
  98.     {
  99.         return this.beehiveName != null && !this.beehiveName.isEmpty();
  100.     }
  101.  
  102.     public void setCustomInventoryName(String p_145951_1_)
  103.     {
  104.         this.beehiveName = p_145951_1_;
  105.     }
  106.  
  107.     public void readFromNBT(NBTTagCompound compound)
  108.     {
  109.         super.readFromNBT(compound);
  110.         NBTTagList nbttaglist = compound.getTagList("Items", 10);
  111.         this.beehiveItemStacks = new ItemStack[this.getSizeInventory()];
  112.  
  113.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  114.         {
  115.             NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
  116.             int j = nbttagcompound.getByte("Slot");
  117.  
  118.             if (j >= 0 && j < this.beehiveItemStacks.length)
  119.             {
  120.                 this.beehiveItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
  121.             }
  122.         }
  123.  
  124.         this.prodTimePassed = compound.getInteger("ProdPass");
  125.         this.prodTimeNeeded = compound.getInteger("ProdNeed");
  126.  
  127.         if (compound.hasKey("CustomName", 8))
  128.         {
  129.             this.beehiveName = compound.getString("CustomName");
  130.         }
  131.     }
  132.  
  133.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  134.     {
  135.         super.writeToNBT(compound);
  136.         compound.setInteger("ProdPass", this.prodTimePassed);
  137.         compound.setInteger("ProdNeed", this.prodTimeNeeded);
  138.         NBTTagList nbttaglist = new NBTTagList();
  139.  
  140.         for (int i = 0; i < this.beehiveItemStacks.length; ++i)
  141.         {
  142.             if (this.beehiveItemStacks[i] != null)
  143.             {
  144.                 NBTTagCompound nbttagcompound = new NBTTagCompound();
  145.                 nbttagcompound.setByte("Slot", (byte)i);
  146.                 this.beehiveItemStacks[i].writeToNBT(nbttagcompound);
  147.                 nbttaglist.appendTag(nbttagcompound);
  148.             }
  149.         }
  150.  
  151.         compound.setTag("Items", nbttaglist);
  152.  
  153.         if (this.hasCustomName())
  154.         {
  155.             compound.setString("CustomName", this.beehiveName);
  156.         }
  157.  
  158.         return compound;
  159.     }
  160.  
  161.     /**
  162.      * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  163.      */
  164.     public int getInventoryStackLimit()
  165.     {
  166.         return 64;
  167.     }
  168.    
  169.     /**
  170.      * Like the old updateEntity(), except more generic.
  171.      */
  172.     public void update()
  173.     {
  174.         boolean flag1 = false;
  175.  
  176.         if (!this.worldObj.isRemote)
  177.         {
  178.                 if (this.canProduce())
  179.                 {
  180.                     ++this.prodTimePassed;
  181.  
  182.                     if (this.prodTimePassed == this.prodTimeNeeded)
  183.                     {
  184.                         this.prodTimePassed = 0;
  185.                         this.produceHoney();
  186.                         flag1 = true;
  187.                     }
  188.                 }
  189.                 else
  190.                 {
  191.                     this.prodTimePassed = 0;
  192.                 }
  193.  
  194.         }
  195.  
  196.         if (flag1)
  197.         {
  198.             this.markDirty();
  199.         }
  200.     }
  201.  
  202.     private boolean canProduce()
  203.     {
  204.         if (this.beehiveItemStacks[0] == null || this.beehiveItemStacks[0].getItem() != ItemsBiodiversity.beeQueen)
  205.         {
  206.             return false;
  207.         }
  208.         else
  209.         {
  210.             if (this.beehiveItemStacks[1] == null) return true;
  211.             int result = beehiveItemStacks[1].stackSize + 1;
  212.             return result <= getInventoryStackLimit() && result <= this.beehiveItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  213.         }
  214.     }
  215.  
  216.     public void produceHoney()
  217.     {
  218.         if (this.canProduce())
  219.         {
  220.             if (this.beehiveItemStacks[1] == null)
  221.             {
  222.                 this.beehiveItemStacks[1] = new ItemStack(ItemsBiodiversity.honey);
  223.             }
  224.             else if (this.beehiveItemStacks[1].getItem() == ItemsBiodiversity.honey)
  225.             {
  226.                 this.beehiveItemStacks[1].stackSize += 1; // Forge BugFix: Results may have multiple items
  227.             }
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Don't rename this method to canInteractWith due to conflicts with Container
  233.      */
  234.     public boolean isUseableByPlayer(EntityPlayer player)
  235.     {
  236.         return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
  237.     }
  238.  
  239.     public void openInventory(EntityPlayer player)
  240.     {
  241.     }
  242.  
  243.     public void closeInventory(EntityPlayer player)
  244.     {
  245.     }
  246.  
  247.     public String getGuiID()
  248.     {
  249.         return "biodiversity:beehive";
  250.     }
  251.  
  252.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  253.     {
  254.         return new ContainerBeehive(playerInventory, this);
  255.     }
  256.  
  257.     public int getField(int id)
  258.     {
  259.         switch (id)
  260.         {
  261.             case 0:
  262.                 return this.prodTimePassed;
  263.             case 1:
  264.                 return this.prodTimeNeeded;
  265.             default:
  266.                 return 0;
  267.         }
  268.     }
  269.  
  270.     public void setField(int id, int value)
  271.     {
  272.         switch (id)
  273.         {
  274.             case 0:
  275.                 this.prodTimePassed = value;
  276.                 break;
  277.             case 1:
  278.                 this.prodTimeNeeded = value;
  279.         }
  280.     }
  281.  
  282.     public int getFieldCount()
  283.     {
  284.         return 2;
  285.     }
  286.  
  287.     public void clear()
  288.     {
  289.         for (int i = 0; i < this.beehiveItemStacks.length; ++i)
  290.         {
  291.             this.beehiveItemStacks[i] = null;
  292.         }
  293.     }
  294.  
  295.     @Override
  296.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  297.         return false;
  298.     }
  299.  
  300.     @Override
  301.     public int[] getSlotsForFace(EnumFacing side) {
  302.         return null;
  303.     }
  304.  
  305.     @Override
  306.     public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
  307.         return false;
  308.     }
  309.  
  310.     @Override
  311.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
  312.         return false;
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment