From eeb3b1667680d7d5495503b9d4f594ba32876f2b Mon Sep 17 00:00:00 2001 From: Prototik Date: Sun, 11 May 2014 19:23:43 +0800 Subject: [PATCH] Support for IO modes in MjAPI --- api/buildcraft/api/mj/BatteryObject.java | 42 ++++++++++++++++++--- api/buildcraft/api/mj/IBatteryIOObject.java | 17 +++++++++ api/buildcraft/api/mj/IOMode.java | 20 ++++++++++ api/buildcraft/api/mj/MjBattery.java | 5 +++ 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 api/buildcraft/api/mj/IBatteryIOObject.java create mode 100644 api/buildcraft/api/mj/IOMode.java diff --git a/api/buildcraft/api/mj/BatteryObject.java b/api/buildcraft/api/mj/BatteryObject.java index 86d52e01..a762a4e0 100755 --- a/api/buildcraft/api/mj/BatteryObject.java +++ b/api/buildcraft/api/mj/BatteryObject.java @@ -22,7 +22,7 @@ import buildcraft.api.core.JavaTools; * battery field is of type double, and is the only piece of data specific to * this object. Others are class-wide. */ -public class BatteryObject implements IBatteryObject { +public class BatteryObject implements IBatteryIOObject { protected Field energyStored; protected Object obj; protected MjBattery batteryData; @@ -32,6 +32,9 @@ public class BatteryObject implements IBatteryObject { */ @Override public double getEnergyRequested() { + if (!batteryData.mode().canReceive) { + return 0; + } try { return JavaTools.bounds(batteryData.maxCapacity() - energyStored.getDouble(obj), batteryData.minimumConsumption(), batteryData.maxReceivedPerCycle()); @@ -54,6 +57,9 @@ public class BatteryObject implements IBatteryObject { */ @Override public double addEnergy(double mj, boolean ignoreCycleLimit) { + if (!batteryData.mode().canReceive) { + return 0; + } try { double contained = energyStored.getDouble(obj); double maxAccepted = batteryData.maxCapacity() - contained + batteryData.minimumConsumption(); @@ -126,8 +132,14 @@ public class BatteryObject implements IBatteryObject { * {@inheritDoc} */ @Override - public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) { - final ForgeDirection[] sides = batteryData != null ? batteryData.sides() : new ForgeDirection[] { ForgeDirection.UNKNOWN }; + public BatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption) { + overrideBattery(maxCapacity, maxReceivedPerCycle, minimumConsumption, + batteryData.kind(), batteryData.sides(), batteryData.mode()); + return this; + } + + public void overrideBattery(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption, + final String kind, final ForgeDirection[] sides, final IOMode mode) { batteryData = new MjBattery() { @Override public double maxCapacity() { @@ -151,20 +163,38 @@ public class BatteryObject implements IBatteryObject { @Override public String kind() { - return MjAPI.DEFAULT_POWER_FRAMEWORK; + return kind; } @Override public ForgeDirection[] sides() { return sides; } - }; - return this; + @Override + public IOMode mode() { + return mode; + } + }; } @Override public String kind() { return batteryData.kind(); } + + @Override + public IOMode mode() { + return batteryData.mode(); + } + + @Override + public boolean canSend() { + return batteryData.mode().canSend; + } + + @Override + public boolean canReceive() { + return batteryData.mode().canReceive; + } } \ No newline at end of file diff --git a/api/buildcraft/api/mj/IBatteryIOObject.java b/api/buildcraft/api/mj/IBatteryIOObject.java new file mode 100644 index 00000000..03947c93 --- /dev/null +++ b/api/buildcraft/api/mj/IBatteryIOObject.java @@ -0,0 +1,17 @@ +/** + * 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; + +public interface IBatteryIOObject extends IBatteryObject { + IOMode mode(); + + boolean canSend(); + + boolean canReceive(); +} diff --git a/api/buildcraft/api/mj/IOMode.java b/api/buildcraft/api/mj/IOMode.java new file mode 100644 index 00000000..20d7a897 --- /dev/null +++ b/api/buildcraft/api/mj/IOMode.java @@ -0,0 +1,20 @@ +/** + * 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; + +public enum IOMode { + Both(true, true), Receive(true, false), Send(false, true), None(false, false); + + public final boolean canReceive, canSend; + + IOMode(boolean canReceive, boolean canSend) { + this.canReceive = canReceive; + this.canSend = canSend; + } +} diff --git a/api/buildcraft/api/mj/MjBattery.java b/api/buildcraft/api/mj/MjBattery.java index 738efd19..f5f39037 100755 --- a/api/buildcraft/api/mj/MjBattery.java +++ b/api/buildcraft/api/mj/MjBattery.java @@ -65,4 +65,9 @@ public @interface MjBattery { * @return Sides on which this battery should works. */ ForgeDirection[] sides() default { ForgeDirection.UNKNOWN }; + + /** + * @return Current battery input/output mode + */ + IOMode mode() default IOMode.Receive; } \ No newline at end of file