package com.watschman.betterenhancement.tile; import com.watschman.betterenhancement.blocks.ModBlockFurnace; import com.watschman.betterenhancement.recipes.machines.RubyFurnaceRecipes; import com.watschman.betterenhancement.reference.Reference; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.*; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.ITickable; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.event.ForgeEventFactory; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.ItemStackHandler; import javax.annotation.Nullable; public class TileEntityRubyFurnace extends TileEntity implements ITickable { private ItemStackHandler handler = new ItemStackHandler(4); private ItemStack smelting = ItemStack.EMPTY; private String customName; private int burnTime; private int currentBurnTime; private int cookTime; private int totalCookTime = 100; private int stackSizeLimit = 64; private boolean canSmelt() { if(((ItemStack)this.handler.getStackInSlot(0)).isEmpty() || ((ItemStack)this.handler.getStackInSlot(1)).isEmpty()) { return false; } else { ItemStack result = RubyFurnaceRecipes.getInstance().getFurnaceResult((ItemStack)this.handler.getStackInSlot(0), (ItemStack)this.handler.getStackInSlot(1)); if(result.isEmpty()) { return false; } else { ItemStack output = (ItemStack)this.handler.getStackInSlot(3); if(output.isEmpty()) { return true; } if(!output.isItemEqual(result)) { return false; } int res = output.getCount() + result.getCount(); return res <= this.stackSizeLimit && res <= output.getMaxStackSize(); } } } public void setCustomName (String customName) { this.customName = customName; } public boolean hasCustomName() { return this.customName != null && !this.customName.isEmpty(); } public boolean isUsableByPlayer(EntityPlayer player) { return this.world.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 int getField(int id) { switch (id) { case 0: return this.burnTime; case 1: return this.currentBurnTime; 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.burnTime = value; break; case 1: this.currentBurnTime = value; break; case 2: this.cookTime = value; break; case 3: this.totalCookTime = value; break; default: Reference.MOD_LOGGER.error("Switch Statement ended in Default branch even though it shouldn't."); } } public void update() { if(this.isBurning()) { this.burnTime--; // My Test - may break; Block currentBlock = this.world.getBlockState(this.pos).getBlock(); if(currentBlock instanceof ModBlockFurnace) { ((ModBlockFurnace)currentBlock).setState(true, world, pos); } /* --this.burnTime; ModBlockFurnace.setState(true, world, pos); */ // End of My Test ItemStack[] inputs = new ItemStack[] {handler.getStackInSlot(0), handler.getStackInSlot(1)}; ItemStack fuel = this.handler.getStackInSlot(2); if(this.isBurning() || !fuel.isEmpty() && !this.handler.getStackInSlot(0).isEmpty() || this.handler.getStackInSlot(1).isEmpty()) { if(!this.isBurning() && this.canSmelt()) { this.burnTime = this.getItemBurnTime(fuel); this.currentBurnTime = this.burnTime; if(this.isBurning() && !fuel.isEmpty()) { Item item = fuel.getItem(); fuel.shrink(1); if(fuel.isEmpty()) { ItemStack itemStack = item.getContainerItem(fuel); this.handler.setStackInSlot(2, itemStack); } } } } if(this.isBurning() && this.canSmelt() && cookTime > 0) { this.cookTime++; if(this.cookTime == this.totalCookTime) { if(this.handler.getStackInSlot(3).getCount() > 0) { this.handler.getStackInSlot(3).grow(1); } else { this.handler.insertItem(3, this.smelting, false); } this.smelting = ItemStack.EMPTY; this.cookTime = 0; return; } } else { if(this.canSmelt() && this.isBurning()) { ItemStack output = RubyFurnaceRecipes.getInstance().getFurnaceResult(inputs[0], inputs[1]); if(!output.isEmpty()) { this.smelting = output; this.cookTime++; inputs[0].shrink(1); inputs[1].shrink(1); this.handler.setStackInSlot(0, inputs[0]); this.handler.setStackInSlot(1, inputs[1]); } } } } } /* public void smeltItem() { if(this.canSmelt()) { ItemStack input1 = (ItemStack)this.handler.getStackInSlot(0); ItemStack input2 = (ItemStack)this.handler.getStackInSlot(1); ItemStack result = RubyFurnaceRecipes.getInstence().getFurnaceResult(input1, input2); ItemStack output = (ItemStack)this.handler.getStackInSlot(3); if(output.isEmpty()) { this.handler.setStackInSlot(3, result.copy()); } else if(output.getItem() == result.getItem()) { output.grow(result.getCount()); } input1.shrink(1); input2.shrink(1); } } */ public boolean isBurning() { return this.burnTime > 0; } /* SUPPOSEDLY STATIC */ @SideOnly(Side.CLIENT) public static boolean isBurning(TileEntityRubyFurnace tileEntity) { return tileEntity.getField(0) > 0; } public int getItemBurnTime(ItemStack fuel) { if(fuel.isEmpty()) { return 0; } else { Item item = fuel.getItem(); if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.AIR) { Block block = Block.getBlockFromItem(item); if(block == Blocks.WOODEN_SLAB) { return 150; } if(block.getDefaultState().getMaterial() == Material.WOOD) { return 300; } if(block == Blocks.COAL_BLOCK) { return 16000; } } if(item instanceof ItemTool && "WOOD".equals(((ItemTool)item).getToolMaterialName())) { return 200; } if(item instanceof ItemSword && "WOOD".equals(((ItemSword)item).getToolMaterialName())) { return 200; } if(item instanceof ItemHoe && "WOOD".equals(((ItemHoe)item).getMaterialName())) { 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 50; } if(item == Items.BLAZE_ROD) { return 2400; } return ForgeEventFactory.getItemBurnTime(fuel); } } public boolean isItemFuel(ItemStack fuel) { return this.getItemBurnTime(fuel) > 0; } /* SUPPOSEDLY STATIC END */ @Override public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) { if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return true; } else { return false; } } @Nullable @Override public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) { if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return (T)this.handler; } return super.getCapability(capability, facing); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); this.handler.deserializeNBT(compound.getCompoundTag("Inventory")); this.cookTime = compound.getInteger("CookTime"); this.burnTime = compound.getInteger("BurnTime"); this.totalCookTime = compound.getInteger("CookTimeTotal"); this.currentBurnTime = this.getItemBurnTime((ItemStack)this.handler.getStackInSlot(2)); if(compound.hasKey("CustomName", 8)) { this.setCustomName(compound.getString("CustomName")); } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("BurnTime", (short)this.burnTime); compound.setInteger("CookTime", (short)this.cookTime); compound.setInteger("CookTimeTotal", (short)this.totalCookTime); compound.setTag("Inventory", this.handler.serializeNBT()); if(this.hasCustomName()) { compound.setString("CustomName", this.customName); } return compound; } @Nullable @Override public ITextComponent getDisplayName() { return this.hasCustomName() ? new TextComponentString(this.customName) : new TextComponentTranslation("container.betterenhancement.ruby_furnace"); } }