Complete redesign and repackage of Item Railings, some cleanup before moving to 1.7

This commit is contained in:
tgame14 2014-05-25 21:27:55 +03:00
parent 9bbc8e4d2c
commit 78ba973732
7 changed files with 142 additions and 433 deletions

View file

@ -0,0 +1,10 @@
package resonantinduction.electrical.itemrailing;
/**
* @since 25/05/14
* @author tgame14
*/
public enum EnumRailingMaterial
{
DEFAULT, EXTENTION;
}

View file

@ -1,57 +1,15 @@
package resonantinduction.electrical.itemrailing; package resonantinduction.electrical.itemrailing;
import java.util.Arrays;
import net.minecraft.item.ItemStack;
import resonant.lib.grid.TickingGrid; import resonant.lib.grid.TickingGrid;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer;
/** /**
* @since 18/03/14 * @since 25/05/14
* @author tgame14 * @author tgame14
*/ */
public class GridRailing extends TickingGrid<NodeRailing> public class GridRailing extends TickingGrid<NodeRailing>
{ {
public final static String CATEGORY_RAILING = "Item_Railings"; public GridRailing (NodeRailing node, Class type)
public GridRailing (NodeRailing railing, Class type)
{ {
super(railing, type); super(node, type);
} }
public IItemRailing findTargetForIItemTransfer(IItemRailingTransfer itemwrapper)
{
if (itemwrapper.getColor() == null)
{
return findNearestInventory(itemwrapper);
}
return findNearestColoredTarget(itemwrapper);
}
public IItemRailing findNearestInventory(IItemRailingTransfer itemwrapper)
{
IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray();
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.getRailing(), itemwrapper.getColor()));
return arr[0];
}
public IItemRailing chooseNextInstantGoal(IItemRailingTransfer itemwrapper)
{
IItemRailing[] arr = (IItemRailing[]) itemwrapper.getRailing().getConnectionMap().entrySet().toArray();
Arrays.sort(arr, new RailingDistanceComparator(itemwrapper.getEndGoal()));
return arr[0];
}
public void onItemEnterGrid(IItemRailing railing, ItemStack item)
{
}
} }

View file

@ -1,80 +0,0 @@
package resonantinduction.electrical.itemrailing;
import java.lang.ref.WeakReference;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import resonant.lib.render.EnumColor;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer;
/**
* An object that is a wrapper for all items through railings
*
* @since 16/03/14
* @author tgame14
*/
public class ItemRailingTransfer implements IItemRailingTransfer
{
private ItemStack stack;
private EnumColor color;
private WeakReference<IItemRailing> railing;
private WeakReference<IItemRailing> endTarget;
public ItemRailingTransfer(ItemStack stack, PartRailing railing)
{
this.stack = stack.copy();
this.color = null;
this.railing = new WeakReference<IItemRailing>(railing.getNode());
this.endTarget = new WeakReference<IItemRailing>(railing.getNode().getGrid().findTargetForIItemTransfer(this));
}
public ItemRailingTransfer(Item item, PartRailing railing)
{
this(new ItemStack(item), railing);
}
@Override
public ItemStack getItemStack()
{
return this.stack;
}
@Override
public EnumColor getColor()
{
return this.color;
}
public IItemRailingTransfer setColor(EnumColor color)
{
this.color = color;
return this;
}
@Override
public IItemRailing getRailing()
{
return this.railing.get();
}
@Override
public IItemRailingTransfer setRailing(IItemRailing railing)
{
this.railing = new WeakReference<IItemRailing>(railing);
return this;
}
@Override
public IItemRailing getEndGoal()
{
return endTarget.get();
}
@Override
public IItemRailingTransfer setEndGoal(IItemRailing goal)
{
this.endTarget = new WeakReference<IItemRailing>(goal);
return this;
}
}

View file

