Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package anagkai.biodiversity.advancedblocks.Beehive;
- import javax.annotation.Nullable;
- import anagkai.biodiversity.items.ItemsBiodiversity;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.datafix.DataFixer;
- import net.minecraft.util.datafix.FixTypes;
- import net.minecraft.util.datafix.walkers.ItemStackDataLists;
- public class TileEntityBeehive extends TileEntity implements ITickable, ISidedInventory
- {
- private static final int[] SLOTS_LEFT = new int[] {0};
- private static final int[] SLOTS_RIGHT = new int[] {1};
- private ItemStack[] beehiveItemStacks = new ItemStack[2];
- int prodTimePassed;
- private int prodTimeNeeded = 320;
- private String beehiveName;
- /**
- * Returns the number of slots in the inventory.
- */
- public int getSizeInventory()
- {
- return this.beehiveItemStacks.length;
- }
- /**
- * Returns the stack in the given slot.
- */
- @Nullable
- public ItemStack getStackInSlot(int index)
- {
- return this.beehiveItemStacks[index];
- }
- /**
- * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
- */
- @Nullable
- public ItemStack decrStackSize(int index, int count)
- {
- return ItemStackHelper.getAndSplit(this.beehiveItemStacks, index, count);
- }
- /**
- * Removes a stack from the given slot and returns it.
- */
- @Nullable
- public ItemStack removeStackFromSlot(int index)
- {
- return ItemStackHelper.getAndRemove(this.beehiveItemStacks, index);
- }
- /**
- * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
- */
- public void setInventorySlotContents(int index, @Nullable ItemStack stack)
- {
- boolean flag = stack != null && stack.isItemEqual(this.beehiveItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.beehiveItemStacks[index]);
- this.beehiveItemStacks[index] = stack;
- if (stack != null && stack.stackSize > this.getInventoryStackLimit())
- {
- stack.stackSize = this.getInventoryStackLimit();
- }
- if (index == 0 && !flag)
- {
- this.prodTimeNeeded = 320;
- this.prodTimePassed = 0;
- this.markDirty();
- }
- }
- /**
- * Get the name of this object. For players this returns their username
- */
- public String getName()
- {
- return this.hasCustomName() ? this.beehiveName : "container.beehive";
- }
- /**
- * Returns true if this thing is named
- */
- public boolean hasCustomName()
- {
- return this.beehiveName != null && !this.beehiveName.isEmpty();
- }
- public void setCustomInventoryName(String p_145951_1_)
- {
- this.beehiveName = p_145951_1_;
- }
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- this.beehiveItemStacks = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i)
- {
- NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
- int j = nbttagcompound.getByte("Slot");
- if (j >= 0 && j < this.beehiveItemStacks.length)
- {
- this.beehiveItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
- }
- }
- this.prodTimePassed = compound.getInteger("ProdPass");
- this.prodTimeNeeded = compound.getInteger("ProdNeed");
- if (compound.hasKey("CustomName", 8))
- {
- this.beehiveName = compound.getString("CustomName");
- }
- }
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("ProdPass", this.prodTimePassed);
- compound.setInteger("ProdNeed", this.prodTimeNeeded);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.beehiveItemStacks.length; ++i)
- {
- if (this.beehiveItemStacks[i] != null)
- {
- NBTTagCompound nbttagcompound = new NBTTagCompound();
- nbttagcompound.setByte("Slot", (byte)i);
- this.beehiveItemStacks[i].writeToNBT(nbttagcompound);
- nbttaglist.appendTag(nbttagcompound);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (this.hasCustomName())
- {
- compound.setString("CustomName", this.beehiveName);
- }
- return compound;
- }
- /**
- * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
- */
- public int getInventoryStackLimit()
- {
- return 64;
- }
- /**
- * Like the old updateEntity(), except more generic.
- */
- public void update()
- {
- boolean flag1 = false;
- if (!this.worldObj.isRemote)
- {
- if (this.canProduce())
- {
- ++this.prodTimePassed;
- if (this.prodTimePassed == this.prodTimeNeeded)
- {
- this.prodTimePassed = 0;
- this.produceHoney();
- flag1 = true;
- }
- }
- else
- {
- this.prodTimePassed = 0;
- }
- }
- if (flag1)
- {
- this.markDirty();
- }
- }
- private boolean canProduce()
- {
- if (this.beehiveItemStacks[0] == null || this.beehiveItemStacks[0].getItem() != ItemsBiodiversity.beeQueen)
- {
- return false;
- }
- else
- {
- if (this.beehiveItemStacks[1] == null) return true;
- int result = beehiveItemStacks[1].stackSize + 1;
- return result <= getInventoryStackLimit() && result <= this.beehiveItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
- }
- }
- public void produceHoney()
- {
- if (this.canProduce())
- {
- if (this.beehiveItemStacks[1] == null)
- {
- this.beehiveItemStacks[1] = new ItemStack(ItemsBiodiversity.honey);
- }
- else if (this.beehiveItemStacks[1].getItem() == ItemsBiodiversity.honey)
- {
- this.beehiveItemStacks[1].stackSize += 1; // Forge BugFix: Results may have multiple items
- }
- }
- }
- /**
- * Don't rename this method to canInteractWith due to conflicts with Container
- */
- public boolean isUseableByPlayer(EntityPlayer player)
- {
- 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;
- }
- public void openInventory(EntityPlayer player)
- {
- }
- public void closeInventory(EntityPlayer player)
- {
- }
- public String getGuiID()
- {
- return "biodiversity:beehive";
- }
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
- {
- return new ContainerBeehive(playerInventory, this);
- }
- public int getField(int id)
- {
- switch (id)
- {
- case 0:
- return this.prodTimePassed;
- case 1:
- return this.prodTimeNeeded;
- default:
- return 0;
- }
- }
- public void setField(int id, int value)
- {
- switch (id)
- {
- case 0:
- this.prodTimePassed = value;
- break;
- case 1:
- this.prodTimeNeeded = value;
- }
- }
- public int getFieldCount()
- {
- return 2;
- }
- public void clear()
- {
- for (int i = 0; i < this.beehiveItemStacks.length; ++i)
- {
- this.beehiveItemStacks[i] = null;
- }
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return false;
- }
- @Override
- public int[] getSlotsForFace(EnumFacing side) {
- return null;
- }
- @Override
- public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
- return false;
- }
- @Override
- public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment