Update MjAPI

This commit is contained in:
Prototik 2014-05-10 15:00:35 +08:00
parent 2738fbaef0
commit 04859cce7d
7 changed files with 53 additions and 10 deletions

View file

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

View file

@ -75,4 +75,9 @@ public interface IBatteryObject {
* @return Current battery object instance * @return Current battery object instance
*/ */
IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption); 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; package buildcraft.api.mj;
public interface IBatteryProvider { 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; import net.minecraftforge.common.util.ForgeDirection;
public interface ISidedBatteryProvider extends IBatteryProvider { 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; IBatteryObject battery;
if (o instanceof ISidedBatteryProvider) { if (o instanceof ISidedBatteryProvider) {
battery = ((ISidedBatteryProvider) o).getMjBattery(side); battery = ((ISidedBatteryProvider) o).getMjBattery(kind, side);
if (battery == null && side != ForgeDirection.UNKNOWN) { if (battery == null && side != ForgeDirection.UNKNOWN) {
battery = ((ISidedBatteryProvider) o).getMjBattery(ForgeDirection.UNKNOWN); battery = ((ISidedBatteryProvider) o).getMjBattery(kind, ForgeDirection.UNKNOWN);
} }
} else { } else {
battery = ((IBatteryProvider) o).getMjBattery(); battery = ((IBatteryProvider) o).getMjBattery(kind);
} }
if (battery != null) { 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) { if (f == null) {
return null; return null;
@ -149,6 +152,7 @@ public final class MjAPI {
private static final class BatteryHolder { private static final class BatteryHolder {
private String kind; private String kind;
private ForgeDirection side;
private Class clazz; private Class clazz;
@Override @Override
@ -162,13 +166,14 @@ public final class MjAPI {
BatteryHolder that = (BatteryHolder) o; 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 @Override
public int hashCode() { public int hashCode() {
int result = kind.hashCode(); int result = kind.hashCode();
result = 31 * result + clazz.hashCode(); result = 31 * result + clazz.hashCode();
result = 31 * result + side.hashCode();
return result; return result;
} }
} }
@ -179,10 +184,11 @@ public final class MjAPI {
public BatteryKind kind; 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(); BatteryHolder holder = new BatteryHolder();
holder.clazz = c; holder.clazz = c;
holder.kind = kind; holder.kind = kind;
holder.side = side;
BatteryField bField = mjBatteries.get(holder); BatteryField bField = mjBatteries.get(holder);
@ -191,6 +197,9 @@ public final class MjAPI {
MjBattery battery = f.getAnnotation(MjBattery.class); MjBattery battery = f.getAnnotation(MjBattery.class);
if (battery != null && kind.equals(battery.kind())) { if (battery != null && kind.equals(battery.kind())) {
if (!contains(battery.sides(), side) && !contains(battery.sides(), ForgeDirection.UNKNOWN)) {
continue;
}
f.setAccessible(true); f.setAccessible(true);
bField = new BatteryField(); bField = new BatteryField();
bField.field = f; bField.field = f;
@ -216,6 +225,15 @@ public final class MjAPI {
return bField == invalidBatteryField ? null : bField; 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 { static {
mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class); mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class);
} }

View file

@ -14,6 +14,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import net.minecraftforge.common.util.ForgeDirection;
/** /**
* This annotation is used for tiles that need to interface with BuildCraft * 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, * energy framework, a.k.a MinecraftJoule or MJ. In order to receive power,
@ -55,4 +57,9 @@ public @interface MjBattery {
* coexist in the same tile. * coexist in the same tile.
*/ */
String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK; String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK;
/**
* @return Sides on which this battery should works. Can be overrided by {@link ISidedBatteryProvider}
*/
ForgeDirection[] sides() default { ForgeDirection.UNKNOWN };
} }

View file

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