Skip to content

Instantly share code, notes, and snippets.

Created March 7, 2016 11:50
Broken Furnace
package com.jamengulfer.crystalalchemy.tileentities.furnace;
import java.util.ArrayList;
import com.jamengulfer.crystalalchemy.flow.Flow;
import com.jamengulfer.crystalalchemy.flow.FlowType;
import com.jamengulfer.crystalalchemy.flow.IFlowConsumer;
import com.jamengulfer.crystalalchemy.flow.IFlowSource;
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.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.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ITickable;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class TileEntityFurnace extends TileEntity implements ITickable, IInventory, IFlowConsumer {
private ArrayList<IFlowSource> flowSources = new ArrayList<IFlowSource>();
private ItemStack[] inventory = new ItemStack[3];
private String customName;
private String name = "Alchemical Furnace";
private int furnaceBurnTime;
private int currentItemBurnTime;
private int cookTime;
private int totalCookTime;
private int currentItemCost = 0;
private int currentFlowAmount = 0;
@Override
public int getField(int id) {
switch (id) {
case 0:
return this.furnaceBurnTime;
case 1:
return this.currentItemBurnTime;
case 2:
return this.cookTime;
case 3:
return this.totalCookTime;
case 4:
System.out.println("Current Flow Output: " + this.currentFlowAmount);
return this.currentFlowAmount;
case 5:
return this.currentItemCost;
default:
return 0;
}
}
@Override
public void setField(int id, int value) {
switch (id) {
case 0:
this.furnaceBurnTime = value;
break;
case 1:
this.currentItemBurnTime = value;
break;
case 2:
this.cookTime = value;
break;
case 3:
this.totalCookTime = value;
break;
case 4:
this.currentFlowAmount = value;
break;
case 5:
this.currentItemCost = value;
break;
}
}
@Override
public int getFieldCount() {
return 6;
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger("BurnTime", furnaceBurnTime);
nbt.setInteger("CookTime", cookTime);
nbt.setInteger("CookTimeTotal", totalCookTime);
nbt.setInteger("FlowAmount", currentFlowAmount);
nbt.setInteger("CurrentCost", currentItemCost);
NBTTagList list = new NBTTagList();
for (int i = 0; i < this.getSizeInventory(); i++) {
if (this.getStackInSlot(i) != null) {
NBTTagCompound stackTag = new NBTTagCompound();
stackTag.setByte("Slot", (byte) i);
this.getStackInSlot(i).writeToNBT(stackTag);
list.appendTag(stackTag);
}
}
nbt.setTag("Items", list);
if (this.hasCustomName()) {
nbt.setString("CustomName", this.getCustomName());
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("Items", 10);
for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound stackTag = list.getCompoundTagAt(i);
int slot = stackTag.getByte("Slot") & 255;
this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
}
if (nbt.hasKey("CustomName", 8)) {
this.setCustomName(nbt.getString("CustomName"));
}
furnaceBurnTime = nbt.getInteger("BurnTime");
cookTime = nbt.getInteger("CookTime");
totalCookTime = nbt.getInteger("CookTimeTotal");
currentFlowAmount = nbt.getInteger("FlowAmount");
currentItemCost = nbt.getInteger("CurrentCost");
currentItemBurnTime = getItemBurnTime(this.inventory[1]);
}
public boolean isBurning() {
//return furnaceBurnTime > 0;
return currentItemCost > 0;
}
@SideOnly(Side.CLIENT)
public static boolean isBurning(IInventory inv) {
return inv.getField(0) > 0;
}
public void update() {
//boolean flag = this.isBurning();
boolean isDirty = false;
if (this.inventory[0] != null) {
if (this.canSmelt()) {
if (currentItemCost == 0) {
currentItemCost = this.getFlowCost(this.inventory[0]);
}
if (flowSources.size() > 0 && currentItemCost > 0) {
IFlowSource source = flowSources.get(0);
Flow costFlow = new Flow();
costFlow.addFlowType(FlowType.PURE, 500);
Flow returnFlow = source.consumeFlow(costFlow);
this.currentFlowAmount += returnFlow.totalFlow;
System.out.println("Current flow: " + this.currentFlowAmount);
if (this.currentFlowAmount >= currentItemCost) {
this.smeltItem();
this.currentFlowAmount = 0;
}
isDirty = true;
}
}
} else if(currentItemCost != 0 || this.currentFlowAmount != 0){
currentItemCost = 0;
this.currentFlowAmount = 0;
isDirty = true;
}
if (isDirty) {
this.markDirty();
}
}
public int getCookTime(ItemStack stack) {
return 200;
}
public int getFlowCost(ItemStack stack) {
return 100000;
}
private boolean canSmelt() {
if (this.inventory[0] == null) {
return false;
} else {
ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.inventory[0]);
if (itemstack == null) {
return false;
}
if (this.inventory[2] == null) {
return true;
}
if (!this.inventory[2].isItemEqual(itemstack)) {
return false;
}
int result = this.inventory[2].stackSize + itemstack.stackSize;
return result <= getInventoryStackLimit() && result <= this.inventory[2].getMaxStackSize();
}
}
public void smeltItem() {
if (this.canSmelt()) {
ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.inventory[0]);
if (this.inventory[2] == null) {
this.inventory[2] = itemstack.copy();
} else if (this.inventory[2].getItem() == itemstack.getItem()) {
this.inventory[2].stackSize += itemstack.stackSize;
}
if (this.inventory[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.inventory[0].getMetadata() == 1 && this.inventory[1] != null && this.inventory[1].getItem() == Items.bucket) {
this.inventory[1] = new ItemStack(Items.water_bucket);
}
this.inventory[0].stackSize--;
if (this.inventory[0].stackSize <= 0) {
this.inventory[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 String getGuiID() {
return "crystalalchemy:furnace";
}
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerEntity) {
return new ContainerFurnace(playerInventory, this);
}
@Override
public void addSource(IFlowSource source) {
if (!flowSources.contains(source)) {
flowSources.add(source);
}
}
@Override
public void removeSource(IFlowSource source) {
flowSources.remove(source);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment