Fixed multipart registration and null connections with WeakReferences

This commit is contained in:
Calclavia 2014-02-15 23:59:44 +08:00
parent 466f5b0839
commit 08c0aad457
4 changed files with 21 additions and 12 deletions

View file

@ -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

View file

@ -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

View file

@ -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());
}
}

View file

@ -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