Clean up, made Comparator of ItemRailing to compare against distance to source ItemRailing

This commit is contained in:
tgame14 2014-04-17 12:32:53 +03:00
parent 27bfc697f9
commit f9483a2db5
4 changed files with 67 additions and 57 deletions

View file

@ -1,12 +1,10 @@
package resonantinduction.electrical.itemrailing;
import calclavia.lib.grid.NodeGrid;
import com.google.common.collect.Sets;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer;
import universalelectricity.api.vector.IVector3;
import java.util.*;
import java.util.Arrays;
/**
* @since 18/03/14
@ -31,14 +29,14 @@ public class GridRailing extends NodeGrid<NodeRailing>
public IItemRailing findNearestInventory(IItemRailingTransfer itemwrapper)
{
IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray();
Arrays.sort(arr, new RailingDistanceComparator.RailingInventoryDistanceComparator());
Arrays.sort(arr, new RailingDistanceComparator.RailingInventoryDistanceComparator(itemwrapper.getRailing()));
return arr[0];
}
public IItemRailing findNearestColoredTarget(IItemRailingTransfer itemwrapper)
{
IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray();
Arrays.sort(arr, new RailingDistanceComparator.RailingColoredDistanceComparator(itemwrapper.getColor()));
Arrays.sort(arr, new RailingDistanceComparator.RailingColoredDistanceComparator(itemwrapper.getRailing(), itemwrapper.getColor()));
return arr[0];
}
}

View file

@ -12,57 +12,25 @@ import java.util.ArrayList;
import java.util.List;
/**
* @since 18/03/14
* @author tgame14
* @since 18/03/14
*/
public class NodeRailing extends Node<IItemRailing, GridRailing, NodeRailing> implements IVectorWorld
{
public NodeRailing (PartRailing parent)
{
super(parent);
}
public NodeRailing(PartRailing parent)
{
super(parent);
}
@Override
protected GridRailing newGrid ()
{
return new GridRailing(getClass());
}
@Override
protected GridRailing newGrid()
{
return new GridRailing(getClass());
}
/**
*
* @return possibly null, a IInventory to target
*/
public IInventory[] getInventoriesNearby()
{
List<IInventory> invList = new ArrayList<IInventory>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity te = parent.getWorldPos().translate(dir).getTileEntity(parent.getWorldPos().world());
if (te != null && te instanceof IInventory)
{
invList.add((IInventory) te);
}
}
return (IInventory[]) invList.toArray();
}
public boolean isLeaf()
{
int connectionsCount = 0;
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
if (parent.getWorldPos().translate(dir).getTileEntity(parent.getWorldPos().world()) instanceof IItemRailing)
{
connectionsCount++;
if (connectionsCount >= 2)
{
return false;
}
}
}
return true;
}
@Override
public double z()

View file

@ -15,6 +15,7 @@ import resonantinduction.core.prefab.part.PartFramedConnection;
import resonantinduction.electrical.Electrical;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer;
import universalelectricity.api.energy.EnergyNetworkLoader;
import universalelectricity.api.energy.IConductor;
import universalelectricity.api.energy.IEnergyNetwork;
import universalelectricity.api.vector.IVectorWorld;
@ -22,6 +23,8 @@ import universalelectricity.api.vector.VectorWorld;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* @since 16/03/14
@ -107,13 +110,34 @@ public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, I
@Override
public IInventory[] getInventoriesNearby()
{
return node.getInventoriesNearby();
List<IInventory> invList = new ArrayList<IInventory>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity te = this.getWorldPos().translate(dir).getTileEntity(this.getWorldPos().world());
if (te != null && te instanceof IInventory)
{
invList.add((IInventory) te);
}
}
return (IInventory[]) invList.toArray();
}
@Override
public boolean isLeaf()
{
return node.isLeaf();
int connectionsCount = 0;
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
if (this.getWorldPos().translate(dir).getTileEntity(this.getWorldPos().world()) instanceof IItemRailing)
{
connectionsCount++;
if (connectionsCount >= 2)
{
return false;
}
}
}
return true;
}
@Override
@ -158,12 +182,18 @@ public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, I
return tile instanceof IConductor ? (IConductor) ((IConductor) tile).getInstance(ForgeDirection.UNKNOWN) : null;
}
@Override
public IEnergyNetwork getNetwork ()
{
return null;
}
@Override
public IEnergyNetwork getNetwork()
{
if (network == null)
{
setNetwork(EnergyNetworkLoader.getNewNetwork(this));
}
return network;
}
//TODO: Fix up
@Override
public void setMaterial (int i)
{

View file

@ -9,17 +9,29 @@ import java.util.Comparator;
* @author tgame14
* @since 16/04/14
*/
public class RailingDistanceComparator implements Comparator<IItemRailing>
public abstract class RailingDistanceComparator implements Comparator<IItemRailing>
{
private IItemRailing source;
public RailingDistanceComparator(IItemRailing source)
{
this.source = source;
}
@Override
public int compare(IItemRailing o1, IItemRailing o2)
{
return (int) o1.getWorldPos().floor().distance(o2.getWorldPos());
return (int) (source.getWorldPos().floor().distance(o1.getWorldPos()) - source.getWorldPos().floor().distance(o2.getWorldPos()));
}
public static class RailingInventoryDistanceComparator extends RailingDistanceComparator
{
public RailingInventoryDistanceComparator(IItemRailing source)
{
super(source);
}
@Override
public int compare(IItemRailing o1, IItemRailing o2)
{
@ -41,12 +53,14 @@ public class RailingDistanceComparator implements Comparator<IItemRailing>
}
}
public static class RailingColoredDistanceComparator extends RailingDistanceComparator
{
private EnumColor color;
public RailingColoredDistanceComparator(EnumColor color)
public RailingColoredDistanceComparator(IItemRailing source, EnumColor color)
{
super(source);
this.color = color;
}