@ -1,181 +1,94 @@
package resonantinduction.electrical.itemrailing; package resonantinduction.electrical.itemrailing;
import java.util.ArrayList; import codechicken.multipart.TMultiPart;
import java.util.HashMap; import net.minecraft.nbt.NBTTagCompound;
import java.util.HashSet; import net.minecraft.tileentity.TileEntity;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.minecraft.inventory.IInventory;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonant.lib.config.Config;
import resonant.lib.grid.Node; import resonant.lib.grid.Node;
import resonant.lib.render.EnumColor; import resonant.lib.grid.TickingGrid;
import resonant.lib.type.Pair;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingProvider; import resonantinduction.electrical.itemrailing.interfaces.IItemRailingProvider;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer; import universalelectricity.api.vector.IVector3;
import universalelectricity.api.vector.IVectorWorld; import universalelectricity.api.vector.Vector3;
import universalelectricity.api.vector.VectorWorld;
import com.google.common.collect.Lists;
/** /**
* @since 25/05/14
* @author tgame14 * @author tgame14
* @since 18/03/14
*/ */
public class NodeRailing extends Node<IItemRailingProvider, GridRailing, Object> implements IVectorWorld, IItemRailing public class NodeRailing extends Node<IItemRailingProvider, GridRailing, Object>
{ {
private int maxItemSpeed;
private byte connectionMap;
private EnumColor color;
@Config(category = GridRailing.CATEGORY_RAILING) protected byte connectionMap = Byte.parseByte("111111", 2);
private static int MAX_TICKS_IN_RAILING = 5;
/** hold a timer here per item */ public NodeRailing (IItemRailingProvider parent)
private Set<Pair<IItemRailingTransfer, Integer>> itemNodeSet; {
super(parent);
}
public NodeRailing setConnection(byte connectionMap)
{
this.connectionMap = connectionMap;
return this;
}
public NodeRailing(IItemRailingProvider parent) @Override
{ protected GridRailing newGrid ()
super(parent); {
this.itemNodeSet = new HashSet<Pair<IItemRailingTransfer, Integer>>(); return new GridRailing(this, NodeRailing.class);
this.color = null; }
this.connectionMap = Byte.parseByte("111111", 2);
this.maxItemSpeed = 20;
}
public NodeRailing setConnection(byte connectionMap) @Override
{ public void update (float deltaTime)
this.connectionMap = connectionMap; {
return this; super.update(deltaTime);
} }
@Override @Override
public void update(float deltaTime) protected void doRecache ()
{ {
if (!world().isRemote) connections.clear();
{
Iterator<Map.Entry<Object, ForgeDirection>> iterator = new HashMap(getConnections()).entrySet().iterator();
for (Pair<IItemRailingTransfer, Integer> pair : this.itemNodeSet) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
if (pair.right() <= 0) TileEntity tile = new Vector3(position()).translate(dir).getTileEntity(world());
{
//TODO move to next item railing
} if (tile instanceof IItemRailingProvider)
else {
{ NodeRailing check = (NodeRailing) ((IItemRailingProvider) tile).getNode(NodeRailing.class, dir.getOpposite());
pair.setRight(pair.right() - 1);
}
if (check != null && canConnect(dir, check) && check.canConnect(dir.getOpposite(), this))
{
connections.put(check, dir);
}
}
}
}
} public IVector3 position()
{
return parent.getVectorWorld();
}
while (iterator.hasNext()) public World world()
{ {
Map.Entry<Object, ForgeDirection> entry = iterator.next(); return parent.getVectorWorld().world();
Object obj = entry.getKey(); }
if (obj instanceof NodeRailing) @Override
{ public boolean canConnect (ForgeDirection from, Object source)
{
} return (source instanceof NodeRailing) && (connectionMap & (1 << from.ordinal())) != 0;
} }
}
}
@Override
protected GridRailing newGrid()
{
return new GridRailing(this, getClass());
}
@Override
public double z()
{
return this.getWorldPos().z();
}
@Override
public World world()
{
return parent.getWorldPos().world();
}
@Override
public double x()
{
return parent.getWorldPos().x();
}
@Override
public double y()
{
return parent.getWorldPos().y();
}
@Override
public boolean canItemEnter (IItemRailingTransfer item)
{
return this.color != null ? this.color == item.getColor() : false;
}
@Override
public boolean canConnectToRailing (IItemRailing railing, ForgeDirection from)
{
return this.color != null ? this.color == railing.getRailingColor() : true;
}
@Override
public EnumColor getRailingColor ()
{
return this.color;
}
@Override
public IItemRailing setRailingColor (EnumColor color)
{
this.color = color;
return this;
}
@Override
public VectorWorld getWorldPos()
{
return (VectorWorld) parent.getWorldPos();
}
@Override
public Map<Object, ForgeDirection> getConnectionMap()
{
return new HashMap<Object, ForgeDirection>(this.getConnections());
}
@Override
public IInventory[] getInventoriesNearby()
{
ArrayList<IInventory> invList = Lists.<IInventory>newArrayList();
for (Object tile : this.getConnections().entrySet())
{
if (tile instanceof IInventory)
{
invList.add((IInventory) tile);
}
}
return (IInventory[]) invList.toArray();
}
@Override
public boolean isLeaf()
{
return connectionMap < 2;
}
@Override
public void load (NBTTagCompound nbt)
{
super.load(nbt);
}
@Override
public void save (NBTTagCompound nbt)
{
super.save(nbt);
}
} }

