updated code for UE api

Removed TileEntityElectricMachine and replaced it with the UE
UniversalMachine class
Removed the BC to UE class as it is no longer needed or functional

Added NetworkSharedPower so that tileEntities can share power in a
network allowing things like Electric Fences or battery box grids.
However, it doesn't unloaded power then it is removed so should only be
used for running machines
This commit is contained in:
DarkGuardsman 2013-07-21 22:53:49 -04:00
parent 6365e06b08
commit 609954e058
8 changed files with 264 additions and 296 deletions

View file

@ -50,11 +50,7 @@ public class FluidRestrictionHandler
public static boolean hasRestrictedStack(Fluid stack)
{
if (stack == null)
{
return false;
}
return restrictedStacks.inverse().containsKey(stack);
return stack != null && restrictedStacks.inverse().containsKey(stack);
}
/** gets the liquid stack that is restricted to this color */

View file

@ -0,0 +1,69 @@
package dark.core.tile.network;
import net.minecraft.tileentity.TileEntity;
import universalelectricity.core.block.IElectricalStorage;
import dark.core.api.INetworkPart;
/** 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
{
private float sharedPower = 0;
private float maxPower = 1;
public NetworkSharedPower(INetworkPart... parts)
{
super(parts);
}
@Override
public NetworkTileEntities newInstance()
{
return new NetworkSharedPower();
}
public float dumpPower(TileEntity source, float power, boolean doFill)
{
float room = (maxPower - sharedPower);
if (this.networkMember.contains(source) && Math.ceil(room) > 0)
{
if (doFill)
{
this.sharedPower = Math.max(this.sharedPower + power, this.maxPower);
}
return Math.max(Math.min(Math.abs(room - power), power), 0);
}
return 0;
}
public boolean drainPower(TileEntity source, float power, boolean doDrain)
{
if (this.networkMember.contains(source) && this.sharedPower >= power)
{
if (doDrain)
{
this.sharedPower -= power;
}
return true;
}
return false;
}
@Override
public void cleanUpMembers()
{
super.cleanUpMembers();
this.maxPower = 0;
for (INetworkPart part : this.networkMember)
{
if (part instanceof IElectricalStorage)
{
this.maxPower += ((IElectricalStorage) part).getMaxEnergyStored();
}
}
}
}

View file

@ -27,13 +27,13 @@ public abstract class NetworkTileEntities
/** Creates a new instance of this network to be used to merge or split networks while still
* maintaining each class that extends the base network class
*
*
* @return - new network instance using the current networks properties */
public abstract NetworkTileEntities newInstance();
/** Adds a TileEntity to the network. extends this to catch non-network parts and add them to
* other tile lists
*
*
* @param tileEntity - tileEntity instance
* @param member - add to network member list
* @return */
@ -67,7 +67,7 @@ public abstract class NetworkTileEntities
{
return this.networkMember.contains(ent);
}
public boolean removeTile(TileEntity ent)
{
return this.networkMember.remove(ent);
@ -127,14 +127,17 @@ public abstract class NetworkTileEntities
return this.networkMember;
}
/** Combines two networks together into one
*
/** Combines two networks together into one. Calls to preMerge and doMerge instead of doing the
* merge process itself
*
* @param network
* @param part */
public void merge(NetworkTileEntities network, INetworkPart part)
{
if (network != null && network != this && network.getClass().equals(this.getClass()))
{
this.refresh();
network.refresh();
if (this.preMergeProcessing(network, part))
{
this.mergeDo(network);
@ -144,17 +147,17 @@ public abstract class NetworkTileEntities
/** Processing that needs too be done before the network merges. Use this to do final network
* merge calculations and to cause network merge failure
*
*
* @param network the network that is to merge with this one
* @param part the part at which started the network merge. Use this to cause damage if two
* networks merge with real world style failures
*
*
* @return false if the merge needs to be canceled.
*
*
* Cases in which the network should fail to merge are were the two networks merge with error.
* Or, in the case of pipes the two networks merge and the merge point was destroyed by
* combination of liquids.
*
*
* Ex Lava and water */
public boolean preMergeProcessing(NetworkTileEntities network, INetworkPart part)
{
@ -246,7 +249,7 @@ public abstract class NetworkTileEntities
}
/** invalidates/remove a tile from the networks that surround and connect to it
*
*
* @param tileEntity - tile */
public static void invalidate(TileEntity tileEntity)
{

View file

@ -1,73 +0,0 @@
package dark.library.machine;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.electricity.ElectricityPack;
import buildcraft.api.power.PowerProvider;
import dark.core.api.PowerSystems;
public class BcToUeProvider extends PowerProvider
{
public TileEntityElectricMachine tileEntity;
public BcToUeProvider(TileEntityElectricMachine tile)
{
tileEntity = tile;
}
@Override
public void receiveEnergy(float quantity, ForgeDirection from)
{
powerSources[from.ordinal()] = 2;
tileEntity.receiveElectricity(from, new ElectricityPack((PowerSystems.BC3_RATIO * quantity), tileEntity.getVoltage()), true);
}
@Override
public float useEnergy(float min, float max, boolean doUse)
{
float result = 0;
if (tileEntity.getEnergyStored() >= min)
{
if (tileEntity.getEnergyStored() <= max)
{
result = (float) tileEntity.getEnergyStored();
if (doUse)
{
tileEntity.setEnergyStored(0);
}
}
else
{
result = max;
if (doUse)
{
tileEntity.setEnergyStored(tileEntity.getEnergyStored() - max);
}
}
}
return result;
}
@Override
public float getEnergyStored()
{
return (float) this.tileEntity.getEnergyStored();
}
@Override
public int getMaxEnergyReceived()
{
return (int) Math.ceil(this.tileEntity.getMaxEnergyStored());
}
@Override
public int getMaxEnergyStored()
{
return (int) Math.ceil(this.tileEntity.getMaxEnergyStored());
}
}

View file

@ -0,0 +1,91 @@
package dark.library.machine;
import net.minecraft.nbt.NBTTagCompound;
import universalelectricity.core.block.IElectrical;
import universalelectricity.core.electricity.ElectricityPack;
/** An optional easy way for you to handle electrical storage without hassle.
*
* @author Calclavia */
public class ElectricityHandler
{
public IElectrical tileEntity;
public float energyStored = 0;
public ElectricityHandler(IElectrical tileEntity)
{
this.tileEntity = tileEntity;
}
public ElectricityHandler(IElectrical tileEntity, float energyStored)
{
this(tileEntity);
this.setEnergyStored(energyStored);
}
public float receiveElectricity(ElectricityPack receive, boolean doReceive)
{
if (receive != null)
{
float prevEnergyStored = this.getEnergyStored();
float newStoredEnergy = this.getEnergyStored() + receive.getWatts();
if (doReceive)
{
this.setEnergyStored(newStoredEnergy);
}
return Math.max(newStoredEnergy - prevEnergyStored, 0);
}
return 0;
}
public float receiveElectricity(float energy, boolean doReceive)
{
return this.receiveElectricity(ElectricityPack.getFromWatts(energy, this.tileEntity.getVoltage()), doReceive);
}
public ElectricityPack provideElectricity(ElectricityPack request, boolean doProvide)
{
if (request != null)
{
float requestedEnergy = Math.min(request.getWatts(), this.energyStored);
if (doProvide)
{
this.setEnergyStored(this.energyStored - requestedEnergy);
}
return ElectricityPack.getFromWatts(requestedEnergy, this.tileEntity.getVoltage());
}
return new ElectricityPack();
}
public ElectricityPack provideElectricity(float energy, boolean doProvide)
{
return this.provideElectricity(ElectricityPack.getFromWatts(energy, this.tileEntity.getVoltage()), doProvide);
}
public ElectricityHandler setEnergyStored(float energy)
{
this.energyStored = energy;
return this;
}
public float getEnergyStored()
{
return this.energyStored;
}
public void readFromNBT(NBTTagCompound nbt)
{
this.energyStored = nbt.getFloat("energyStored");
}
public void writeToNBT(NBTTagCompound nbt)
{
nbt.setFloat("energyStored", this.energyStored);
}
}

View file

@ -1,187 +0,0 @@
package dark.library.machine;
import java.util.Random;
import dark.core.api.IDisableable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.block.IElectrical;
import universalelectricity.core.block.IElectricalStorage;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.prefab.tile.ElectricityHandler;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
public abstract class TileEntityElectricMachine extends TileEntityAdvanced implements IDisableable, IElectrical, IElectricalStorage, IPowerReceptor
{
/** Internal Battery & Energy handler */
private ElectricityHandler electricityHandler;
/** Rool the dice and see what you get */
protected Random random = new Random();
/** Remaining ticks of time to remain disabled */
protected int ticksDisabled = 0;
/** Max energy storage limit */
protected float maxEnergy;
/** Energy needed to run per tick regardless of function */
protected float tickEnergy;
/** Should this machine run without power */
protected boolean runWithOutPower = false;
/** BuildCraft power provider? */
private IPowerProvider powerProvider;
public TileEntityElectricMachine(float d, float d2)
{
this.maxEnergy = d;
this.tickEnergy = d2;
}
@Override
public void updateEntity()
{
super.updateEntity();
if (this.ticksDisabled > 0)
{
this.ticksDisabled--;
this.whileDisable();
}
}
public ElectricityHandler ElectricHandler()
{
if (this.electricityHandler == null)
{
this.electricityHandler = new ElectricityHandler(this, 0, this.maxEnergy);
}
return this.electricityHandler;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return true;
}
@Override
public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive)
{
if (this.runWithOutPower || receive == null || !this.canConnect(from))
{
return 0;
}
if (receive != null && receive.voltage > (Math.sqrt(2) * this.getVoltage()) && this.random.nextBoolean())
{
if (doReceive)
{
this.onDisable(20 + this.random.nextInt(100));
}
return 0;
}
return this.ElectricHandler().receiveElectricity(receive, doReceive);
}
@Override
public ElectricityPack provideElectricity(ForgeDirection from, ElectricityPack request, boolean doProvide)
{
if (from == ForgeDirection.UNKNOWN)
{
return this.electricityHandler.provideElectricity(request, doProvide);
}
return null;
}
@Override
public float getVoltage()
{
return 240;
}
@Override
public void setEnergyStored(float energy)
{
this.ElectricHandler().setEnergyStored(energy);
}
@Override
public float getEnergyStored()
{
return this.ElectricHandler().getEnergyStored();
}
@Override
public float getMaxEnergyStored()
{
return this.ElectricHandler().getMaxEnergyStored();
}
/** Called every tick while this tile entity is disabled. */
protected void whileDisable()
{
//TODO generate electric sparks
}
@Override
public void onDisable(int duration)
{
this.ticksDisabled = duration;
}
@Override
public boolean isDisabled()
{
return this.ticksDisabled > 0;
}
/** Buildcraft */
@Override
public void setPowerProvider(IPowerProvider provider)
{
this.powerProvider = provider;
}
@Override
public IPowerProvider getPowerProvider()
{
if (this.powerProvider == null)
{
this.powerProvider = new BcToUeProvider(this);
}
return this.powerProvider;
}
@Override
public void doWork()
{
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.ElectricHandler().readFromNBT(nbt);
this.ticksDisabled = nbt.getInteger("disabledTicks");
this.runWithOutPower = nbt.getBoolean("shouldPower");
if (nbt.hasKey("wattsReceived"))
{
this.ElectricHandler().setEnergyStored((float) nbt.getDouble("wattsReceived"));
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
this.ElectricHandler().writeToNBT(nbt);
nbt.setInteger("disabledTicks", this.ticksDisabled);
nbt.setBoolean("shouldPower", this.runWithOutPower);
}
}

View file

@ -1,29 +1,47 @@
package dark.library.machine;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;
import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.electricity.ElectricityPack;
import dark.core.api.IDisableable;
import dark.core.api.PowerSystems;
public class TileEntityRunnableMachine extends TileEntityElectricMachine
public class TileEntityRunnableMachine extends TileEntityUniversalElectrical implements IDisableable
{
public TileEntityRunnableMachine(float d)
{
super(d * 2, d);
}
/** Forge Ore Directory name of the item to toggle power */
public static String powerToggleItemID = "battery";
/** Power Systems this machine can support */
private PowerSystems[] powerList = new PowerSystems[] { PowerSystems.BUILDCRAFT, PowerSystems.MEKANISM };
protected PowerSystems[] powerList = new PowerSystems[] { PowerSystems.BUILDCRAFT, PowerSystems.MEKANISM, PowerSystems.INDUSTRIALCRAFT };
protected Random random = new Random();
protected int ticksDisabled = 0;
protected boolean runWithOutPower = false;
@Override
public void updateEntity()
{
super.updateEntity();
if (this.ticksDisabled > 0)
{
this.ticksDisabled--;
this.whileDisable();
}
}
/** Does this tile have power to run and do work */
public boolean canRun()
{
boolean power = this.getEnergyStored() >= this.tickEnergy || this.runWithOutPower || PowerSystems.runPowerLess(powerList);
return !this.isDisabled() && power;
;
return !this.isDisabled() && (this.runWithOutPower || PowerSystems.runPowerLess(powerList));
}
/** Called when a player activates the tile's block */
@ -49,20 +67,30 @@ public class TileEntityRunnableMachine extends TileEntityElectricMachine
}
@Override
public float getRequest(ForgeDirection direction)
public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive)
{
return Math.max(this.getMaxEnergyStored() - this.getEnergyStored(), 0);
if (!this.runWithOutPower && 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
if (receive != null && receive.voltage > (Math.sqrt(2) * this.getVoltage()) && this.random.nextBoolean())
{
if (doReceive)
{
this.onDisable(20 + this.random.nextInt(100));
}
return 0;
}
return super.receiveElectricity(from, receive, doReceive);
}
return 0;
}
@Override
public int powerRequest(ForgeDirection from)
public float getRequest(ForgeDirection direction)
{
if (this.canConnect(from) && !this.runWithOutPower)
{
return (int) Math.ceil(this.getRequest(from) * PowerSystems.TO_BC_RATIO);
}
return 0;
return Math.max(this.getMaxEnergyStored() - this.getEnergyStored(), 0);
}
@Override
@ -70,4 +98,49 @@ public class TileEntityRunnableMachine extends TileEntityElectricMachine
{
return 0;
}
@Override
public float getMaxEnergyStored()
{
// TODO Auto-generated method stub
return 0;
}
/** Called every tick while this tile entity is disabled. */
protected void whileDisable()
{
//TODO generate electric sparks
}
@Override
public void onDisable(int duration)
{
this.ticksDisabled = duration;
}
@Override
public boolean isDisabled()
{
return this.ticksDisabled > 0;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.ticksDisabled = nbt.getInteger("disabledTicks");
this.runWithOutPower = nbt.getBoolean("shouldPower");
if (nbt.hasKey("wattsReceived"))
{
this.energyStored = (float) nbt.getDouble("wattsReceived");
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("disabledTicks", this.ticksDisabled);
nbt.setBoolean("shouldPower", this.runWithOutPower);
}
}

View file

@ -29,10 +29,6 @@ import dark.library.machine.TileEntityRunnableMachine;
/** @author Calclavia, DarkGuardsman */
public abstract class TileEntityTerminal extends TileEntityRunnableMachine implements ISpecialAccess, IPacketReceiver, ITerminal
{
public TileEntityTerminal(float watttick)
{
super(watttick);
}
public enum PacketType
{