From da4d87d6b59526bb23259c308581aa464eefc00d Mon Sep 17 00:00:00 2001 From: Calclavia Date: Sun, 22 Dec 2013 23:24:09 +0800 Subject: [PATCH] Wire merge now working --- Calclavia-Library | 2 +- src/resonantinduction/MultipartRI.java | 3 +- src/resonantinduction/wire/part/FlatWire.java | 107 ++++++++++-------- src/resonantinduction/wire/part/ITest.java | 23 ++++ .../wire/part/PartConductor.java | 15 ++- .../wire/part/PartWireBase.java | 2 +- .../wire/part/TraitConductor.java | 2 - .../wire/part/TraitTest.java | 35 ++++++ 8 files changed, 130 insertions(+), 59 deletions(-) create mode 100644 src/resonantinduction/wire/part/ITest.java create mode 100644 src/resonantinduction/wire/part/TraitTest.java diff --git a/Calclavia-Library b/Calclavia-Library index 1bc4a8bf2..c263c191e 160000 --- a/Calclavia-Library +++ b/Calclavia-Library @@ -1 +1 @@ -Subproject commit 1bc4a8bf21e124ca5f650d1d9da86a2962a71747 +Subproject commit c263c191ede3ada3d5f1d0c76231398df60d5b9a diff --git a/src/resonantinduction/MultipartRI.java b/src/resonantinduction/MultipartRI.java index 90d9010db..49e9f3582 100644 --- a/src/resonantinduction/MultipartRI.java +++ b/src/resonantinduction/MultipartRI.java @@ -14,8 +14,7 @@ public class MultipartRI implements IPartFactory { MultiPartRegistry.registerParts(this, new String[] { "resonant_induction_flat_wire" }); MultipartGenerator.registerTrait("universalelectricity.api.energy.IConductor", "resonantinduction.wire.part.TraitConductor"); - // MultipartGenerator.registerTrait("resonantinduction.wire.part.ITest", - // "resonantinduction.wire.part.TraitTest"); + //MultipartGenerator.registerTrait("resonantinduction.wire.part.ITest", "resonantinduction.wire.part.TraitTest"); MultipartGenerator.registerTrait("ic2.api.energy.tile.IEnergySink", "resonantinduction.wire.part.TraitEnergySink"); } diff --git a/src/resonantinduction/wire/part/FlatWire.java b/src/resonantinduction/wire/part/FlatWire.java index e776df291..055137314 100644 --- a/src/resonantinduction/wire/part/FlatWire.java +++ b/src/resonantinduction/wire/part/FlatWire.java @@ -184,6 +184,8 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio } } } + + this.getNetwork().split(this); } } @@ -201,13 +203,12 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio updateInternalConnections(); - // if (updateOpenConnections()) + if (updateOpenConnections()) { updateExternalConnections(); } this.recalculateConnections(); - this.getNetwork().reconstruct(); tile().markDirty(); } } @@ -227,9 +228,9 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio if (changed) { sendConnUpdate(); - this.recalculateConnections(); - this.getNetwork().reconstruct(); } + + this.recalculateConnections(); } } @@ -240,6 +241,7 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio { boolean changed = updateInternalConnections(); + if (updateOpenConnections()) { changed |= updateExternalConnections(); @@ -248,9 +250,9 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio if (changed) { sendConnUpdate(); - this.recalculateConnections(); - this.getNetwork().reconstruct(); } + + this.recalculateConnections(); } } @@ -267,9 +269,9 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio if (updateExternalConnections()) { sendConnUpdate(); - this.recalculateConnections(); - this.getNetwork().reconstruct(); } + + this.recalculateConnections(); } } @@ -279,62 +281,42 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio if (!world().isRemote) { System.out.println(this.getNetwork()); - System.out.println(Integer.toHexString(this.connMap)); this.getConnections(); + this.recalculateConnections(); } return super.activate(player, part, item); } @Override - public Object[] getConnections() + public void recalculateConnections() { - this.cachedConnections = new Object[6]; + this.connections = new Object[6]; /** * Calculate all external connections of this conductor. */ for (byte r = 0; r < 4; r++) { - int absDir = Rotation.rotateSide(side, r); - ForgeDirection dir = ForgeDirection.getOrientation(absDir); - - BlockCoord pos = new BlockCoord(tile()).offset(absDir); - TileEntity tileEntity = world().getBlockTileEntity(pos.x, pos.y, pos.z); - - TileMultipart t = Utility.getMultipartTile(world(), pos); - - if (this.canConnect(dir)) - { - this.cachedConnections[r] = tileEntity; - System.out.println("EXT" + tileEntity); - } + int absDir = Rotation.rotateSide(this.side, r); + this.setExternalConnection(absDir); } // Connect to the face of the block the wire is placed on. - BlockCoord pos = new BlockCoord(tile()).offset(this.side); - TileEntity tileEntity = world().getBlockTileEntity(pos.x, pos.y, pos.z); - - TileMultipart t = Utility.getMultipartTile(world(), pos); - - if (this.canConnect(ForgeDirection.getOrientation(this.side))) - { - this.cachedConnections[this.side] = tileEntity; - System.out.println("EXT" + tileEntity); - } + this.setExternalConnection(this.side); for (byte r = 0; r < 4; r++) { - int absDir = Rotation.rotateSide(side, r); + int absDir = Rotation.rotateSide(this.side, r); // Check straight ahead. - if (tile().partMap(PartMap.edgeBetween(absDir, side)) == null) + if (tile().partMap(PartMap.edgeBetween(absDir, this.side)) == null) { TMultiPart tp = tile().partMap(absDir); - if (tp instanceof FlatWire) + if (this.canConnectTo(tp)) { - this.cachedConnections[absDir] = tp; - System.out.println("INT" + tp); + this.connections[absDir] = tp; + this.getNetwork().merge(((FlatWire) tp).getNetwork()); continue; } } @@ -343,25 +325,60 @@ public class FlatWire extends PartWireBase implements TFacePart, JNormalOcclusio BlockCoord cornerPos = new BlockCoord(tile()); cornerPos.offset(absDir); - if (!canConnectThroughCorner(cornerPos, absDir ^ 1, side)) + if (!canConnectThroughCorner(cornerPos, absDir ^ 1, this.side)) continue; - cornerPos.offset(side); + cornerPos.offset(this.side); TileMultipart tpCorner = Utility.getMultipartTile(world(), cornerPos); if (tpCorner != null) { TMultiPart tp = tpCorner.partMap(absDir ^ 1); - if (tp instanceof FlatWire) + if (this.canConnectTo(tp)) { - this.cachedConnections[absDir] = tp; - System.out.println("COR" + tp); + this.connections[absDir] = tp; + this.getNetwork().merge(((FlatWire) tp).getNetwork()); } } + + // Cannot find any wire connections on this side. Set null. + this.connections[absDir] = null; + } + + this.getNetwork().reconstruct(); + } + + public void setExternalConnection(int absSide) + { + BlockCoord pos = new BlockCoord(tile()).offset(absSide); + TileMultipart t = Utility.getMultipartTile(world(), pos); + + if (t != null) + { + TMultiPart tp = t.partMap(this.side); + + if (this.canConnectTo(tp)) + { + this.connections[absSide] = tp; + this.getNetwork().merge(((FlatWire) tp).getNetwork()); + return; + } } - return this.cachedConnections; + TileEntity tileEntity = world().getBlockTileEntity(pos.x, pos.y, pos.z); + + if (this.canConnectTo(tileEntity)) + { + this.connections[absSide] = tileEntity; + } + + } + + @Override + public Object[] getConnections() + { + return this.connections; } public boolean canStay() diff --git a/src/resonantinduction/wire/part/ITest.java b/src/resonantinduction/wire/part/ITest.java new file mode 100644 index 000000000..f9fcea20a --- /dev/null +++ b/src/resonantinduction/wire/part/ITest.java @@ -0,0 +1,23 @@ +package resonantinduction.wire.part; + +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.api.energy.IEnergyInterface; + +/** + * @author Calclavia + * + */ +public interface ITest +{ + /** + * Adds energy to an block. Returns the quantity of energy that was accepted. This should always + * return 0 if the block cannot be externally charged. + * + * @param from Orientation the energy is sent in from. + * @param receive Maximum amount of energy (joules) to be sent into the block. + * @param doReceive If false, the charge will only be simulated. + * @return Amount of energy that was accepted by the block. + */ + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive); + +} diff --git a/src/resonantinduction/wire/part/PartConductor.java b/src/resonantinduction/wire/part/PartConductor.java index 897068fbc..995541101 100644 --- a/src/resonantinduction/wire/part/PartConductor.java +++ b/src/resonantinduction/wire/part/PartConductor.java @@ -3,7 +3,6 @@ package resonantinduction.wire.part; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import resonantinduction.base.PartAdvanced; -import resonantinduction.wire.IAdvancedConductor; import universalelectricity.api.CompatibilityModule; import universalelectricity.api.energy.EnergyNetworkLoader; import universalelectricity.api.energy.IConductor; @@ -12,11 +11,11 @@ import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.VectorHelper; //@UniversalClass -public abstract class PartConductor extends PartAdvanced implements IAdvancedConductor +public abstract class PartConductor extends PartAdvanced implements IConductor { private IEnergyNetwork network; - protected Object[] cachedConnections = new Object[6]; + protected Object[] connections = new Object[6]; /** * Universal Electricity conductor functions. @@ -24,7 +23,7 @@ public abstract class PartConductor extends PartAdvanced implements IAdvancedCon @Override public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) { - return this.getNetwork().produce(this, receive, doReceive); + return this.getNetwork().produce(new Vector3(tile()).modifyPositionFromSide(from).getTileEntity(world()), from.getOpposite(), receive, doReceive); } @Override @@ -70,7 +69,7 @@ public abstract class PartConductor extends PartAdvanced implements IAdvancedCon @Override public Object[] getConnections() { - return this.cachedConnections; + return this.connections; } /** @@ -94,11 +93,11 @@ public abstract class PartConductor extends PartAdvanced implements IAdvancedCon public abstract boolean canConnectTo(Object obj); /** - * Recalculates all the netwirk connections + * Recalculates all the network connections */ protected void recalculateConnections() { - this.cachedConnections = new Object[6]; + this.connections = new Object[6]; /** * Calculate all external connections with this conductor. */ @@ -109,7 +108,7 @@ public abstract class PartConductor extends PartAdvanced implements IAdvancedCon if (this.canConnect(side)) { TileEntity tileEntity = VectorHelper.getTileEntityFromSide(world(), new Vector3(tile()), side); - cachedConnections[i] = tileEntity; + connections[i] = tileEntity; } } } diff --git a/src/resonantinduction/wire/part/PartWireBase.java b/src/resonantinduction/wire/part/PartWireBase.java index b34d8ba98..1df7a5522 100644 --- a/src/resonantinduction/wire/part/PartWireBase.java +++ b/src/resonantinduction/wire/part/PartWireBase.java @@ -23,7 +23,7 @@ import codechicken.lib.data.MCDataOutput; * @author Calclavia * */ -public abstract class PartWireBase extends PartConductor +public abstract class PartWireBase extends PartConductor implements IAdvancedConductor { public static final int DEFAULT_COLOR = 16; public int dyeID = DEFAULT_COLOR; diff --git a/src/resonantinduction/wire/part/TraitConductor.java b/src/resonantinduction/wire/part/TraitConductor.java index 8e3f0c94b..530122c2f 100644 --- a/src/resonantinduction/wire/part/TraitConductor.java +++ b/src/resonantinduction/wire/part/TraitConductor.java @@ -88,8 +88,6 @@ public class TraitConductor extends TileMultipart implements IConductor @Override public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) { - System.out.println("RECEIVING"+receive); - /*TMultiPart part = partMap(from.ordinal()); if (part != null) diff --git a/src/resonantinduction/wire/part/TraitTest.java b/src/resonantinduction/wire/part/TraitTest.java new file mode 100644 index 000000000..bac8ff72f --- /dev/null +++ b/src/resonantinduction/wire/part/TraitTest.java @@ -0,0 +1,35 @@ +package resonantinduction.wire.part; + +import java.util.HashSet; +import java.util.Set; + +import net.minecraftforge.common.ForgeDirection; +import codechicken.multipart.TMultiPart; +import codechicken.multipart.TileMultipart; + +public class TraitTest extends TileMultipart implements ITest +{ + public Set interfaces = new HashSet(); + + @Override + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) + { + + TMultiPart part = partMap(from.ordinal()); + + if (part != null) + { + for (ITest conductor : this.interfaces) + { + if (conductor == part) + { + ((ITest) conductor).onReceiveEnergy(from, receive, doReceive); + System.out.println("RECEIVING"); + } + } + } + return 0; + + } + +}