From 33422087a37ff6fdad723a5fec434b0e5df8360d Mon Sep 17 00:00:00 2001 From: CovertJaguar Date: Sun, 14 Jul 2013 12:09:54 -0700 Subject: [PATCH] Clean up Iron Engine APIs --- common/buildcraft/BuildCraftEnergy.java | 6 +- common/buildcraft/api/core/StackWrapper.java | 50 +++++++++++ .../api/fuels/IronEngineCoolant.java | 89 +++++++++---------- .../buildcraft/api/fuels/IronEngineFuel.java | 47 +++++----- common/buildcraft/energy/TileEngineIron.java | 3 +- 5 files changed, 123 insertions(+), 72 deletions(-) create mode 100644 common/buildcraft/api/core/StackWrapper.java diff --git a/common/buildcraft/BuildCraftEnergy.java b/common/buildcraft/BuildCraftEnergy.java index dc212f53..cc2a4777 100644 --- a/common/buildcraft/BuildCraftEnergy.java +++ b/common/buildcraft/BuildCraftEnergy.java @@ -198,9 +198,9 @@ public class BuildCraftEnergy { RefineryRecipe.registerRefineryRecipe(new RefineryRecipe(FluidRegistry.getFluid("oil"), null, FluidRegistry.getFluid("fuel"), 12, 1)); // Iron Engine Fuels - IronEngineFuel.addFuel(FluidRegistry.getFluid("lava"), 1, 20000); - IronEngineFuel.addFuel(FluidRegistry.getFluid("oil"), 3, 20000); - IronEngineFuel.addFuel(FluidRegistry.getFluid("fuel"), 6, 100000); + IronEngineFuel.addFuel("lava", 1, 20000); + IronEngineFuel.addFuel("oil", 3, 20000); + IronEngineFuel.addFuel("fuel", 6, 100000); // Iron Engine Coolants IronEngineCoolant.addCoolant(FluidRegistry.getFluid("water"), 0.0023F); diff --git a/common/buildcraft/api/core/StackWrapper.java b/common/buildcraft/api/core/StackWrapper.java new file mode 100644 index 00000000..f46652c4 --- /dev/null +++ b/common/buildcraft/api/core/StackWrapper.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) SpaceToad, 2011-2012 + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package buildcraft.api.core; + +import net.minecraft.item.ItemStack; + +/** + * + * @author CovertJaguar + */ +public class StackWrapper { + + public final ItemStack stack; + + public StackWrapper(ItemStack stack) { + this.stack = stack; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 67 * hash + stack.itemID; + hash = 67 * hash + stack.getItemDamage(); + if (stack.stackTagCompound != null) + hash = 67 * hash + stack.stackTagCompound.hashCode(); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final StackWrapper other = (StackWrapper) obj; + if (stack.itemID != other.stack.itemID) + return false; + if (stack.getHasSubtypes() && stack.getItemDamage() != other.stack.getItemDamage()) + return false; + if (stack.stackTagCompound != null && !stack.stackTagCompound.equals(other.stack.stackTagCompound)) + return false; + return true; + } +} diff --git a/common/buildcraft/api/fuels/IronEngineCoolant.java b/common/buildcraft/api/fuels/IronEngineCoolant.java index b3e2f5bb..347fabd7 100644 --- a/common/buildcraft/api/fuels/IronEngineCoolant.java +++ b/common/buildcraft/api/fuels/IronEngineCoolant.java @@ -1,7 +1,13 @@ +/** + * Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public License + * 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ package buildcraft.api.fuels; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; +import buildcraft.api.core.StackWrapper; import java.util.HashMap; import java.util.Map; import net.minecraft.item.Item; @@ -11,19 +17,19 @@ import net.minecraftforge.fluids.FluidStack; public final class IronEngineCoolant { - public static BiMap liquidCoolants = HashBiMap.create(); - public static Map solidCoolants = new HashMap(); + public static Map liquidCoolants = new HashMap(); + public static Map solidCoolants = new HashMap(); public static FluidStack getFluidCoolant(ItemStack stack) { - return solidCoolants.get(new ItemData(stack.itemID, stack.getItemDamage())); + return solidCoolants.get(new StackWrapper(stack)); } public static Coolant getCoolant(ItemStack stack) { return getCoolant(getFluidCoolant(stack)); } - public static Coolant getCoolant(FluidStack liquid) { - return liquid != null ? liquidCoolants.get(liquid.getFluid().getName()) : null; + public static Coolant getCoolant(FluidStack fluidStack) { + return fluidStack != null ? liquidCoolants.get(fluidStack.getFluid().getName()) : null; } private IronEngineCoolant() { @@ -34,9 +40,9 @@ public final class IronEngineCoolant { float getDegreesCoolingPerMB(float currentHeat); } - public static void addCoolant(final Fluid liquid, final float degreesCoolingPerMB) { - if (liquid != null) { - liquidCoolants.put(liquid.getName(), new Coolant() { + public static void addCoolant(final Fluid fluid, final float degreesCoolingPerMB) { + if (fluid != null) { + liquidCoolants.put(fluid.getName(), new Coolant() { @Override public float getDegreesCoolingPerMB(float currentHeat) { return degreesCoolingPerMB; @@ -45,46 +51,33 @@ public final class IronEngineCoolant { } } + /** + * Adds a solid coolant like Ice Blocks. The FluidStack must contain a registered + * Coolant Fluid or nothing will happen. You do not need to call this for + * Fluid Containers. + * + * @param stack + * @param coolant + */ + public static void addCoolant(final ItemStack stack, final FluidStack coolant) { + if (stack != null && Item.itemsList[stack.itemID] != null && coolant != null) { + solidCoolants.put(new StackWrapper(stack), coolant); + } + } + + /** + * Adds a solid coolant like Ice Blocks. The FluidStack must contain a registered + * Coolant Fluid or nothing will happen. You do not need to call this for + * Fluid Containers. + * + * @param stack + * @param coolant + */ public static void addCoolant(final int itemId, final int metadata, final FluidStack coolant) { - if (Item.itemsList[itemId] != null && coolant != null) { - solidCoolants.put(new ItemData(itemId, metadata), coolant); - } + addCoolant(new ItemStack(itemId, 1, metadata), coolant); } - public static class ItemData { - - public final int itemId, meta; - - public ItemData(int itemId, int meta) { - this.itemId = itemId; - this.meta = meta; - } - - @Override - public int hashCode() { - int hash = 5; - hash = 67 * hash + this.itemId; - hash = 67 * hash + this.meta; - return hash; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final ItemData other = (ItemData) obj; - if (this.itemId != other.itemId) - return false; - if (this.meta != other.meta) - return false; - return true; - } + public static boolean isCoolant(Fluid fluid) { + return liquidCoolants.containsKey(fluid.getName()); } - - public static boolean isCoolant(Fluid fluid) - { - return liquidCoolants.inverse().containsKey(fluid); - } } diff --git a/common/buildcraft/api/fuels/IronEngineFuel.java b/common/buildcraft/api/fuels/IronEngineFuel.java index 5b51bd6f..3ab5ca2a 100644 --- a/common/buildcraft/api/fuels/IronEngineFuel.java +++ b/common/buildcraft/api/fuels/IronEngineFuel.java @@ -1,43 +1,50 @@ /** - * Copyright (c) SpaceToad, 2011 - * http://www.mod-buildcraft.com + * Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com * - * BuildCraft is distributed under the terms of the Minecraft Mod Public - * License 1.0, or MMPL. Please check the contents of the license located in + * BuildCraft is distributed under the terms of the Minecraft Mod Public License + * 1.0, or MMPL. Please check the contents of the license located in * http://www.mod-buildcraft.com/MMPL-1.0.txt */ - package buildcraft.api.fuels; -import com.google.common.collect.Maps; +import java.util.HashMap; import java.util.Map; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; public class IronEngineFuel { - public static Map fuels = Maps.newHashMap(); + public static Map fuels = new HashMap(); - public static IronEngineFuel getFuelForFluid(Fluid liquid) { + public static Fuel getFuelForFluid(Fluid liquid) { return liquid == null ? null : fuels.get(liquid.getName()); } - public final Fluid liquid; - public final float powerPerCycle; - public final int totalBurningTime; - - public IronEngineFuel(String fluidName, float powerPerCycle, int totalBurningTime) { - this(FluidRegistry.getFluid(fluidName), powerPerCycle, totalBurningTime); + private IronEngineFuel() { } - public IronEngineFuel(Fluid liquid, float powerPerCycle, int totalBurningTime) { - this.liquid = liquid; - this.powerPerCycle = powerPerCycle; - this.totalBurningTime = totalBurningTime; - fuels.put(liquid.getName(), this); + public static class Fuel { + + public final Fluid liquid; + public final float powerPerCycle; + public final int totalBurningTime; + + private Fuel(String fluidName, float powerPerCycle, int totalBurningTime) { + this(FluidRegistry.getFluid(fluidName), powerPerCycle, totalBurningTime); + } + + private Fuel(Fluid liquid, float powerPerCycle, int totalBurningTime) { + this.liquid = liquid; + this.powerPerCycle = powerPerCycle; + this.totalBurningTime = totalBurningTime; + } } public static void addFuel(Fluid fluid, float powerPerCycle, int totalBurningTime) { - new IronEngineFuel(fluid, powerPerCycle, totalBurningTime); + fuels.put(fluid.getName(), new Fuel(fluid, powerPerCycle, totalBurningTime)); + } + + public static void addFuel(String fluidName, float powerPerCycle, int totalBurningTime) { + fuels.put(fluidName, new Fuel(fluidName, powerPerCycle, totalBurningTime)); } } diff --git a/common/buildcraft/energy/TileEngineIron.java b/common/buildcraft/energy/TileEngineIron.java index 01680a17..085ca1f0 100644 --- a/common/buildcraft/energy/TileEngineIron.java +++ b/common/buildcraft/energy/TileEngineIron.java @@ -12,6 +12,7 @@ import buildcraft.BuildCraftEnergy; import buildcraft.api.fuels.IronEngineCoolant; import buildcraft.api.fuels.IronEngineCoolant.Coolant; import buildcraft.api.fuels.IronEngineFuel; +import buildcraft.api.fuels.IronEngineFuel.Fuel; import buildcraft.api.gates.ITrigger; import buildcraft.core.DefaultProps; import buildcraft.core.GuiIds; @@ -42,7 +43,7 @@ public class TileEngineIron extends TileEngine implements IFluidHandler { int burnTime = 0; private FluidTank fuelTank; private FluidTank coolantTank; - private IronEngineFuel currentFuel = null; + private Fuel currentFuel = null; public int penaltyCooling = 0; boolean lastPowered = false;