diff --git a/api/buildcraft/api/mj/BatteryObject.java b/api/buildcraft/api/mj/BatteryObject.java new file mode 100755 index 00000000..26a35d61 --- /dev/null +++ b/api/buildcraft/api/mj/BatteryObject.java @@ -0,0 +1,134 @@ +package buildcraft.api.mj; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +import buildcraft.api.core.JavaTools; + +public class BatteryObject implements IBatteryObject { + protected Field f; + protected Object o; + protected MjBattery b; + + /** + * {@inheritDoc} + */ + @Override + public double getEnergyRequested() { + try { + return JavaTools.bounds(b.maxCapacity() - f.getDouble(o), b.minimumConsumption(), b.maxReceivedPerCycle()); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public double addEnergy(double mj) { + return addEnergy(mj, false); + } + + /** + * {@inheritDoc} + */ + @Override + public double addEnergy(double mj, boolean ignoreCycleLimit) { + try { + double contained = f.getDouble(o); + double maxAccepted = b.maxCapacity() - contained + b.minimumConsumption(); + if (!ignoreCycleLimit && maxAccepted > b.maxReceivedPerCycle()) { + maxAccepted = b.maxReceivedPerCycle(); + } + double used = Math.min(maxAccepted, mj); + if (used > 0) { + f.setDouble(o, Math.min(contained + used, b.maxCapacity())); + return used; + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public double getEnergyStored() { + try { + return f.getDouble(o); + } catch (IllegalAccessException e) { + e.printStackTrace(); + return 0; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void setEnergyStored(double mj) { + try { + f.setDouble(o, mj); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public double maxCapacity() { + return b.maxCapacity(); + } + + /** + * {@inheritDoc} + */ + @Override + public double minimumConsumption() { + return b.minimumConsumption(); + } + + /** + * {@inheritDoc} + */ + @Override + public double maxReceivedPerCycle() { + return b.maxReceivedPerCycle(); + } + + /** + * {@inheritDoc} + */ + @Override + 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; + } +} \ No newline at end of file diff --git a/api/buildcraft/api/mj/MjAPI.java b/api/buildcraft/api/mj/MjAPI.java index 0d5e3ea8..5bbacf1f 100755 --- a/api/buildcraft/api/mj/MjAPI.java +++ b/api/buildcraft/api/mj/MjAPI.java @@ -8,7 +8,6 @@ */ package buildcraft.api.mj; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @@ -28,134 +27,6 @@ public final class MjAPI { BatteryKind kind; } - public static class BatteryObject implements IBatteryObject { - private Field f; - private Object o; - private MjBattery b; - - /** - * {@inheritDoc} - */ - @Override - public double getEnergyRequested() { - try { - return JavaTools.bounds(b.maxCapacity() - f.getDouble(o), b.minimumConsumption(), b.maxReceivedPerCycle()); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public double addEnergy(double mj) { - return addEnergy(mj, false); - } - - /** - * {@inheritDoc} - */ - @Override - public double addEnergy(double mj, boolean ignoreCycleLimit) { - try { - double contained = f.getDouble(o); - double maxAccepted = b.maxCapacity() - contained + b.minimumConsumption(); - if (!ignoreCycleLimit && maxAccepted > b.maxReceivedPerCycle()) { - maxAccepted = b.maxReceivedPerCycle(); - } - double used = Math.min(maxAccepted, mj); - if (used > 0) { - f.setDouble(o, Math.min(contained + used, b.maxCapacity())); - return used; - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public double getEnergyStored() { - try { - return f.getDouble(o); - } catch (IllegalAccessException e) { - e.printStackTrace(); - return 0; - } - } - - /** - * {@inheritDoc} - */ - @Override - public void setEnergyStored(double mj) { - try { - f.setDouble(o, mj); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - /** - * {@inheritDoc} - */ - @Override - public double maxCapacity() { - return b.maxCapacity(); - } - - /** - * {@inheritDoc} - */ - @Override - public double minimumConsumption() { - return b.minimumConsumption(); - } - - /** - * {@inheritDoc} - */ - @Override - public double maxReceivedPerCycle() { - return b.maxReceivedPerCycle(); - } - - /** - * {@inheritDoc} - */ - @Override - 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; - } - } - /** * Deactivate constructor */ diff --git a/api/buildcraft/api/power/PowerHandler.java b/api/buildcraft/api/power/PowerHandler.java index f774359c..a9a532e8 100644 --- a/api/buildcraft/api/power/PowerHandler.java +++ b/api/buildcraft/api/power/PowerHandler.java @@ -13,6 +13,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.core.SafeTimeTracker; +import buildcraft.api.mj.BatteryObject; import buildcraft.api.mj.IBatteryObject; import buildcraft.api.mj.IBatteryProvider; import buildcraft.api.mj.MjAPI; @@ -151,7 +152,7 @@ public final class PowerHandler implements IBatteryProvider { this.perdition = DEFAULT_PERDITION; if (battery instanceof IBatteryObject) { - this.battery = (MjAPI.BatteryObject) battery; + this.battery = (BatteryObject) battery; } else if (battery != null) { this.battery = MjAPI.getMjBattery(battery); } else {