Skip to content

Commit

Permalink
Power API rewrite
Browse files Browse the repository at this point in the history
PowerFrameWork is gone.
PowerProvider is final.
Can have multiple PowerProviders (one for each block side).
PowerProviders can either accept power from pipes or they can't, defined
in constructor.
Removed a bunch of excess code that just cluttered the API.
  • Loading branch information
CovertJaguar committed Jun 25, 2013
1 parent d7fb14b commit 1f8c9f7
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 619 deletions.
13 changes: 0 additions & 13 deletions common/buildcraft/BuildCraftCore.java
Expand Up @@ -28,7 +28,6 @@
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.core.IIconProvider;
import buildcraft.api.gates.ActionManager;
import buildcraft.api.power.PowerFramework;
import buildcraft.core.BlockIndex;
import buildcraft.core.BlockSpring;
import buildcraft.core.BuildCraftConfiguration;
Expand All @@ -41,7 +40,6 @@
import buildcraft.core.ItemBuildCraft;
import buildcraft.core.ItemSpring;
import buildcraft.core.ItemWrench;
import buildcraft.core.RedstonePowerFramework;
import buildcraft.core.SpringPopulate;
import buildcraft.core.TickHandlerCoreClient;
import buildcraft.core.Version;
Expand Down Expand Up @@ -212,17 +210,6 @@ public void loadConfiguration(FMLPreInitializationEvent evt) {
longFactor.comment = "delay between full client sync packets, increasing it saves bandwidth, decreasing makes for better client syncronization.";
longUpdateFactor = longFactor.getInt(40);

String powerFrameworkClassName = "buildcraft.energy.PneumaticPowerFramework";
if (!forcePneumaticPower) {
powerFrameworkClassName = powerFrameworkClass.getString();
}
try {
PowerFramework.currentFramework = (PowerFramework) Class.forName(powerFrameworkClassName).getConstructor().newInstance();
} catch (Throwable e) {
bcLog.throwing("BuildCraftCore", "loadConfiguration", e);
PowerFramework.currentFramework = new RedstonePowerFramework();
}

Property wrenchId = BuildCraftCore.mainConfiguration.getItem("wrench.id", DefaultProps.WRENCH_ID);

wrenchItem = (new ItemWrench(wrenchId.getInt(DefaultProps.WRENCH_ID))).setUnlocalizedName("wrenchItem");
Expand Down
41 changes: 0 additions & 41 deletions common/buildcraft/api/power/IPowerProvider.java

This file was deleted.

17 changes: 2 additions & 15 deletions common/buildcraft/api/power/IPowerReceptor.java
Expand Up @@ -11,20 +11,7 @@

public interface IPowerReceptor {

public void setPowerProvider(IPowerProvider provider);
public PowerProvider getPowerProvider(ForgeDirection side);

public IPowerProvider getPowerProvider();

public void doWork();

/**
* Used to request power from pipes. The return cannot be relied on to be
* anything more than a approximate guide to the power needed. When
* transferring power, you much check the return value of
* PowerProvider.receiverEnergy().
*
* @param from
* @return
*/
public int powerRequest(ForgeDirection from);
public void doWork(PowerProvider workProvider);
}
55 changes: 0 additions & 55 deletions common/buildcraft/api/power/PowerFramework.java

This file was deleted.

108 changes: 37 additions & 71 deletions common/buildcraft/api/power/PowerProvider.java
@@ -1,104 +1,77 @@
/**
* Copyright (c) SpaceToad, 2011
* 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
/**
* Copyright (c) SpaceToad, 2011 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.power;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import buildcraft.api.core.SafeTimeTracker;

public abstract class PowerProvider implements IPowerProvider {
public final class PowerProvider {

protected int latency;
protected int minEnergyReceived;
protected int maxEnergyReceived;
protected int maxEnergyStored;
protected int minActivationEnergy;
protected float energyStored = 0;

protected int powerLoss = 1;
protected int powerLossRegularity = 1;

public SafeTimeTracker timeTracker = new SafeTimeTracker();
public final boolean canAcceptPowerFromPipes;
public SafeTimeTracker energyLossTracker = new SafeTimeTracker();
public int[] powerSources = {0, 0, 0, 0, 0, 0};

public int[] powerSources = { 0, 0, 0, 0, 0, 0 };

@Override
public SafeTimeTracker getTimeTracker() {
return this.timeTracker;
public PowerProvider() {
this.canAcceptPowerFromPipes = true;
}

@Override
public int getLatency() {
return this.latency;
public PowerProvider(boolean canAcceptPowerFromPipes) {
this.canAcceptPowerFromPipes = canAcceptPowerFromPipes;
}

@Override
public int getMinEnergyReceived() {
return this.minEnergyReceived;
}

@Override
public int getMaxEnergyReceived() {
return this.maxEnergyReceived;
}

@Override
public int getMaxEnergyStored() {
return this.maxEnergyStored;
}

@Override
public int getActivationEnergy() {
return this.minActivationEnergy;
}

@Override
public float getEnergyStored() {
return this.energyStored;
}

@Override
public void configure(int latency, int minEnergyReceived, int maxEnergyReceived, int minActivationEnergy, int maxStoredEnergy) {
this.latency = latency;
public void configure(int minEnergyReceived, int maxEnergyReceived, int minActivationEnergy, int maxStoredEnergy) {
this.minEnergyReceived = minEnergyReceived;
this.maxEnergyReceived = maxEnergyReceived;
this.maxEnergyStored = maxStoredEnergy;
this.minActivationEnergy = minActivationEnergy;
}

@Override
public void configurePowerPerdition(int powerLoss, int powerLossRegularity) {
this.powerLoss = powerLoss;
this.powerLossRegularity = powerLossRegularity;
}

@Override
public boolean update(IPowerReceptor receptor) {
if (!preConditions(receptor))
return false;

TileEntity tile = (TileEntity) receptor;
boolean result = false;

if (energyStored >= minActivationEnergy) {
if (latency == 0) {
receptor.doWork();
result = true;
} else {
if (timeTracker.markTimeIfDelay(tile.worldObj, latency)) {
receptor.doWork();
result = true;
}
}
receptor.doWork(this);
result = true;
}

if (powerLoss > 0 && energyLossTracker.markTimeIfDelay(tile.worldObj, powerLossRegularity)) {
Expand All @@ -118,12 +91,6 @@ public boolean update(IPowerReceptor receptor) {
return result;
}

@Override
public boolean preConditions(IPowerReceptor receptor) {
return true;
}

@Override
public float useEnergy(float min, float max, boolean doUse) {
float result = 0;

Expand All @@ -144,43 +111,42 @@ public float useEnergy(float min, float max, boolean doUse) {
return result;
}

@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
latency = nbttagcompound.getInteger("latency");
minEnergyReceived = nbttagcompound.getInteger("minEnergyReceived");
maxEnergyReceived = nbttagcompound.getInteger("maxEnergyReceived");
maxEnergyStored = nbttagcompound.getInteger("maxStoreEnergy");
minActivationEnergy = nbttagcompound.getInteger("minActivationEnergy");
public void readFromNBT(NBTTagCompound data) {
readFromNBT(data, "powerProvider");
}

try {
energyStored = nbttagcompound.getFloat("storedEnergy");
} catch (Throwable c) {
energyStored = 0;
}
public void readFromNBT(NBTTagCompound data, String tag) {
NBTTagCompound nbt = data.getCompoundTag(tag);
energyStored = nbt.getFloat("storedEnergy");
}

public void writeToNBT(NBTTagCompound data) {
writeToNBT(data, "powerProvider");
}

public void writeToNBT(NBTTagCompound data, String tag) {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setFloat("storedEnergy", energyStored);
data.setCompoundTag(tag, nbt);
}

@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
nbttagcompound.setInteger("latency", latency);
nbttagcompound.setInteger("minEnergyReceived", minEnergyReceived);
nbttagcompound.setInteger("maxEnergyReceived", maxEnergyReceived);
nbttagcompound.setInteger("maxStoreEnergy", maxEnergyStored);
nbttagcompound.setInteger("minActivationEnergy", minActivationEnergy);
nbttagcompound.setFloat("storedEnergy", energyStored);
public int powerRequest() {
float needed = maxEnergyStored - energyStored;
return (int) Math.ceil(Math.min(maxEnergyReceived, needed));
}

@Override
public void receiveEnergy(float quantity, ForgeDirection from) {
public float receiveEnergy(float quantity, ForgeDirection from) {
powerSources[from.ordinal()] = 2;

energyStored += quantity;

if (energyStored > maxEnergyStored) {
quantity -= energyStored - maxEnergyStored;
energyStored = maxEnergyStored;
}
return quantity;
}

@Override
public boolean isPowerSource(ForgeDirection from) {
return powerSources[from.ordinal()] != 0;
}
Expand Down

0 comments on commit 1f8c9f7

Please sign in to comment.