Merge branch 'Prototik-mjapi' into 6.0.x

This commit is contained in:
SpaceToad 2014-05-10 15:08:23 +02:00
commit c0e07d7c0c
7 changed files with 67 additions and 21 deletions

View file

@ -12,6 +12,8 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.logging.Level;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools;
@ -125,6 +127,7 @@ public class BatteryObject implements IBatteryObject {
*/
@Override
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) {
final ForgeDirection[] sides = batteryData != null ? batteryData.sides() : new ForgeDirection[] { ForgeDirection.UNKNOWN };
batteryData = new MjBattery() {
@Override
public double maxCapacity() {
@ -150,8 +153,18 @@ public class BatteryObject implements IBatteryObject {
public String kind() {
return MjAPI.DEFAULT_POWER_FRAMEWORK;
}
@Override
public ForgeDirection[] sides() {
return sides;
}
};
return this;
}
@Override
public String kind() {
return batteryData.kind();
}
}

View file

@ -75,4 +75,9 @@ public interface IBatteryObject {
* @return Current battery object instance
*/
IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption);
/**
* @return kind of this energy battery
*/
String kind();
}

View file

@ -9,5 +9,5 @@
package buildcraft.api.mj;
public interface IBatteryProvider {
IBatteryObject getMjBattery();
IBatteryObject getMjBattery(String kind);
}

View file

@ -11,5 +11,5 @@ package buildcraft.api.mj;
import net.minecraftforge.common.util.ForgeDirection;
public interface ISidedBatteryProvider extends IBatteryProvider {
IBatteryObject getMjBattery(ForgeDirection direction);
IBatteryObject getMjBattery(String kind, ForgeDirection direction);
}

View file

@ -69,12 +69,12 @@ public final class MjAPI {
IBatteryObject battery;
if (o instanceof ISidedBatteryProvider) {
battery = ((ISidedBatteryProvider) o).getMjBattery(side);
battery = ((ISidedBatteryProvider) o).getMjBattery(kind, side);
if (battery == null && side != ForgeDirection.UNKNOWN) {
battery = ((ISidedBatteryProvider) o).getMjBattery(ForgeDirection.UNKNOWN);
battery = ((ISidedBatteryProvider) o).getMjBattery(kind, ForgeDirection.UNKNOWN);
}
} else {
battery = ((IBatteryProvider) o).getMjBattery();
battery = ((IBatteryProvider) o).getMjBattery(kind);
}
if (battery != null) {
@ -82,7 +82,10 @@ public final class MjAPI {
}
}
BatteryField f = getMjBatteryField(o.getClass(), kind);
BatteryField f = getMjBatteryField(o.getClass(), kind, side);
if (f == null && side != ForgeDirection.UNKNOWN) {
f = getMjBatteryField(o.getClass(), kind, ForgeDirection.UNKNOWN);
}
if (f == null) {
return null;
@ -149,6 +152,7 @@ public final class MjAPI {
private static final class BatteryHolder {
private String kind;
private ForgeDirection side;
private Class clazz;
@Override
@ -162,13 +166,14 @@ public final class MjAPI {
BatteryHolder that = (BatteryHolder) o;
return kind.equals(that.kind) && clazz.equals(that.clazz);
return kind.equals(that.kind) && clazz.equals(that.clazz) && side.equals(that.side);
}
@Override
public int hashCode() {
int result = kind.hashCode();
result = 31 * result + clazz.hashCode();
result = 31 * result + side.hashCode();
return result;
}
}
@ -179,10 +184,11 @@ public final class MjAPI {
public BatteryKind kind;
}
private static BatteryField getMjBatteryField(Class c, String kind) {
private static BatteryField getMjBatteryField(Class c, String kind, ForgeDirection side) {
BatteryHolder holder = new BatteryHolder();
holder.clazz = c;
holder.kind = kind;
holder.side = side;
BatteryField bField = mjBatteries.get(holder);
@ -191,6 +197,9 @@ public final class MjAPI {
MjBattery battery = f.getAnnotation(MjBattery.class);
if (battery != null && kind.equals(battery.kind())) {
if (!contains(battery.sides(), side) && !contains(battery.sides(), ForgeDirection.UNKNOWN)) {
continue;
}
f.setAccessible(true);
bField = new BatteryField();
bField.field = f;
@ -216,6 +225,15 @@ public final class MjAPI {
return bField == invalidBatteryField ? null : bField;
}
private static <T> boolean contains(T[] array, T value) {
for (T t : array) {
if (t == value) {
return true;
}
}
return false;
}
static {
mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class);
}

View file

@ -14,21 +14,26 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.minecraftforge.common.util.ForgeDirection;
/**
* This annotation is used for tiles that need to interface with BuildCraft
* energy framework, a.k.a MinecraftJoule or MJ. In order to receive power,
* tiles, need to declare a double field, with the annotation
* MjBattery. BuildCraft machines able to provide power will then connect to
* these tiles, and feed energy up to max capacity. It's the responsibility
* of the implementer to manually decrease the value of the energy, as he
* simulates energy consumption. On each cycle, per power input, machines can
* receive up to "maxReceivedPerCycle" units of energy. As an optional behavior,
* the system can have a minimum amount of energy consumed even if the system
* is at max capacity, modelized by the "minimumConsumption" value.
* tiles, need to declare a double field, with the annotation MjBattery. MJ
* provider machines able to provide power will then connect to these tiles, and
* feed energy up to max capacity. It's the responsibility of the implementer to
* manually decrease the value of the energy, as he simulates energy
* consumption. On each cycle, per power input, machines can receive up to
* "maxReceivedPerCycle" units of energy. As an optional behavior, the system
* can have a minimum amount of energy consumed even if the system is at max
* capacity, modelized by the "minimumConsumption" value.
*
* If the field designated by MjBattery is an object, then it will be considered
* as a nested battery, and will look for the field in the designated object.
*
* If the field designated by MjBattery is an object, then BuildCraft will
* consider that this is a case of a nested battery, and will look for the
* field in the designated object.
* All the properties defined in this annotation are class wide. If you need to
* change them on a tile by tile basis, you will need to use interfaces, either
* {@link IBatteryProvider} or {@link ISidedBatteryProvider}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@ -55,4 +60,9 @@ public @interface MjBattery {
* coexist in the same tile.
*/
String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK;
/**
* @return Sides on which this battery should works.
*/
ForgeDirection[] sides() default { ForgeDirection.UNKNOWN };
}

View file

@ -185,8 +185,8 @@ public final class PowerHandler implements IBatteryProvider {
}
@Override
public IBatteryObject getMjBattery() {
return battery;
public IBatteryObject getMjBattery(String kind) {
return battery.kind().equals(kind) ? battery : null;
}
/**