updated energy framework to be openned to different channels, close #1756

This commit is contained in:
SpaceToad 2014-05-09 18:13:02 +02:00
parent 5c8bd331d3
commit 1febf3c714
3 changed files with 136 additions and 38 deletions

View file

@ -1,14 +1,29 @@
/**
* 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; package buildcraft.api.mj;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.logging.Level;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools; import buildcraft.api.core.JavaTools;
/**
* A battery object is a wrapper around a battery field in an object. This
* 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 IBatteryObject {
protected Field f; protected Field energyStored;
protected Object o; protected Object obj;
protected MjBattery b; protected MjBattery batteryData;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -16,9 +31,10 @@ public class BatteryObject implements IBatteryObject {
@Override @Override
public double getEnergyRequested() { public double getEnergyRequested() {
try { try {
return JavaTools.bounds(b.maxCapacity() - f.getDouble(o), b.minimumConsumption(), b.maxReceivedPerCycle()); return JavaTools.bounds(batteryData.maxCapacity() - energyStored.getDouble(obj),
batteryData.minimumConsumption(), batteryData.maxReceivedPerCycle());
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); BCLog.logger.log(Level.WARNING, "can't get energy requested", e);
} }
return 0; return 0;
} }
@ -37,18 +53,18 @@ public class BatteryObject implements IBatteryObject {
@Override @Override
public double addEnergy(double mj, boolean ignoreCycleLimit) { public double addEnergy(double mj, boolean ignoreCycleLimit) {
try { try {
double contained = f.getDouble(o); double contained = energyStored.getDouble(obj);
double maxAccepted = b.maxCapacity() - contained + b.minimumConsumption(); double maxAccepted = batteryData.maxCapacity() - contained + batteryData.minimumConsumption();
if (!ignoreCycleLimit && maxAccepted > b.maxReceivedPerCycle()) { if (!ignoreCycleLimit && maxAccepted > batteryData.maxReceivedPerCycle()) {
maxAccepted = b.maxReceivedPerCycle(); maxAccepted = batteryData.maxReceivedPerCycle();
} }
double used = Math.min(maxAccepted, mj); double used = Math.min(maxAccepted, mj);
if (used > 0) { if (used > 0) {
f.setDouble(o, Math.min(contained + used, b.maxCapacity())); energyStored.setDouble(obj, Math.min(contained + used, batteryData.maxCapacity()));
return used; return used;
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); BCLog.logger.log(Level.WARNING, "can't add energy", e);
} }
return 0; return 0;
@ -60,9 +76,9 @@ public class BatteryObject implements IBatteryObject {
@Override @Override
public double getEnergyStored() { public double getEnergyStored() {
try { try {
return f.getDouble(o); return energyStored.getDouble(obj);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); BCLog.logger.log(Level.WARNING, "can't get return energy stored", e);
return 0; return 0;
} }
} }
@ -73,8 +89,9 @@ public class BatteryObject implements IBatteryObject {
@Override @Override
public void setEnergyStored(double mj) { public void setEnergyStored(double mj) {
try { try {
f.setDouble(o, mj); energyStored.setDouble(obj, mj);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
BCLog.logger.log(Level.WARNING, "can't set energy stored", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -84,7 +101,7 @@ public class BatteryObject implements IBatteryObject {
*/ */
@Override @Override
public double maxCapacity() { public double maxCapacity() {
return b.maxCapacity(); return batteryData.maxCapacity();
} }
/** /**
@ -92,7 +109,7 @@ public class BatteryObject implements IBatteryObject {
*/ */
@Override @Override
public double minimumConsumption() { public double minimumConsumption() {
return b.minimumConsumption(); return batteryData.minimumConsumption();
} }
/** /**
@ -100,15 +117,16 @@ public class BatteryObject implements IBatteryObject {
*/ */
@Override @Override
public double maxReceivedPerCycle() { public double maxReceivedPerCycle() {
return b.maxReceivedPerCycle(); return batteryData.maxReceivedPerCycle();
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) { public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle,
b = new MjBattery() { final double minimumConsumption) {
batteryData = new MjBattery() {
@Override @Override
public double maxCapacity() { public double maxCapacity() {
return maxCapacity; return maxCapacity;
@ -128,7 +146,13 @@ public class BatteryObject implements IBatteryObject {
public Class<? extends Annotation> annotationType() { public Class<? extends Annotation> annotationType() {
return MjBattery.class; return MjBattery.class;
} }
@Override
public String kind() {
return MjAPI.DEFAULT_POWER_FRAMEWORK;
}
}; };
return this; return this;
} }
} }

View file

@ -11,21 +11,21 @@ package buildcraft.api.mj;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools; import buildcraft.api.core.JavaTools;
/**
* The class MjAPI provides services to the Minecraft Joules power framework.
* BuildCraft implements a default power model on top of this, the "kinesis"
* power model. Third party mods may provide they own version of Minecraft
* Joules batteries and provide different models.
*/
public final class MjAPI { public final class MjAPI {
private static Map<Class, BatteryField> MjBatteries = new HashMap<Class, BatteryField>(); public static final String DEFAULT_POWER_FRAMEWORK = "buildcraft.kinesis";
private static Map<Class, BatteryField> mjBatteries = new HashMap<Class, BatteryField>();
private enum BatteryKind { private static Map<String, Class<? extends BatteryObject>> mjBatteryKinds = new HashMap<String, Class<? extends BatteryObject>>();
Value, Container
}
private static class BatteryField {
Field field;
MjBattery battery;
BatteryKind kind;
}
/** /**
* Deactivate constructor * Deactivate constructor
@ -33,13 +33,28 @@ public final class MjAPI {
private MjAPI() { private MjAPI() {
} }
/**
* Returns the default battery related to the object given in parameter. For
* performance optimization, it's good to cache this object in the providing
* power framework if possible.
*/
public static IBatteryObject getMjBattery(Object o) { public static IBatteryObject getMjBattery(Object o) {
return getMjBattery(o, DEFAULT_POWER_FRAMEWORK);
}
/**
* Returns the battery related to the object given in parameter. For
* performance optimization, it's good to cache this object in the providing
* power framework if possible.
*/
public static IBatteryObject getMjBattery(Object o, String kind) {
if (o == null) { if (o == null) {
return null; return null;
} }
if (o instanceof IBatteryProvider) { if (o instanceof IBatteryProvider) {
IBatteryObject battery = ((IBatteryProvider) o).getMjBattery(); IBatteryObject battery = ((IBatteryProvider) o).getMjBattery();
if (battery != null) { if (battery != null) {
return battery; return battery;
} }
@ -49,12 +64,25 @@ public final class MjAPI {
if (f == null) { if (f == null) {
return null; return null;
} else if (!mjBatteryKinds.containsKey(kind)) {
return null;
} else if (f.kind == BatteryKind.Value) { } else if (f.kind == BatteryKind.Value) {
BatteryObject obj = new BatteryObject(); try {
obj.o = o; BatteryObject obj = mjBatteryKinds.get(kind).newInstance();
obj.f = f.field; obj.obj = o;
obj.b = f.battery; obj.energyStored = f.field;
return obj; obj.batteryData = f.battery;
return obj;
} catch (InstantiationException e) {
BCLog.logger.log(Level.WARNING, "can't instantiate class for energy kind \"" + kind + "\"");
return null;
} catch (IllegalAccessException e) {
BCLog.logger.log(Level.WARNING, "can't instantiate class for energy kind \"" + kind + "\"");
return null;
}
} else { } else {
try { try {
return getMjBattery(f.field.get(o)); return getMjBattery(f.field.get(o));
@ -65,8 +93,41 @@ public final class MjAPI {
} }
} }
public static IBatteryObject[] getAllMjBatteries(Object o) {
IBatteryObject[] result = new IBatteryObject[mjBatteries.size()];
int id = 0;
for (String kind : mjBatteryKinds.keySet()) {
result[id] = getMjBattery(o, kind);
id++;
}
return result;
}
public static void registerMJBatteryKind(String kind, Class<? extends BatteryObject> clas) {
if (!mjBatteryKinds.containsKey(kind)) {
mjBatteryKinds.put(kind, clas);
} else {
BCLog.logger.log(Level.WARNING,
"energy kind \"" + kind + "\" already registered with " + clas.getCanonicalName());
}
}
private enum BatteryKind {
Value, Container
}
private static class BatteryField {
public Field field;
public MjBattery battery;
public BatteryKind kind;
}
private static BatteryField getMjBatteryField(Class c) { private static BatteryField getMjBatteryField(Class c) {
BatteryField bField = MjBatteries.get(c); BatteryField bField = mjBatteries.get(c);
if (bField == null) { if (bField == null) {
for (Field f : JavaTools.getAllFields(c)) { for (Field f : JavaTools.getAllFields(c)) {
MjBattery battery = f.getAnnotation(MjBattery.class); MjBattery battery = f.getAnnotation(MjBattery.class);
@ -86,14 +147,20 @@ public final class MjAPI {
bField.kind = BatteryKind.Container; bField.kind = BatteryKind.Container;
} }
MjBatteries.put(c, bField); mjBatteries.put(c, bField);
return bField; return bField;
} }
} }
MjBatteries.put(c, null);
mjBatteries.put(c, null);
} }
return bField; return bField;
} }
static {
mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class);
}
} }

View file

@ -48,4 +48,11 @@ public @interface MjBattery {
* @return Minimal energy for keep machine is active * @return Minimal energy for keep machine is active
*/ */
double minimumConsumption() default 0.1; double minimumConsumption() default 0.1;
/**
* @return The kind of battery stored. Specific power systems can be created
* through this system, as several battery of different kind can
* coexist in the same tile.
*/
String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK;
} }