View file

@ -2,17 +2,28 @@ package resonantinduction.electrical.itemrailing;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import codechicken.lib.vec.Vector3;
import codechicken.microblock.IHollowConnect;
import codechicken.multipart.JNormalOcclusion;
import codechicken.multipart.TSlottedPart;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonant.api.IExternalInventory;
import resonant.api.IExternalInventoryBox;
import resonant.api.grid.INode; import resonant.api.grid.INode;
import resonant.lib.utility.inventory.ExternalInventory;
import resonantinduction.core.prefab.part.PartFramedConnection; import resonantinduction.core.prefab.part.PartFramedConnection;
import resonantinduction.core.prefab.part.PartFramedNode;
import resonantinduction.electrical.Electrical; import resonantinduction.electrical.Electrical;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingProvider; import resonantinduction.electrical.itemrailing.interfaces.IItemRailingProvider;
import universalelectricity.api.energy.EnergyNetworkLoader; import universalelectricity.api.energy.EnergyNetworkLoader;
import universalelectricity.api.energy.IConductor; import universalelectricity.api.energy.IConductor;
import universalelectricity.api.energy.IEnergyNetwork; import universalelectricity.api.energy.IEnergyNetwork;
import universalelectricity.api.net.IConnector;
import universalelectricity.api.net.INetwork;
import universalelectricity.api.vector.IVectorWorld;
import universalelectricity.api.vector.VectorWorld; import universalelectricity.api.vector.VectorWorld;
import codechicken.multipart.TileMultipart; import codechicken.multipart.TileMultipart;
@ -20,26 +31,19 @@ import codechicken.multipart.TileMultipart;
* @since 16/03/14 * @since 16/03/14
* @author tgame14 * @author tgame14
*/ */
public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, IConductor, IEnergyNetwork> implements IConductor, IItemRailingProvider public class PartRailing extends PartFramedNode<EnumRailingMaterial, NodeRailing, IItemRailingProvider> implements IItemRailingProvider, TSlottedPart, JNormalOcclusion, IHollowConnect, IExternalInventory
{ {
protected ExternalInventory inventory;
public static enum EnumRailing protected boolean markPacketUpdate;
{
DEFAULT, EXTENTION;
}
public NodeRailing getNode()
{
return node;
}
private NodeRailing node;
public PartRailing () public PartRailing ()
{ {
super(Electrical.itemInsulation); super(Electrical.itemInsulation);
this.material = EnumRailing.DEFAULT; this.material = EnumRailingMaterial.DEFAULT;
this.node = new NodeRailing(this); this.node = new NodeRailing(this);
this.markPacketUpdate = true;
this.requiresInsulation = false;
this.inventory = new ExternalInventory(tile(), 5);
} }
@ -72,71 +76,23 @@ public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, I
return new VectorWorld(getWorld(), x(), y(), z()); return new VectorWorld(getWorld(), x(), y(), z());
} }
@Override
public float getResistance ()
{
return 0;
}
@Override
public long getCurrentCapacity ()
{
return 0;
}
@Override
public long onReceiveEnergy (ForgeDirection from, long receive, boolean doReceive)
{
return 0;
}
@Override
public long onExtractEnergy (ForgeDirection from, long extract, boolean doExtract)
{
return 0;
}
@Override @Override
public boolean doesTick () public boolean doesTick ()
{ {
return false; return false;
} }
@Override
protected boolean canConnectTo (TileEntity tile, ForgeDirection to)
{
Object obj = tile instanceof TileMultipart ? ((TileMultipart) tile).partMap(ForgeDirection.UNKNOWN.ordinal()) : tile;
return obj instanceof IInventory ? true : obj instanceof PartRailing;
}
@Override
protected IConductor getConnector (TileEntity tile)
{
return tile instanceof IConductor ? (IConductor) ((IConductor) tile).getInstance(ForgeDirection.UNKNOWN) : null;
}
@Override
public IEnergyNetwork getNetwork()
{
if (network == null)
{
setNetwork(EnergyNetworkLoader.getNewNetwork(this));
}
return network;
}
//TODO: Fix up to proper data //TODO: Fix up to proper data
@Override @Override
public void setMaterial (int i) public void setMaterial (int i)
{ {
this.material = EnumRailing.values()[i]; this.material = EnumRailingMaterial.values()[i];
} }
@Override @Override
protected ItemStack getItem () protected ItemStack getItem ()
{ {
return new ItemStack(Electrical.itemRailing); return new ItemStack(Electrical.itemRailing, 1, getMaterialID());
} }
@Override @Override
@ -145,4 +101,42 @@ public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, I
return "resonant_induction_itemrailing"; return "resonant_induction_itemrailing";
} }
@Override
public void renderDynamic (Vector3 pos, float frame, int pass)
{
super.renderDynamic(pos, frame, pass);
//TODO: Implement
}
@Override
public IVectorWorld getVectorWorld ()
{
return new VectorWorld(getWorld(), x(), y(), z());
}
@Override
public void onInventoryChanged ()
{
//TODO: Implement
}
@Override
public IExternalInventoryBox getInventory ()
{
return this.inventory;
}
@Override
public boolean canStore (ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
@Override
public boolean canRemove (ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
} }

