Started working on IC2 Trait

This commit is contained in:
Calclavia 2013-12-24 00:22:35 +08:00
parent 1369a9af98
commit 415a187b0c
5 changed files with 87 additions and 36 deletions

View file

@ -16,7 +16,7 @@ public class MultipartRI implements IPartFactory
{ {
MultiPartRegistry.registerParts(this, new String[] { "resonant_induction_flat_wire", "resonant_induction_multimeter", "resonant_induction_transformer" }); MultiPartRegistry.registerParts(this, new String[] { "resonant_induction_flat_wire", "resonant_induction_multimeter", "resonant_induction_transformer" });
MultipartGenerator.registerTrait("universalelectricity.api.energy.IConductor", "resonantinduction.wire.part.TraitConductor"); MultipartGenerator.registerTrait("universalelectricity.api.energy.IConductor", "resonantinduction.wire.part.TraitConductor");
MultipartGenerator.registerTrait("ic2.api.energy.tile.IEnergySink", "resonantinduction.wire.part.TraitEnergySink"); //MultipartGenerator.registerTrait("ic2.api.energy.tile.IEnergySink", "resonantinduction.wire.part.TraitEnergySink");
} }
@Override @Override

View file

@ -1,16 +1,21 @@
package resonantinduction.wire.part; package resonantinduction.wire.part;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyTile;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import resonantinduction.base.PartAdvanced; import resonantinduction.base.PartAdvanced;
import universalelectricity.api.CompatibilityModule; import universalelectricity.api.CompatibilityModule;
import universalelectricity.api.UniversalClass;
import universalelectricity.api.energy.EnergyNetworkLoader; import universalelectricity.api.energy.EnergyNetworkLoader;
import universalelectricity.api.energy.IConductor; import universalelectricity.api.energy.IConductor;
import universalelectricity.api.energy.IEnergyNetwork; import universalelectricity.api.energy.IEnergyNetwork;
import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.Vector3;
import universalelectricity.api.vector.VectorHelper; import universalelectricity.api.vector.VectorHelper;
//@UniversalClass @UniversalClass
public abstract class PartConductor extends PartAdvanced implements IConductor public abstract class PartConductor extends PartAdvanced implements IConductor
{ {
private IEnergyNetwork network; private IEnergyNetwork network;
@ -112,4 +117,25 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
} }
} }
} }
/**
* IC2 Functions
*/
@Override
public void onWorldJoin()
{
if (tile() instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent((IEnergyTile) tile()));
}
}
@Override
public void onWorldSeparate()
{
if (tile() instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile) tile()));
}
}
} }

View file

@ -215,6 +215,8 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal
} }
this.recalculateConnections(); this.recalculateConnections();
super.onChunkLoad();
} }
@Override @Override
@ -257,6 +259,8 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal
this.recalculateConnections(); this.recalculateConnections();
} }
super.onPartChanged(part);
} }
@Override @Override
@ -276,6 +280,7 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal
this.recalculateConnections(); this.recalculateConnections();
} }
super.onNeighborChanged();
} }
@Override @Override

View file

@ -90,7 +90,7 @@ public class TraitConductor extends TileMultipart implements IConductor
{ {
for (IConductor conductor : this.interfaces) for (IConductor conductor : this.interfaces)
{ {
if (conductor.canConnect(direction)) if (conductor.canConnect(direction.getOpposite()))
{ {
return true; return true;
} }

View file

@ -5,6 +5,8 @@ import ic2.api.energy.tile.IEnergySink;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import universalelectricity.api.CompatibilityType;
import universalelectricity.api.energy.IConductor;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import codechicken.multipart.TMultiPart; import codechicken.multipart.TMultiPart;
@ -12,16 +14,16 @@ import codechicken.multipart.TileMultipart;
public class TraitEnergySink extends TileMultipart implements IEnergySink public class TraitEnergySink extends TileMultipart implements IEnergySink
{ {
public Set<IEnergySink> ic2Sinks = new HashSet<IEnergySink>(); public Set<IConductor> interfaces = new HashSet<IConductor>();
@Override @Override
public void copyFrom(TileMultipart that) public void copyFrom(TileMultipart that)
{ {
super.copyFrom(that); super.copyFrom(that);
if(that instanceof TraitEnergySink) if (that instanceof TraitConductor)
{ {
ic2Sinks = ((TraitEnergySink)that).ic2Sinks; this.interfaces = ((TraitConductor) that).interfaces;
} }
} }
@ -29,10 +31,10 @@ public class TraitEnergySink extends TileMultipart implements IEnergySink
public void bindPart(TMultiPart part) public void bindPart(TMultiPart part)
{ {
super.bindPart(part); super.bindPart(part);
if(part instanceof IEnergySink) if (part instanceof IConductor)
{ {
ic2Sinks.add((IEnergySink) part); this.interfaces.add((IConductor) part);
} }
} }
@ -40,10 +42,10 @@ public class TraitEnergySink extends TileMultipart implements IEnergySink
public void partRemoved(TMultiPart part, int p) public void partRemoved(TMultiPart part, int p)
{ {
super.partRemoved(part, p); super.partRemoved(part, p);
if(part instanceof IEnergySink) if (part instanceof IConductor)
{ {
ic2Sinks.remove(part); this.interfaces.remove(part);
} }
} }
@ -51,21 +53,28 @@ public class TraitEnergySink extends TileMultipart implements IEnergySink
public void clearParts() public void clearParts()
{ {
super.clearParts(); super.clearParts();
this.interfaces.clear();
ic2Sinks.clear();
} }
@Override @Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection from)
{ {
for(IEnergySink sink : ic2Sinks) if (this.partMap(from.ordinal()) == null)
{ {
if(sink.acceptsEnergyFrom(emitter, direction)) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
return true; if (dir != from.getOpposite())
{
TMultiPart part = this.partMap(dir.ordinal());
if (this.interfaces.contains(part))
{
return ((IConductor) part).canConnect(from);
}
}
} }
} }
return false; return false;
} }
@ -74,36 +83,47 @@ public class TraitEnergySink extends TileMultipart implements IEnergySink
{ {
double demanded = 0; double demanded = 0;
for(IEnergySink sink : ic2Sinks) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
demanded += sink.demandedEnergyUnits(); TMultiPart part = this.partMap(dir.ordinal());
if (this.interfaces.contains(part))
{
return ((IConductor) part).onReceiveEnergy(ForgeDirection.UNKNOWN, Integer.MAX_VALUE, false) * CompatibilityType.INDUSTRIALCRAFT.ratio;
}
} }
return demanded; return demanded;
} }
@Override @Override
public double injectEnergyUnits(ForgeDirection directionFrom, double amount) public double injectEnergyUnits(ForgeDirection from, double amount)
{ {
for(IEnergySink sink : ic2Sinks) double consumed = 0;
if (this.partMap(from.ordinal()) == null)
{ {
amount = sink.injectEnergyUnits(directionFrom, Math.min(amount, sink.demandedEnergyUnits())); for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
if (dir != from.getOpposite())
{
TMultiPart part = this.partMap(dir.ordinal());
if (this.interfaces.contains(part))
{
consumed = ((IConductor) part).onReceiveEnergy(from, (long) (amount * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio), true) * CompatibilityType.INDUSTRIALCRAFT.ratio;
}
}
}
} }
return amount; return amount - consumed;
} }
@Override @Override
public int getMaxSafeInput() public int getMaxSafeInput()
{ {
int safe = 0; return Integer.MAX_VALUE;
for(IEnergySink sink : ic2Sinks)
{
safe += sink.getMaxSafeInput();
}
return safe;
} }
} }