Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.grossik.farmcraft2.pan;
- import javax.annotation.Nullable;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntityLockable;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public class TileEntityPan extends TileEntityLockable implements ITickable, ISidedInventory
- {
- //0 je nalevo, 2 napravo
- private static final int[] slotsTop = new int[] {0};
- private static final int[] slotsBottom = new int[] {2};
- private static final int[] slotsSides = new int[] {0};
- private ItemStack[] furnaceItemStacks = new ItemStack[3];
- private int furnaceBurnTime;
- private int cookTime;
- private int totalCookTime;
- private String furnaceCustomName;
- public int getSizeInventory()
- {
- return this.furnaceItemStacks.length;
- }
- public ItemStack getStackInSlot(int index)
- {
- return this.furnaceItemStacks[index];
- }
- @Nullable
- public ItemStack decrStackSize(int index, int count)
- {
- return ItemStackHelper.getAndSplit(this.furnaceItemStacks, index, count);
- }
- @Nullable
- public ItemStack removeStackFromSlot(int index)
- {
- return ItemStackHelper.getAndRemove(this.furnaceItemStacks, index);
- }
- public boolean isBurning()
- {
- return this.totalCookTime > 0;
- }
- public void setInventorySlotContents(int index, ItemStack stack)
- {
- boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
- this.furnaceItemStacks[index] = stack;
- if (stack != null && stack.stackSize > this.getInventoryStackLimit())
- {
- stack.stackSize = this.getInventoryStackLimit();
- }
- if (index == 0 && !flag)
- {
- this.totalCookTime = 120;
- this.cookTime = 0;
- this.markDirty();
- }
- }
- public String getName()
- {
- return this.hasCustomName() ? this.furnaceCustomName : "Pan";
- }
- public boolean hasCustomName()
- {
- return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
- }
- public void setCustomInventoryName(String p_145951_1_)
- {
- this.furnaceCustomName = p_145951_1_;
- }
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- this.furnaceItemStacks = 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.furnaceItemStacks.length)
- {
- this.furnaceItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
- }
- }
- this.furnaceBurnTime = compound.getInteger("BurnTime");
- this.cookTime = compound.getInteger("CookTime");
- this.totalCookTime = compound.getInteger("CookTimeTotal");
- if (compound.hasKey("CustomName", 8))
- {
- this.furnaceCustomName = compound.getString("CustomName");
- }
- }
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("BurnTime", this.furnaceBurnTime);
- compound.setInteger("CookTime", this.cookTime);
- compound.setInteger("CookTimeTotal", this.totalCookTime);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.furnaceItemStacks.length; ++i)
- {
- if (this.furnaceItemStacks[i] != null)
- {
- NBTTagCompound nbttagcompound = new NBTTagCompound();
- nbttagcompound.setByte("Slot", (byte)i);
- this.furnaceItemStacks[i].writeToNBT(nbttagcompound);
- nbttaglist.appendTag(nbttagcompound);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (this.hasCustomName())
- {
- compound.setString("CustomName", this.furnaceCustomName);
- }
- return compound;
- }
- public int getInventoryStackLimit()
- {
- return 1;
- }
- @SideOnly(Side.CLIENT)
- public static boolean isBurning(IInventory p_174903_0_)
- {
- return p_174903_0_.getField(3) > 0;
- }
- public void update()
- {
- boolean flag = this.isBurning();
- boolean flag1 = false;
- if (this.isBurning() && this.canSmelt())
- {
- ++this.cookTime;
- if (this.cookTime == this.totalCookTime)
- {
- this.cookTime = 0;
- this.totalCookTime = this.getCookTime(this.furnaceItemStacks[2]);
- this.smeltItem();
- flag1 = true;
- }
- }
- else
- {
- this.cookTime = 0;
- this.totalCookTime = 0;
- }
- }
- public int getCookTime(ItemStack stack)
- {
- return 0;
- }
- private boolean canSmelt()
- {
- if (this.furnaceItemStacks[0] == null)
- {
- return false;
- }
- else
- {
- ItemStack itemstack = PanRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
- if (itemstack == null) return false;
- if (this.furnaceItemStacks[2] == null) return true;
- if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
- int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
- return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
- }
- }
- /**
- * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
- */
- public void smeltItem()
- {
- if (this.canSmelt())
- {
- ItemStack itemstack = PanRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
- if (this.furnaceItemStacks[2] == null)
- {
- this.furnaceItemStacks[2] = itemstack.copy();
- }
- else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
- {
- this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
- }
- --this.furnaceItemStacks[0].stackSize;
- if (this.furnaceItemStacks[0].stackSize <= 0)
- {
- this.furnaceItemStacks[0] = null;
- }
- }
- }
- public boolean isItemFuel(ItemStack p_145954_0_)
- {
- return this.totalCookTime > 0;
- }
- /**
- * Do not make give this method the name canInteractWith because it clashes 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)
- {
- }
- /**
- * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
- */
- public boolean isItemValidForSlot(int index, ItemStack stack)
- {
- if (index == 2)
- {
- return false;
- }
- return true;
- }
- public int[] getSlotsForFace(EnumFacing side)
- {
- return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
- }
- public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
- {
- return this.isItemValidForSlot(index, itemStackIn);
- }
- public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
- {
- if (direction == EnumFacing.DOWN && index == 1)
- {
- Item item = stack.getItem();
- if (item != Items.WATER_BUCKET && item != Items.BUCKET)
- {
- return false;
- }
- }
- return true;
- }
- public String getGuiID()
- {
- return "10";
- }
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
- {
- return new ContainerPan(playerInventory, this);
- }
- public int getField(int id)
- {
- switch (id)
- {
- case 0:
- return this.furnaceBurnTime;
- case 1:
- return this.furnaceBurnTime;
- case 2:
- return this.cookTime;
- case 3:
- return this.totalCookTime;
- default:
- return 0;
- }
- }
- public void setField(int id, int value)
- {
- switch (id)
- {
- case 0:
- this.furnaceBurnTime = value;
- break;
- case 1:
- break;
- case 2:
- this.cookTime = value;
- break;
- case 3:
- this.totalCookTime = value;
- }
- }
- public int getFieldCount()
- {
- return 4;
- }
- public void clear()
- {
- for (int i = 0; i < this.furnaceItemStacks.length; ++i)
- {
- this.furnaceItemStacks[i] = null;
- }
- }
- net.minecraftforge.items.IItemHandler handlerTop = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.UP);
- net.minecraftforge.items.IItemHandler handlerBottom = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.DOWN);
- net.minecraftforge.items.IItemHandler handlerSide = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.WEST);
- @SuppressWarnings("unchecked")
- @Override
- public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
- {
- if (facing != null && capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
- if (facing == EnumFacing.DOWN)
- return (T) handlerBottom;
- else if (facing == EnumFacing.UP)
- return (T) handlerTop;
- else
- return (T) handlerSide;
- return super.getCapability(capability, facing);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment