Added IPowerLess interface

Mainly worthless but is used to make it easier for other classes to
check if a tile runs without power. Example is in NetworkSharedPower
class were it need to check for to see if it can run power less based
off it network parts.
This commit is contained in:
DarkGuardsman 2013-07-29 02:35:10 -04:00
parent 4788e2d51a
commit e306156684
4 changed files with 116 additions and 29 deletions

View file

@ -0,0 +1,20 @@
package dark.api;
import universalelectricity.core.block.IElectricalStorage;
/** Tiles that use NetworkSharedPower class should implements this. All methods in IElectricalStorage
* should point to the network instead of the tile. This is why more energy methods are added to
* this interface
*
* @author DarkGuardsman */
public interface INetworkEnergyPart extends INetworkPart, IElectricalStorage
{
/** Gets the energy stored in the part */
public float getPartEnergy();
/** Gets the max energy storage limit of the part */
public float getPartMaxEnergy();
/** Sets the energy stored in the part */
public void setPartEnergy(float energy);
}

View file

@ -0,0 +1,10 @@
package dark.api;
public interface IPowerLess
{
/** Should this run without power */
public boolean runPowerLess();
/** Set if this should run powerless */
public void setPowerLess(boolean bool);
}

View file

@ -21,10 +21,11 @@ import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import dark.api.IDisableable; import dark.api.IDisableable;
import dark.api.IPowerLess;
import dark.api.PowerSystems; import dark.api.PowerSystems;
import dark.core.DarkMain; import dark.core.DarkMain;
public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements IDisableable, IPacketReceiver public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements IDisableable, IPacketReceiver, IPowerLess
{ {
/** Forge Ore Directory name of the item to toggle power */ /** Forge Ore Directory name of the item to toggle power */
@ -34,11 +35,9 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
protected int ticksDisabled = 0; protected int ticksDisabled = 0;
protected float WATTS_PER_TICK = 2; protected float WATTS_PER_TICK, MAX_WATTS;
protected float MAX_WATTS = 40;
protected boolean runPowerLess = false; protected boolean unpowered, running;
protected boolean running = false;
@Override @Override
public void updateEntity() public void updateEntity()
@ -47,7 +46,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
if (!this.worldObj.isRemote) if (!this.worldObj.isRemote)
{ {
boolean prevRun = this.running; boolean prevRun = this.running;
this.running = this.canRun() && this.consumePower(); this.running = this.canRun() && this.consumePower(this.WATTS_PER_TICK, true);
if (prevRun != this.running) if (prevRun != this.running)
{ {
PacketManager.sendPacketToClients(this.getDescriptionPacket(), worldObj, new Vector3(this), 64); PacketManager.sendPacketToClients(this.getDescriptionPacket(), worldObj, new Vector3(this), 64);
@ -61,21 +60,41 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
} }
} }
/** Called to consume power per tick */ /** Called to consume power from the internal storage */
public boolean consumePower() public boolean consumePower(float watts, boolean doDrain)
{ {
if (!this.runPowerLess && this.getEnergyStored() >= this.WATTS_PER_TICK) if (!this.runPowerLess() && this.getEnergyStored() >= watts)
{ {
this.setEnergyStored(this.getEnergyStored() - this.WATTS_PER_TICK); if (doDrain)
{
this.setEnergyStored(this.getEnergyStored() - watts);
}
return true; return true;
} }
return this.canRun(); return this.runPowerLess();
} }
/** Does this tile have power to run and do work */ /** Does this tile have power to run and do work */
public boolean canRun() public boolean canRun()
{ {
return !this.isDisabled() && (this.runPowerLess || PowerSystems.runPowerLess(PowerSystems.UE_SUPPORTED_SYSTEMS)); return !this.isDisabled() && (this.runPowerLess() || this.getEnergyStored() >= this.WATTS_PER_TICK);
}
@Override
public boolean runPowerLess()
{
return this.unpowered || PowerSystems.runPowerLess(PowerSystems.UE_SUPPORTED_SYSTEMS);
}
@Override
public void setPowerLess(boolean bool)
{
this.unpowered = bool;
}
public void togglePowerMode()
{
this.setPowerLess(!this.runPowerLess());
} }
/** Called when a player activates the tile's block */ /** Called when a player activates the tile's block */
@ -90,7 +109,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
{ {
if (stack.isItemEqual(itemStack)) if (stack.isItemEqual(itemStack))
{ {
this.runPowerLess = !this.runPowerLess; this.togglePowerMode();
return true; return true;
} }
} }
@ -102,7 +121,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
@Override @Override
public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive) public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive)
{ {
if (!this.runPowerLess && receive != null && this.canConnect(from)) if (!this.runPowerLess() && receive != null && this.canConnect(from))
{ {
// Only do voltage disable if the voltage is higher than the peek voltage and if random chance // Only do voltage disable if the voltage is higher than the peek voltage and if random chance
//TODO replace random with timed damage to only disable after so many ticks //TODO replace random with timed damage to only disable after so many ticks
@ -193,7 +212,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
{ {
super.readFromNBT(nbt); super.readFromNBT(nbt);
this.ticksDisabled = nbt.getInteger("disabledTicks"); this.ticksDisabled = nbt.getInteger("disabledTicks");
this.runPowerLess = nbt.getBoolean("shouldPower"); this.unpowered = nbt.getBoolean("shouldPower");
if (nbt.hasKey("wattsReceived")) if (nbt.hasKey("wattsReceived"))
{ {
this.energyStored = (float) nbt.getDouble("wattsReceived"); this.energyStored = (float) nbt.getDouble("wattsReceived");
@ -205,6 +224,6 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
{ {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setInteger("disabledTicks", this.ticksDisabled); nbt.setInteger("disabledTicks", this.ticksDisabled);
nbt.setBoolean("shouldPower", this.runPowerLess); nbt.setBoolean("shouldPower", this.unpowered);
} }
} }

View file

@ -3,15 +3,16 @@ package dark.core.tile.network;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import universalelectricity.core.block.IElectricalStorage; import universalelectricity.core.block.IElectricalStorage;
import dark.api.INetworkPart; import dark.api.INetworkPart;
import dark.api.IPowerLess;
/** Used for tile networks that only need to share power or act like a group battery that doesn't /** Used for tile networks that only need to share power or act like a group battery that doesn't
* store power on world save * store power on world save
* *
* @author DarkGuardsman */ * @author DarkGuardsman */
public class NetworkSharedPower extends NetworkTileEntities public class NetworkSharedPower extends NetworkTileEntities implements IElectricalStorage, IPowerLess
{ {
private float sharedPower = 0; private float energy;
private float maxPower = 1; private boolean runPowerLess;
public NetworkSharedPower(INetworkPart... parts) public NetworkSharedPower(INetworkPart... parts)
{ {
@ -26,12 +27,12 @@ public class NetworkSharedPower extends NetworkTileEntities
public float dumpPower(TileEntity source, float power, boolean doFill) public float dumpPower(TileEntity source, float power, boolean doFill)
{ {
float room = (maxPower - sharedPower); float room = (this.getMaxEnergyStored() - this.getEnergyStored());
if (this.networkMember.contains(source) && Math.ceil(room) > 0) if (!this.runPowerLess && this.networkMember.contains(source) && Math.ceil(room) > 0)
{ {
if (doFill) if (doFill)
{ {
this.sharedPower = Math.max(this.sharedPower + power, this.maxPower); this.setEnergyStored(Math.max(this.getEnergyStored() + power, this.getMaxEnergyStored()));
} }
return Math.max(Math.min(Math.abs(room - power), power), 0); return Math.max(Math.min(Math.abs(room - power), power), 0);
} }
@ -40,11 +41,11 @@ public class NetworkSharedPower extends NetworkTileEntities
public boolean drainPower(TileEntity source, float power, boolean doDrain) public boolean drainPower(TileEntity source, float power, boolean doDrain)
{ {
if (this.networkMember.contains(source) && this.sharedPower >= power) if (this.networkMember.contains(source) && (this.getEnergyStored() >= power || this.runPowerLess))
{ {
if (doDrain) if (doDrain && !this.runPowerLess)
{ {
this.sharedPower -= power; this.setEnergyStored(this.getEnergyStored() - power);
} }
return true; return true;
} }
@ -55,15 +56,52 @@ public class NetworkSharedPower extends NetworkTileEntities
public void cleanUpMembers() public void cleanUpMembers()
{ {
super.cleanUpMembers(); super.cleanUpMembers();
this.maxPower = 0;
for (INetworkPart part : this.networkMember) for (INetworkPart part : this.networkMember)
{ {
if (part instanceof IElectricalStorage) if (part instanceof IPowerLess && ((IPowerLess) part).runPowerLess())
{ {
this.maxPower += ((IElectricalStorage) part).getMaxEnergyStored(); this.setPowerLess(((IPowerLess) part).runPowerLess());
break;
} }
} }
} }
@Override
public boolean runPowerLess()
{
return this.runPowerLess;
}
@Override
public void setPowerLess(boolean bool)
{
this.runPowerLess = bool;
for (INetworkPart part : this.networkMember)
{
if (part instanceof IPowerLess)
{
((IPowerLess) part).setPowerLess(bool);
}
}
}
@Override
public void setEnergyStored(float energy)
{
this.energy = energy;
}
@Override
public float getEnergyStored()
{
return this.energy;
}
@Override
public float getMaxEnergyStored()
{
return Integer.MAX_VALUE;
}
} }