Merge branch 'Prototik-power-legacy' into 6.0.x
This commit is contained in:
commit
7fdc3feb37
9 changed files with 511 additions and 125 deletions
|
@ -8,11 +8,13 @@
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
import buildcraft.api.core.JavaTools;
|
import buildcraft.api.core.JavaTools;
|
||||||
|
import buildcraft.api.power.PowerHandler;
|
||||||
|
|
||||||
public final class MjAPI {
|
public final class MjAPI {
|
||||||
|
|
||||||
|
@ -26,56 +28,43 @@ public final class MjAPI {
|
||||||
public Field field;
|
public Field field;
|
||||||
public MjBattery battery;
|
public MjBattery battery;
|
||||||
public BatteryKind kind;
|
public BatteryKind kind;
|
||||||
|
}
|
||||||
|
|
||||||
public double getEnergyRequested (Object obj) {
|
public static class BatteryObject {
|
||||||
|
Field f;
|
||||||
|
Object o;
|
||||||
|
MjBattery b;
|
||||||
|
|
||||||
|
public double getEnergyRequested() {
|
||||||
try {
|
try {
|
||||||
double contained = field.getDouble(obj);
|
double contained = f.getDouble(o);
|
||||||
|
|
||||||
double left = contained + battery.maxReceivedPerCycle() > battery
|
double left = contained + b.maxReceivedPerCycle() > b
|
||||||
.maxCapacity() ? battery.maxCapacity() - contained : battery
|
.maxCapacity() ? b.maxCapacity() - contained : b
|
||||||
.maxReceivedPerCycle();
|
.maxReceivedPerCycle();
|
||||||
|
|
||||||
if (left > 0) {
|
if (left > 0) {
|
||||||
return left;
|
return left;
|
||||||
} else {
|
} else {
|
||||||
return battery.minimumConsumption();
|
return b.minimumConsumption();
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static class BatteryObject {
|
|
||||||
BatteryField f;
|
|
||||||
Object o;
|
|
||||||
|
|
||||||
public double getEnergyRequested () {
|
|
||||||
return f.getEnergyRequested(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
public double addEnergy(double watts) {
|
public double addEnergy(double watts) {
|
||||||
try {
|
try {
|
||||||
double e = f.field.getDouble(o);
|
double e = f.getDouble(o);
|
||||||
double max = f.battery.maxCapacity();
|
double max = b.maxCapacity();
|
||||||
|
|
||||||
double used = 0;
|
double used = e + watts <= max ? watts : max - e;
|
||||||
|
|
||||||
if (e + watts <= max) {
|
f.setDouble(o, e + used);
|
||||||
used = watts;
|
|
||||||
} else {
|
|
||||||
used = max - e;
|
|
||||||
}
|
|
||||||
|
|
||||||
f.field.setDouble(o, e + used);
|
|
||||||
|
|
||||||
return used;
|
return used;
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -85,23 +74,56 @@ public final class MjAPI {
|
||||||
|
|
||||||
public double getEnergyStored() {
|
public double getEnergyStored() {
|
||||||
try {
|
try {
|
||||||
return f.field.getDouble(o);
|
return f.getDouble(o);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEnergyStored(double watts) {
|
||||||
|
try {
|
||||||
|
f.setDouble(o, watts);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public double maxCapacity() {
|
public double maxCapacity() {
|
||||||
return f.battery.maxCapacity();
|
return b.maxCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double minimumConsumption() {
|
public double minimumConsumption() {
|
||||||
return f.battery.minimumConsumption();
|
return b.minimumConsumption();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double maxReceivedPerCycle() {
|
public double maxReceivedPerCycle() {
|
||||||
return f.battery.maxReceivedPerCycle();
|
return b.maxReceivedPerCycle();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,27 +133,29 @@ public final class MjAPI {
|
||||||
private MjAPI() {
|
private MjAPI() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BatteryObject getMjBattery (Object o) {
|
public static BatteryObject getMjBattery(Object o) {
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
BatteryField f = getMjBattery (o.getClass());
|
if (o.getClass() == PowerHandler.class) {
|
||||||
|
return ((PowerHandler) o).getMjBattery();
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryField f = getMjBattery(o.getClass());
|
||||||
|
|
||||||
if (f == null) {
|
if (f == null) {
|
||||||
return null;
|
return null;
|
||||||
} else if (f.kind == BatteryKind.Value) {
|
} else if (f.kind == BatteryKind.Value) {
|
||||||
BatteryObject obj = new BatteryObject();
|
BatteryObject obj = new BatteryObject();
|
||||||
obj.o = o;
|
obj.o = o;
|
||||||
obj.f = f;
|
obj.f = f.field;
|
||||||
|
obj.b = f.battery;
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return getMjBattery(f.field.get(o));
|
return getMjBattery(f.field.get(o));
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -139,10 +163,10 @@ public final class MjAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BatteryField getMjBattery (Class c) {
|
private static BatteryField getMjBattery(Class c) {
|
||||||
if (!MjBatteries.containsKey(c)) {
|
if (!MjBatteries.containsKey(c)) {
|
||||||
for (Field f : JavaTools.getAllFields(c)) {
|
for (Field f : JavaTools.getAllFields(c)) {
|
||||||
MjBattery battery = f.getAnnotation (MjBattery.class);
|
MjBattery battery = f.getAnnotation(MjBattery.class);
|
||||||
|
|
||||||
if (battery != null) {
|
if (battery != null) {
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
|
|
65
api/buildcraft/api/mj/MjAPILegacy.java
Normal file
65
api/buildcraft/api/mj/MjAPILegacy.java
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
|
import buildcraft.api.power.IPowerReceptor;
|
||||||
|
import buildcraft.api.power.PowerHandler;
|
||||||
|
|
||||||
|
public class MjAPILegacy implements IPowerReceptor {
|
||||||
|
private final PowerHandler powerHandler;
|
||||||
|
private final World world;
|
||||||
|
|
||||||
|
protected MjAPILegacy(World world, MjAPI.BatteryObject battery, PowerHandler.Type type) {
|
||||||
|
if (battery == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
this.world = world;
|
||||||
|
this.powerHandler = new PowerHandler(this, type, battery);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MjAPILegacy from(World world, MjAPI.BatteryObject battery, PowerHandler.Type type) {
|
||||||
|
return new MjAPILegacy(world, battery, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MjAPILegacy from(World world, Object object, PowerHandler.Type type) {
|
||||||
|
return new MjAPILegacy(world, battery(object), type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MjAPILegacy from(TileEntity tileEntity, PowerHandler.Type type) {
|
||||||
|
return new MjAPILegacy(tileEntity.getWorldObj(), battery(tileEntity), type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MjAPI.BatteryObject battery(Object object) {
|
||||||
|
MjAPI.BatteryObject battery = MjAPI.getMjBattery(object);
|
||||||
|
if (battery == null) {
|
||||||
|
throw new IllegalArgumentException(String.format("Object %s not using MjAPI, can't create legacy wrapper", object));
|
||||||
|
}
|
||||||
|
return battery;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PowerHandler.PowerReceiver getPowerReceiver(ForgeDirection side) {
|
||||||
|
return powerHandler.getPowerReceiver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doWork(PowerHandler workProvider) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public World getWorld() {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,18 +13,20 @@ 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.MjAPI;
|
||||||
|
import buildcraft.api.mj.MjBattery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PowerHandler is similar to FluidTank in that it holds your power and
|
* The PowerHandler is similar to FluidTank in that it holds your power and
|
||||||
* allows standardized interaction between machines.
|
* allows standardized interaction between machines.
|
||||||
*
|
* <p/>
|
||||||
* To receive power to your machine you needs create an instance of PowerHandler
|
* To receive power to your machine you needs create an instance of PowerHandler
|
||||||
* and implement IPowerReceptor on the TileEntity.
|
* and implement IPowerReceptor on the TileEntity.
|
||||||
*
|
* <p/>
|
||||||
* If you plan emit power, you need only implement IPowerEmitter. You do not
|
* If you plan emit power, you need only implement IPowerEmitter. You do not
|
||||||
* need a PowerHandler. Engines have a PowerHandler because they can also
|
* need a PowerHandler. Engines have a PowerHandler because they can also
|
||||||
* receive power from other Engines.
|
* receive power from other Engines.
|
||||||
*
|
* <p/>
|
||||||
* See TileRefinery for a simple example of a power using machine.
|
* See TileRefinery for a simple example of a power using machine.
|
||||||
*
|
*
|
||||||
* @see IPowerReceptor
|
* @see IPowerReceptor
|
||||||
|
@ -59,7 +61,7 @@ public final class PowerHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extend this class to create custom Perdition algorithms (its not final).
|
* Extend this class to create custom Perdition algorithms (its not final).
|
||||||
*
|
* <p/>
|
||||||
* NOTE: It is not possible to create a Zero perdition algorithm.
|
* NOTE: It is not possible to create a Zero perdition algorithm.
|
||||||
*/
|
*/
|
||||||
public static class PerditionCalculator {
|
public static class PerditionCalculator {
|
||||||
|
@ -107,7 +109,7 @@ public final class PowerHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Taxes a flat rate on all incoming power.
|
* Taxes a flat rate on all incoming power.
|
||||||
*
|
* <p/>
|
||||||
* Defaults to 0% tax rate.
|
* Defaults to 0% tax rate.
|
||||||
*
|
*
|
||||||
* @return percent of input to tax
|
* @return percent of input to tax
|
||||||
|
@ -116,6 +118,7 @@ public final class PowerHandler {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final PerditionCalculator DEFAULT_PERDITION = new PerditionCalculator();
|
public static final PerditionCalculator DEFAULT_PERDITION = new PerditionCalculator();
|
||||||
public static final double ROLLING_AVERAGE_WEIGHT = 100.0;
|
public static final double ROLLING_AVERAGE_WEIGHT = 100.0;
|
||||||
public static final double ROLLING_AVERAGE_NUMERATOR = ROLLING_AVERAGE_WEIGHT - 1;
|
public static final double ROLLING_AVERAGE_NUMERATOR = ROLLING_AVERAGE_WEIGHT - 1;
|
||||||
|
@ -123,27 +126,36 @@ public final class PowerHandler {
|
||||||
public final int[] powerSources = new int[6];
|
public final int[] powerSources = new int[6];
|
||||||
public final IPowerReceptor receptor;
|
public final IPowerReceptor receptor;
|
||||||
|
|
||||||
private double minEnergyReceived;
|
|
||||||
private double maxEnergyReceived;
|
|
||||||
private double maxEnergyStored;
|
|
||||||
private double activationEnergy;
|
private double activationEnergy;
|
||||||
private double energyStored = 0;
|
|
||||||
private final SafeTimeTracker doWorkTracker = new SafeTimeTracker();
|
private final SafeTimeTracker doWorkTracker = new SafeTimeTracker();
|
||||||
private final SafeTimeTracker sourcesTracker = new SafeTimeTracker();
|
private final SafeTimeTracker sourcesTracker = new SafeTimeTracker();
|
||||||
private final SafeTimeTracker perditionTracker = new SafeTimeTracker();
|
private final SafeTimeTracker perditionTracker = new SafeTimeTracker();
|
||||||
private PerditionCalculator perdition;
|
private PerditionCalculator perdition;
|
||||||
private final PowerReceiver receiver;
|
private final PowerReceiver receiver;
|
||||||
private final Type type;
|
private final Type type;
|
||||||
|
private MjAPI.BatteryObject battery;
|
||||||
// Tracking
|
// Tracking
|
||||||
private double averageLostPower = 0;
|
private double averageLostPower = 0;
|
||||||
private double averageReceivedPower = 0;
|
private double averageReceivedPower = 0;
|
||||||
private double averageUsedPower = 0;
|
private double averageUsedPower = 0;
|
||||||
|
|
||||||
public PowerHandler(IPowerReceptor receptor, Type type) {
|
public PowerHandler(IPowerReceptor receptor, Type type) {
|
||||||
|
this(receptor, type, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PowerHandler(IPowerReceptor receptor, Type type, Object battery) {
|
||||||
this.receptor = receptor;
|
this.receptor = receptor;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.receiver = new PowerReceiver();
|
this.receiver = new PowerReceiver();
|
||||||
this.perdition = DEFAULT_PERDITION;
|
this.perdition = DEFAULT_PERDITION;
|
||||||
|
|
||||||
|
if (battery instanceof MjAPI.BatteryObject) {
|
||||||
|
this.battery = (MjAPI.BatteryObject) battery;
|
||||||
|
} else if (battery != null) {
|
||||||
|
this.battery = MjAPI.getMjBattery(battery);
|
||||||
|
} else {
|
||||||
|
this.battery = MjAPI.getMjBattery(new AnonymousBattery());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PowerReceiver getPowerReceiver() {
|
public PowerReceiver getPowerReceiver() {
|
||||||
|
@ -151,15 +163,15 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMinEnergyReceived() {
|
public double getMinEnergyReceived() {
|
||||||
return minEnergyReceived;
|
return battery.minimumConsumption();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMaxEnergyReceived() {
|
public double getMaxEnergyReceived() {
|
||||||
return maxEnergyReceived;
|
return battery.getEnergyRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMaxEnergyStored() {
|
public double getMaxEnergyStored() {
|
||||||
return maxEnergyStored;
|
return battery.maxCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getActivationEnergy() {
|
public double getActivationEnergy() {
|
||||||
|
@ -167,43 +179,51 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getEnergyStored() {
|
public double getEnergyStored() {
|
||||||
return energyStored;
|
return battery.getEnergyStored();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MjAPI.BatteryObject getMjBattery() {
|
||||||
|
return battery;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup your PowerHandler's settings.
|
* Setup your PowerHandler's settings.
|
||||||
*
|
*
|
||||||
* @param minEnergyReceived This is the minimum about of power that will be
|
* @param minEnergyReceived
|
||||||
* accepted by the PowerHandler. This should generally be greater than the
|
* This is the minimum about of power that will be accepted by
|
||||||
* activationEnergy if you plan to use the doWork() callback. Anything
|
* the PowerHandler. This should generally be greater than the
|
||||||
* greater than 1 will prevent Redstone Engines from powering this Provider.
|
* activationEnergy if you plan to use the doWork() callback.
|
||||||
* @param maxEnergyReceived The maximum amount of power accepted by the
|
* Anything greater than 1 will prevent Redstone Engines from
|
||||||
* PowerHandler. This should generally be less than 500. Too low and larger
|
* powering this Provider.
|
||||||
* engines will overheat while trying to power the machine. Too high, and
|
* @param maxEnergyReceived
|
||||||
* the engines will never warm up. Greater values also place greater strain
|
* The maximum amount of power accepted by the PowerHandler. This
|
||||||
* on the power net.
|
* should generally be less than 500. Too low and larger engines
|
||||||
* @param activationEnergy If the stored energy is greater than this value,
|
* will overheat while trying to power the machine. Too high, and
|
||||||
* the doWork() callback is called (once per tick).
|
* the engines will never warm up. Greater values also place
|
||||||
* @param maxStoredEnergy The maximum amount of power this PowerHandler can
|
* greater strain on the power net.
|
||||||
* store. Values tend to range between 100 and 5000. With 1000 and 1500
|
* @param activationEnergy
|
||||||
|
* If the stored energy is greater than this value, the doWork()
|
||||||
|
* callback is called (once per tick).
|
||||||
|
* @param maxStoredEnergy
|
||||||
|
* The maximum amount of power this PowerHandler can store.
|
||||||
|
* Values tend to range between 100 and 5000. With 1000 and 1500
|
||||||
* being common.
|
* being common.
|
||||||
*/
|
*/
|
||||||
public void configure(double minEnergyReceived, double maxEnergyReceivedI, double activationEnergy,
|
public void configure(double minEnergyReceived, double maxEnergyReceived, double activationEnergy,
|
||||||
double maxStoredEnergy) {
|
double maxStoredEnergy) {
|
||||||
double localMaxEnergyReceived = maxEnergyReceivedI;
|
double localMaxEnergyReceived = maxEnergyReceived;
|
||||||
|
|
||||||
if (minEnergyReceived > localMaxEnergyReceived) {
|
if (minEnergyReceived > localMaxEnergyReceived) {
|
||||||
localMaxEnergyReceived = minEnergyReceived;
|
localMaxEnergyReceived = minEnergyReceived;
|
||||||
}
|
}
|
||||||
this.minEnergyReceived = minEnergyReceived;
|
|
||||||
this.maxEnergyReceived = localMaxEnergyReceived;
|
|
||||||
this.maxEnergyStored = maxStoredEnergy;
|
|
||||||
this.activationEnergy = activationEnergy;
|
this.activationEnergy = activationEnergy;
|
||||||
|
|
||||||
|
battery.reconfigure(maxStoredEnergy, localMaxEnergyReceived, minEnergyReceived);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows you define perdition in terms of loss/ticks.
|
* Allows you define perdition in terms of loss/ticks.
|
||||||
*
|
* <p/>
|
||||||
* This function is mostly for legacy implementations. See
|
* This function is mostly for legacy implementations. See
|
||||||
* PerditionCalculator for more complex perdition formulas.
|
* PerditionCalculator for more complex perdition formulas.
|
||||||
*
|
*
|
||||||
|
@ -222,7 +242,7 @@ public final class PowerHandler {
|
||||||
/**
|
/**
|
||||||
* Allows you to define a new PerditionCalculator class to handler perdition
|
* Allows you to define a new PerditionCalculator class to handler perdition
|
||||||
* calculations.
|
* calculations.
|
||||||
*
|
* <p/>
|
||||||
* For example if you want exponentially increasing loss based on amount
|
* For example if you want exponentially increasing loss based on amount
|
||||||
* stored.
|
* stored.
|
||||||
*
|
*
|
||||||
|
@ -247,7 +267,7 @@ public final class PowerHandler {
|
||||||
/**
|
/**
|
||||||
* Ticks the power handler. You should call this if you can, but its not
|
* Ticks the power handler. You should call this if you can, but its not
|
||||||
* required.
|
* required.
|
||||||
*
|
* <p/>
|
||||||
* If you don't call it, the possibility exists for some weirdness with the
|
* If you don't call it, the possibility exists for some weirdness with the
|
||||||
* perdition algorithm and work callback as its possible they will not be
|
* perdition algorithm and work callback as its possible they will not be
|
||||||
* called on every tick they otherwise would be. You should be able to
|
* called on every tick they otherwise would be. You should be able to
|
||||||
|
@ -260,13 +280,14 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyPerdition() {
|
private void applyPerdition() {
|
||||||
|
double energyStored = getEnergyStored();
|
||||||
if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) {
|
if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) {
|
||||||
double prev = energyStored;
|
double prev = energyStored;
|
||||||
double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
|
double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
|
||||||
if (newEnergy == 0 || newEnergy < energyStored) {
|
if (newEnergy == 0 || newEnergy < energyStored) {
|
||||||
energyStored = newEnergy;
|
battery.setEnergyStored(energyStored = newEnergy);
|
||||||
} else {
|
} else {
|
||||||
energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
|
battery.setEnergyStored(energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()));
|
||||||
}
|
}
|
||||||
validateEnergy();
|
validateEnergy();
|
||||||
|
|
||||||
|
@ -275,7 +296,7 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyWork() {
|
private void applyWork() {
|
||||||
if (energyStored >= activationEnergy) {
|
if (getEnergyStored() >= activationEnergy) {
|
||||||
if (doWorkTracker.markTimeIfDelay(receptor.getWorld(), 1)) {
|
if (doWorkTracker.markTimeIfDelay(receptor.getWorld(), 1)) {
|
||||||
receptor.doWork(this);
|
receptor.doWork(this);
|
||||||
}
|
}
|
||||||
|
@ -311,6 +332,7 @@ public final class PowerHandler {
|
||||||
|
|
||||||
double result = 0;
|
double result = 0;
|
||||||
|
|
||||||
|
double energyStored = getEnergyStored();
|
||||||
if (energyStored >= min) {
|
if (energyStored >= min) {
|
||||||
if (energyStored <= max) {
|
if (energyStored <= max) {
|
||||||
result = energyStored;
|
result = energyStored;
|
||||||
|
@ -324,6 +346,9 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (energyStored != getEnergyStored()) {
|
||||||
|
battery.setEnergyStored(energyStored);
|
||||||
|
}
|
||||||
|
|
||||||
validateEnergy();
|
validateEnergy();
|
||||||
|
|
||||||
|
@ -340,7 +365,7 @@ public final class PowerHandler {
|
||||||
|
|
||||||
public void readFromNBT(NBTTagCompound data, String tag) {
|
public void readFromNBT(NBTTagCompound data, String tag) {
|
||||||
NBTTagCompound nbt = data.getCompoundTag(tag);
|
NBTTagCompound nbt = data.getCompoundTag(tag);
|
||||||
energyStored = nbt.getDouble("energyStored");
|
battery.setEnergyStored(nbt.getDouble("energyStored"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeToNBT(NBTTagCompound data) {
|
public void writeToNBT(NBTTagCompound data) {
|
||||||
|
@ -349,7 +374,7 @@ public final class PowerHandler {
|
||||||
|
|
||||||
public void writeToNBT(NBTTagCompound data, String tag) {
|
public void writeToNBT(NBTTagCompound data, String tag) {
|
||||||
NBTTagCompound nbt = new NBTTagCompound();
|
NBTTagCompound nbt = new NBTTagCompound();
|
||||||
nbt.setDouble("energyStored", energyStored);
|
nbt.setDouble("energyStored", battery.getEnergyStored());
|
||||||
data.setTag(tag, nbt);
|
data.setTag(tag, nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,15 +384,15 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMinEnergyReceived() {
|
public double getMinEnergyReceived() {
|
||||||
return minEnergyReceived;
|
return PowerHandler.this.getMinEnergyReceived();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMaxEnergyReceived() {
|
public double getMaxEnergyReceived() {
|
||||||
return maxEnergyReceived;
|
return PowerHandler.this.getMaxEnergyReceived();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMaxEnergyStored() {
|
public double getMaxEnergyStored() {
|
||||||
return maxEnergyStored;
|
return PowerHandler.this.getMaxEnergyStored();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getActivationEnergy() {
|
public double getActivationEnergy() {
|
||||||
|
@ -375,7 +400,7 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getEnergyStored() {
|
public double getEnergyStored() {
|
||||||
return energyStored;
|
return PowerHandler.this.getEnergyStored();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getAveragePowerReceived() {
|
public double getAveragePowerReceived() {
|
||||||
|
@ -405,12 +430,12 @@ public final class PowerHandler {
|
||||||
*/
|
*/
|
||||||
public double powerRequest() {
|
public double powerRequest() {
|
||||||
update();
|
update();
|
||||||
return Math.min(maxEnergyReceived, maxEnergyStored - energyStored);
|
return battery.getEnergyRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add power to the PowerReceiver from an external source.
|
* Add power to the PowerReceiver from an external source.
|
||||||
*
|
* <p/>
|
||||||
* IPowerEmitters are responsible for calling this themselves.
|
* IPowerEmitters are responsible for calling this themselves.
|
||||||
*
|
*
|
||||||
* @param quantity
|
* @param quantity
|
||||||
|
@ -420,10 +445,10 @@ public final class PowerHandler {
|
||||||
public double receiveEnergy(Type source, final double quantity, ForgeDirection from) {
|
public double receiveEnergy(Type source, final double quantity, ForgeDirection from) {
|
||||||
double used = quantity;
|
double used = quantity;
|
||||||
if (source == Type.ENGINE) {
|
if (source == Type.ENGINE) {
|
||||||
if (used < minEnergyReceived) {
|
if (used < getMinEnergyReceived()) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (used > maxEnergyReceived) {
|
} else if (used > getMaxEnergyReceived()) {
|
||||||
used = maxEnergyReceived;
|
used = getMaxEnergyReceived();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,7 +461,7 @@ public final class PowerHandler {
|
||||||
applyWork();
|
applyWork();
|
||||||
|
|
||||||
if (source == Type.ENGINE && type.eatsEngineExcess()) {
|
if (source == Type.ENGINE && type.eatsEngineExcess()) {
|
||||||
used = Math.min(quantity, maxEnergyReceived);
|
used = Math.min(quantity, getMaxEnergyReceived());
|
||||||
}
|
}
|
||||||
|
|
||||||
averageReceivedPower = (averageReceivedPower * ROLLING_AVERAGE_NUMERATOR + used) * ROLLING_AVERAGE_DENOMINATOR;
|
averageReceivedPower = (averageReceivedPower * ROLLING_AVERAGE_NUMERATOR + used) * ROLLING_AVERAGE_DENOMINATOR;
|
||||||
|
@ -446,29 +471,16 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the amount the power changed by
|
* @return the amount the power changed by
|
||||||
*/
|
*/
|
||||||
public double addEnergy(double quantityI) {
|
public double addEnergy(double quantity) {
|
||||||
double quantity = quantityI;
|
final double used = battery.addEnergy(quantity);
|
||||||
|
|
||||||
energyStored += quantity;
|
|
||||||
|
|
||||||
if (energyStored > maxEnergyStored) {
|
|
||||||
quantity -= energyStored - maxEnergyStored;
|
|
||||||
energyStored = maxEnergyStored;
|
|
||||||
} else if (energyStored < 0) {
|
|
||||||
quantity -= energyStored;
|
|
||||||
energyStored = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
applyPerdition();
|
applyPerdition();
|
||||||
|
return used;
|
||||||
return quantity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnergy(double quantity) {
|
public void setEnergy(double quantity) {
|
||||||
this.energyStored = quantity;
|
battery.setEnergyStored(quantity);
|
||||||
validateEnergy();
|
validateEnergy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,11 +489,21 @@ public final class PowerHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateEnergy() {
|
private void validateEnergy() {
|
||||||
|
double energyStored = getEnergyStored();
|
||||||
|
double maxEnergyStored = getMaxEnergyStored();
|
||||||
if (energyStored < 0) {
|
if (energyStored < 0) {
|
||||||
energyStored = 0;
|
energyStored = 0;
|
||||||
}
|
}
|
||||||
if (energyStored > maxEnergyStored) {
|
if (energyStored > maxEnergyStored) {
|
||||||
energyStored = maxEnergyStored;
|
energyStored = maxEnergyStored;
|
||||||
}
|
}
|
||||||
|
if (energyStored != battery.getEnergyStored()) {
|
||||||
|
battery.setEnergyStored(energyStored);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class AnonymousBattery {
|
||||||
|
@MjBattery
|
||||||
|
public double mjStored;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
# Master language file
|
# Master language file
|
||||||
|
|
||||||
chat.pipe.power.iron.mode=Switched to %d MJ/t limit
|
chat.pipe.power.iron.mode=Switched to %d MJ/t limit
|
||||||
|
chat.pipe.power.energyConverter=Energy conversion: %s
|
||||||
|
chat.pipe.power.energyConverter.from_old_to_new=From old API to new API
|
||||||
|
chat.pipe.power.energyConverter.from_new_to_old=From new API to old API
|
||||||
|
|
||||||
color.black=Black
|
color.black=Black
|
||||||
color.blue=Blue
|
color.blue=Blue
|
||||||
|
@ -203,6 +206,8 @@ tile.refineryBlock.name=Refinery
|
||||||
tile.spring.oil.name=Oil Spring
|
tile.spring.oil.name=Oil Spring
|
||||||
tile.spring.water.name=Water Spring
|
tile.spring.water.name=Water Spring
|
||||||
tile.tankBlock.name=Tank
|
tile.tankBlock.name=Tank
|
||||||
|
tile.energyConverter.name=Energy Converter
|
||||||
|
tile.energyConverter.tooltip=Convert BC energy between old and new power API|Added for compatibility with other mods|while they not migrated to new power API.|Could be removed in the future.
|
||||||
|
|
||||||
tile.architect.rotate=Rotate: On
|
tile.architect.rotate=Rotate: On
|
||||||
tile.architect.norotate=Rotate: Off
|
tile.architect.norotate=Rotate: Off
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 206 B |
|
@ -53,6 +53,7 @@ import buildcraft.core.Version;
|
||||||
import buildcraft.core.proxy.CoreProxy;
|
import buildcraft.core.proxy.CoreProxy;
|
||||||
import buildcraft.core.triggers.BCTrigger;
|
import buildcraft.core.triggers.BCTrigger;
|
||||||
import buildcraft.energy.BlockBuildcraftFluid;
|
import buildcraft.energy.BlockBuildcraftFluid;
|
||||||
|
import buildcraft.energy.BlockEnergyConverter;
|
||||||
import buildcraft.energy.BlockEnergyEmitter;
|
import buildcraft.energy.BlockEnergyEmitter;
|
||||||
import buildcraft.energy.BlockEnergyReceiver;
|
import buildcraft.energy.BlockEnergyReceiver;
|
||||||
import buildcraft.energy.BlockEngine;
|
import buildcraft.energy.BlockEngine;
|
||||||
|
@ -60,8 +61,10 @@ import buildcraft.energy.BucketHandler;
|
||||||
import buildcraft.energy.EnergyProxy;
|
import buildcraft.energy.EnergyProxy;
|
||||||
import buildcraft.energy.GuiHandler;
|
import buildcraft.energy.GuiHandler;
|
||||||
import buildcraft.energy.ItemBucketBuildcraft;
|
import buildcraft.energy.ItemBucketBuildcraft;
|
||||||
|
import buildcraft.energy.ItemEnergyConverter;
|
||||||
import buildcraft.energy.ItemEngine;
|
import buildcraft.energy.ItemEngine;
|
||||||
import buildcraft.energy.SchematicEngine;
|
import buildcraft.energy.SchematicEngine;
|
||||||
|
import buildcraft.energy.TileEnergyConverter;
|
||||||
import buildcraft.energy.TileEnergyEmitter;
|
import buildcraft.energy.TileEnergyEmitter;
|
||||||
import buildcraft.energy.TileEnergyReceiver;
|
import buildcraft.energy.TileEnergyReceiver;
|
||||||
import buildcraft.energy.TileEngine.EnergyStage;
|
import buildcraft.energy.TileEngine.EnergyStage;
|
||||||
|
@ -89,6 +92,7 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
public static Block blockOil;
|
public static Block blockOil;
|
||||||
public static Block blockFuel;
|
public static Block blockFuel;
|
||||||
public static Block blockRedPlasma;
|
public static Block blockRedPlasma;
|
||||||
|
public static Block blockEnergyConverter;
|
||||||
public static Item bucketOil;
|
public static Item bucketOil;
|
||||||
public static Item bucketFuel;
|
public static Item bucketFuel;
|
||||||
public static Item bucketRedPlasma;
|
public static Item bucketRedPlasma;
|
||||||
|
@ -199,7 +203,7 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
BuildCraftCore.mainConfiguration.save();
|
BuildCraftCore.mainConfiguration.save();
|
||||||
|
|
||||||
if (oilDesertBiomeId > 0) {
|
if (oilDesertBiomeId > 0) {
|
||||||
if (BiomeGenBase.getBiomeGenArray () [oilDesertBiomeId] != null) {
|
if (BiomeGenBase.getBiomeGenArray()[oilDesertBiomeId] != null) {
|
||||||
oilDesertBiomeId = findUnusedBiomeID("oilDesert");
|
oilDesertBiomeId = findUnusedBiomeID("oilDesert");
|
||||||
// save changes to config file
|
// save changes to config file
|
||||||
BuildCraftCore.mainConfiguration.get("biomes", "biomeOilDesert", oilDesertBiomeId).set(oilDesertBiomeId);
|
BuildCraftCore.mainConfiguration.get("biomes", "biomeOilDesert", oilDesertBiomeId).set(oilDesertBiomeId);
|
||||||
|
@ -209,7 +213,7 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oilOceanBiomeId > 0) {
|
if (oilOceanBiomeId > 0) {
|
||||||
if (BiomeGenBase.getBiomeGenArray () [oilOceanBiomeId] != null) {
|
if (BiomeGenBase.getBiomeGenArray()[oilOceanBiomeId] != null) {
|
||||||
oilOceanBiomeId = findUnusedBiomeID("oilOcean");
|
oilOceanBiomeId = findUnusedBiomeID("oilOcean");
|
||||||
// save changes to config file
|
// save changes to config file
|
||||||
BuildCraftCore.mainConfiguration.get("biomes", "biomeOilOcean", oilOceanBiomeId).set(oilOceanBiomeId);
|
BuildCraftCore.mainConfiguration.get("biomes", "biomeOilOcean", oilOceanBiomeId).set(oilOceanBiomeId);
|
||||||
|
@ -221,6 +225,12 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
engineBlock = new BlockEngine();
|
engineBlock = new BlockEngine();
|
||||||
CoreProxy.proxy.registerBlock(engineBlock, ItemEngine.class);
|
CoreProxy.proxy.registerBlock(engineBlock, ItemEngine.class);
|
||||||
|
|
||||||
|
if (BuildCraftCore.mainConfiguration.get(Configuration.CATEGORY_GENERAL, "energyConverter", true,
|
||||||
|
"Set true for enable energy converter").getBoolean(true)) {
|
||||||
|
blockEnergyConverter = new BlockEnergyConverter();
|
||||||
|
CoreProxy.proxy.registerBlock(blockEnergyConverter, ItemEnergyConverter.class);
|
||||||
|
CoreProxy.proxy.registerTileEntity(TileEnergyConverter.class, "EnergyConverter");
|
||||||
|
}
|
||||||
|
|
||||||
// Oil and fuel
|
// Oil and fuel
|
||||||
buildcraftFluidOil = new Fluid("oil").setDensity(800).setViscosity(1500);
|
buildcraftFluidOil = new Fluid("oil").setDensity(800).setViscosity(1500);
|
||||||
|
@ -312,11 +322,11 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
// Receiver / emitter
|
// Receiver / emitter
|
||||||
|
|
||||||
if (!BuildCraftCore.NEXTGEN_PREALPHA) {
|
if (!BuildCraftCore.NEXTGEN_PREALPHA) {
|
||||||
emitterBlock = new BlockEnergyEmitter ();
|
emitterBlock = new BlockEnergyEmitter();
|
||||||
CoreProxy.proxy.registerBlock(emitterBlock.setBlockName("energyEmitterBlock"));
|
CoreProxy.proxy.registerBlock(emitterBlock.setBlockName("energyEmitterBlock"));
|
||||||
CoreProxy.proxy.registerTileEntity(TileEnergyEmitter.class, "net.minecraft.src.builders.TileEnergyEmitter");
|
CoreProxy.proxy.registerTileEntity(TileEnergyEmitter.class, "net.minecraft.src.builders.TileEnergyEmitter");
|
||||||
|
|
||||||
receiverBlock = new BlockEnergyReceiver ();
|
receiverBlock = new BlockEnergyReceiver();
|
||||||
CoreProxy.proxy.registerBlock(receiverBlock.setBlockName("energyReceiverBlock"));
|
CoreProxy.proxy.registerBlock(receiverBlock.setBlockName("energyReceiverBlock"));
|
||||||
CoreProxy.proxy.registerTileEntity(TileEnergyReceiver.class, "net.minecraft.src.builders.TileEnergyReceiver");
|
CoreProxy.proxy.registerTileEntity(TileEnergyReceiver.class, "net.minecraft.src.builders.TileEnergyReceiver");
|
||||||
}
|
}
|
||||||
|
@ -366,9 +376,15 @@ public class BuildCraftEnergy extends BuildCraftMod {
|
||||||
'g', Blocks.glass, 'G', BuildCraftCore.stoneGearItem, 'p', Blocks.piston);
|
'g', Blocks.glass, 'G', BuildCraftCore.stoneGearItem, 'p', Blocks.piston);
|
||||||
CoreProxy.proxy.addCraftingRecipe(new ItemStack(engineBlock, 1, 2), "www", " g ", "GpG", 'w', Items.iron_ingot,
|
CoreProxy.proxy.addCraftingRecipe(new ItemStack(engineBlock, 1, 2), "www", " g ", "GpG", 'w', Items.iron_ingot,
|
||||||
'g', Blocks.glass, 'G', BuildCraftCore.ironGearItem, 'p', Blocks.piston);
|
'g', Blocks.glass, 'G', BuildCraftCore.ironGearItem, 'p', Blocks.piston);
|
||||||
|
|
||||||
|
if (blockEnergyConverter != null) {
|
||||||
|
CoreProxy.proxy.addCraftingRecipe(new ItemStack(blockEnergyConverter, 1), "rcr", "sgs", "rcr",
|
||||||
|
'r', Items.redstone, 'c', BuildCraftTransport.pipePowerCobblestone,
|
||||||
|
's', BuildCraftTransport.pipePowerStone, 'g', Blocks.glass);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int findUnusedBiomeID (String biomeName) {
|
private int findUnusedBiomeID(String biomeName) {
|
||||||
int freeBiomeID = 0;
|
int freeBiomeID = 0;
|
||||||
// code to find a free biome
|
// code to find a free biome
|
||||||
for (int i = 1; i < 256; i++) {
|
for (int i = 1; i < 256; i++) {
|
||||||
|
|
65
common/buildcraft/energy/BlockEnergyConverter.java
Normal file
65
common/buildcraft/energy/BlockEnergyConverter.java
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/**
|
||||||
|
* 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.energy;
|
||||||
|
|
||||||
|
import net.minecraft.block.material.Material;
|
||||||
|
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.IIcon;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
|
import buildcraft.core.BlockBuildCraft;
|
||||||
|
import buildcraft.core.IItemPipe;
|
||||||
|
|
||||||
|
public class BlockEnergyConverter extends BlockBuildCraft {
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
private IIcon blockTexture;
|
||||||
|
|
||||||
|
public BlockEnergyConverter() {
|
||||||
|
super(Material.ground);
|
||||||
|
setBlockName("energyConverter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer player, int side, float par7, float par8, float par9) {
|
||||||
|
TileEntity tile = world.getTileEntity(i, j, k);
|
||||||
|
|
||||||
|
// Do not open guis when having a pipe in hand
|
||||||
|
if (player.getCurrentEquippedItem() != null &&
|
||||||
|
player.getCurrentEquippedItem().getItem() instanceof IItemPipe) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tile instanceof TileEnergyConverter
|
||||||
|
&& ((TileEnergyConverter) tile).onBlockActivated(player, ForgeDirection.getOrientation(side));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TileEntity createNewTileEntity(World world, int metadata) {
|
||||||
|
return new TileEnergyConverter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
public void registerBlockIcons(IIconRegister par1IconRegister) {
|
||||||
|
blockTexture = par1IconRegister.registerIcon("buildcraft:blockEnergyConverter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
public IIcon getIcon(int i, int j) {
|
||||||
|
return blockTexture;
|
||||||
|
}
|
||||||
|
}
|
31
common/buildcraft/energy/ItemEnergyConverter.java
Normal file
31
common/buildcraft/energy/ItemEnergyConverter.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/**
|
||||||
|
* 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.energy;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.item.ItemBlock;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
public class ItemEnergyConverter extends ItemBlock {
|
||||||
|
public ItemEnergyConverter(Block block) {
|
||||||
|
super(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean adv) {
|
||||||
|
super.addInformation(itemStack, player, list, adv);
|
||||||
|
list.add(TileEnergyConverter.getLocalizedModeName(itemStack));
|
||||||
|
list.add("");
|
||||||
|
// This is a bit too big in the tooltip at this stage.
|
||||||
|
// list.addAll(Arrays.asList(StringUtils.localize("tile.energyConverter.tooltip").split("\\|")));
|
||||||
|
}
|
||||||
|
}
|
158
common/buildcraft/energy/TileEnergyConverter.java
Normal file
158
common/buildcraft/energy/TileEnergyConverter.java
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
/**
|
||||||
|
* 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.energy;
|
||||||
|
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.ChatComponentText;
|
||||||
|
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
|
import buildcraft.api.core.NetworkData;
|
||||||
|
import buildcraft.api.mj.MjAPI;
|
||||||
|
import buildcraft.api.mj.MjBattery;
|
||||||
|
import buildcraft.api.power.IPowerReceptor;
|
||||||
|
import buildcraft.api.power.PowerHandler;
|
||||||
|
import buildcraft.api.tools.IToolWrench;
|
||||||
|
import buildcraft.core.TileBuffer;
|
||||||
|
import buildcraft.core.TileBuildCraft;
|
||||||
|
import buildcraft.core.utils.StringUtils;
|
||||||
|
|
||||||
|
public class TileEnergyConverter extends TileBuildCraft implements IPowerReceptor {
|
||||||
|
private static enum Mode {
|
||||||
|
FromOldToNew("from_old_to_new"), FromNewToOld("from_new_to_old");
|
||||||
|
private final String localizeName;
|
||||||
|
|
||||||
|
Mode(String localizeName) {
|
||||||
|
this.localizeName = localizeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mode next() {
|
||||||
|
if (this == FromOldToNew) {
|
||||||
|
return FromNewToOld;
|
||||||
|
}
|
||||||
|
return FromOldToNew;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MjBattery(maxCapacity = 1000, maxReceivedPerCycle = 64, minimumConsumption = 1)
|
||||||
|
@NetworkData
|
||||||
|
private double mjStored = 0;
|
||||||
|
@NetworkData
|
||||||
|
private Mode mode = Mode.FromOldToNew;
|
||||||
|
private TileBuffer[] tileCache;
|
||||||
|
private PowerHandler powerHandler, zeroPowerHandler;
|
||||||
|
|
||||||
|
public TileEnergyConverter() {
|
||||||
|
powerHandler = new PowerHandler(this, PowerHandler.Type.MACHINE, MjAPI.getMjBattery(this));
|
||||||
|
zeroPowerHandler = new PowerHandler(this, PowerHandler.Type.MACHINE);
|
||||||
|
zeroPowerHandler.configure(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getLocalizedModeName(ItemStack stack) {
|
||||||
|
int mode = 0;
|
||||||
|
if (stack != null && stack.getTagCompound() != null) {
|
||||||
|
mode = stack.getTagCompound().getInteger("converterMode");
|
||||||
|
if (mode >= Mode.values().length || mode < 0) {
|
||||||
|
mode = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.localize("chat.pipe.power.energyConverter." + Mode.values()[mode].localizeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean onBlockActivated(EntityPlayer player, ForgeDirection side) {
|
||||||
|
if (!getWorld().isRemote) {
|
||||||
|
Item equipped = player.getCurrentEquippedItem() != null ? player.getCurrentEquippedItem().getItem() : null;
|
||||||
|
|
||||||
|
if (equipped instanceof IToolWrench && ((IToolWrench) equipped).canWrench(player, xCoord, yCoord, zCoord)) {
|
||||||
|
mode = mode.next();
|
||||||
|
|
||||||
|
player.addChatMessage(new ChatComponentText(String.format(
|
||||||
|
StringUtils.localize("chat.pipe.power.energyConverter"),
|
||||||
|
StringUtils.localize("chat.pipe.power.energyConverter." + mode.localizeName))));
|
||||||
|
|
||||||
|
sendNetworkUpdate();
|
||||||
|
|
||||||
|
((IToolWrench) equipped).wrenchUsed(player, xCoord, yCoord, zCoord);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !player.isSneaking();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToNBT(NBTTagCompound nbt) {
|
||||||
|
super.writeToNBT(nbt);
|
||||||
|
nbt.setDouble("mjStored", mjStored);
|
||||||
|
nbt.setInteger("converterMode", mode.ordinal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readFromNBT(NBTTagCompound nbt) {
|
||||||
|
super.readFromNBT(nbt);
|
||||||
|
mjStored = nbt.getDouble("mjStored");
|
||||||
|
mode = Mode.values()[nbt.getInteger("converterMode")];
|
||||||
|
}
|
||||||
|
|
||||||
|
public TileBuffer getTileBuffer(ForgeDirection side) {
|
||||||
|
if (tileCache == null) {
|
||||||
|
tileCache = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tileCache[side.ordinal()];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateEntity() {
|
||||||
|
super.updateEntity();
|
||||||
|
if (mode == Mode.FromOldToNew) {
|
||||||
|
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
|
||||||
|
TileEntity tile = getTileBuffer(side).getTile();
|
||||||
|
if (tile instanceof TileEnergyConverter) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
MjAPI.BatteryObject object = MjAPI.getMjBattery(tile);
|
||||||
|
if (object != null && mjStored > 0) {
|
||||||
|
double wantToUse = Math.min(mjStored, object.getEnergyRequested());
|
||||||
|
object.addEnergy(wantToUse);
|
||||||
|
mjStored -= wantToUse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (mode == Mode.FromNewToOld) {
|
||||||
|
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
|
||||||
|
TileEntity tile = getTileBuffer(side).getTile();
|
||||||
|
if (tile instanceof TileEnergyConverter) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (tile instanceof IPowerReceptor && mjStored > 0) {
|
||||||
|
IPowerReceptor receptor = (IPowerReceptor) tile;
|
||||||
|
PowerHandler.PowerReceiver powerReceiver = receptor.getPowerReceiver(side.getOpposite());
|
||||||
|
double wantToUse = Math.min(mjStored, powerReceiver.getMaxEnergyReceived());
|
||||||
|
if (wantToUse > powerReceiver.getMinEnergyReceived()) {
|
||||||
|
powerReceiver.receiveEnergy(PowerHandler.Type.MACHINE, wantToUse, side);
|
||||||
|
mjStored -= wantToUse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PowerHandler.PowerReceiver getPowerReceiver(ForgeDirection side) {
|
||||||
|
return (mode == Mode.FromOldToNew ? powerHandler : zeroPowerHandler).getPowerReceiver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doWork(PowerHandler workProvider) {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue