Fix #929 Bottler doesn't work with some mod Fluid Containers

This commit is contained in:
mezz committed 2015-10-27 23:38:50 -07:00
1 parent 784bdba4fb
commit 8824585282
6 files changed
+212 -226

No files matched your search

@@ -97,38 +97,75 @@ public final class FluidHelper {
return false;
}
public static boolean fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill) {
public enum FillStatus {
SUCCESS, INVALID_INPUT, NO_FLUID, NO_SPACE
}
public static FillStatus fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill) {
return fillContainers(fluidHandler, inv, inputSlot, outputSlot, fluidToFill, true);
}
public static boolean fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill, boolean doFill) {
public static FillStatus fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill, boolean doFill) {
if (fluidToFill == null) {
return FillStatus.INVALID_INPUT;
}
ItemStack input = inv.getStackInSlot(inputSlot);
ItemStack output = inv.getStackInSlot(outputSlot);
ItemStack filled = getFilledContainer(fluidToFill, input);
if (filled != null && (output == null || (output.stackSize < output.getMaxStackSize() && InventoryUtil.isItemEqual(filled, output)))) {
FluidStack fluidInContainer = getFluidStackInContainer(filled);
FluidStack drain = fluidHandler.drain(ForgeDirection.UNKNOWN, fluidInContainer, false);
if (drain != null && drain.amount == fluidInContainer.amount) {
if (doFill) {
fluidHandler.drain(ForgeDirection.UNKNOWN, fluidInContainer, true);
if (output == null) {
inv.setInventorySlotContents(outputSlot, filled);
} else {
output.stackSize++;
}
inv.decrStackSize(inputSlot, 1);
}
return true;
int containerCapacity = getFluidCapacity(fluidToFill, input);
if (containerCapacity <= 0) {
return FillStatus.INVALID_INPUT;
}
int fillAmount = containerCapacity;
if (input.getItem() instanceof IFluidContainerItem) {
fillAmount = Math.min(Constants.BUCKET_VOLUME, containerCapacity);
}
FluidStack canDrain = fluidHandler.drain(ForgeDirection.UNKNOWN, new FluidStack(fluidToFill, fillAmount), false);
if (canDrain == null || canDrain.amount == 0) {
return FillStatus.NO_FLUID;
}
ItemStack filled = getFilledContainer(canDrain, input);
if (filled == null) {
return FillStatus.NO_FLUID; // standard containers will not fill if there isn't enough fluid
}
boolean moveToOutput = (getFluidStackInContainer(filled).amount >= containerCapacity);
if (moveToOutput) {
if ((output != null) && (output.stackSize >= output.getMaxStackSize() || !InventoryUtil.isItemEqual(filled, output))) {
return FillStatus.NO_SPACE;
}
} else {
if (input.stackSize > 1) {
return FillStatus.NO_SPACE;
}
}
return false;
if (doFill) {
fluidHandler.drain(ForgeDirection.UNKNOWN, canDrain, true);
if (moveToOutput) {
if (output == null) {
inv.setInventorySlotContents(outputSlot, filled);
} else {
output.stackSize++;
}
inv.decrStackSize(inputSlot, 1);
} else {
inv.setInventorySlotContents(inputSlot, filled);
}
}
return FillStatus.SUCCESS;
}
public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int slot) {
return drainContainers(fluidHandler, inv, slot, slot, Constants.BUCKET_VOLUME);
return drainContainers(fluidHandler, inv, slot, slot);
}
public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, int maxAmount) {
public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot) {
ItemStack input = inv.getStackInSlot(inputSlot);
ItemStack output = inv.getStackInSlot(outputSlot);
@@ -140,8 +177,11 @@ public final class FluidHelper {
if (fluidInContainer == null) {
return false;
}
if (fluidInContainer.amount > maxAmount) {
fluidInContainer.amount = maxAmount;
if (input.getItem() instanceof IFluidContainerItem) {
if (fluidInContainer.amount > Constants.BUCKET_VOLUME) {
fluidInContainer.amount = Constants.BUCKET_VOLUME;
}
}
int used = fluidHandler.fill(ForgeDirection.UNKNOWN, fluidInContainer, false);
@@ -198,11 +238,6 @@ public final class FluidHelper {
}
public static boolean isEmptyContainer(ItemStack empty) {
if (FluidContainerRegistry.isEmptyContainer(empty)) {
return true;
}
Item item = empty.getItem();
if (item instanceof IFluidContainerItem) {
IFluidContainerItem containerItem = (IFluidContainerItem) item;
@@ -210,7 +245,17 @@ public final class FluidHelper {
return fluid == null || fluid.amount == 0;
}
return false;
return FluidContainerRegistry.isEmptyContainer(empty);
}
public static boolean isFillableContainer(ItemStack container) {
FluidStack fluid = getFluidStackInContainer(container);
if (fluid == null || fluid.amount == 0) {
return isEmptyContainer(container);
}
int capacity = getFluidCapacity(fluid.getFluid(), container);
return fluid.amount < capacity;
}
public static ItemStack getDrainedContainer(ItemStack container, int drainAmount) {
@@ -328,4 +373,18 @@ public final class FluidHelper {
return containsFluidStack(stack, new FluidStack(fluid, 1));
}
public static int getFluidCapacity(Fluid fluid, ItemStack container) {
Item item = container.getItem();
if (item instanceof IFluidContainerItem) {
IFluidContainerItem containerItem = (IFluidContainerItem) item;
return containerItem.getCapacity(container);
}
if (fluid == null) {
return FluidContainerRegistry.getContainerCapacity(container);
} else {
return FluidContainerRegistry.getContainerCapacity(new FluidStack(fluid, Constants.BUCKET_VOLUME), container);
}
}
}
@@ -19,6 +19,8 @@ import forestry.factory.tiles.TileBottler;
public class GuiBottler extends GuiForestryTitled<ContainerBottler, TileBottler> {
private static final int PROGRESS_SIZE = 24;
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));
@@ -29,7 +31,7 @@ public class GuiBottler extends GuiForestryTitled<ContainerBottler, TileBottler>
super.drawGuiContainerBackgroundLayer(var1, mouseX, mouseY);
TileBottler bottler = inventory;
int i1 = bottler.getFillProgressScaled(24);
drawTexturedModalRect(guiLeft + 80, guiTop + 39, 176, 74, i1, 16);
int i1 = bottler.getProgressScaled(PROGRESS_SIZE);
drawTexturedModalRect(guiLeft + 80, guiTop + 39, 176, 74, PROGRESS_SIZE - i1, 16);
}
}
@@ -12,6 +12,8 @@ package forestry.factory.recipes.craftguide;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import forestry.core.config.Constants;
import forestry.core.config.ForestryBlock;
import forestry.factory.tiles.TileBottler;
@@ -45,14 +47,17 @@ public class CraftGuideBottler implements RecipeProvider {
ItemStack machine = ForestryBlock.factoryTESR.getItemStack(1, Constants.DEFINITION_BOTTLER_META);
RecipeTemplate template = generator.createRecipeTemplate(slots, machine);
for (TileBottler.BottlerRecipe recipe : TileBottler.RecipeManager.recipes()) {
Object[] array = new Object[4];
array[0] = recipe.can;
array[1] = recipe.input;
array[2] = recipe.bottled;
array[3] = machine;
generator.addRecipe(template, array);
for (FluidContainerRegistry.FluidContainerData container : FluidContainerRegistry.getRegisteredFluidContainerData()) {
TileBottler.BottlerRecipe recipe = TileBottler.BottlerRecipe.getRecipe(container.fluid, container.emptyContainer);
if (recipe != null) {
Object[] array = new Object[4];
array[0] = recipe.empty;
array[1] = recipe.input;
array[2] = recipe.filled;
array[3] = machine;
generator.addRecipe(template, array);
}
}
}
}
@@ -40,7 +40,7 @@ public class NEIHandlerBottler extends RecipeHandlerBase {
@Override
public void prepare() {
for (FluidContainerData container : FluidContainerRegistry.getRegisteredFluidContainerData()) {
TileBottler.BottlerRecipe recipe = TileBottler.RecipeManager.findMatchingRecipe(container.fluid, container.emptyContainer);
TileBottler.BottlerRecipe recipe = TileBottler.BottlerRecipe.getRecipe(container.fluid, container.emptyContainer);
if (recipe != null) {
recipes.add(recipe);
}
@@ -57,11 +57,11 @@ public class NEIHandlerBottler extends RecipeHandlerBase {
if (recipe.input != null) {
this.fluid = new PositionedFluidTank(recipe.input, 10000, new Rectangle(48, 6, 16, 58), NEIHandlerBottler.this.getGuiTexture(), new Point(176, 0));
}
if (recipe.can != null) {
this.input = new PositionedStack(recipe.can, 111, 8);
if (recipe.empty != null) {
this.input = new PositionedStack(recipe.empty, 111, 8);
}
if (recipe.bottled != null) {
this.output = new PositionedStack(recipe.bottled, 111, 44);
if (recipe.filled != null) {
this.output = new PositionedStack(recipe.filled, 111, 44);
}
}
@@ -128,7 +128,7 @@ public class NEIHandlerBottler extends RecipeHandlerBase {
@Override
public void loadCraftingRecipes(ItemStack result) {
for (TileBottler.BottlerRecipe recipe : recipes) {
if (NEIServerUtils.areStacksSameTypeCrafting(recipe.bottled, result)) {
if (NEIServerUtils.areStacksSameTypeCrafting(recipe.filled, result)) {
this.arecipes.add(new CachedBottlerRecipe(recipe));
}
}
@@ -137,7 +137,7 @@ public class NEIHandlerBottler extends RecipeHandlerBase {
@Override
public void loadUsageRecipes(ItemStack ingred) {
for (TileBottler.BottlerRecipe recipe : recipes) {
if (NEIServerUtils.areStacksSameTypeCrafting(recipe.can, ingred)) {
if (NEIServerUtils.areStacksSameTypeCrafting(recipe.empty, ingred)) {
this.arecipes.add(new CachedBottlerRecipe(recipe));
}
}
@@ -10,14 +10,11 @@
******************************************************************************/
package forestry.factory.tiles;
import com.google.common.base.Objects;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@@ -29,14 +26,15 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import cpw.mods.fml.common.Optional;
import forestry.api.core.ForestryAPI;
import forestry.api.core.IErrorLogic;
import forestry.core.config.Config;
import forestry.core.config.Constants;
import forestry.core.errors.EnumErrorCode;
@@ -51,100 +49,22 @@ import forestry.core.network.GuiId;
import forestry.core.render.TankRenderInfo;
import forestry.core.tiles.ILiquidTankTile;
import forestry.core.tiles.TilePowered;
import forestry.core.utils.InventoryUtil;
import forestry.factory.triggers.FactoryTriggers;
import buildcraft.api.statements.ITriggerExternal;
public class TileBottler extends TilePowered implements ISidedInventory, ILiquidTankTile {
private static final int TICKS_PER_RECIPE_TIME = 10;
private static final int ENERGY_PER_RECIPE_TIME = 750;
/* CONSTANTS */
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;
private static final short CYCLES_FILLING_DEFAULT = 5;
public static class BottlerRecipe {
public final int cyclesPerUnit;
public final FluidStack input;
public final ItemStack can;
public final ItemStack bottled;
public BottlerRecipe(int cyclesPerUnit, FluidStack input, ItemStack can, ItemStack bottled) {
this.cyclesPerUnit = cyclesPerUnit;
this.input = input;
this.can = can;
this.bottled = bottled;
}
public boolean matches(FluidStack res, ItemStack empty) {
return res != null && res.containsFluid(input) && can.isItemEqual(empty);
}
}
public static class RecipeManager {
private static final Set<BottlerRecipe> recipes = new HashSet<>();
/**
* @return Recipe matching both res and empty, null if none
*/
public static BottlerRecipe findMatchingRecipe(FluidStack res, ItemStack empty) {
// We need both ingredients
if (res == null || empty == null || !FluidHelper.isEmptyContainer(empty)) {
return null;
}
for (BottlerRecipe recipe : recipes) {
if (recipe.matches(res, empty)) {
return recipe;
}
}
// No recipe matched. See if the liquid dictionary has anything.
ItemStack filled = FluidHelper.getFilledContainer(res, empty);
if (filled != null) {
BottlerRecipe recipe = new BottlerRecipe(CYCLES_FILLING_DEFAULT, res, empty, filled);
recipes.add(recipe);
return recipe;
}
return null;
}
/**
* @return true if any recipe has a matching input
*/
public static boolean isInput(FluidStack res) {
if (res == null) {
return false;
}
return FluidRegistry.isFluidRegistered(res.getFluid());
}
public static Set<BottlerRecipe> recipes() {
return Collections.unmodifiableSet(recipes);
}
public static Map<Object[], Object[]> getRecipes() {
HashMap<Object[], Object[]> recipeList = new HashMap<>();
for (BottlerRecipe recipe : recipes) {
recipeList.put(new Object[]{recipe.input, recipe.can}, new Object[]{recipe.bottled});
}
return recipeList;
}
}
private final StandardTank resourceTank;
private final TankManager tankManager;
private BottlerRecipe currentRecipe;
private int fillingTime;
private int fillingTotalTime;
public TileBottler() {
super(1100, 4000, 200);
@@ -165,22 +85,13 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
nbttagcompound.setInteger("FillingTime", fillingTime);
nbttagcompound.setInteger("FillingTotalTime", fillingTotalTime);
tankManager.writeTanksToNBT(nbttagcompound);
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
fillingTime = nbttagcompound.getInteger("FillingTime");
fillingTotalTime = nbttagcompound.getInteger("FillingTotalTime");
tankManager.readTanksFromNBT(nbttagcompound);
checkRecipe();
}
@@ -207,67 +118,34 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
if (getStackInSlot(SLOT_INPUT_FULL_CAN) != null) {
FluidHelper.drainContainers(tankManager, this, SLOT_INPUT_FULL_CAN);
}
checkRecipe();
}
@Override
public boolean workCycle() {
checkRecipe();
// Continue work if nothing needs to be added
if (fillingTime <= 0) {
return false;
}
if (currentRecipe == null) {
return false;
}
boolean added = InventoryUtil.tryAddStack(this, currentRecipe.bottled, SLOT_OUTPUT, 1, true, false);
if (getErrorLogic().setCondition(!added, EnumErrorCode.NOSPACE)) {
return false;
}
fillingTime--;
if (fillingTime > 0) {
return true;
}
FluidHelper.fillContainers(tankManager, this, SLOT_INPUT_EMPTY_CAN, SLOT_OUTPUT, currentRecipe.input.getFluid());
checkRecipe();
resetRecipe();
return true;
FluidHelper.FillStatus status = FluidHelper.fillContainers(tankManager, this, SLOT_INPUT_EMPTY_CAN, SLOT_OUTPUT, currentRecipe.input.getFluid());
return status == FluidHelper.FillStatus.SUCCESS;
}
private void checkRecipe() {
ItemStack emptyCan = getStackInSlot(SLOT_INPUT_EMPTY_CAN);
BottlerRecipe recipe = RecipeManager.findMatchingRecipe(resourceTank.getFluid(), emptyCan);
FluidStack resource = resourceTank.getFluid();
if (currentRecipe != recipe) {
currentRecipe = recipe;
resetRecipe();
if (currentRecipe == null || !currentRecipe.matches(emptyCan, resource)) {
currentRecipe = BottlerRecipe.getRecipe(resource, 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) Constants.BUCKET_VOLUME;
fillTime *= viscosityMultiplier;
setTicksPerWorkCycle(Math.round(fillTime * TICKS_PER_RECIPE_TIME));
setEnergyPerWorkCycle(Math.round(fillTime * ENERGY_PER_RECIPE_TIME));
}
}
getErrorLogic().setCondition(currentRecipe == null, EnumErrorCode.NORECIPE);
}
private void resetRecipe() {
if (currentRecipe == null) {
fillingTime = 0;
fillingTotalTime = 0;
return;
}
fillingTime = currentRecipe.cyclesPerUnit;
fillingTotalTime = currentRecipe.cyclesPerUnit;
}
// / STATE INFORMATION
@Override
public boolean hasResourcesMin(float percentage) {
IInventoryAdapter inventory = getInternalInventory();
@@ -280,20 +158,21 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
@Override
public boolean hasWork() {
return currentRecipe != null;
}
checkRecipe();
public int getFillProgressScaled(int i) {
if (fillingTotalTime == 0) {
return 0;
IErrorLogic errorLogic = getErrorLogic();
FluidHelper.FillStatus status;
if (errorLogic.setCondition(currentRecipe == null, EnumErrorCode.NORECIPE)) {
status = FluidHelper.FillStatus.SUCCESS;
} else {
status = FluidHelper.fillContainers(tankManager, this, SLOT_INPUT_EMPTY_CAN, SLOT_OUTPUT, currentRecipe.input.getFluid(), false);
}
return ((fillingTotalTime - fillingTime) * i) / fillingTotalTime;
}
public int getResourceScaled(int i) {
return (resourceTank.getFluidAmount() * i) / Constants.PROCESSOR_TANK_CAPACITY;
errorLogic.setCondition(status == FluidHelper.FillStatus.NO_FLUID, EnumErrorCode.NORESOURCE);
errorLogic.setCondition(status == FluidHelper.FillStatus.NO_SPACE, EnumErrorCode.NOSPACE);
return currentRecipe != null && status == FluidHelper.FillStatus.SUCCESS;
}
@Override
@@ -304,22 +183,10 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
/* SMP GUI */
@Override
public void getGUINetworkData(int i, int j) {
i -= tankManager.maxMessageId() + 1;
switch (i) {
case 0:
fillingTime = j;
break;
case 1:
fillingTotalTime = j;
break;
}
}
@Override
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int i = tankManager.maxMessageId() + 1;
iCrafting.sendProgressBarUpdate(container, i, fillingTime);
iCrafting.sendProgressBarUpdate(container, i + 1, fillingTotalTime);
}
/* ILIQUIDCONTAINER */
@@ -376,10 +243,10 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
@Override
public boolean canSlotAccept(int slotIndex, ItemStack itemStack) {
if (slotIndex == SLOT_INPUT_EMPTY_CAN) {
return FluidContainerRegistry.isEmptyContainer(itemStack);
return FluidHelper.isFillableContainer(itemStack);
} else if (slotIndex == SLOT_INPUT_FULL_CAN) {
FluidStack fluidStack = FluidHelper.getFluidStackInContainer(itemStack);
return RecipeManager.isInput(fluidStack);
return fluidStack != null && FluidRegistry.isFluidRegistered(fluidStack.getFluid());
}
return false;
}
@@ -389,4 +256,60 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
return slotIndex == SLOT_OUTPUT;
}
}
public static class BottlerRecipe {
public final FluidStack input;
public final ItemStack empty;
public final ItemStack filled;
private BottlerRecipe(ItemStack empty, ItemStack filled) {
this.input = FluidHelper.getFluidStackInContainer(filled);
if (empty.getItem() instanceof IFluidContainerItem) {
FluidStack emptyFluid = FluidHelper.getFluidStackInContainer(empty);
if (emptyFluid != null) {
this.input.amount -= emptyFluid.amount;
}
if (this.input.amount > Constants.BUCKET_VOLUME) {
this.input.amount = Constants.BUCKET_VOLUME;
}
}
this.empty = empty;
this.filled = filled;
}
public boolean matches(ItemStack emptyCan, FluidStack resource) {
if (emptyCan == null || resource == null || !emptyCan.isItemEqual(empty)) {
return false;
}
if (empty.getItem() instanceof IFluidContainerItem) {
return true;
} else {
return resource.containsFluid(input);
}
}
public static BottlerRecipe getRecipe(FluidStack res, ItemStack empty) {
if (res == null || empty == null) {
return null;
}
ItemStack filled = FluidHelper.getFilledContainer(res.getFluid(), empty);
if (filled == null) {
return null;
}
return new BottlerRecipe(empty, filled);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.addValue(input.amount).addValue(input.getLocalizedName())
.add("empty", empty)
.add("filled", filled)
.toString();
}
}
}
@@ -143,11 +143,17 @@ public class TileRaintank extends TileBase implements ISidedInventory, ILiquidTa
}
if (!isFilling()) {
tryToStartFillling();
FluidHelper.FillStatus canFill = FluidHelper.fillContainers(tankManager, this, SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.getFluid(), false);
if (canFill == FluidHelper.FillStatus.SUCCESS) {
fillingTime = Constants.RAINTANK_FILLING_TIME;
}
} else {
fillingTime--;
if (fillingTime <= 0 && FluidHelper.fillContainers(tankManager, this, SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.getFluid())) {
fillingTime = 0;
if (fillingTime <= 0) {
FluidHelper.FillStatus filled = FluidHelper.fillContainers(tankManager, this, SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.getFluid());
if (filled == FluidHelper.FillStatus.SUCCESS) {
fillingTime = 0;
}
}
}
}
@@ -156,15 +162,6 @@ public class TileRaintank extends TileBase implements ISidedInventory, ILiquidTa
return fillingTime > 0;
}
private void tryToStartFillling() {
// Nothing to do if no empty cans are available
if (!FluidHelper.fillContainers(tankManager, getInternalInventory(), SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.getFluid(), false)) {
return;
}
fillingTime = Constants.RAINTANK_FILLING_TIME;
}
public int getFillProgressScaled(int i) {
return (fillingTime * i) / Constants.RAINTANK_FILLING_TIME;
}