From 415a187b0c08479cdfe54c422e7fcd35974d63af Mon Sep 17 00:00:00 2001 From: Calclavia Date: Tue, 24 Dec 2013 00:22:35 +0800 Subject: [PATCH] Started working on IC2 Trait --- src/resonantinduction/MultipartRI.java | 2 +- .../wire/part/PartConductor.java | 28 +++++- .../wire/part/PartFlatWire.java | 5 ++ .../wire/part/TraitConductor.java | 2 +- .../wire/part/TraitEnergySink.java | 86 ++++++++++++------- 5 files changed, 87 insertions(+), 36 deletions(-) diff --git a/src/resonantinduction/MultipartRI.java b/src/resonantinduction/MultipartRI.java index 616bfd2d0..34250eabd 100644 --- a/src/resonantinduction/MultipartRI.java +++ b/src/resonantinduction/MultipartRI.java @@ -16,7 +16,7 @@ public class MultipartRI implements IPartFactory { 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("ic2.api.energy.tile.IEnergySink", "resonantinduction.wire.part.TraitEnergySink"); + //MultipartGenerator.registerTrait("ic2.api.energy.tile.IEnergySink", "resonantinduction.wire.part.TraitEnergySink"); } @Override diff --git a/src/resonantinduction/wire/part/PartConductor.java b/src/resonantinduction/wire/part/PartConductor.java index 995541101..3c1639ef7 100644 --- a/src/resonantinduction/wire/part/PartConductor.java +++ b/src/resonantinduction/wire/part/PartConductor.java @@ -1,16 +1,21 @@ 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.minecraftforge.common.ForgeDirection; +import net.minecraftforge.common.MinecraftForge; import resonantinduction.base.PartAdvanced; import universalelectricity.api.CompatibilityModule; +import universalelectricity.api.UniversalClass; import universalelectricity.api.energy.EnergyNetworkLoader; import universalelectricity.api.energy.IConductor; import universalelectricity.api.energy.IEnergyNetwork; import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.VectorHelper; -//@UniversalClass +@UniversalClass public abstract class PartConductor extends PartAdvanced implements IConductor { 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())); + } + } } diff --git a/src/resonantinduction/wire/part/PartFlatWire.java b/src/resonantinduction/wire/part/PartFlatWire.java index 33f58ce07..10a59d2aa 100644 --- a/src/resonantinduction/wire/part/PartFlatWire.java +++ b/src/resonantinduction/wire/part/PartFlatWire.java @@ -215,6 +215,8 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal } this.recalculateConnections(); + + super.onChunkLoad(); } @Override @@ -257,6 +259,8 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal this.recalculateConnections(); } + + super.onPartChanged(part); } @Override @@ -276,6 +280,7 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal this.recalculateConnections(); } + super.onNeighborChanged(); } @Override diff --git a/src/resonantinduction/wire/part/TraitConductor.java b/src/resonantinduction/wire/part/TraitConductor.java index 7c65c52d0..dca3d5129 100644 --- a/src/resonantinduction/wire/part/TraitConductor.java +++ b/src/resonantinduction/wire/part/TraitConductor.java @@ -90,7 +90,7 @@ public class TraitConductor extends TileMultipart implements IConductor { for (IConductor conductor : this.interfaces) { - if (conductor.canConnect(direction)) + if (conductor.canConnect(direction.getOpposite())) { return true; } diff --git a/src/resonantinduction/wire/part/TraitEnergySink.java b/src/resonantinduction/wire/part/TraitEnergySink.java index f9c3710d9..1a0b27c97 100644 --- a/src/resonantinduction/wire/part/TraitEnergySink.java +++ b/src/resonantinduction/wire/part/TraitEnergySink.java @@ -5,6 +5,8 @@ import ic2.api.energy.tile.IEnergySink; import java.util.HashSet; import java.util.Set; +import universalelectricity.api.CompatibilityType; +import universalelectricity.api.energy.IConductor; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import codechicken.multipart.TMultiPart; @@ -12,16 +14,16 @@ import codechicken.multipart.TileMultipart; public class TraitEnergySink extends TileMultipart implements IEnergySink { - public Set ic2Sinks = new HashSet(); + public Set interfaces = new HashSet(); @Override public void copyFrom(TileMultipart 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) { 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) { 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() { super.clearParts(); - - ic2Sinks.clear(); + this.interfaces.clear(); } @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; } @@ -74,36 +83,47 @@ public class TraitEnergySink extends TileMultipart implements IEnergySink { 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; } @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 public int getMaxSafeInput() { - int safe = 0; - - for(IEnergySink sink : ic2Sinks) - { - safe += sink.getMaxSafeInput(); - } - - return safe; + return Integer.MAX_VALUE; } }