diff --git a/src/dark/api/INetworkEnergyPart.java b/src/dark/api/INetworkEnergyPart.java new file mode 100644 index 000000000..6d85c55cc --- /dev/null +++ b/src/dark/api/INetworkEnergyPart.java @@ -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); +} diff --git a/src/dark/api/IPowerLess.java b/src/dark/api/IPowerLess.java new file mode 100644 index 000000000..57c391ece --- /dev/null +++ b/src/dark/api/IPowerLess.java @@ -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); +} diff --git a/src/dark/core/blocks/TileEntityMachine.java b/src/dark/core/blocks/TileEntityMachine.java index c2fe00985..f374feac4 100644 --- a/src/dark/core/blocks/TileEntityMachine.java +++ b/src/dark/core/blocks/TileEntityMachine.java @@ -21,10 +21,11 @@ import com.google.common.io.ByteArrayDataInput; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import dark.api.IDisableable; +import dark.api.IPowerLess; import dark.api.PowerSystems; 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 */ @@ -34,11 +35,9 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im protected int ticksDisabled = 0; - protected float WATTS_PER_TICK = 2; - protected float MAX_WATTS = 40; + protected float WATTS_PER_TICK, MAX_WATTS; - protected boolean runPowerLess = false; - protected boolean running = false; + protected boolean unpowered, running; @Override public void updateEntity() @@ -47,7 +46,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im if (!this.worldObj.isRemote) { 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) { 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 */ - public boolean consumePower() + /** Called to consume power from the internal storage */ + 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 this.canRun(); + return this.runPowerLess(); } /** Does this tile have power to run and do work */ 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 */ @@ -90,7 +109,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im { if (stack.isItemEqual(itemStack)) { - this.runPowerLess = !this.runPowerLess; + this.togglePowerMode(); return true; } } @@ -102,7 +121,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im @Override 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 //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); this.ticksDisabled = nbt.getInteger("disabledTicks"); - this.runPowerLess = nbt.getBoolean("shouldPower"); + this.unpowered = nbt.getBoolean("shouldPower"); if (nbt.hasKey("wattsReceived")) { this.energyStored = (float) nbt.getDouble("wattsReceived"); @@ -205,6 +224,6 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im { super.writeToNBT(nbt); nbt.setInteger("disabledTicks", this.ticksDisabled); - nbt.setBoolean("shouldPower", this.runPowerLess); + nbt.setBoolean("shouldPower", this.unpowered); } } diff --git a/src/dark/core/tile/network/NetworkSharedPower.java b/src/dark/core/tile/network/NetworkSharedPower.java index 52caf8745..41d0ce3a9 100644 --- a/src/dark/core/tile/network/NetworkSharedPower.java +++ b/src/dark/core/tile/network/NetworkSharedPower.java @@ -3,15 +3,16 @@ package dark.core.tile.network; import net.minecraft.tileentity.TileEntity; import universalelectricity.core.block.IElectricalStorage; 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 * store power on world save - * + * * @author DarkGuardsman */ -public class NetworkSharedPower extends NetworkTileEntities +public class NetworkSharedPower extends NetworkTileEntities implements IElectricalStorage, IPowerLess { - private float sharedPower = 0; - private float maxPower = 1; + private float energy; + private boolean runPowerLess; public NetworkSharedPower(INetworkPart... parts) { @@ -26,12 +27,12 @@ public class NetworkSharedPower extends NetworkTileEntities public float dumpPower(TileEntity source, float power, boolean doFill) { - float room = (maxPower - sharedPower); - if (this.networkMember.contains(source) && Math.ceil(room) > 0) + float room = (this.getMaxEnergyStored() - this.getEnergyStored()); + if (!this.runPowerLess && this.networkMember.contains(source) && Math.ceil(room) > 0) { 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); } @@ -40,11 +41,11 @@ public class NetworkSharedPower extends NetworkTileEntities 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; } @@ -55,15 +56,52 @@ public class NetworkSharedPower extends NetworkTileEntities public void cleanUpMembers() { super.cleanUpMembers(); - this.maxPower = 0; 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; + } + }