diff --git a/src/main/java/forestry/core/fluids/FluidHelper.java b/src/main/java/forestry/core/fluids/FluidHelper.java index 6d3d68c6b..aab7c1ba3 100644 --- a/src/main/java/forestry/core/fluids/FluidHelper.java +++ b/src/main/java/forestry/core/fluids/FluidHelper.java @@ -11,6 +11,7 @@ package forestry.core.fluids; import forestry.core.utils.InventoryUtil; +import forestry.core.utils.ItemStackUtil; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; @@ -48,9 +49,9 @@ public final class FluidHelper { } public enum FillStatus { - SUCCESS, INVALID_INPUT, NO_FLUID, NO_SPACE + SUCCESS, INVALID_INPUT, NO_FLUID, NO_SPACE, NO_SPACE_FLUID } - + public static FillStatus fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill, boolean doFill) { if (fluidToFill == null) { return FillStatus.INVALID_INPUT; @@ -109,14 +110,14 @@ public final class FluidHelper { output.stackSize++; } inv.decrStackSize(inputSlot, 1); - } else { + }else{ inv.setInventorySlotContents(inputSlot, filled); } } return FillStatus.SUCCESS; } - + public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot) { ItemStack input = inv.getStackInSlot(inputSlot); if (input == null) { @@ -130,10 +131,11 @@ public final class FluidHelper { if (input.stackSize == 1 || drainedItemSimulated.stackSize == 0) { ItemStack drainedItem = FluidUtil.tryEmptyContainer(input, fluidHandler, Fluid.BUCKET_VOLUME, null, true); + if (drainedItem != null) { - if (drainedItem.stackSize > 0) { + if(drainedItem.stackSize > 0){ inv.setInventorySlotContents(inputSlot, drainedItem); - } else { + }else { inv.decrStackSize(inputSlot, 1); } return true; @@ -143,6 +145,62 @@ public final class FluidHelper { return false; } + public static FillStatus drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot) { + ItemStack input = inv.getStackInSlot(inputSlot); + if (input == null) { + return FillStatus.INVALID_INPUT; + } + ItemStack outputStack = inv.getStackInSlot(outputSlot); + + ItemStack drainedItemSimulated = FluidUtil.tryEmptyContainer(input, fluidHandler, Fluid.BUCKET_VOLUME, null, false); + if (drainedItemSimulated == null) { + return FillStatus.INVALID_INPUT; + } + + if (outputStack == null || drainedItemSimulated.stackSize == 0 || ItemStackUtil.isIdenticalItem(outputStack, drainedItemSimulated) && outputStack.stackSize + drainedItemSimulated.stackSize < outputStack.getMaxStackSize()) { + ItemStack drainedItem = FluidUtil.tryEmptyContainer(input, fluidHandler, Fluid.BUCKET_VOLUME, null, true); + + if (drainedItem != null) { + if(drainedItem.stackSize > 0){ + ItemStack newStack = drainedItem.copy(); + if(outputStack != null){ + newStack.stackSize+=outputStack.stackSize; + } + if (isFillableContainerAndEmpty(newStack)) { + inv.setInventorySlotContents(outputSlot, newStack); + inv.decrStackSize(inputSlot, 1); + } + }else { + inv.decrStackSize(inputSlot, 1); + } + return FillStatus.SUCCESS; + } + } + + return FillStatus.NO_SPACE; + } + + public static boolean isFillableContainerAndEmpty(ItemStack container) { + IFluidHandler fluidHandler = FluidUtil.getFluidHandler(container); + if (fluidHandler == null) { + return false; + } + + IFluidTankProperties[] tankProperties = fluidHandler.getTankProperties(); + for (IFluidTankProperties properties : tankProperties) { + if (properties.canFill() && properties.getCapacity() > 0) { + FluidStack contents = properties.getContents(); + if (contents == null) { + return true; + } else if (contents.amount > 0) { + return false; + } + } + } + + return false; + } + public static boolean isFillableContainerWithRoom(ItemStack container) { IFluidHandler fluidHandler = FluidUtil.getFluidHandler(container); if (fluidHandler == null) { diff --git a/src/main/java/forestry/energy/EnergyHelper.java b/src/main/java/forestry/energy/EnergyHelper.java index dd902e703..cd36fe33b 100644 --- a/src/main/java/forestry/energy/EnergyHelper.java +++ b/src/main/java/forestry/energy/EnergyHelper.java @@ -21,6 +21,9 @@ public class EnergyHelper { * @return true if the energy to do work was consumed */ public static boolean consumeEnergyToDoWork(EnergyManager energyManager, int ticksPerWorkCycle, int energyPerWorkCycle) { + if(energyPerWorkCycle == 0){ + return true; + } int energyPerCycle = (int) Math.ceil(energyPerWorkCycle / (float) ticksPerWorkCycle); if (energyManager.getEnergyStored() < energyPerCycle) { return false; diff --git a/src/main/java/forestry/factory/gui/ContainerBottler.java b/src/main/java/forestry/factory/gui/ContainerBottler.java index 0a423cc49..965177a1c 100755 --- a/src/main/java/forestry/factory/gui/ContainerBottler.java +++ b/src/main/java/forestry/factory/gui/ContainerBottler.java @@ -23,9 +23,12 @@ public class ContainerBottler extends ContainerLiquidTanks { public ContainerBottler(InventoryPlayer player, TileBottler tile) { super(tile, player, 8, 84); - - this.addSlotToContainer(new SlotEmptyLiquidContainerIn(tile, InventoryBottler.SLOT_INPUT_EMPTY_CAN, 116, 19)); - this.addSlotToContainer(new SlotOutput(tile, InventoryBottler.SLOT_OUTPUT, 116, 55)); - this.addSlotToContainer(new SlotLiquidIn(tile, InventoryBottler.SLOT_INPUT_FULL_CAN, 26, 38)); + + this.addSlotToContainer(new SlotLiquidIn(tile, InventoryBottler.SLOT_INPUT_FULL_CONTAINER, 18, 7)); + this.addSlotToContainer(new SlotOutput(tile, InventoryBottler.SLOT_LEFT_PROCESSING, 18, 35).setPickupWatcher(tile)); + this.addSlotToContainer(new SlotOutput(tile, InventoryBottler.SLOT_OUTPUT_EMPTY_CONTAINER, 18, 63)); + this.addSlotToContainer(new SlotEmptyLiquidContainerIn(tile, InventoryBottler.SLOT_INPUT_EMPTY_CONTAINER, 142, 7)); + this.addSlotToContainer(new SlotOutput(tile, InventoryBottler.SLOT_RIGHT_PROCESSING, 142, 35).setPickupWatcher(tile)); + this.addSlotToContainer(new SlotOutput(tile, InventoryBottler.SLOT_OUTPUT_FULL_CONTAINER, 142, 63)); } } diff --git a/src/main/java/forestry/factory/gui/GuiBottler.java b/src/main/java/forestry/factory/gui/GuiBottler.java index 02eb9a9c8..242042381 100755 --- a/src/main/java/forestry/factory/gui/GuiBottler.java +++ b/src/main/java/forestry/factory/gui/GuiBottler.java @@ -10,26 +10,57 @@ ******************************************************************************/ package forestry.factory.gui; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.RenderHelper; import net.minecraft.entity.player.InventoryPlayer; - import forestry.core.config.Constants; import forestry.core.gui.GuiForestryTitled; import forestry.core.gui.widgets.TankWidget; +import forestry.core.render.ColourProperties; +import forestry.core.utils.Translator; import forestry.factory.tiles.TileBottler; public class GuiBottler extends GuiForestryTitled { public GuiBottler(InventoryPlayer inventory, TileBottler processor) { super(Constants.TEXTURE_PATH_GUI + "/bottler.png", new ContainerBottler(inventory, processor), processor); - widgetManager.add(new TankWidget(this.widgetManager, 53, 17, 0)); + widgetManager.add(new TankWidget(this.widgetManager, 80, 14, 0)); } @Override protected void drawGuiContainerBackgroundLayer(float var1, int mouseX, int mouseY) { - super.drawGuiContainerBackgroundLayer(var1, mouseX, mouseY); - TileBottler bottler = inventory; + bindTexture(textureFile); - int progress = bottler.getProgressScaled(24); - drawTexturedModalRect(guiLeft + 80, guiTop + 39, 176, 74, progress, 16); + int x = (width - xSize) / 2; + int y = (height - ySize) / 2; + drawTexturedModalRect(x, y, 0, 0, xSize, ySize); + + RenderHelper.enableGUIStandardItemLighting(); + GlStateManager.disableLighting(); + GlStateManager.enableRescaleNormal(); + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + GlStateManager.pushMatrix(); + { + GlStateManager.translate(guiLeft, guiTop, 0.0F); + drawWidgets(); + } + GlStateManager.popMatrix(); + + String name = Translator.translateToLocal(inventory.getUnlocalizedTitle()); + textLayout.line = 5; + textLayout.drawCenteredLine(name, 0, ColourProperties.INSTANCE.get("gui.title")); + bindTexture(textureFile); + + bindTexture(textureFile); + + TileBottler bottler = inventory; + int progressArrow = bottler.getProgressScaled(22); + if(progressArrow > 0){ + if(bottler.isFillRecipe){ + drawTexturedModalRect(guiLeft + 108, guiTop + 35, 177, 74, progressArrow, 16); + }else{ + drawTexturedModalRect(guiLeft + 46, guiTop + 35, 177, 74, progressArrow, 16); + } + } } } diff --git a/src/main/java/forestry/factory/inventory/InventoryBottler.java b/src/main/java/forestry/factory/inventory/InventoryBottler.java index e15d72404..240f4f1c1 100644 --- a/src/main/java/forestry/factory/inventory/InventoryBottler.java +++ b/src/main/java/forestry/factory/inventory/InventoryBottler.java @@ -22,19 +22,22 @@ import forestry.core.inventory.InventoryAdapterTile; import forestry.factory.tiles.TileBottler; public class InventoryBottler extends InventoryAdapterTile { - public static final short SLOT_INPUT_EMPTY_CAN = 0; - public static final short SLOT_OUTPUT = 1; - public static final short SLOT_INPUT_FULL_CAN = 2; + public static final short SLOT_INPUT_FULL_CONTAINER = 0; + public static final short SLOT_INPUT_EMPTY_CONTAINER = 1; + public static final short SLOT_LEFT_PROCESSING = 2; + public static final short SLOT_RIGHT_PROCESSING = 3; + public static final short SLOT_OUTPUT_EMPTY_CONTAINER = 4; + public static final short SLOT_OUTPUT_FULL_CONTAINER = 5; public InventoryBottler(TileBottler tileBottler) { - super(tileBottler, 3, "Items"); + super(tileBottler, 6, "Items"); } @Override public boolean canSlotAccept(int slotIndex, ItemStack itemStack) { - if (slotIndex == SLOT_INPUT_EMPTY_CAN) { + if (slotIndex == SLOT_INPUT_EMPTY_CONTAINER) { return FluidHelper.isFillableContainerWithRoom(itemStack); - } else if (slotIndex == SLOT_INPUT_FULL_CAN) { + } else if (slotIndex == SLOT_INPUT_FULL_CONTAINER) { FluidStack fluidStack = FluidUtil.getFluidContained(itemStack); return fluidStack != null && FluidRegistry.isFluidRegistered(fluidStack.getFluid()); } @@ -43,6 +46,6 @@ public class InventoryBottler extends InventoryAdapterTile { @Override public boolean canExtractItem(int slotIndex, ItemStack itemstack, EnumFacing side) { - return slotIndex == SLOT_OUTPUT; + return slotIndex == SLOT_OUTPUT_EMPTY_CONTAINER || slotIndex == SLOT_OUTPUT_FULL_CONTAINER; } } diff --git a/src/main/java/forestry/factory/recipes/BottlerRecipe.java b/src/main/java/forestry/factory/recipes/BottlerRecipe.java index 4de21dd1e..69c5fad35 100644 --- a/src/main/java/forestry/factory/recipes/BottlerRecipe.java +++ b/src/main/java/forestry/factory/recipes/BottlerRecipe.java @@ -18,7 +18,28 @@ import net.minecraftforge.fluids.FluidUtil; import net.minecraftforge.fluids.capability.IFluidHandler; public class BottlerRecipe { - public static BottlerRecipe create(Fluid res, ItemStack empty) { + + public static BottlerRecipe createFilled(ItemStack filled) { + if (filled == null) { + return null; + } + + ItemStack empty = filled.copy(); + empty.stackSize = 1; + IFluidHandler fluidHandler = FluidUtil.getFluidHandler(empty); + if (fluidHandler == null) { + return null; + } + + FluidStack fill = fluidHandler.drain(Integer.MAX_VALUE, true); + if (fill != null && fill.amount > 0) { + return new BottlerRecipe(empty, fill, filled, false); + } + + return null; + } + + public static BottlerRecipe createEmpty(Fluid res, ItemStack empty) { if (res == null || empty == null) { return null; } @@ -33,7 +54,7 @@ public class BottlerRecipe { int fillAmount = fluidHandler.fill(new FluidStack(res, Integer.MAX_VALUE), true); if (fillAmount > 0) { - return new BottlerRecipe(empty, new FluidStack(res, fillAmount), filled); + return new BottlerRecipe(empty, new FluidStack(res, fillAmount), filled, true); } return null; @@ -42,14 +63,20 @@ public class BottlerRecipe { public final FluidStack input; public final ItemStack empty; public final ItemStack filled; + public final boolean fillRecipe; - private BottlerRecipe(ItemStack empty, FluidStack input, ItemStack filled) { + private BottlerRecipe(ItemStack empty, FluidStack input, ItemStack filled, boolean fillRecipe) { this.input = input; this.empty = empty; this.filled = filled; + this.fillRecipe = fillRecipe; } - public boolean matches(ItemStack emptyCan, FluidStack resource) { - return emptyCan != null && resource != null && emptyCan.isItemEqual(empty) && resource.containsFluid(input); + public boolean matcheEmpty(ItemStack emptyCan, FluidStack resource) { + return emptyCan != null && resource != null && emptyCan.isItemEqual(empty) && resource.containsFluid(input) && fillRecipe; + } + + public boolean matcheFilled(ItemStack filledCan) { + return filled != null && !fillRecipe && filled.isItemEqual(filledCan); } } diff --git a/src/main/java/forestry/factory/recipes/jei/bottler/BottlerRecipeMaker.java b/src/main/java/forestry/factory/recipes/jei/bottler/BottlerRecipeMaker.java index 4aa92a2b8..05d055257 100644 --- a/src/main/java/forestry/factory/recipes/jei/bottler/BottlerRecipeMaker.java +++ b/src/main/java/forestry/factory/recipes/jei/bottler/BottlerRecipeMaker.java @@ -19,7 +19,7 @@ public class BottlerRecipeMaker { for (FluidContainerData container : FluidContainerRegistry.getRegisteredFluidContainerData()) { FluidStack fluid = container.fluid; if (fluid != null) { - BottlerRecipe recipe = BottlerRecipe.create(fluid.getFluid(), container.emptyContainer); + BottlerRecipe recipe = BottlerRecipe.createEmpty(fluid.getFluid(), container.emptyContainer); if (recipe != null) { recipes.add(new BottlerRecipeWrapper(recipe)); } diff --git a/src/main/java/forestry/factory/tiles/TileBottler.java b/src/main/java/forestry/factory/tiles/TileBottler.java index 94ba02413..10f00fb0e 100644 --- a/src/main/java/forestry/factory/tiles/TileBottler.java +++ b/src/main/java/forestry/factory/tiles/TileBottler.java @@ -13,14 +13,17 @@ package forestry.factory.tiles; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.IOException; +import java.util.EnumMap; import forestry.api.core.IErrorLogic; import forestry.core.config.Constants; import forestry.core.errors.EnumErrorCode; import forestry.core.fluids.FluidHelper; import forestry.core.fluids.TankManager; +import forestry.core.fluids.FluidHelper.FillStatus; import forestry.core.fluids.tanks.StandardTank; import forestry.core.inventory.IInventoryAdapter; +import forestry.core.inventory.watchers.ISlotPickupWatcher; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.render.TankRenderInfo; @@ -35,19 +38,31 @@ import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidUtil; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandler; +import net.minecraftforge.fluids.capability.IFluidTankProperties; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; -public class TileBottler extends TilePowered implements ISidedInventory, ILiquidTankTile { +public class TileBottler extends TilePowered implements ISidedInventory, ILiquidTankTile, ISlotPickupWatcher { private static final int TICKS_PER_RECIPE_TIME = 5; private static final int ENERGY_PER_RECIPE_TIME = 1000; private final StandardTank resourceTank; private final TankManager tankManager; + private EnumMap canDump; + private boolean dumpingFluid = false; + private BottlerRecipe currentRecipe; + @SideOnly(Side.CLIENT) + public boolean isFillRecipe; public TileBottler() { super("bottler", 1100, 4000); @@ -56,6 +71,8 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid resourceTank = new StandardTank(Constants.PROCESSOR_TANK_CAPACITY); tankManager = new TankManager(this, resourceTank); + + canDump = new EnumMap(EnumFacing.class); } /* SAVING & LOADING */ @@ -71,7 +88,8 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); tankManager.readFromNBT(nbttagcompound); - checkRecipe(); + checkEmptyRecipe(); + checkFillRecipe(); } @Override @@ -91,43 +109,169 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid super.updateServerSide(); if (updateOnInterval(20)) { - FluidHelper.drainContainers(tankManager, this, InventoryBottler.SLOT_INPUT_FULL_CAN); + boolean movedStack = false; + ItemStack leftProcessingStack = getStackInSlot(InventoryBottler.SLOT_LEFT_PROCESSING); + ItemStack rightProcessingStack = getStackInSlot(InventoryBottler.SLOT_RIGHT_PROCESSING); + if(leftProcessingStack == null){ + ItemStack inputStack = getStackInSlot(InventoryBottler.SLOT_INPUT_FULL_CONTAINER); + if(inputStack != null){ + leftProcessingStack = inputStack.copy(); + leftProcessingStack.stackSize = 1; + setInventorySlotContents(InventoryBottler.SLOT_LEFT_PROCESSING, leftProcessingStack); + decrStackSize(InventoryBottler.SLOT_INPUT_FULL_CONTAINER, 1); + movedStack = true; + } + } + if(rightProcessingStack == null){ + ItemStack inputStack = getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CONTAINER); + if(inputStack != null){ + rightProcessingStack = inputStack.copy(); + rightProcessingStack.stackSize = 1; + setInventorySlotContents(InventoryBottler.SLOT_RIGHT_PROCESSING, rightProcessingStack); + decrStackSize(InventoryBottler.SLOT_INPUT_EMPTY_CONTAINER, 1); + movedStack = true; + } + } } + + if (canDump.isEmpty()) { + for(EnumFacing facing : EnumFacing.VALUES){ + canDump.put(facing, FluidHelper.canAcceptFluid(worldObj, pos.offset(facing), facing.getOpposite(), tankManager.getFluid(0))); + } + } + + if (canDump()) { + if (dumpingFluid || updateOnInterval(20)) { + dumpingFluid = dumpFluid(); + } + } + } + + private boolean canDump() { + for(EnumFacing facing : EnumFacing.VALUES){ + if(canDump.get(facing)){ + return true; + } + } + return false; + } + + private boolean dumpFluid() { + if (!resourceTank.isEmpty()) { + for(EnumFacing facing : EnumFacing.VALUES){ + if(canDump.get(facing)){ + IFluidHandler fluidDestination = FluidUtil.getFluidHandler(worldObj, pos.offset(facing), facing.getOpposite()); + if (fluidDestination != null) { + if (FluidUtil.tryFluidTransfer(fluidDestination, tankManager, Fluid.BUCKET_VOLUME / 20, true) != null) { + return true; + } + } + } + } + } + return false; } @Override public boolean workCycle() { - FluidHelper.FillStatus status = FluidHelper.fillContainers(tankManager, this, InventoryBottler.SLOT_INPUT_EMPTY_CAN, InventoryBottler.SLOT_OUTPUT, currentRecipe.input.getFluid(), true); - return status == FluidHelper.FillStatus.SUCCESS; - } - - private void checkRecipe() { - ItemStack emptyCan = getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN); - FluidStack resource = resourceTank.getFluid(); - if (resource == null) { - return; + FluidHelper.FillStatus status; + if(currentRecipe.fillRecipe){ + status = FluidHelper.fillContainers(tankManager, this, InventoryBottler.SLOT_RIGHT_PROCESSING, InventoryBottler.SLOT_OUTPUT_FULL_CONTAINER, currentRecipe.input.getFluid(), true); + }else{ + status = FluidHelper.drainContainers(tankManager, this, InventoryBottler.SLOT_LEFT_PROCESSING, InventoryBottler.SLOT_OUTPUT_EMPTY_CONTAINER); } + if(status == FluidHelper.FillStatus.SUCCESS){ + currentRecipe = null; + return true; + } + return false; + } + + @Override + public void onNeighborTileChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) { + super.onNeighborTileChange(world, pos, neighbor); - if (currentRecipe == null || !currentRecipe.matches(emptyCan, resource)) { - currentRecipe = BottlerRecipe.create(resource.getFluid(), emptyCan); - if (currentRecipe != null) { - float viscosityMultiplier = resource.getFluid().getViscosity(resource) / 1000.0f; - viscosityMultiplier = (viscosityMultiplier - 1f) / 20f + 1f; // scale down the effect - - int fillAmount = Math.min(currentRecipe.input.amount, resource.amount); - float fillTime = fillAmount / (float) Fluid.BUCKET_VOLUME; - fillTime *= viscosityMultiplier; - - setTicksPerWorkCycle(Math.round(fillTime * TICKS_PER_RECIPE_TIME)); - setEnergyPerWorkCycle(Math.round(fillTime * ENERGY_PER_RECIPE_TIME)); + for(EnumFacing facing : EnumFacing.VALUES){ + canDump.put(facing, FluidHelper.canAcceptFluid(worldObj, pos.offset(facing), facing.getOpposite(), tankManager.getFluid(0))); + } + } + + private void checkFillRecipe(){ + ItemStack emptyCan = getStackInSlot(InventoryBottler.SLOT_RIGHT_PROCESSING); + if(emptyCan != null){ + FluidStack resource = resourceTank.getFluid(); + if (resource == null) { + return; } + //Fill Container + if (currentRecipe == null || !currentRecipe.matcheEmpty(emptyCan, resource) ) { + currentRecipe = BottlerRecipe.createEmpty(resource.getFluid(), emptyCan); + if (currentRecipe != null) { + float viscosityMultiplier = resource.getFluid().getViscosity(resource) / 1000.0f; + viscosityMultiplier = (viscosityMultiplier - 1f) / 20f + 1f; // scale down the effect + + int fillAmount = Math.min(currentRecipe.input.amount, resource.amount); + float fillTime = fillAmount / (float) Fluid.BUCKET_VOLUME; + fillTime *= viscosityMultiplier; + + setTicksPerWorkCycle(Math.round(fillTime * TICKS_PER_RECIPE_TIME)); + setEnergyPerWorkCycle(Math.round(fillTime * ENERGY_PER_RECIPE_TIME)); + } + } + } + } + + private void checkEmptyRecipe(){ + ItemStack filledCan = getStackInSlot(InventoryBottler.SLOT_LEFT_PROCESSING); + if(filledCan != null){ + //Empty Container + if (currentRecipe == null || !currentRecipe.matcheFilled(filledCan) && !currentRecipe.fillRecipe) { + currentRecipe = BottlerRecipe.createFilled(filledCan); + if (currentRecipe != null) { + FluidStack resource = currentRecipe.input; + float viscosityMultiplier = resource.getFluid().getViscosity(resource) / 1000.0f; + viscosityMultiplier = (viscosityMultiplier - 1f) / 20f + 1f; // scale down the effect + + int fillAmount = Math.min(currentRecipe.input.amount, resource.amount); + float fillTime = fillAmount / (float) Fluid.BUCKET_VOLUME; + fillTime *= viscosityMultiplier; + + setTicksPerWorkCycle(Math.round(fillTime * TICKS_PER_RECIPE_TIME)); + setEnergyPerWorkCycle(0); + } + } + } + } + + @Override + public void onPickupFromSlot(int slotIndex, EntityPlayer player) { + if(slotIndex == InventoryBottler.SLOT_LEFT_PROCESSING){ + setTicksPerWorkCycle(0); + }else if(slotIndex == InventoryBottler.SLOT_RIGHT_PROCESSING){ + setTicksPerWorkCycle(0); + } + } + + @Override + public void readGuiData(DataInputStreamForestry data) throws IOException { + super.readGuiData(data); + isFillRecipe = data.readBoolean(); + } + + @Override + public void writeGuiData(DataOutputStreamForestry data) throws IOException { + super.writeGuiData(data); + if(currentRecipe == null){ + data.writeBoolean(false); + }else{ + data.writeBoolean(currentRecipe.fillRecipe); } } @Override public boolean hasResourcesMin(float percentage) { IInventoryAdapter inventory = getInternalInventory(); - ItemStack emptyCan = inventory.getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN); + ItemStack emptyCan = inventory.getStackInSlot(InventoryBottler.SLOT_RIGHT_PROCESSING); if (emptyCan == null) { return false; } @@ -137,21 +281,48 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid @Override public boolean hasWork() { - checkRecipe(); - + FluidHelper.FillStatus emptyStatus; + FluidHelper.FillStatus fillStatus; IErrorLogic errorLogic = getErrorLogic(); - - FluidHelper.FillStatus status; - - if (currentRecipe == null) { - status = FluidHelper.FillStatus.NO_FLUID; - } else { - status = FluidHelper.fillContainers(tankManager, this, InventoryBottler.SLOT_INPUT_EMPTY_CAN, InventoryBottler.SLOT_OUTPUT, currentRecipe.input.getFluid(), false); + errorLogic.clearErrors(); + + checkEmptyRecipe(); + if (currentRecipe != null) { + IFluidTankProperties properties = tankManager.getTankProperties()[0]; + if(properties != null){ + if(properties.getContents() != null && properties.getContents().amount >= properties.getCapacity()){ + emptyStatus = FillStatus.NO_SPACE_FLUID; + }else{ + emptyStatus = FillStatus.SUCCESS; + } + }else{ + emptyStatus = FillStatus.SUCCESS; + } + }else{ + emptyStatus = null; + } + if(emptyStatus == null || emptyStatus != FillStatus.SUCCESS){ + checkFillRecipe(); + if (currentRecipe == null) { + fillStatus = FluidHelper.FillStatus.NO_FLUID; + }else{ + fillStatus = FluidHelper.fillContainers(tankManager, this, InventoryBottler.SLOT_RIGHT_PROCESSING, InventoryBottler.SLOT_OUTPUT_FULL_CONTAINER, currentRecipe.input.getFluid(), false); + } + }else{ + return true; + } + + if(fillStatus == FillStatus.SUCCESS){ + return true; } - errorLogic.setCondition(status == FluidHelper.FillStatus.NO_FLUID, EnumErrorCode.NO_RESOURCE_LIQUID); - errorLogic.setCondition(status == FluidHelper.FillStatus.NO_SPACE, EnumErrorCode.NO_SPACE_INVENTORY); - return status == FluidHelper.FillStatus.SUCCESS; + errorLogic.setCondition(fillStatus == FluidHelper.FillStatus.NO_FLUID, EnumErrorCode.NO_RESOURCE_LIQUID); + errorLogic.setCondition(fillStatus == FluidHelper.FillStatus.NO_SPACE, EnumErrorCode.NO_SPACE_INVENTORY); + errorLogic.setCondition(emptyStatus == FluidHelper.FillStatus.NO_SPACE_FLUID, EnumErrorCode.NO_SPACE_TANK); + if(emptyStatus == FillStatus.INVALID_INPUT || fillStatus == FillStatus.INVALID_INPUT || errorLogic.hasErrors()){ + return false; + } + return true; } @Override diff --git a/src/main/resources/assets/forestry/textures/gui/bottler.png b/src/main/resources/assets/forestry/textures/gui/bottler.png index 0e5a4493c..64495434c 100755 Binary files a/src/main/resources/assets/forestry/textures/gui/bottler.png and b/src/main/resources/assets/forestry/textures/gui/bottler.png differ