Added NodeRegistry
This commit is contained in:
parent
0bc9aab4ca
commit
34c6b75057
8 changed files with 164 additions and 115 deletions
|
@ -1,23 +0,0 @@
|
|||
package resonantinduction.mechanical.energy.grid;
|
||||
|
||||
import resonantinduction.core.grid.Grid;
|
||||
import resonantinduction.core.grid.INodeProvider;
|
||||
import resonantinduction.core.grid.Node;
|
||||
|
||||
public abstract class EnergyNode<P extends INodeProvider, G extends Grid, N> extends Node<P, G, N>
|
||||
{
|
||||
public EnergyNode(P parent)
|
||||
{
|
||||
super(parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Gets the power of this node. Note that power by definition is energy per second.
|
||||
*/
|
||||
public abstract double getPower();
|
||||
|
||||
/**
|
||||
* @return Gets the energy buffered in this node at this instance.
|
||||
*/
|
||||
public abstract double getEnergy();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package resonantinduction.mechanical.energy.grid;
|
||||
|
||||
public interface IEnergyNode
|
||||
{
|
||||
/**
|
||||
* @return Gets the power of this node. Note that power by definition is energy per second.
|
||||
*/
|
||||
public double getPower();
|
||||
|
||||
/**
|
||||
* @return Gets the energy buffered in this node at this instance.
|
||||
*/
|
||||
public double getEnergy();
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package resonantinduction.mechanical.energy.grid;
|
||||
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
|
||||
public interface IMechanicalNode extends IEnergyNode
|
||||
{
|
||||
public double getTorque();
|
||||
|
||||
public double getAngularVelocity();
|
||||
|
||||
public void apply(double torque, double angularVelocity);
|
||||
|
||||
public float getRatio(ForgeDirection dir, MechanicalNode with);
|
||||
|
||||
public boolean inverseRotation(ForgeDirection dir, MechanicalNode with);
|
||||
}
|
|
@ -9,6 +9,7 @@ import net.minecraft.world.World;
|
|||
import net.minecraftforge.common.ForgeDirection;
|
||||
import resonantinduction.core.ResonantInduction;
|
||||
import resonantinduction.core.grid.INodeProvider;
|
||||
import resonantinduction.core.grid.Node;
|
||||
import resonantinduction.core.grid.TickingGrid;
|
||||
import universalelectricity.api.vector.Vector3;
|
||||
import codechicken.multipart.TMultiPart;
|
||||
|
@ -29,7 +30,7 @@ import codechicken.multipart.TMultiPart;
|
|||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
public class MechanicalNode extends EnergyNode<INodeProvider, TickingGrid, MechanicalNode>
|
||||
public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalNode> implements IMechanicalNode
|
||||
{
|
||||
public double torque = 0;
|
||||
public double prevAngularVelocity, angularVelocity = 0;
|
||||
|
@ -148,27 +149,32 @@ public class MechanicalNode extends EnergyNode<INodeProvider, TickingGrid, Mecha
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(double torque, double angularVelocity)
|
||||
{
|
||||
this.torque += torque;
|
||||
this.angularVelocity += angularVelocity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTorque()
|
||||
{
|
||||
return angularVelocity != 0 ? torque : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAngularVelocity()
|
||||
{
|
||||
return torque != 0 ? angularVelocity : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getRatio(ForgeDirection dir, MechanicalNode with)
|
||||
{
|
||||
return 0.5f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inverseRotation(ForgeDirection dir, MechanicalNode with)
|
||||
{
|
||||
return true;
|
||||
|
@ -223,6 +229,7 @@ public class MechanicalNode extends EnergyNode<INodeProvider, TickingGrid, Mecha
|
|||
return parent instanceof TMultiPart ? new Vector3(((TMultiPart) parent).x(), ((TMultiPart) parent).y(), ((TMultiPart) parent).z()) : parent instanceof TileEntity ? new Vector3((TileEntity) parent) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnect(ForgeDirection from, Object source)
|
||||
{
|
||||
return (source instanceof MechanicalNode) && (connectionMap & (1 << from.ordinal())) != 0;
|
||||
|
|
|
@ -16,114 +16,110 @@ import resonantinduction.api.recipe.RecipeResource.OreDictResource;
|
|||
|
||||
public final class MachineRecipes
|
||||
{
|
||||
public static enum RecipeType
|
||||
{
|
||||
CRUSHER,
|
||||
GRINDER,
|
||||
MIXER,
|
||||
SMELTER,
|
||||
SAWMILL;
|
||||
}
|
||||
public static enum RecipeType
|
||||
{
|
||||
CRUSHER, GRINDER, MIXER, SMELTER, SAWMILL;
|
||||
}
|
||||
|
||||
private final Map<RecipeType, Map<RecipeResource[], RecipeResource[]>> recipes = new HashMap<RecipeType, Map<RecipeResource[], RecipeResource[]>>();
|
||||
private final Map<RecipeType, Map<RecipeResource[], RecipeResource[]>> recipes = new HashMap<RecipeType, Map<RecipeResource[], RecipeResource[]>>();
|
||||
|
||||
public static final MachineRecipes INSTANCE = new MachineRecipes();
|
||||
public static MachineRecipes INSTANCE = new MachineRecipes();
|
||||
|
||||
private MachineRecipes()
|
||||
{
|
||||
for (RecipeType machine : RecipeType.values())
|
||||
{
|
||||
this.recipes.put(machine, new HashMap<RecipeResource[], RecipeResource[]>());
|
||||
}
|
||||
}
|
||||
public MachineRecipes()
|
||||
{
|
||||
for (RecipeType machine : RecipeType.values())
|
||||
{
|
||||
recipes.put(machine, new HashMap<RecipeResource[], RecipeResource[]>());
|
||||
}
|
||||
}
|
||||
|
||||
public RecipeResource getResourceFromObject(Object obj)
|
||||
{
|
||||
if (obj instanceof String)
|
||||
return new OreDictResource((String) obj);
|
||||
|
||||
if (obj instanceof Block)
|
||||
return new ItemStackResource(new ItemStack((Block) obj));
|
||||
|
||||
if (obj instanceof Item)
|
||||
return new ItemStackResource(new ItemStack((Item) obj));
|
||||
|
||||
if (obj instanceof ItemStack)
|
||||
return new ItemStackResource((ItemStack) obj);
|
||||
public RecipeResource getResourceFromObject(Object obj)
|
||||
{
|
||||
if (obj instanceof String)
|
||||
return new OreDictResource((String) obj);
|
||||
|
||||
if (obj instanceof FluidStack)
|
||||
return new FluidStackResource((FluidStack) obj);
|
||||
if (obj instanceof Block)
|
||||
return new ItemStackResource(new ItemStack((Block) obj));
|
||||
|
||||
if (obj instanceof RecipeResource)
|
||||
return (RecipeResource) obj;
|
||||
if (obj instanceof Item)
|
||||
return new ItemStackResource(new ItemStack((Item) obj));
|
||||
|
||||
return null;
|
||||
}
|
||||
if (obj instanceof ItemStack)
|
||||
return new ItemStackResource((ItemStack) obj);
|
||||
|
||||
public void addRecipe(RecipeType machine, RecipeResource[] input, RecipeResource[] output)
|
||||
{
|
||||
this.recipes.get(machine).put(input, output);
|
||||
}
|
||||
if (obj instanceof FluidStack)
|
||||
return new FluidStackResource((FluidStack) obj);
|
||||
|
||||
public void addRecipe(RecipeType machine, Object inputObj, Object... outputObj)
|
||||
{
|
||||
RecipeResource input = getResourceFromObject(inputObj);
|
||||
RecipeResource[] outputs = new RecipeResource[outputObj.length];
|
||||
if (obj instanceof RecipeResource)
|
||||
return (RecipeResource) obj;
|
||||
|
||||
for (int i = 0; i < outputs.length; i++)
|
||||
{
|
||||
RecipeResource output = getResourceFromObject(outputObj[i]);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (input == null || output == null)
|
||||
throw new RuntimeException("Resonant Induction tried to add invalid machine recipe: " + input + " => " + output);
|
||||
public void addRecipe(RecipeType machine, RecipeResource[] input, RecipeResource[] output)
|
||||
{
|
||||
this.recipes.get(machine).put(input, output);
|
||||
}
|
||||
|
||||
outputs[i] = output;
|
||||
}
|
||||
public void addRecipe(RecipeType machine, Object inputObj, Object... outputObj)
|
||||
{
|
||||
RecipeResource input = getResourceFromObject(inputObj);
|
||||
RecipeResource[] outputs = new RecipeResource[outputObj.length];
|
||||
|
||||
addRecipe(machine, new RecipeResource[] { input }, outputs);
|
||||
}
|
||||
for (int i = 0; i < outputs.length; i++)
|
||||
{
|
||||
RecipeResource output = getResourceFromObject(outputObj[i]);
|
||||
|
||||
public void removeRecipe(RecipeType machine, RecipeResource[] input)
|
||||
{
|
||||
this.recipes.get(machine).remove(input);
|
||||
}
|
||||
if (input == null || output == null)
|
||||
throw new RuntimeException("Resonant Induction tried to add invalid machine recipe: " + input + " => " + output);
|
||||
|
||||
public Map<RecipeResource[], RecipeResource[]> getRecipes(RecipeType machine)
|
||||
{
|
||||
return new HashMap<RecipeResource[], RecipeResource[]>(this.recipes.get(machine));
|
||||
}
|
||||
outputs[i] = output;
|
||||
}
|
||||
|
||||
public Map<RecipeType, Map<RecipeResource[], RecipeResource[]>> getRecipes()
|
||||
{
|
||||
return new HashMap<RecipeType, Map<RecipeResource[], RecipeResource[]>>(this.recipes);
|
||||
}
|
||||
addRecipe(machine, new RecipeResource[] { input }, outputs);
|
||||
}
|
||||
|
||||
public RecipeResource[] getOutput(RecipeType machine, RecipeResource... input)
|
||||
{
|
||||
Iterator<Entry<RecipeResource[], RecipeResource[]>> it = this.getRecipes(machine).entrySet().iterator();
|
||||
public void removeRecipe(RecipeType machine, RecipeResource[] input)
|
||||
{
|
||||
this.recipes.get(machine).remove(input);
|
||||
}
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
Entry<RecipeResource[], RecipeResource[]> entry = it.next();
|
||||
public Map<RecipeResource[], RecipeResource[]> getRecipes(RecipeType machine)
|
||||
{
|
||||
return new HashMap<RecipeResource[], RecipeResource[]>(this.recipes.get(machine));
|
||||
}
|
||||
|
||||
if (Arrays.equals(entry.getKey(), input))
|
||||
{
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
public Map<RecipeType, Map<RecipeResource[], RecipeResource[]>> getRecipes()
|
||||
{
|
||||
return new HashMap<RecipeType, Map<RecipeResource[], RecipeResource[]>>(this.recipes);
|
||||
}
|
||||
|
||||
return new RecipeResource[] {};
|
||||
}
|
||||
public RecipeResource[] getOutput(RecipeType machine, RecipeResource... input)
|
||||
{
|
||||
Iterator<Entry<RecipeResource[], RecipeResource[]>> it = this.getRecipes(machine).entrySet().iterator();
|
||||
|
||||
public RecipeResource[] getOutput(RecipeType machine, Object... inputs)
|
||||
{
|
||||
RecipeResource[] resourceInputs = new RecipeResource[inputs.length];
|
||||
while (it.hasNext())
|
||||
{
|
||||
Entry<RecipeResource[], RecipeResource[]> entry = it.next();
|
||||
|
||||
for (int i = 0; i < inputs.length; i++)
|
||||
{
|
||||
resourceInputs[i] = getResourceFromObject(inputs[i]);
|
||||
}
|
||||
if (Arrays.equals(entry.getKey(), input))
|
||||
{
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return getOutput(machine, resourceInputs);
|
||||
}
|
||||
return new RecipeResource[] {};
|
||||
}
|
||||
|
||||
public RecipeResource[] getOutput(RecipeType machine, Object... inputs)
|
||||
{
|
||||
RecipeResource[] resourceInputs = new RecipeResource[inputs.length];
|
||||
|
||||
for (int i = 0; i < inputs.length; i++)
|
||||
{
|
||||
resourceInputs[i] = getResourceFromObject(inputs[i]);
|
||||
}
|
||||
|
||||
return getOutput(machine, resourceInputs);
|
||||
}
|
||||
}
|
||||
|
|
35
src/main/java/resonantinduction/core/grid/NodeRegistry.java
Normal file
35
src/main/java/resonantinduction/core/grid/NodeRegistry.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package resonantinduction.core.grid;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* A dynamic node loader for registering different nodes for different node interfaces.
|
||||
*
|
||||
* @author Calclavia
|
||||
*
|
||||
*/
|
||||
public class NodeRegistry
|
||||
{
|
||||
private static final HashMap<Class, Class> INTERFACE_NODE_MAP = new HashMap<Class, Class>();
|
||||
|
||||
public static void register(Class nodeInterface, Class nodeClass)
|
||||
{
|
||||
INTERFACE_NODE_MAP.put(nodeInterface, nodeClass);
|
||||
}
|
||||
|
||||
public static <N> Class<? extends N> get(INodeProvider parent, Class<N> nodeInterface)
|
||||
{
|
||||
Class nodeClass = INTERFACE_NODE_MAP.get(nodeInterface);
|
||||
|
||||
try
|
||||
{
|
||||
return (Class<? extends N>) nodeClass.getConstructor(INodeProvider.class).newInstance(parent);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -48,18 +48,18 @@ public class ItemOreResource extends Item
|
|||
@Override
|
||||
public String getItemDisplayName(ItemStack is)
|
||||
{
|
||||
String dustName = getMaterialFromStack(is);
|
||||
String material = getMaterialFromStack(is);
|
||||
|
||||
if (dustName != null)
|
||||
if (material != null)
|
||||
{
|
||||
List<ItemStack> list = OreDictionary.getOres("ingot" + dustName.substring(0, 1).toUpperCase() + dustName.substring(1));
|
||||
List<ItemStack> list = OreDictionary.getOres("ingot" + material.substring(0, 1).toUpperCase() + material.substring(1));
|
||||
|
||||
if (list.size() > 0)
|
||||
{
|
||||
ItemStack type = list.get(0);
|
||||
|
||||
String name = type.getDisplayName().replace(LanguageUtility.getLocal("misc.resonantinduction.ingot"), "").replaceAll("^ ", "").replaceAll(" $", "");
|
||||
return (LanguageUtility.getLocal(this.getUnlocalizedName() + ".name")).replace("%v", name).replace(" ", " ");
|
||||
return (LanguageUtility.getLocal(getUnlocalizedName() + ".name")).replace("%v", name).replace(" ", " ");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -103,7 +103,11 @@ public class ResourceGenerator implements IVirtualObject
|
|||
OreDictionary.registerOre("oreGold", Block.oreGold);
|
||||
OreDictionary.registerOre("oreIron", Block.oreIron);
|
||||
OreDictionary.registerOre("oreLapis", Block.oreLapis);
|
||||
regenerateOreResources();
|
||||
}
|
||||
|
||||
public static void regenerateOreResources()
|
||||
{
|
||||
// Vanilla fluid recipes
|
||||
MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER, new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME), new ItemStack(Block.stone));
|
||||
|
||||
|
|
Loading…
Reference in a new issue