diff --git a/src/main/java/tconstruct/smeltery/gui/SmelteryGui.java b/src/main/java/tconstruct/smeltery/gui/SmelteryGui.java index 2f7671e9f7c..57f8c82770d 100644 --- a/src/main/java/tconstruct/smeltery/gui/SmelteryGui.java +++ b/src/main/java/tconstruct/smeltery/gui/SmelteryGui.java @@ -45,7 +45,7 @@ public void initGui () super.initGui(); if (logic != null) - logic.updateFuelGague(); + logic.updateFuelDisplay(); } @Override diff --git a/src/main/java/tconstruct/smeltery/logic/SmelteryLogic.java b/src/main/java/tconstruct/smeltery/logic/SmelteryLogic.java index c660b9a130d..af268d97826 100644 --- a/src/main/java/tconstruct/smeltery/logic/SmelteryLogic.java +++ b/src/main/java/tconstruct/smeltery/logic/SmelteryLogic.java @@ -292,6 +292,13 @@ public void updateEntity () * if (worldObj.isRemote) return; */ + + if(validStructure) + if (tick % 4 == 0) { + checkHasItems(); + heatItems(); + } + if (tick % 20 == 0) { if (!validStructure) @@ -311,10 +318,6 @@ public void updateEntity () worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } } - - if(validStructure) - if (tick % 4 == 0) - heatItems(); } void detectEntities () @@ -438,6 +441,17 @@ private void handleItemEntity (EntityItem item) } } + private void checkHasItems() + { + inUse = false; + for (int i = 0; i < maxBlockCapacity; i++) + if(this.isStackInSlot(i) && meltingTemps[i] > 200) + { + inUse = true; + break; + } + } + private void heatItems () { if (useTime > 0) @@ -542,7 +556,6 @@ private void updateCurrentLiquid() { private void updateTemperatures () { - inUse = true; for (int i = 0; i < maxBlockCapacity; i++) { meltingTemps[i] = Smeltery.getLiquifyTemperature(inventory[i]) * 10; // temperatures are *10 for more progress control @@ -551,138 +564,92 @@ private void updateTemperatures () public void updateFuelDisplay () { - if (activeLavaTank == null) - return; - - if (!worldObj.blockExists(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z)) - { + // ensure our active tank is valid + verifyFuelTank(); + if (activeLavaTank == null) { fuelAmount = 0; fuelGague = 0; return; } - TileEntity tankContainer = worldObj.getTileEntity(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z); - if (tankContainer == null) - { - fuelAmount = 0; - fuelGague = 0; - return; - } - - if (tankContainer instanceof IFluidHandler) - { - FluidStack liquid = ((IFluidHandler) tankContainer).drain(ForgeDirection.DOWN, 4000, false); - if (liquid != null && Smeltery.isSmelteryFuel(liquid.getFluid())) - { - FluidTankInfo[] info = ((IFluidHandler) tankContainer).getTankInfo(ForgeDirection.DOWN); - if (info.length > 0) - { - int capacity = info[0].capacity; - fuelAmount = liquid.amount; - fuelGague = liquid.amount * 52 / capacity; - } - } - else - { - fuelAmount = 0; - fuelGague = 0; - } - } + // checks are all done before in verifyFuelTank. Don't do this without checks! + IFluidHandler tankContainer = (IFluidHandler) worldObj.getTileEntity(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z); + FluidTankInfo[] info = tankContainer.getTankInfo(ForgeDirection.DOWN); + int capacity = info[0].capacity; + fuelAmount = info[0].fluid.amount; + fuelGague = (int)((float)fuelAmount * 52f / (float)capacity); } + // actually is updateFuel. public void updateFuelGague () { - if(activeLavaTank == null && lavaTanks.size() > 0) - activeLavaTank = lavaTanks.get(0); - if (activeLavaTank == null || useTime > 0) + // no need to update + if(useTime > 0 || !inUse) return; - if (!worldObj.blockExists(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z)) - { - fuelAmount = 0; - fuelGague = 0; - return; - } + // ensure active lava tank + verifyFuelTank(); - TileEntity tankContainer = worldObj.getTileEntity(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z); - if (tankContainer == null) + // checks are all done before in verifyFuelTank. Don't do this without checks! + IFluidHandler tankContainer = (IFluidHandler) worldObj.getTileEntity(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z); + + // get liquid from the tank + FluidStack liquid = tankContainer.drain(ForgeDirection.DOWN, 15, false); + if (liquid != null && Smeltery.isSmelteryFuel(liquid.getFluid())) // doublecheck that everything is ok { - fuelAmount = 0; - fuelGague = 0; - return; + // drain actual liquid, non simulated + liquid = tankContainer.drain(ForgeDirection.DOWN, 15, true); + useTime += Smeltery.getFuelDuration(liquid.getFluid()); + internalTemp = Smeltery.getFuelPower(liquid.getFluid()); + + // update fuel display + updateFuelDisplay(); // this also ensures that the next fuel tank is displayed if this drain made the current one empty } - if (tankContainer instanceof IFluidHandler) + } + + protected void verifyFuelTank() + { + // is our current tank still good? + if(activeLavaTank != null && worldObj.blockExists(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z)) { - needsUpdate = true; - FluidStack liquid = ((IFluidHandler) tankContainer).drain(ForgeDirection.DOWN, 15, false); - if (liquid != null && Smeltery.isSmelteryFuel(liquid.getFluid())) + TileEntity tankContainer = worldObj.getTileEntity(activeLavaTank.x, activeLavaTank.y, activeLavaTank.z); + if (tankContainer instanceof IFluidHandler) { - liquid = ((IFluidHandler) tankContainer).drain(ForgeDirection.DOWN, 15, true); - useTime += Smeltery.getFuelDuration(liquid.getFluid()); - internalTemp = Smeltery.getFuelPower(liquid.getFluid()); - - FluidTankInfo[] info = ((IFluidHandler) tankContainer).getTankInfo(ForgeDirection.DOWN); - liquid = info[0].fluid; - int capacity = info[0].capacity; - if (liquid != null) - { - fuelAmount = liquid.amount; - fuelGague = liquid.amount * 52 / capacity; - } - else - { - fuelAmount = 0; - fuelGague = 0; - } - } - else - { - boolean foundTank = false; - int iter = 0; - while (!foundTank && iter < lavaTanks.size()) - { - CoordTuple possibleTank = lavaTanks.get(iter); - TileEntity newTankContainer = worldObj.getTileEntity(possibleTank.x, possibleTank.y, possibleTank.z); - if (newTankContainer instanceof IFluidHandler) - { - // TConstruct.logger.info("Tank: "+possibleTank.toString()); - FluidStack newliquid = ((IFluidHandler) newTankContainer).drain(ForgeDirection.UNKNOWN, 150, false); - if (newliquid != null && Smeltery.isSmelteryFuel(newliquid.getFluid()) && newliquid.amount > 0) - { - // TConstruct.logger.info("Tank: "+possibleTank.toString()); - foundTank = true; - activeLavaTank = possibleTank; - iter = lavaTanks.size(); - - /* - * IFluidTank newTank = ((IFluidHandler) - * newTankContainer).getTank(ForgeDirection.UNKNOWN, - * liquid); liquid = newTank.getFluid(); int - * capacity = newTank.getCapacity(); - */ - FluidTankInfo[] info = ((IFluidHandler) tankContainer).getTankInfo(ForgeDirection.DOWN); - liquid = info[0].fluid; - int capacity = info[0].capacity; - if (liquid != null) - { - fuelAmount = liquid.amount; - fuelGague = liquid.amount * 52 / capacity; - } - else - { - fuelAmount = 0; - fuelGague = 0; - } - } - } - iter++; - if (iter >= lavaTanks.size()) - foundTank = true; - } - // TConstruct.logger.info("Searching for tank, size: "+lavaTanks.size()); + FluidStack liquid = ((IFluidHandler) tankContainer).drain(ForgeDirection.DOWN, 15, false); + // current tank still has liquid and it's a fuel. everythin ok. + if (liquid != null && Smeltery.isSmelteryFuel(liquid.getFluid())) + return; } } + + + // our tank got derped or is empty. time to look for a new one! + activeLavaTank = null; + for(CoordTuple tank : lavaTanks) + { + // does the tank still exist? + if (!worldObj.blockExists(tank.x, tank.y, tank.z)) + continue; + + // yes, it does, but is it a tank? + TileEntity tankContainer = worldObj.getTileEntity(tank.x, tank.y, tank.z); + if (! (tankContainer instanceof IFluidHandler)) + continue; + + // yes it is, but does it contain a liquid? + FluidTankInfo[] info = ((IFluidHandler) tankContainer).getTankInfo(ForgeDirection.DOWN); + if(info.length <= 0 || info[0].fluid == null || info[0].fluid.amount <= 0) + continue; + + // is it also a smeltery fuel? + if(!Smeltery.isSmelteryFuel(info[0].fluid.getFluid())) + continue; + + // we found a tank! :) + activeLavaTank = tank; + return; + } } @SideOnly(Side.CLIENT)