extracted battery object to first-level class
This commit is contained in:
parent
bed9b59b0e
commit
f299a7293b
3 changed files with 136 additions and 130 deletions
134
api/buildcraft/api/mj/BatteryObject.java
Executable file
134
api/buildcraft/api/mj/BatteryObject.java
Executable file
|
@ -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<? extends Annotation> annotationType() {
|
||||||
|
return MjBattery.class;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,6 @@
|
||||||
*/
|
*/
|
||||||
package buildcraft.api.mj;
|
package buildcraft.api.mj;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
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;
|
||||||
|
@ -28,134 +27,6 @@ public final class MjAPI {
|
||||||
BatteryKind kind;
|
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<? extends Annotation> annotationType() {
|
|
||||||
return MjBattery.class;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deactivate constructor
|
* Deactivate constructor
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -13,6 +13,7 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
import buildcraft.api.core.SafeTimeTracker;
|
import buildcraft.api.core.SafeTimeTracker;
|
||||||
|
import buildcraft.api.mj.BatteryObject;
|
||||||
import buildcraft.api.mj.IBatteryObject;
|
import buildcraft.api.mj.IBatteryObject;
|
||||||
import buildcraft.api.mj.IBatteryProvider;
|
import buildcraft.api.mj.IBatteryProvider;
|
||||||
import buildcraft.api.mj.MjAPI;
|
import buildcraft.api.mj.MjAPI;
|
||||||
|
@ -151,7 +152,7 @@ public final class PowerHandler implements IBatteryProvider {
|
||||||
this.perdition = DEFAULT_PERDITION;
|
this.perdition = DEFAULT_PERDITION;
|
||||||
|
|
||||||
if (battery instanceof IBatteryObject) {
|
if (battery instanceof IBatteryObject) {
|
||||||
this.battery = (MjAPI.BatteryObject) battery;
|
this.battery = (BatteryObject) battery;
|
||||||
} else if (battery != null) {
|
} else if (battery != null) {
|
||||||
this.battery = MjAPI.getMjBattery(battery);
|
this.battery = MjAPI.getMjBattery(battery);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue