Updated UE API

This commit is contained in:
Calclavia 2013-08-25 21:37:11 +08:00
parent e228e11031
commit 791c60fd77
3 changed files with 35 additions and 78 deletions

View file

@ -5,6 +5,7 @@ import net.minecraft.world.World;
import net.minecraftforge.event.Cancelable; import net.minecraftforge.event.Cancelable;
import net.minecraftforge.event.Event; import net.minecraftforge.event.Event;
import universalelectricity.core.block.IElectrical; import universalelectricity.core.block.IElectrical;
import universalelectricity.core.grid.IElectricityNetwork;
public class ElectricalEvent extends Event public class ElectricalEvent extends Event
{ {
@ -27,34 +28,40 @@ public class ElectricalEvent extends Event
} }
} }
public static class NetworkEvent extends ElectricalEvent
{
public final IElectricityNetwork network;
public ElectricityPack electricityPack;
public TileEntity[] ignoreTiles;
public NetworkEvent(IElectricityNetwork network, ElectricityPack electricityPack, TileEntity... ignoreTiles)
{
this.network = network;
this.electricityPack = electricityPack;
this.ignoreTiles = ignoreTiles;
}
}
/** /**
* Internal Events * Internal Events. These events are fired when something happens in the network.
* *
* @author Calclavia * @author Calclavia
* *
*/ */
@Cancelable @Cancelable
public static class ElectricityProductionEvent extends ElectricalEvent public static class ElectricityProductionEvent extends NetworkEvent
{ {
public ElectricityPack electricityPack; public ElectricityProductionEvent(IElectricityNetwork network, ElectricityPack electricityPack, TileEntity... ignoreTiles)
public TileEntity[] ignoreTiles;
public ElectricityProductionEvent(ElectricityPack electricityPack, TileEntity... ignoreTiles)
{ {
this.electricityPack = electricityPack; super(network, electricityPack, ignoreTiles);
this.ignoreTiles = ignoreTiles;
} }
} }
public static class ElectricityRequestEvent extends ElectricalEvent public static class ElectricityRequestEvent extends NetworkEvent
{ {
public ElectricityPack electricityPack; public ElectricityRequestEvent(IElectricityNetwork network, ElectricityPack electricityPack, TileEntity... ignoreTiles)
public TileEntity[] ignoreTiles;
public ElectricityRequestEvent(ElectricityPack electricityPack, TileEntity... ignoreTiles)
{ {
this.electricityPack = electricityPack; super(network, electricityPack, ignoreTiles);
this.ignoreTiles = ignoreTiles;
} }
} }

View file