View file

@ -1,88 +0,0 @@
package resonantinduction.electrical.itemrailing;
import java.util.Comparator;
import resonant.lib.render.EnumColor;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
/**
* @author tgame14
* @since 16/04/14
*/
public 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) (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)
{
if (o1.getInventoriesNearby() != null && o2.getInventoriesNearby() != null)
{
return super.compare(o1, o2);
}
else if (o1.getInventoriesNearby() == null && o2.getInventoriesNearby() == null)
{
return super.compare(o1, o2);
}
if (o1.getInventoriesNearby() != null)
{
return 1;
}
return -1;
}
}
public static class RailingColoredDistanceComparator extends RailingDistanceComparator
{
private EnumColor color;
public RailingColoredDistanceComparator(IItemRailing source, EnumColor color)
{
super(source);
this.color = color;
}
@Override
public int compare(IItemRailing o1, IItemRailing o2)
{
if (o1.getRailingColor() == o2.getRailingColor())
{
return super.compare(o1, o2);
}
else if (color == o1.getRailingColor())
{
return 1;
}
else if (color == o2.getRailingColor())
{
return -1;
}
return super.compare(o1, o2);
}
}
}

View file

@ -9,5 +9,7 @@ import universalelectricity.api.vector.IVectorWorld;
*/ */
public interface IItemRailingProvider extends INodeProvider public interface IItemRailingProvider extends INodeProvider
{ {
public IVectorWorld getWorldPos(); public IVectorWorld getVectorWorld();
public void onInventoryChanged();
} }