Possible Tickless compatible system PowerProvider
doWork() callback is now handled in receiveEnergy() as well as in the tick update. So you can respond more effectively even if you don't want to tick.
This commit is contained in:
parent
4f6ba954cd
commit
013fe4ecb4
13 changed files with 55 additions and 46 deletions
|
@ -14,23 +14,26 @@ import buildcraft.api.core.SafeTimeTracker;
|
|||
|
||||
public final class PowerProvider {
|
||||
|
||||
protected int minEnergyReceived;
|
||||
protected int maxEnergyReceived;
|
||||
protected int maxEnergyStored;
|
||||
protected int minActivationEnergy;
|
||||
protected float energyStored = 0;
|
||||
protected int powerLoss = 1;
|
||||
protected int powerLossRegularity = 1;
|
||||
private int minEnergyReceived;
|
||||
private int maxEnergyReceived;
|
||||
private int maxEnergyStored;
|
||||
private int minActivationEnergy;
|
||||
private float energyStored = 0;
|
||||
private int powerLoss = 1;
|
||||
private int powerLossRegularity = 1;
|
||||
public final boolean canAcceptPowerFromPipes;
|
||||
public SafeTimeTracker energyLossTracker = new SafeTimeTracker();
|
||||
public int[] powerSources = {0, 0, 0, 0, 0, 0};
|
||||
private SafeTimeTracker energyLossTracker = new SafeTimeTracker();
|
||||
private SafeTimeTracker doWorkTracker = new SafeTimeTracker();
|
||||
public final int[] powerSources = {0, 0, 0, 0, 0, 0};
|
||||
private final IPowerReceptor receptor;
|
||||
|
||||
public PowerProvider() {
|
||||
this.canAcceptPowerFromPipes = true;
|
||||
public PowerProvider(IPowerReceptor receptor) {
|
||||
this(receptor, true);
|
||||
}
|
||||
|
||||
public PowerProvider(boolean canAcceptPowerFromPipes) {
|
||||
public PowerProvider(IPowerReceptor receptor, boolean canAcceptPowerFromPipes) {
|
||||
this.canAcceptPowerFromPipes = canAcceptPowerFromPipes;
|
||||
this.receptor = receptor;
|
||||
}
|
||||
|
||||
public int getMinEnergyReceived() {
|
||||
|
@ -66,21 +69,9 @@ public final class PowerProvider {
|
|||
}
|
||||
|
||||
public boolean update(IPowerReceptor receptor) {
|
||||
TileEntity tile = (TileEntity) receptor;
|
||||
boolean result = false;
|
||||
applyPerdition();
|
||||
|
||||
if (energyStored >= minActivationEnergy) {
|
||||
receptor.doWork(this);
|
||||
result = true;
|
||||
}
|
||||
|
||||
if (powerLoss > 0 && energyLossTracker.markTimeIfDelay(tile.worldObj, powerLossRegularity)) {
|
||||
|
||||
energyStored -= powerLoss;
|
||||
if (energyStored < 0) {
|
||||
energyStored = 0;
|
||||
}
|
||||
}
|
||||
boolean work = applyWork();
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
if (powerSources[i] > 0) {
|
||||
|
@ -88,7 +79,26 @@ public final class PowerProvider {
|
|||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return work;
|
||||
}
|
||||
|
||||
private void applyPerdition() {
|
||||
TileEntity tile = (TileEntity) receptor;
|
||||
if (powerLoss > 0 && energyLossTracker.markTimeIfDelay(tile.worldObj, powerLossRegularity)) {
|
||||
energyStored -= powerLoss;
|
||||
if (energyStored < 0) {
|
||||
energyStored = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean applyWork() {
|
||||
TileEntity tile = (TileEntity) receptor;
|
||||
if (energyStored >= minActivationEnergy && doWorkTracker.markTimeIfDelay(tile.worldObj, 1)) {
|
||||
receptor.doWork(this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public float useEnergy(float min, float max, boolean doUse) {
|
||||
|
@ -143,6 +153,10 @@ public final class PowerProvider {
|
|||
quantity -= energyStored - maxEnergyStored;
|
||||
energyStored = maxEnergyStored;
|
||||
}
|
||||
|
||||
applyPerdition();
|
||||
applyWork();
|
||||
|
||||
return quantity;
|
||||
}
|
||||
|
||||
|
|
|
@ -177,7 +177,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP
|
|||
public TileBuilder() {
|
||||
super();
|
||||
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
powerProvider.configure(25, 25, 25, 25);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ public class TileFiller extends TileBuildCraft implements ISidedInventory, IPowe
|
|||
|
||||
public TileFiller() {
|
||||
contents = new ItemStack[getSizeInventory()];
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
initPowerProvider();
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ public class TileEngine extends TileBuildCraft implements IPowerReceptor, IInven
|
|||
public boolean isRedstonePowered = false;
|
||||
|
||||
public TileEngine() {
|
||||
provider = new PowerProvider(false);
|
||||
provider = new PowerProvider(this, false);
|
||||
provider.configurePowerPerdition(1, 100);
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ public class TileMiningWell extends TileBuildCraft implements IMachine, IPowerRe
|
|||
PowerProvider powerProvider;
|
||||
|
||||
public TileMiningWell() {
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
powerProvider.configure(100, 100, 60, 1000);
|
||||
powerProvider.configurePowerPerdition(1, 1);
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor
|
|||
private PowerProvider powerProvider;
|
||||
|
||||
public TilePump() {
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
initPowerProvider();
|
||||
tank = new LiquidTank(MAX_LIQUID);
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ public class TileQuarry extends TileBuildCraft implements IMachine, IPowerRecept
|
|||
public static final int MAX_ENERGY = 15000;
|
||||
|
||||
public TileQuarry() {
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
initPowerProvider();
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ public class TileRefinery extends TileBuildCraft implements ITankContainer, IPow
|
|||
private boolean isActive;
|
||||
|
||||
public TileRefinery() {
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
initPowerProvider();
|
||||
|
||||
filters[0] = 0;
|
||||
|
|
|
@ -40,7 +40,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
|
|||
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
|
||||
|
||||
public TileLaser() {
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
initPowerProvider();
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ public class PipeItemsObsidian extends Pipe implements IPowerReceptor {
|
|||
entitiesDropped[i] = -1;
|
||||
}
|
||||
|
||||
powerProvider = new PowerProvider();
|
||||
powerProvider = new PowerProvider(this);
|
||||
powerProvider.configure(1, 64, 1, 256);
|
||||
powerProvider.configurePowerPerdition(1, 1);
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ public class PipeItemsWood extends Pipe implements IPowerReceptor {
|
|||
protected PipeItemsWood(PipeTransportItems transport, PipeLogic logic, int itemID) {
|
||||
super(transport, logic, itemID);
|
||||
|
||||
powerProvider = new PowerProvider(false);
|
||||
powerProvider = new PowerProvider(this, false);
|
||||
powerProvider.configure(1, 64, 1, 64);
|
||||
powerProvider.configurePowerPerdition(64, 1);
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ public class PipeLiquidsWood extends Pipe implements IPowerReceptor {
|
|||
protected PipeLiquidsWood(PipeLogic logic, int itemID) {
|
||||
super(new PipeTransportLiquids(), logic, itemID);
|
||||
|
||||
powerProvider = new PowerProvider(false);
|
||||
powerProvider = new PowerProvider(this, false);
|
||||
powerProvider.configure(1, 100, 1, 250);
|
||||
powerProvider.configurePowerPerdition(1, 1);
|
||||
}
|
||||
|
|
|
@ -12,15 +12,12 @@ import buildcraft.BuildCraftTransport;
|
|||
import buildcraft.api.core.IIconProvider;
|
||||
import buildcraft.api.power.IPowerReceptor;
|
||||
import buildcraft.api.power.PowerProvider;
|
||||
import buildcraft.core.utils.Utils;
|
||||
import buildcraft.transport.Pipe;
|
||||
import buildcraft.transport.PipeIconProvider;
|
||||
import buildcraft.transport.PipeTransportPower;
|
||||
import buildcraft.transport.TileGenericPipe;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
public class PipePowerWood extends Pipe implements IPowerReceptor {
|
||||
|
||||
|
@ -33,7 +30,7 @@ public class PipePowerWood extends Pipe implements IPowerReceptor {
|
|||
public PipePowerWood(int itemID) {
|
||||
super(new PipeTransportPower(), new PipeLogicWood(), itemID);
|
||||
|
||||
powerProvider = new PowerProvider(false);
|
||||
powerProvider = new PowerProvider(this, false);
|
||||
initPowerProvider();
|
||||
((PipeTransportPower) transport).initFromPipe(getClass());
|
||||
}
|
||||
|
@ -63,7 +60,6 @@ public class PipePowerWood extends Pipe implements IPowerReceptor {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PowerProvider getPowerProvider(ForgeDirection side) {
|
||||
return powerProvider;
|
||||
|
@ -96,10 +92,10 @@ public class PipePowerWood extends Pipe implements IPowerReceptor {
|
|||
sources++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (sources <= 0)
|
||||
return;
|
||||
|
||||
|
||||
float energyToRemove;
|
||||
|
||||
if (powerProvider.getEnergyStored() > 40) {
|
||||
|
@ -138,7 +134,6 @@ public class PipePowerWood extends Pipe implements IPowerReceptor {
|
|||
return !full;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound data) {
|
||||
super.writeToNBT(data);
|
||||
|
|
Loading…
Reference in a new issue