@ -23,56 +23,6 @@ import universalelectricity.core.vector.VectorHelper;
*/ */
public class ElectricityHelper public class ElectricityHelper
{ {
@ForgeSubscribe
public void onProduce(ElectricityProduceEvent evt)
{
// Needs work with shuffling to be able to evenly distribute to all
// connected networks
// instead of just defaulting to one of them.
Vector3 position = new Vector3((TileEntity) evt.tileEntity);
HashMap<IElectricityNetwork, ForgeDirection> networks = new HashMap<IElectricityNetwork, ForgeDirection>();
for (ForgeDirection direction : getDirections((TileEntity) evt.tileEntity))
{
IElectricityNetwork network = getNetworkFromTileEntity(VectorHelper.getTileEntityFromSide(evt.world, position, direction), direction.getOpposite());
if (network != null)
{
networks.put(network, direction);
ElectricityPack provided = evt.tileEntity.provideElectricity(direction, network.getRequest((TileEntity) evt.tileEntity), true);
if (provided != null && provided.getWatts() > 0)
{
network.produce(provided, (TileEntity) evt.tileEntity);
}
}
}
/*
* ElectricityPack request = this.getNetwork().getRequest(this);
*
* if (request.getWatts() > 0) { List<ElectricityPack> providedPacks = new
* ArrayList<ElectricityPack>(); List<TileEntity> tileEntities = new
* ArrayList<TileEntity>();
*
* for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { Vector3 position = new
* Vector3(this).modifyPositionFromSide(direction); TileEntity tileEntity =
* position.getTileEntity(this.worldObj);
*
* if (tileEntity instanceof IElectrical) { tileEntities.add(tileEntity); IElectrical
* electrical = (IElectrical) tileEntity;
*
* if (electrical.canConnect(direction.getOpposite())) { if
* (electrical.getProvide(direction.getOpposite()) > 0) { providedPacks.add
* (electrical.provideElectricity(direction.getOpposite(), request, true)); } } } }
*
* ElectricityPack mergedPack = ElectricityPack.merge(providedPacks);
*
* if (mergedPack.getWatts() > 0) { this.getNetwork().produce(mergedPack,
* tileEntities.toArray(new TileEntity[0])); } }
*/
}
public static EnumSet<ForgeDirection> getDirections(TileEntity tileEntity) public static EnumSet<ForgeDirection> getDirections(TileEntity tileEntity)
{ {
EnumSet<ForgeDirection> possibleSides = EnumSet.noneOf(ForgeDirection.class); EnumSet<ForgeDirection> possibleSides = EnumSet.noneOf(ForgeDirection.class);

View file

@ -42,14 +42,20 @@ public class ElectricityNetwork implements IElectricityNetwork
private final Set<IConductor> conductors = new HashSet<IConductor>(); private final Set<IConductor> conductors = new HashSet<IConductor>();
public float acceptorResistance = 500;
@Override @Override
public float produce(ElectricityPack electricity, TileEntity... ignoreTiles) public float produce(ElectricityPack electricity, TileEntity... ignoreTiles)
{ {
ElectricityProductionEvent evt = new ElectricityProductionEvent(electricity, ignoreTiles); ElectricityProductionEvent evt = new ElectricityProductionEvent(this, electricity, ignoreTiles);
MinecraftForge.EVENT_BUS.post(evt); MinecraftForge.EVENT_BUS.post(evt);
float energy = electricity.getWatts(); float totalEnergy = electricity.getWatts();
float totalEnergy = energy; float networkResistance = getTotalResistance();
float proportionWasted = getTotalResistance() / (getTotalResistance() + acceptorResistance);
float energyWasted = totalEnergy * proportionWasted;
float totalUsableEnergy = totalEnergy - energyWasted;
float remainingUsableEnergy = totalUsableEnergy;
float voltage = electricity.voltage; float voltage = electricity.voltage;
if (!evt.isCanceled()) if (!evt.isCanceled())
@ -72,19 +78,13 @@ public class ElectricityNetwork implements IElectricityNetwork
{ {
if (electricalTile.canConnect(direction) && this.getConductors().contains(VectorHelper.getConnectorFromSide(tileEntity.worldObj, new Vector3(tileEntity), direction))) if (electricalTile.canConnect(direction) && this.getConductors().contains(VectorHelper.getConnectorFromSide(tileEntity.worldObj, new Vector3(tileEntity), direction)))
{ {
float energyToSend = totalEnergy * (electricalTile.getRequest(direction) / totalEnergyRequest); float energyToSend = totalUsableEnergy * (electricalTile.getRequest(direction) / totalEnergyRequest);
if (energyToSend > 0) if (energyToSend > 0)
{ {
ElectricityPack electricityToSend = ElectricityPack.getFromWatts(energyToSend, voltage); ElectricityPack electricityToSend = ElectricityPack.getFromWatts(energyToSend, voltage);
// Calculate energy loss caused by resistance. remainingUsableEnergy -= ((IElectrical) tileEntity).receiveElectricity(direction, electricityToSend, true);
float ampsReceived = electricityToSend.amperes - (electricityToSend.amperes * electricityToSend.amperes * this.getTotalResistance()) / electricityToSend.voltage;
float voltsReceived = electricityToSend.voltage - (electricityToSend.amperes * this.getTotalResistance());
electricityToSend = new ElectricityPack(ampsReceived, voltsReceived);
energy -= ((IElectrical) tileEntity).receiveElectricity(direction, electricityToSend, true);
} }
} }
} }
@ -94,7 +94,7 @@ public class ElectricityNetwork implements IElectricityNetwork
} }
} }
return energy; return remainingUsableEnergy;
} }
/** /**
@ -136,7 +136,7 @@ public class ElectricityNetwork implements IElectricityNetwork
} }
ElectricityPack mergedPack = ElectricityPack.merge(requests); ElectricityPack mergedPack = ElectricityPack.merge(requests);
ElectricityRequestEvent evt = new ElectricityRequestEvent(mergedPack, ignoreTiles); ElectricityRequestEvent evt = new ElectricityRequestEvent(this, mergedPack, ignoreTiles);
MinecraftForge.EVENT_BUS.post(evt); MinecraftForge.EVENT_BUS.post(evt);
return mergedPack; return mergedPack;
} }