From 08c0aad457acdf776611602196d5ce5e2a432f15 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Sat, 15 Feb 2014 23:59:44 +0800 Subject: [PATCH] Fixed multipart registration and null connections with WeakReferences --- .../mechanical/MultipartMechanical.java | 2 +- .../resonantinduction/mechanical/gear/PartGear.java | 11 ++++++----- .../mechanical/gear/PartGearShaft.java | 7 ++++--- .../mechanical/network/PartMechanical.java | 13 ++++++++++--- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/mechanical/src/main/java/resonantinduction/mechanical/MultipartMechanical.java b/mechanical/src/main/java/resonantinduction/mechanical/MultipartMechanical.java index 72932a46..5ebf00eb 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/MultipartMechanical.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/MultipartMechanical.java @@ -18,7 +18,7 @@ public class MultipartMechanical implements IPartFactory { MultiPartRegistry.registerParts(this, PART_TYPES); MultipartGenerator.registerPassThroughInterface("resonantinduction.api.fluid.IFluidPipe"); - MultipartGenerator.registerTrait("resonantinduction.mechanical.network.IMechanical", "resonantinduction.mechanical.trait.TraitMechanical"); + MultipartGenerator.registerTrait("resonantinduction.api.mechanical.IMechanical", "resonantinduction.mechanical.trait.TraitMechanical"); } @Override diff --git a/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGear.java b/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGear.java index 92afb577..4b9b806b 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGear.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGear.java @@ -1,5 +1,6 @@ package resonantinduction.mechanical.gear; +import java.lang.ref.WeakReference; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -164,7 +165,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock @Override public void refresh() { - connections = new Object[6]; + connections = new WeakReference[6]; /** * Only call refresh if this is the main block of a multiblock gear or a single gear block. @@ -183,7 +184,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock if (instance != null && instance != this && !(instance instanceof PartGearShaft) && instance.canConnect(placementSide.getOpposite(), this)) { - connections[placementSide.ordinal()] = instance; + connections[placementSide.ordinal()] = new WeakReference(instance); getNetwork().merge(instance.getNetwork()); } @@ -214,7 +215,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock if (connections[checkDir.ordinal()] == null && instance != this && checkDir != placementSide && instance != null && instance.canConnect(checkDir.getOpposite(), this)) { - connections[checkDir.ordinal()] = instance; + connections[checkDir.ordinal()] = new WeakReference(instance); getNetwork().merge(instance.getNetwork()); } } @@ -239,7 +240,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock if (instance != null && instance != this && instance.canConnect(checkDir.getOpposite(), this) && !(instance instanceof PartGearShaft)) { - connections[checkDir.ordinal()] = instance; + connections[checkDir.ordinal()] = new WeakReference(instance); getNetwork().merge(instance.getNetwork()); } } @@ -289,7 +290,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock return new Object[6]; } - return connections; + return super.getConnections(); } @Override diff --git a/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGearShaft.java b/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGearShaft.java index bb9589bc..e371bd23 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGearShaft.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/gear/PartGearShaft.java @@ -1,5 +1,6 @@ package resonantinduction.mechanical.gear; +import java.lang.ref.WeakReference; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -65,7 +66,7 @@ public class PartGearShaft extends PartMechanical @Override public void refresh() { - connections = new Object[6]; + connections = new WeakReference[6]; /** Check for internal connections, the FRONT and BACK. */ for (int i = 0; i < 6; i++) @@ -78,7 +79,7 @@ public class PartGearShaft extends PartMechanical if (instance != null && instance != this && instance.canConnect(checkDir.getOpposite(), this)) { - connections[checkDir.ordinal()] = instance; + connections[checkDir.ordinal()] = new WeakReference(instance); getNetwork().merge(instance.getNetwork()); } } @@ -100,7 +101,7 @@ public class PartGearShaft extends PartMechanical // Only connect to shafts outside of this block space. if (instance != null && instance != this && instance instanceof PartGearShaft && instance.canConnect(checkDir.getOpposite(), this)) { - connections[checkDir.ordinal()] = instance; + connections[checkDir.ordinal()] = new WeakReference(instance); getNetwork().merge(instance.getNetwork()); } } diff --git a/mechanical/src/main/java/resonantinduction/mechanical/network/PartMechanical.java b/mechanical/src/main/java/resonantinduction/mechanical/network/PartMechanical.java index 5dbb3125..513d4247 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/network/PartMechanical.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/network/PartMechanical.java @@ -1,5 +1,6 @@ package resonantinduction.mechanical.network; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -29,7 +30,7 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu private IMechanicalNetwork network; /** The mechanical connections this connector has made */ - protected Object[] connections = new Object[6]; + protected WeakReference[] connections = new WeakReference[6]; protected float prevAngularVelocity, angularVelocity; @@ -66,7 +67,7 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu i++; System.out.println("Connected with: " + i + ":" + getNetwork()); - //efresh(); + // efresh(); } @@ -126,7 +127,13 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu @Override public Object[] getConnections() { - return connections; + Object[] actualConnections = new Object[6]; + + for (int i = 0; i < connections.length; i++) + if (connections[i] != null) + actualConnections[i] = connections[i].get(); + + return actualConnections; } @Override