Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chef.mod.tileentity;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.SlotFurnaceFuel;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemBlock;
- import net.minecraft.item.ItemHoe;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.ItemSword;
- import net.minecraft.item.ItemTool;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.server.gui.IUpdatePlayerListBox;
- import net.minecraft.tileentity.TileEntityLockable;
- import net.minecraft.util.EnumFacing;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import com.chef.mod.Debugger;
- import com.chef.mod.blocks.Boiler;
- import com.chef.mod.container.ContainerBoiler;
- import com.chef.mod.crafting.BoilerRecipes;
- public class TileEntityBoiler extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory {
- private static final int[] slotsTop = new int[] { 0, 1 };
- private static final int[] slotsBottom = new int[] { 3 };
- private static final int[] slotsSides = new int[] { 2 };
- private ItemStack slots[];
- public TileEntityBoiler() {
- slots = new ItemStack[4];
- }
- private int burnTime;
- private int currentItemBurnTime;
- public static int heath = 0;
- public static int water;
- private int cookTime;
- public int cookingFurnaceSpeed = 1340;
- public static final int maxWater = 40000;
- // Speed of the furnace. The higher the number, the slower it goes. Normal
- // [200].
- private String furnaceCustomName;
- public int getSizeInventory() {
- return this.slots.length;
- }
- public ItemStack getStackInSlot(int index) {
- return this.slots[index];
- }
- public ItemStack decrStackSize(int index, int count) {
- if (this.slots[index] != null) {
- ItemStack itemstack;
- if (this.slots[index].stackSize <= count) {
- itemstack = this.slots[index];
- this.slots[index] = null;
- return itemstack;
- } else {
- itemstack = this.slots[index].splitStack(count);
- if (this.slots[index].stackSize == 0) {
- this.slots[index] = null;
- }
- return itemstack;
- }
- } else {
- return null;
- }
- }
- public ItemStack getStackInSlotOnClosing(int index) {
- if (this.slots[index] != null) {
- ItemStack itemstack = this.slots[index];
- this.slots[index] = null;
- return itemstack;
- } else {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int i, ItemStack itemstack) {
- slots[i] = itemstack;
- if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
- itemstack.stackSize = getInventoryStackLimit();
- }
- }
- public String getName() {
- return this.hasCustomName() ? this.furnaceCustomName : "container.boiler";
- }
- public boolean hasCustomName() {
- return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
- }
- public void setCustomInventoryName(String string) {
- this.furnaceCustomName = string;
- }
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- this.slots = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i) {
- NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
- byte b0 = nbttagcompound1.getByte("Slot");
- if (b0 >= 0 && b0 < this.slots.length) {
- this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
- }
- }
- this.burnTime = compound.getShort("BurnTime");
- this.cookTime = compound.getShort("CookTime");
- this.heath = compound.getShort("Heath");
- this.water = compound.getShort("Water");
- this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
- if (compound.hasKey("CustomName", 8)) {
- this.furnaceCustomName = compound.getString("CustomName");
- }
- }
- public void writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setShort("BurnTime", (short) this.burnTime);
- compound.setShort("CookTime", (short) this.cookTime);
- compound.setShort("Heath", (short) this.heath);
- compound.setShort("Water", (short) this.water);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.slots.length; ++i) {
- if (this.slots[i] != null) {
- NBTTagCompound nbttagcompound1 = new NBTTagCompound();
- nbttagcompound1.setByte("Slot", (byte) i);
- this.slots[i].writeToNBT(nbttagcompound1);
- nbttaglist.appendTag(nbttagcompound1);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (this.hasCustomName()) {
- compound.setString("CustomName", this.furnaceCustomName);
- }
- }
- public int getInventoryStackLimit() {
- return 64;
- }
- public boolean isBurning() {
- return this.burnTime > 0;
- }
- public boolean hasWater() {
- return this.water > 0;
- }
- @SideOnly(Side.CLIENT)
- public static boolean isBurning(IInventory iinventory) {
- return iinventory.getField(0) > 0;
- }
- @SideOnly(Side.CLIENT)
- public static boolean hasWater(IInventory iinventory) {
- return iinventory.getField(2) > 2;
- }
- public void update() {
- boolean flag = this.burnTime > 0;
- boolean flag2 = this.water > 0;
- boolean flag3 = false;
- if (this.isBurning()) {
- this.burnTime--;
- }
- //20 Ticks = 1 second, 600 ticks = 30 seconds
- if (this.hasWater() && this.isBurning() && this.heath >= 600) {
- this.water--;
- }
- if (!this.worldObj.isRemote) {
- if (!this.isBurning() && this.hasWater() && this.canCook()) {
- this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);
- if (this.isBurning()) {
- flag3 = true;
- if (this.slots[1] != null) {
- this.slots[1].stackSize--;
- if (this.slots[1].stackSize == 0) {
- this.slots[1] = slots[1].getItem().getContainerItem(slots[1]);
- }
- }
- }
- }
- if (this.isItemWater(this.slots[2]) && this.water <= (this.maxWater - this.getItemWater(this.slots[2]))) {
- this.water += getItemWater(this.slots[2]);
- if (this.slots[2] != null) {
- flag3 = true;
- this.slots[2].stackSize--;
- if (this.slots[2].stackSize == 0) {
- this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
- }
- }
- }
- if (this.isBurning() && this.water > 10000 && this.canCook()) {
- this.heath++;
- if (this.heath >= 300) {
- this.cookTime++;
- }
- if (this.heath >= 1200) {
- this.heath = 1200;
- }
- if (this.cookTime >= (this.cookingFurnaceSpeed - this.heath)) {
- this.cookTime = 0;
- this.smeltItem();
- flag3 = true;
- }
- } else {
- this.cookTime = 0;
- }
- if (flag != this.hasWater()) {
- flag3 = true;
- Boiler.setState(this.hasWater(), this.worldObj, this.pos);
- }
- }
- if (flag3) {
- this.markDirty();
- }
- //Debugger.log("Heath of furnace: " + heath);
- }
- private boolean canCook() {
- if (slots[0] == null) {
- return false;
- } else {
- ItemStack itemstack = BoilerRecipes.getBoilingResult(slots[0].getItem());
- if (itemstack == null)
- return false;
- if (this.slots[3] == null)
- return true;
- if (!this.slots[3].isItemEqual(itemstack))
- return false;
- int result = this.slots[3].stackSize + itemstack.stackSize;
- return result <= getInventoryStackLimit() && result <= this.slots[3].getMaxStackSize();
- }
- }
- public void smeltItem() {
- if (this.canCook()) {
- ItemStack itemstack = BoilerRecipes.getBoilingResult(this.slots[0].getItem());
- if (this.slots[3] == null) {
- this.slots[3] = itemstack.copy();
- } else if (this.slots[3].getItem() == itemstack.getItem()) {
- this.slots[3].stackSize += itemstack.stackSize; // Forge BugFix:
- // Results may
- // have multiple
- // items
- }
- if (slots[0].stackSize <= 0) {
- slots[0] = new ItemStack(slots[0].getItem().setFull3D());
- } else {
- slots[0].stackSize--;
- }
- if (slots[0].stackSize <= 0) {
- slots[0] = null;
- }
- }
- }
- public static int getItemBurnTime(ItemStack itemstack) {
- if (itemstack == null) {
- return 0;
- } else {
- Item item = itemstack.getItem();
- if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
- Block block = Block.getBlockFromItem(item);
- if (block == Blocks.wooden_slab) {
- return 150;
- }
- if (block.getMaterial() == Material.wood) {
- return 300;
- }
- if (block == Blocks.coal_block) {
- return 16000;
- }
- }
- if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD"))
- return 200;
- if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD"))
- return 200;
- if (item instanceof ItemHoe && ((ItemHoe) item).getMaterialName().equals("WOOD"))
- return 200;
- if (item == Items.stick)
- return 100;
- if (item == Items.coal)
- return 1600;
- if (item == Items.lava_bucket)
- return 20000;
- if (item == Item.getItemFromBlock(Blocks.sapling))
- return 100;
- if (item == Items.blaze_rod)
- return 2400;
- return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(itemstack);
- }
- }
- public static boolean isItemFuel(ItemStack itemstack) {
- return getItemBurnTime(itemstack) > 0;
- }
- public static int getItemWater(ItemStack itemstack) {
- if (itemstack == null) {
- return 0;
- } else {
- Item item = itemstack.getItem();
- if (item == Items.water_bucket)
- return 10000;
- return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(itemstack);
- }
- }
- public static boolean isItemWater(ItemStack itemstack) {
- return getItemWater(itemstack) > 0;
- }
- 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 boolean isItemValidForSlot(int index, ItemStack stack) {
- return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
- }
- 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 int getBurnTimeRemainingScaled(int i) {
- if (this.currentItemBurnTime == 0) {
- this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
- }
- return this.burnTime * i / this.currentItemBurnTime;
- }
- public int getWaterRemainingScaled(int i) {
- return (this.water * i) / this.maxWater;
- }
- public int getCookProgressScaled(int i) {
- return this.cookTime * i / (this.cookingFurnaceSpeed - this.heath);
- }
- public String getGuiID() {
- return "chef:boiler";
- }
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
- return new ContainerBoiler(playerInventory, this);
- }
- public int getField(int id) {
- switch (id) {
- case 0:
- return this.burnTime;
- case 1:
- return this.currentItemBurnTime;
- case 2:
- return this.water;
- case 3:
- return this.heath;
- case 4:
- return this.cookTime;
- default:
- return 0;
- }
- }
- public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.burnTime = value;
- break;
- case 1:
- this.currentItemBurnTime = value;
- break;
- case 2:
- this.water = value;
- break;
- case 3:
- this.heath = value;
- break;
- case 4:
- this.cookTime = value;
- break;
- }
- }
- public int getFieldCount() {
- return 5;
- }
- public void clear() {
- for (int i = 0; i < this.slots.length; ++i) {
- this.slots[i] = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment