From aa68c3cd8154cefd632db5185e64688706a3f34a Mon Sep 17 00:00:00 2001 From: Prototik Date: Sun, 4 May 2014 17:09:03 +0800 Subject: [PATCH 1/2] Add MjAPILegacy for providing an old-style api for the new power api. Add Energy Converter block for the same things in game. --- api/buildcraft/api/mj/MjAPI.java | 104 +++++++----- api/buildcraft/api/mj/MjAPILegacy.java | 56 +++++++ api/buildcraft/api/power/PowerHandler.java | 156 ++++++++++-------- .../assets/buildcraft/lang/en_US.lang | 5 + .../textures/blocks/blockEnergyConverter.png | Bin 0 -> 206 bytes common/buildcraft/BuildCraftEnergy.java | 44 +++-- .../energy/BlockEnergyConverter.java | 57 +++++++ .../energy/ItemEnergyConverter.java | 25 +++ .../energy/TileEnergyConverter.java | 149 +++++++++++++++++ 9 files changed, 472 insertions(+), 124 deletions(-) create mode 100644 api/buildcraft/api/mj/MjAPILegacy.java create mode 100644 buildcraft_resources/assets/buildcraft/textures/blocks/blockEnergyConverter.png create mode 100644 common/buildcraft/energy/BlockEnergyConverter.java create mode 100644 common/buildcraft/energy/ItemEnergyConverter.java create mode 100644 common/buildcraft/energy/TileEnergyConverter.java diff --git a/api/buildcraft/api/mj/MjAPI.java b/api/buildcraft/api/mj/MjAPI.java index 1c190482..b9314d74 100755 --- a/api/buildcraft/api/mj/MjAPI.java +++ b/api/buildcraft/api/mj/MjAPI.java @@ -8,11 +8,13 @@ */ package buildcraft.api.mj; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import buildcraft.api.core.JavaTools; +import buildcraft.api.power.PowerHandler; public final class MjAPI { @@ -26,56 +28,43 @@ public final class MjAPI { public Field field; public MjBattery battery; public BatteryKind kind; + } - public double getEnergyRequested (Object obj) { + public static class BatteryObject { + Field f; + Object o; + MjBattery b; + + public double getEnergyRequested() { try { - double contained = field.getDouble(obj); + double contained = f.getDouble(o); - double left = contained + battery.maxReceivedPerCycle() > battery - .maxCapacity() ? battery.maxCapacity() - contained : battery + double left = contained + b.maxReceivedPerCycle() > b + .maxCapacity() ? b.maxCapacity() - contained : b .maxReceivedPerCycle(); if (left > 0) { return left; } else { - return battery.minimumConsumption(); + return b.minimumConsumption(); } - } catch (IllegalArgumentException e) { - e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return 0; } - } - - public static class BatteryObject { - BatteryField f; - Object o; - - public double getEnergyRequested () { - return f.getEnergyRequested(o); - } public double addEnergy(double watts) { try { - double e = f.field.getDouble(o); - double max = f.battery.maxCapacity(); + double e = f.getDouble(o); + double max = b.maxCapacity(); - double used = 0; + double used = e + watts <= max ? watts : max - e; - if (e + watts <= max) { - used = watts; - } else { - used = max - e; - } - - f.field.setDouble(o, e + used); + f.setDouble(o, e + used); return used; - } catch (IllegalArgumentException e) { - e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } @@ -85,23 +74,56 @@ public final class MjAPI { public double getEnergyStored() { try { - return f.field.getDouble(o); + return f.getDouble(o); } catch (IllegalAccessException e) { e.printStackTrace(); return 0; } } + public void setEnergyStored(double watts) { + try { + f.setDouble(o, watts); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + public double maxCapacity() { - return f.battery.maxCapacity(); + return b.maxCapacity(); } public double minimumConsumption() { - return f.battery.minimumConsumption(); + return b.minimumConsumption(); } public double maxReceivedPerCycle() { - return f.battery.maxReceivedPerCycle(); + return b.maxReceivedPerCycle(); + } + + public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) { + b = new MjBattery() { + @Override + public double maxCapacity() { + return maxCapacity; + } + + @Override + public double maxReceivedPerCycle() { + return maxReceivedPerCycle; + } + + @Override + public double minimumConsumption() { + return minimumConsumption; + } + + @Override + public Class annotationType() { + return MjBattery.class; + } + }; + return this; } } @@ -111,27 +133,29 @@ public final class MjAPI { private MjAPI() { } - public static BatteryObject getMjBattery (Object o) { + public static BatteryObject getMjBattery(Object o) { if (o == null) { return null; } - BatteryField f = getMjBattery (o.getClass()); + if (o.getClass() == PowerHandler.class) { + return ((PowerHandler) o).getMjBattery(); + } + + BatteryField f = getMjBattery(o.getClass()); if (f == null) { return null; } else if (f.kind == BatteryKind.Value) { BatteryObject obj = new BatteryObject(); obj.o = o; - obj.f = f; + obj.f = f.field; + obj.b = f.battery; return obj; } else { try { return getMjBattery(f.field.get(o)); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; @@ -139,10 +163,10 @@ public final class MjAPI { } } - private static BatteryField getMjBattery (Class c) { + private static BatteryField getMjBattery(Class c) { if (!MjBatteries.containsKey(c)) { for (Field f : JavaTools.getAllFields(c)) { - MjBattery battery = f.getAnnotation (MjBattery.class); + MjBattery battery = f.getAnnotation(MjBattery.class); if (battery != null) { f.setAccessible(true); diff --git a/api/buildcraft/api/mj/MjAPILegacy.java b/api/buildcraft/api/mj/MjAPILegacy.java new file mode 100644 index 00000000..e5a07e35 --- /dev/null +++ b/api/buildcraft/api/mj/MjAPILegacy.java @@ -0,0 +1,56 @@ +package buildcraft.api.mj; + +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +import buildcraft.api.power.IPowerReceptor; +import buildcraft.api.power.PowerHandler; + +public class MjAPILegacy implements IPowerReceptor { + private final PowerHandler powerHandler; + private final World world; + + protected MjAPILegacy(World world, MjAPI.BatteryObject battery, PowerHandler.Type type) { + if (battery == null) { + throw new NullPointerException(); + } + this.world = world; + this.powerHandler = new PowerHandler(this, type, battery); + } + + public static MjAPILegacy from(World world, MjAPI.BatteryObject battery, PowerHandler.Type type) { + return new MjAPILegacy(world, battery, type); + } + + public static MjAPILegacy from(World world, Object object, PowerHandler.Type type) { + return new MjAPILegacy(world, battery(object), type); + } + + public static MjAPILegacy from(TileEntity tileEntity, PowerHandler.Type type) { + return new MjAPILegacy(tileEntity.getWorldObj(), battery(tileEntity), type); + } + + private static MjAPI.BatteryObject battery(Object object) { + MjAPI.BatteryObject battery = MjAPI.getMjBattery(object); + if (battery == null) { + throw new IllegalArgumentException(String.format("Object %s not using MjAPI, can't create legacy wrapper", object)); + } + return battery; + } + + @Override + public PowerHandler.PowerReceiver getPowerReceiver(ForgeDirection side) { + return powerHandler.getPowerReceiver(); + } + + @Override + public void doWork(PowerHandler workProvider) { + + } + + @Override + public World getWorld() { + return world; + } +} diff --git a/api/buildcraft/api/power/PowerHandler.java b/api/buildcraft/api/power/PowerHandler.java index 971480dc..a3a076cc 100644 --- a/api/buildcraft/api/power/PowerHandler.java +++ b/api/buildcraft/api/power/PowerHandler.java @@ -9,22 +9,23 @@ package buildcraft.api.power; import net.minecraft.nbt.NBTTagCompound; - import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.core.SafeTimeTracker; +import buildcraft.api.mj.MjAPI; +import buildcraft.api.mj.MjBattery; /** * The PowerHandler is similar to FluidTank in that it holds your power and * allows standardized interaction between machines. - * + *

* To receive power to your machine you needs create an instance of PowerHandler * and implement IPowerReceptor on the TileEntity. - * + *

* If you plan emit power, you need only implement IPowerEmitter. You do not * need a PowerHandler. Engines have a PowerHandler because they can also * receive power from other Engines. - * + *

* See TileRefinery for a simple example of a power using machine. * * @see IPowerReceptor @@ -59,7 +60,7 @@ public final class PowerHandler { /** * Extend this class to create custom Perdition algorithms (its not final). - * + *

* NOTE: It is not possible to create a Zero perdition algorithm. */ public static class PerditionCalculator { @@ -91,8 +92,8 @@ public final class PowerHandler { * every tick. It is triggered by any manipulation of the stored energy. * * @param powerHandler the PowerHandler requesting the perdition update - * @param current the current stored energy - * @param ticksPassed ticks since the last time this function was called + * @param current the current stored energy + * @param ticksPassed ticks since the last time this function was called * @return */ public double applyPerdition(PowerHandler powerHandler, double current, long ticksPassed) { @@ -107,7 +108,7 @@ public final class PowerHandler { /** * Taxes a flat rate on all incoming power. - * + *

* Defaults to 0% tax rate. * * @return percent of input to tax @@ -116,34 +117,44 @@ public final class PowerHandler { return 0; } } + public static final PerditionCalculator DEFAULT_PERDITION = new PerditionCalculator(); public static final double ROLLING_AVERAGE_WEIGHT = 100.0; public static final double ROLLING_AVERAGE_NUMERATOR = ROLLING_AVERAGE_WEIGHT - 1; - public static final double ROLLING_AVERAGE_DENOMINATOR = 1.0 / ROLLING_AVERAGE_WEIGHT; + public static final double ROLLING_AVERAGE_DENOMINATOR = 1.0 / ROLLING_AVERAGE_WEIGHT; public final int[] powerSources = new int[6]; public final IPowerReceptor receptor; - private double minEnergyReceived; - private double maxEnergyReceived; - private double maxEnergyStored; private double activationEnergy; - private double energyStored = 0; private final SafeTimeTracker doWorkTracker = new SafeTimeTracker(); private final SafeTimeTracker sourcesTracker = new SafeTimeTracker(); private final SafeTimeTracker perditionTracker = new SafeTimeTracker(); private PerditionCalculator perdition; private final PowerReceiver receiver; private final Type type; + private MjAPI.BatteryObject battery; // Tracking private double averageLostPower = 0; private double averageReceivedPower = 0; private double averageUsedPower = 0; public PowerHandler(IPowerReceptor receptor, Type type) { + this(receptor, type, null); + } + + public PowerHandler(IPowerReceptor receptor, Type type, Object battery) { this.receptor = receptor; this.type = type; this.receiver = new PowerReceiver(); this.perdition = DEFAULT_PERDITION; + + if (battery instanceof MjAPI.BatteryObject) { + this.battery = (MjAPI.BatteryObject) battery; + } else if (battery != null) { + this.battery = MjAPI.getMjBattery(battery); + } else { + this.battery = MjAPI.getMjBattery(new AnonymousBattery()); + } } public PowerReceiver getPowerReceiver() { @@ -151,15 +162,15 @@ public final class PowerHandler { } public double getMinEnergyReceived() { - return minEnergyReceived; + return battery.minimumConsumption(); } public double getMaxEnergyReceived() { - return maxEnergyReceived; + return battery.getEnergyRequested(); } public double getMaxEnergyStored() { - return maxEnergyStored; + return battery.maxCapacity(); } public double getActivationEnergy() { @@ -167,43 +178,46 @@ public final class PowerHandler { } public double getEnergyStored() { - return energyStored; + return battery.getEnergyStored(); + } + + public MjAPI.BatteryObject getMjBattery() { + return battery; } /** * Setup your PowerHandler's settings. * - * @param minEnergyReceived This is the minimum about of power that will be - * accepted by the PowerHandler. This should generally be greater than the - * activationEnergy if you plan to use the doWork() callback. Anything - * greater than 1 will prevent Redstone Engines from powering this Provider. - * @param maxEnergyReceived The maximum amount of power accepted by the - * PowerHandler. This should generally be less than 500. Too low and larger - * engines will overheat while trying to power the machine. Too high, and - * the engines will never warm up. Greater values also place greater strain - * on the power net. - * @param activationEnergy If the stored energy is greater than this value, - * the doWork() callback is called (once per tick). - * @param maxStoredEnergy The maximum amount of power this PowerHandler can - * store. Values tend to range between 100 and 5000. With 1000 and 1500 - * being common. + * @param minEnergyReceived This is the minimum about of power that will be + * accepted by the PowerHandler. This should generally be greater than the + * activationEnergy if you plan to use the doWork() callback. Anything + * greater than 1 will prevent Redstone Engines from powering this Provider. + * @param maxEnergyReceivedI The maximum amount of power accepted by the + * PowerHandler. This should generally be less than 500. Too low and larger + * engines will overheat while trying to power the machine. Too high, and + * the engines will never warm up. Greater values also place greater strain + * on the power net. + * @param activationEnergy If the stored energy is greater than this value, + * the doWork() callback is called (once per tick). + * @param maxStoredEnergy The maximum amount of power this PowerHandler can + * store. Values tend to range between 100 and 5000. With 1000 and 1500 + * being common. */ public void configure(double minEnergyReceived, double maxEnergyReceivedI, double activationEnergy, - double maxStoredEnergy) { + double maxStoredEnergy) { double localMaxEnergyReceived = maxEnergyReceivedI; if (minEnergyReceived > localMaxEnergyReceived) { localMaxEnergyReceived = minEnergyReceived; } - this.minEnergyReceived = minEnergyReceived; - this.maxEnergyReceived = localMaxEnergyReceived; - this.maxEnergyStored = maxStoredEnergy; this.activationEnergy = activationEnergy; + + battery.reconfigure(maxStoredEnergy, localMaxEnergyReceived, minEnergyReceived); } /** * Allows you define perdition in terms of loss/ticks. - * + *

* This function is mostly for legacy implementations. See * PerditionCalculator for more complex perdition formulas. * @@ -222,7 +236,7 @@ public final class PowerHandler { /** * Allows you to define a new PerditionCalculator class to handler perdition * calculations. - * + *

* For example if you want exponentially increasing loss based on amount * stored. * @@ -247,7 +261,7 @@ public final class PowerHandler { /** * Ticks the power handler. You should call this if you can, but its not * required. - * + *

* If you don't call it, the possibility exists for some weirdness with the * perdition algorithm and work callback as its possible they will not be * called on every tick they otherwise would be. You should be able to @@ -260,13 +274,14 @@ public final class PowerHandler { } private void applyPerdition() { + double energyStored = getEnergyStored(); if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) { double prev = energyStored; double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); if (newEnergy == 0 || newEnergy < energyStored) { - energyStored = newEnergy; + battery.setEnergyStored(energyStored = newEnergy); } else { - energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); + battery.setEnergyStored(energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay())); } validateEnergy(); @@ -275,7 +290,7 @@ public final class PowerHandler { } private void applyWork() { - if (energyStored >= activationEnergy) { + if (getEnergyStored() >= activationEnergy) { if (doWorkTracker.markTimeIfDelay(receptor.getWorld(), 1)) { receptor.doWork(this); } @@ -311,6 +326,7 @@ public final class PowerHandler { double result = 0; + double energyStored = getEnergyStored(); if (energyStored >= min) { if (energyStored <= max) { result = energyStored; @@ -324,6 +340,9 @@ public final class PowerHandler { } } } + if (energyStored != getEnergyStored()) { + battery.setEnergyStored(energyStored); + } validateEnergy(); @@ -340,7 +359,7 @@ public final class PowerHandler { public void readFromNBT(NBTTagCompound data, String tag) { NBTTagCompound nbt = data.getCompoundTag(tag); - energyStored = nbt.getDouble("energyStored"); + battery.setEnergyStored(nbt.getDouble("energyStored")); } public void writeToNBT(NBTTagCompound data) { @@ -349,7 +368,7 @@ public final class PowerHandler { public void writeToNBT(NBTTagCompound data, String tag) { NBTTagCompound nbt = new NBTTagCompound(); - nbt.setDouble("energyStored", energyStored); + nbt.setDouble("energyStored", battery.getEnergyStored()); data.setTag(tag, nbt); } @@ -359,15 +378,15 @@ public final class PowerHandler { } public double getMinEnergyReceived() { - return minEnergyReceived; + return PowerHandler.this.getMinEnergyReceived(); } public double getMaxEnergyReceived() { - return maxEnergyReceived; + return PowerHandler.this.getMaxEnergyReceived(); } public double getMaxEnergyStored() { - return maxEnergyStored; + return PowerHandler.this.getMaxEnergyStored(); } public double getActivationEnergy() { @@ -375,7 +394,7 @@ public final class PowerHandler { } public double getEnergyStored() { - return energyStored; + return PowerHandler.this.getEnergyStored(); } public double getAveragePowerReceived() { @@ -405,12 +424,12 @@ public final class PowerHandler { */ public double powerRequest() { update(); - return Math.min(maxEnergyReceived, maxEnergyStored - energyStored); + return battery.getEnergyRequested(); } /** * Add power to the PowerReceiver from an external source. - * + *

* IPowerEmitters are responsible for calling this themselves. * * @param quantity @@ -420,10 +439,10 @@ public final class PowerHandler { public double receiveEnergy(Type source, final double quantity, ForgeDirection from) { double used = quantity; if (source == Type.ENGINE) { - if (used < minEnergyReceived) { + if (used < getMinEnergyReceived()) { return 0; - } else if (used > maxEnergyReceived) { - used = maxEnergyReceived; + } else if (used > getMaxEnergyReceived()) { + used = getMaxEnergyReceived(); } } @@ -436,7 +455,7 @@ public final class PowerHandler { applyWork(); if (source == Type.ENGINE && type.eatsEngineExcess()) { - used = Math.min(quantity, maxEnergyReceived); + used = Math.min(quantity, getMaxEnergyReceived()); } averageReceivedPower = (averageReceivedPower * ROLLING_AVERAGE_NUMERATOR + used) * ROLLING_AVERAGE_DENOMINATOR; @@ -446,29 +465,16 @@ public final class PowerHandler { } /** - * * @return the amount the power changed by */ - public double addEnergy(double quantityI) { - double quantity = quantityI; - - energyStored += quantity; - - if (energyStored > maxEnergyStored) { - quantity -= energyStored - maxEnergyStored; - energyStored = maxEnergyStored; - } else if (energyStored < 0) { - quantity -= energyStored; - energyStored = 0; - } - + public double addEnergy(double quantity) { + final double used = battery.addEnergy(quantity); applyPerdition(); - - return quantity; + return used; } public void setEnergy(double quantity) { - this.energyStored = quantity; + battery.setEnergyStored(quantity); validateEnergy(); } @@ -477,11 +483,21 @@ public final class PowerHandler { } private void validateEnergy() { + double energyStored = getEnergyStored(); + double maxEnergyStored = getMaxEnergyStored(); if (energyStored < 0) { energyStored = 0; } if (energyStored > maxEnergyStored) { energyStored = maxEnergyStored; } + if (energyStored != battery.getEnergyStored()) { + battery.setEnergyStored(energyStored); + } + } + + private class AnonymousBattery { + @MjBattery + public double mjStored; } } diff --git a/buildcraft_resources/assets/buildcraft/lang/en_US.lang b/buildcraft_resources/assets/buildcraft/lang/en_US.lang index 3b102df6..3f709a97 100755 --- a/buildcraft_resources/assets/buildcraft/lang/en_US.lang +++ b/buildcraft_resources/assets/buildcraft/lang/en_US.lang @@ -1,6 +1,9 @@ # Master language file chat.pipe.power.iron.mode=Switched to %d MJ/t limit +chat.pipe.power.energyConverter=Energy conversion: %s +chat.pipe.power.energyConverter.from_old_to_new=From old API to new API +chat.pipe.power.energyConverter.from_new_to_old=From new API to old API color.black=Black color.blue=Blue @@ -203,6 +206,8 @@ tile.refineryBlock.name=Refinery tile.spring.oil.name=Oil Spring tile.spring.water.name=Water Spring tile.tankBlock.name=Tank +tile.energyConverter.name=Energy Converter +tile.energyConverter.tooltip=Convert BC energy between old and new power API|Added for compatibility with other mods|while they not migrated to new power API.|Could be removed in the future. tile.architect.rotate=Rotate: On tile.architect.norotate=Rotate: Off diff --git a/buildcraft_resources/assets/buildcraft/textures/blocks/blockEnergyConverter.png b/buildcraft_resources/assets/buildcraft/textures/blocks/blockEnergyConverter.png new file mode 100644 index 0000000000000000000000000000000000000000..24b766d0c1cfab85b5dd40ac9b87bcd4306cf0f1 GIT binary patch literal 206 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPGa2=EDU1=8y3>Q+`(iHV8j<>eD6 zPF%ih+4k+*6}DMK0Oc5yyxm;OkH}&M25w;xW@MN(M*=9wUgGKN%6^ZP zg-4maYwMLdppcEHi(?4K_2h&GMmI+TfrNw*CT0can-Uy74m{!t48}3e&dv%wTTC>R s_wvkToZh6_mfbGNv7ISRlZS!9Y`xITN9wPwf#xuHy85}Sb4q9e0BAci!~g&Q literal 0 HcmV?d00001 diff --git a/common/buildcraft/BuildCraftEnergy.java b/common/buildcraft/BuildCraftEnergy.java index 85427c32..e1d1c140 100644 --- a/common/buildcraft/BuildCraftEnergy.java +++ b/common/buildcraft/BuildCraftEnergy.java @@ -53,6 +53,7 @@ import buildcraft.core.Version; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.triggers.BCTrigger; import buildcraft.energy.BlockBuildcraftFluid; +import buildcraft.energy.BlockEnergyConverter; import buildcraft.energy.BlockEnergyEmitter; import buildcraft.energy.BlockEnergyReceiver; import buildcraft.energy.BlockEngine; @@ -60,8 +61,10 @@ import buildcraft.energy.BucketHandler; import buildcraft.energy.EnergyProxy; import buildcraft.energy.GuiHandler; import buildcraft.energy.ItemBucketBuildcraft; +import buildcraft.energy.ItemEnergyConverter; import buildcraft.energy.ItemEngine; import buildcraft.energy.SchematicEngine; +import buildcraft.energy.TileEnergyConverter; import buildcraft.energy.TileEnergyEmitter; import buildcraft.energy.TileEnergyReceiver; import buildcraft.energy.TileEngine.EnergyStage; @@ -89,6 +92,7 @@ public class BuildCraftEnergy extends BuildCraftMod { public static Block blockOil; public static Block blockFuel; public static Block blockRedPlasma; + public static Block blockEnergyConverter; public static Item bucketOil; public static Item bucketFuel; public static Item bucketRedPlasma; @@ -127,8 +131,8 @@ public class BuildCraftEnergy extends BuildCraftMod { try { oilBiomeIDs.add(Integer.parseInt(strippedId)); } catch (NumberFormatException ex) { // not an int so try and - // parse it as a biome - // type + // parse it as a biome + // type try { for (BiomeGenBase b : BiomeDictionary.getBiomesForType(BiomeDictionary.Type.valueOf(strippedId .toUpperCase()))) { @@ -153,8 +157,8 @@ public class BuildCraftEnergy extends BuildCraftMod { try { excessiveOilBiomeIDs.add(Integer.parseInt(strippedId)); } catch (NumberFormatException ex) { // not an int so try and - // parse it as a biome - // type + // parse it as a biome + // type try { for (BiomeGenBase b : BiomeDictionary.getBiomesForType(BiomeDictionary.Type.valueOf(strippedId .toUpperCase()))) { @@ -179,8 +183,8 @@ public class BuildCraftEnergy extends BuildCraftMod { try { excludeOilBiomeIDs.add(Integer.parseInt(strippedId)); } catch (NumberFormatException ex) { // not an int so try and - // parse it as a biome - // type + // parse it as a biome + // type try { for (BiomeGenBase b : BiomeDictionary.getBiomesForType(BiomeDictionary.Type.valueOf(strippedId .toUpperCase()))) { @@ -199,7 +203,7 @@ public class BuildCraftEnergy extends BuildCraftMod { BuildCraftCore.mainConfiguration.save(); if (oilDesertBiomeId > 0) { - if (BiomeGenBase.getBiomeGenArray () [oilDesertBiomeId] != null) { + if (BiomeGenBase.getBiomeGenArray()[oilDesertBiomeId] != null) { oilDesertBiomeId = findUnusedBiomeID("oilDesert"); // save changes to config file BuildCraftCore.mainConfiguration.get("biomes", "biomeOilDesert", oilDesertBiomeId).set(oilDesertBiomeId); @@ -209,7 +213,7 @@ public class BuildCraftEnergy extends BuildCraftMod { } if (oilOceanBiomeId > 0) { - if (BiomeGenBase.getBiomeGenArray () [oilOceanBiomeId] != null) { + if (BiomeGenBase.getBiomeGenArray()[oilOceanBiomeId] != null) { oilOceanBiomeId = findUnusedBiomeID("oilOcean"); // save changes to config file BuildCraftCore.mainConfiguration.get("biomes", "biomeOilOcean", oilOceanBiomeId).set(oilOceanBiomeId); @@ -221,6 +225,12 @@ public class BuildCraftEnergy extends BuildCraftMod { engineBlock = new BlockEngine(); CoreProxy.proxy.registerBlock(engineBlock, ItemEngine.class); + if (BuildCraftCore.mainConfiguration.get(Configuration.CATEGORY_GENERAL, "energyConverter", true, + "Set true for enable energy converter").getBoolean(true)) { + blockEnergyConverter = new BlockEnergyConverter(); + CoreProxy.proxy.registerBlock(blockEnergyConverter, ItemEnergyConverter.class); + CoreProxy.proxy.registerTileEntity(TileEnergyConverter.class, "EnergyConverter"); + } // Oil and fuel buildcraftFluidOil = new Fluid("oil").setDensity(800).setViscosity(1500); @@ -312,11 +322,11 @@ public class BuildCraftEnergy extends BuildCraftMod { // Receiver / emitter if (!BuildCraftCore.NEXTGEN_PREALPHA) { - emitterBlock = new BlockEnergyEmitter (); + emitterBlock = new BlockEnergyEmitter(); CoreProxy.proxy.registerBlock(emitterBlock.setBlockName("energyEmitterBlock")); CoreProxy.proxy.registerTileEntity(TileEnergyEmitter.class, "net.minecraft.src.builders.TileEnergyEmitter"); - receiverBlock = new BlockEnergyReceiver (); + receiverBlock = new BlockEnergyReceiver(); CoreProxy.proxy.registerBlock(receiverBlock.setBlockName("energyReceiverBlock")); CoreProxy.proxy.registerTileEntity(TileEnergyReceiver.class, "net.minecraft.src.builders.TileEnergyReceiver"); } @@ -361,14 +371,20 @@ public class BuildCraftEnergy extends BuildCraftMod { public static void loadRecipes() { CoreProxy.proxy.addCraftingRecipe(new ItemStack(engineBlock, 1, 0), "www", " g ", "GpG", 'w', "plankWood", 'g', Blocks.glass, 'G', - BuildCraftCore.woodenGearItem, 'p', Blocks.piston); + BuildCraftCore.woodenGearItem, 'p', Blocks.piston); CoreProxy.proxy.addCraftingRecipe(new ItemStack(engineBlock, 1, 1), "www", " g ", "GpG", 'w', "cobblestone", - 'g', Blocks.glass, 'G', BuildCraftCore.stoneGearItem, 'p', Blocks.piston); + 'g', Blocks.glass, 'G', BuildCraftCore.stoneGearItem, 'p', Blocks.piston); CoreProxy.proxy.addCraftingRecipe(new ItemStack(engineBlock, 1, 2), "www", " g ", "GpG", 'w', Items.iron_ingot, - 'g', Blocks.glass, 'G', BuildCraftCore.ironGearItem, 'p', Blocks.piston); + 'g', Blocks.glass, 'G', BuildCraftCore.ironGearItem, 'p', Blocks.piston); + + if (blockEnergyConverter != null) { + CoreProxy.proxy.addCraftingRecipe(new ItemStack(blockEnergyConverter, 1), "rcr", "sgs", "rcr", + 'r', Items.redstone, 'c', BuildCraftTransport.pipePowerCobblestone, + 's', BuildCraftTransport.pipePowerStone, 'g', Blocks.glass); + } } - private int findUnusedBiomeID (String biomeName) { + private int findUnusedBiomeID(String biomeName) { int freeBiomeID = 0; // code to find a free biome for (int i = 1; i < 256; i++) { diff --git a/common/buildcraft/energy/BlockEnergyConverter.java b/common/buildcraft/energy/BlockEnergyConverter.java new file mode 100644 index 00000000..1231b950 --- /dev/null +++ b/common/buildcraft/energy/BlockEnergyConverter.java @@ -0,0 +1,57 @@ +package buildcraft.energy; + +import net.minecraft.block.material.Material; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +import net.minecraftforge.common.util.ForgeDirection; + +import buildcraft.core.BlockBuildCraft; +import buildcraft.core.IItemPipe; + +public class BlockEnergyConverter extends BlockBuildCraft { + @SideOnly(Side.CLIENT) + private IIcon blockTexture; + + public BlockEnergyConverter() { + super(Material.ground); + setBlockName("energyConverter"); + } + + @Override + public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer player, int side, float par7, float par8, float par9) { + TileEntity tile = world.getTileEntity(i, j, k); + + // Do not open guis when having a pipe in hand + if (player.getCurrentEquippedItem() != null && + player.getCurrentEquippedItem().getItem() instanceof IItemPipe) { + return false; + } + + return tile instanceof TileEnergyConverter + && ((TileEnergyConverter) tile).onBlockActivated(player, ForgeDirection.getOrientation(side)); + } + + @Override + public TileEntity createNewTileEntity(World world, int metadata) { + return new TileEnergyConverter(); + } + + @Override + @SideOnly(Side.CLIENT) + public void registerBlockIcons(IIconRegister par1IconRegister) { + blockTexture = par1IconRegister.registerIcon("buildcraft:blockEnergyConverter"); + } + + @Override + @SideOnly(Side.CLIENT) + public IIcon getIcon(int i, int j) { + return blockTexture; + } +} diff --git a/common/buildcraft/energy/ItemEnergyConverter.java b/common/buildcraft/energy/ItemEnergyConverter.java new file mode 100644 index 00000000..488e0666 --- /dev/null +++ b/common/buildcraft/energy/ItemEnergyConverter.java @@ -0,0 +1,25 @@ +package buildcraft.energy; + +import java.util.Arrays; +import java.util.List; + +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; + +import buildcraft.core.utils.StringUtils; + +public class ItemEnergyConverter extends ItemBlock { + public ItemEnergyConverter(Block block) { + super(block); + } + + @Override + public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean adv) { + super.addInformation(itemStack, player, list, adv); + list.add(TileEnergyConverter.getLocalizedModeName(itemStack)); + list.add(""); + list.addAll(Arrays.asList(StringUtils.localize("tile.energyConverter.tooltip").split("\\|"))); + } +} diff --git a/common/buildcraft/energy/TileEnergyConverter.java b/common/buildcraft/energy/TileEnergyConverter.java new file mode 100644 index 00000000..b0dec49d --- /dev/null +++ b/common/buildcraft/energy/TileEnergyConverter.java @@ -0,0 +1,149 @@ +package buildcraft.energy; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChatComponentText; +import net.minecraftforge.common.util.ForgeDirection; + +import buildcraft.api.core.NetworkData; +import buildcraft.api.mj.MjAPI; +import buildcraft.api.mj.MjBattery; +import buildcraft.api.power.IPowerReceptor; +import buildcraft.api.power.PowerHandler; +import buildcraft.api.tools.IToolWrench; +import buildcraft.core.TileBuffer; +import buildcraft.core.TileBuildCraft; +import buildcraft.core.utils.StringUtils; + +public class TileEnergyConverter extends TileBuildCraft implements IPowerReceptor { + private static enum Mode { + FromOldToNew("from_old_to_new"), FromNewToOld("from_new_to_old"); + private final String localizeName; + + Mode(String localizeName) { + this.localizeName = localizeName; + } + + public Mode next() { + if (this == FromOldToNew) { + return FromNewToOld; + } + return FromOldToNew; + } + } + + @MjBattery(maxCapacity = 1000, maxReceivedPerCycle = 64, minimumConsumption = 1) + @NetworkData + private double mjStored = 0; + @NetworkData + private Mode mode = Mode.FromOldToNew; + private TileBuffer[] tileCache; + private PowerHandler powerHandler, zeroPowerHandler; + + public TileEnergyConverter() { + powerHandler = new PowerHandler(this, PowerHandler.Type.MACHINE, MjAPI.getMjBattery(this)); + zeroPowerHandler = new PowerHandler(this, PowerHandler.Type.MACHINE); + zeroPowerHandler.configure(0, 0, 0, 0); + } + + public static String getLocalizedModeName(ItemStack stack) { + int mode = 0; + if (stack != null && stack.getTagCompound() != null) { + mode = stack.getTagCompound().getInteger("converterMode"); + if (mode >= Mode.values().length || mode < 0) { + mode = 0; + } + } + return StringUtils.localize("chat.pipe.power.energyConverter." + Mode.values()[mode].localizeName); + } + + public boolean onBlockActivated(EntityPlayer player, ForgeDirection side) { + if (!getWorld().isRemote) { + Item equipped = player.getCurrentEquippedItem() != null ? player.getCurrentEquippedItem().getItem() : null; + + if (equipped instanceof IToolWrench && ((IToolWrench) equipped).canWrench(player, xCoord, yCoord, zCoord)) { + mode = mode.next(); + + player.addChatMessage(new ChatComponentText(String.format( + StringUtils.localize("chat.pipe.power.energyConverter"), + StringUtils.localize("chat.pipe.power.energyConverter." + mode.localizeName)))); + + sendNetworkUpdate(); + + ((IToolWrench) equipped).wrenchUsed(player, xCoord, yCoord, zCoord); + return true; + } + } + + return !player.isSneaking(); + } + + @Override + public void writeToNBT(NBTTagCompound nbt) { + super.writeToNBT(nbt); + nbt.setDouble("mjStored", mjStored); + nbt.setInteger("converterMode", mode.ordinal()); + } + + @Override + public void readFromNBT(NBTTagCompound nbt) { + super.readFromNBT(nbt); + mjStored = nbt.getDouble("mjStored"); + mode = Mode.values()[nbt.getInteger("converterMode")]; + } + + public TileBuffer getTileBuffer(ForgeDirection side) { + if (tileCache == null) { + tileCache = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false); + } + + return tileCache[side.ordinal()]; + } + + @Override + public void updateEntity() { + super.updateEntity(); + if (mode == Mode.FromOldToNew) { + for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) { + TileEntity tile = getTileBuffer(side).getTile(); + if (tile instanceof TileEnergyConverter) { + continue; + } + MjAPI.BatteryObject object = MjAPI.getMjBattery(tile); + if (object != null && mjStored > 0) { + double wantToUse = Math.min(mjStored, object.getEnergyRequested()); + object.addEnergy(wantToUse); + mjStored -= wantToUse; + } + } + } else if (mode == Mode.FromNewToOld) { + for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) { + TileEntity tile = getTileBuffer(side).getTile(); + if (tile instanceof TileEnergyConverter) { + continue; + } + if (tile instanceof IPowerReceptor && mjStored > 0) { + IPowerReceptor receptor = (IPowerReceptor) tile; + PowerHandler.PowerReceiver powerReceiver = receptor.getPowerReceiver(side.getOpposite()); + double wantToUse = Math.min(mjStored, powerReceiver.getMaxEnergyReceived()); + if (wantToUse > powerReceiver.getMinEnergyReceived()) { + powerReceiver.receiveEnergy(PowerHandler.Type.MACHINE, wantToUse, side); + mjStored -= wantToUse; + } + } + } + } + } + + @Override + public PowerHandler.PowerReceiver getPowerReceiver(ForgeDirection side) { + return (mode == Mode.FromOldToNew ? powerHandler : zeroPowerHandler).getPowerReceiver(); + } + + @Override + public void doWork(PowerHandler workProvider) { + } +} From 5ad4b5312011e90a7e0a0d12fd044b26dae56c8a Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Mon, 5 May 2014 11:15:17 +0200 Subject: [PATCH 2/2] minor refactorings --- api/buildcraft/api/mj/MjAPILegacy.java | 9 +++++ api/buildcraft/api/power/PowerHandler.java | 40 +++++++++++-------- .../energy/BlockEnergyConverter.java | 8 ++++ .../energy/ItemEnergyConverter.java | 14 +++++-- .../energy/TileEnergyConverter.java | 9 +++++ 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/api/buildcraft/api/mj/MjAPILegacy.java b/api/buildcraft/api/mj/MjAPILegacy.java index e5a07e35..9d125399 100644 --- a/api/buildcraft/api/mj/MjAPILegacy.java +++ b/api/buildcraft/api/mj/MjAPILegacy.java @@ -1,7 +1,16 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * 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.mj; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; + import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.power.IPowerReceptor; diff --git a/api/buildcraft/api/power/PowerHandler.java b/api/buildcraft/api/power/PowerHandler.java index a3a076cc..ee983df5 100644 --- a/api/buildcraft/api/power/PowerHandler.java +++ b/api/buildcraft/api/power/PowerHandler.java @@ -9,6 +9,7 @@ package buildcraft.api.power; import net.minecraft.nbt.NBTTagCompound; + import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.core.SafeTimeTracker; @@ -188,24 +189,29 @@ public final class PowerHandler { /** * Setup your PowerHandler's settings. * - * @param minEnergyReceived This is the minimum about of power that will be - * accepted by the PowerHandler. This should generally be greater than the - * activationEnergy if you plan to use the doWork() callback. Anything - * greater than 1 will prevent Redstone Engines from powering this Provider. - * @param maxEnergyReceivedI The maximum amount of power accepted by the - * PowerHandler. This should generally be less than 500. Too low and larger - * engines will overheat while trying to power the machine. Too high, and - * the engines will never warm up. Greater values also place greater strain - * on the power net. - * @param activationEnergy If the stored energy is greater than this value, - * the doWork() callback is called (once per tick). - * @param maxStoredEnergy The maximum amount of power this PowerHandler can - * store. Values tend to range between 100 and 5000. With 1000 and 1500 - * being common. + * @param minEnergyReceived + * This is the minimum about of power that will be accepted by + * the PowerHandler. This should generally be greater than the + * activationEnergy if you plan to use the doWork() callback. + * Anything greater than 1 will prevent Redstone Engines from + * powering this Provider. + * @param maxEnergyReceived + * The maximum amount of power accepted by the PowerHandler. This + * should generally be less than 500. Too low and larger engines + * will overheat while trying to power the machine. Too high, and + * the engines will never warm up. Greater values also place + * greater strain on the power net. + * @param activationEnergy + * If the stored energy is greater than this value, the doWork() + * callback is called (once per tick). + * @param maxStoredEnergy + * The maximum amount of power this PowerHandler can store. + * Values tend to range between 100 and 5000. With 1000 and 1500 + * being common. */ - public void configure(double minEnergyReceived, double maxEnergyReceivedI, double activationEnergy, + public void configure(double minEnergyReceived, double maxEnergyReceived, double activationEnergy, double maxStoredEnergy) { - double localMaxEnergyReceived = maxEnergyReceivedI; + double localMaxEnergyReceived = maxEnergyReceived; if (minEnergyReceived > localMaxEnergyReceived) { localMaxEnergyReceived = minEnergyReceived; @@ -496,7 +502,7 @@ public final class PowerHandler { } } - private class AnonymousBattery { + private static class AnonymousBattery { @MjBattery public double mjStored; } diff --git a/common/buildcraft/energy/BlockEnergyConverter.java b/common/buildcraft/energy/BlockEnergyConverter.java index 1231b950..e0eca004 100644 --- a/common/buildcraft/energy/BlockEnergyConverter.java +++ b/common/buildcraft/energy/BlockEnergyConverter.java @@ -1,3 +1,11 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * 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.energy; import net.minecraft.block.material.Material; diff --git a/common/buildcraft/energy/ItemEnergyConverter.java b/common/buildcraft/energy/ItemEnergyConverter.java index 488e0666..1ad83ecc 100644 --- a/common/buildcraft/energy/ItemEnergyConverter.java +++ b/common/buildcraft/energy/ItemEnergyConverter.java @@ -1,6 +1,13 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * 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.energy; -import java.util.Arrays; import java.util.List; import net.minecraft.block.Block; @@ -8,8 +15,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; -import buildcraft.core.utils.StringUtils; - public class ItemEnergyConverter extends ItemBlock { public ItemEnergyConverter(Block block) { super(block); @@ -20,6 +25,7 @@ public class ItemEnergyConverter extends ItemBlock { super.addInformation(itemStack, player, list, adv); list.add(TileEnergyConverter.getLocalizedModeName(itemStack)); list.add(""); - list.addAll(Arrays.asList(StringUtils.localize("tile.energyConverter.tooltip").split("\\|"))); + // This is a bit too big in the tooltip at this stage. + // list.addAll(Arrays.asList(StringUtils.localize("tile.energyConverter.tooltip").split("\\|"))); } } diff --git a/common/buildcraft/energy/TileEnergyConverter.java b/common/buildcraft/energy/TileEnergyConverter.java index b0dec49d..3962630e 100644 --- a/common/buildcraft/energy/TileEnergyConverter.java +++ b/common/buildcraft/energy/TileEnergyConverter.java @@ -1,3 +1,11 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * 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.energy; import net.minecraft.entity.player.EntityPlayer; @@ -6,6 +14,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentText; + import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.core.NetworkData;