Switched to use Java equals()

This commit is contained in:
Calclavia 2014-01-05 17:43:59 +08:00
parent 26a269a6b5
commit 7acfada35c
6 changed files with 168 additions and 184 deletions

View file

@ -50,7 +50,6 @@ public class ClientProxy extends CommonProxy
@Override
public void postInit()
{
ItemDust.computeColors();
}
@Override

View file

@ -326,7 +326,7 @@ public class ResonantInduction
}
/** Auto-gen dusts */
ItemDust.postInit();
ItemDust.generateDusts();
ResonantInduction.proxy.postInit();
/** Inject new furnace tile class */

View file

@ -1,68 +1,65 @@
package resonantinduction.api;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.item.ItemStack;
import resonantinduction.api.RecipeUtils.*;
import resonantinduction.api.RecipeUtils.ItemStackResource;
import resonantinduction.api.RecipeUtils.OreDictResource;
import resonantinduction.api.RecipeUtils.Resource;
public final class MachineRecipes
{
public static enum RecipeType
{
GRINDER,
SAWMILL,
SMELTER,
FURNACE,
ROLLER,
BLAST_FURNACE,
METAL_FORMER;
}
private final Map<RecipeType, Map<Resource[], Resource[]>> recipes = new HashMap<RecipeType, Map<Resource[], Resource[]>>();
public static final MachineRecipes INSTANCE = new MachineRecipes();
private MachineRecipes()
{
for (RecipeType machine : RecipeType.values())
{
this.recipes.put(machine, new HashMap<Resource[], Resource[]>());
}
}
public void addRecipe(RecipeType machine, Resource[] input, Resource[] output)
{
this.recipes.get(machine).put(input, output);
}
public void removeRecipe(RecipeType machine, Resource[] input)
{
this.recipes.get(machine).remove(input);
}
public Map<Resource[], Resource[]>getRecipes(RecipeType machine)
{
return new HashMap<Resource[], Resource[]>(this.recipes.get(machine));
}
public Map<RecipeType, Map<Resource[], Resource[]>> getRecipes()
{
return new HashMap<RecipeType, Map<Resource[], Resource[]>>(this.recipes);
}
public Resource[] getRecipe(RecipeType machine, ItemStack primary, ItemStack... secondary)
{
Resource[] input = new Resource[secondary.length +1];
input[0] = new ItemStackResource(primary);
for (int i = 0; i < secondary.length; i++)
{
input[i+1] = new ItemStackResource(secondary[i]);
}
return this.getRecipes(machine).get(input);
}
public static enum RecipeType
{
GRINDER, SAWMILL, SMELTER, FURNACE, ROLLER, BLAST_FURNACE, METAL_FORMER;
}
private final Map<RecipeType, Map<Resource[], Resource[]>> recipes = new HashMap<RecipeType, Map<Resource[], Resource[]>>();
public static final MachineRecipes INSTANCE = new MachineRecipes();
private MachineRecipes()
{
for (RecipeType machine : RecipeType.values())
{
this.recipes.put(machine, new HashMap<Resource[], Resource[]>());
}
}
public void addRecipe(RecipeType machine, Resource[] input, Resource[] output)
{
this.recipes.get(machine).put(input, output);
}
public void addRecipe(RecipeType machine, ItemStack input, ItemStack output)
{
this.addRecipe(machine, new ItemStackResource[] { new ItemStackResource(input) }, new ItemStackResource[] { new ItemStackResource(output) });
}
public void addRecipe(RecipeType machine, String input, ItemStack output)
{
this.addRecipe(machine, new OreDictResource[] { new OreDictResource(input) }, new ItemStackResource[] { new ItemStackResource(output) });
}
public void removeRecipe(RecipeType machine, Resource[] input)
{
this.recipes.get(machine).remove(input);
}
public Map<Resource[], Resource[]> getRecipes(RecipeType machine)
{
return new HashMap<Resource[], Resource[]>(this.recipes.get(machine));
}
public Map<RecipeType, Map<Resource[], Resource[]>> getRecipes()
{
return new HashMap<RecipeType, Map<Resource[], Resource[]>>(this.recipes);
}
public Resource[] getRecipe(RecipeType machine, ItemStack... inputs)
{
return this.getRecipes(machine).get(inputs);
}
}

View file

@ -6,121 +6,100 @@ import net.minecraftforge.oredict.OreDictionary;
public class RecipeUtils
{
public static abstract class Resource
{
public final boolean hasChance;
public final float chance;
protected Resource()
{
this.hasChance = false;
this.chance = 100;
}
protected Resource(float chance)
{
this.hasChance = true;
this.chance = chance;
}
public abstract boolean isEqual(ItemStack is);
public abstract boolean isEqual(FluidStack fs);
public boolean hasChance()
{
return this.hasChance;
}
public static abstract class Resource
{
public final boolean hasChance;
public final float chance;
public float getChance()
{
return this.chance;
}
}
public static class ItemStackResource extends Resource
{
public final ItemStack itemStack;
protected Resource()
{
this.hasChance = false;
this.chance = 100;
}
public ItemStackResource(ItemStack is)
{
super();
this.itemStack = is;
}
public ItemStackResource(ItemStack is, float chance)
{
super(chance);
this.itemStack = is;
}
protected Resource(float chance)
{
this.hasChance = true;
this.chance = chance;
}
@Override
public boolean isEqual(ItemStack is)
{
return is.equals(this.itemStack);
}
public boolean hasChance()
{
return this.hasChance;
}
@Override
public boolean isEqual(FluidStack fs)
{
return false;
}
}
public static class OreDictResource extends Resource
{
public final String name;
public float getChance()
{
return this.chance;
}
}
public OreDictResource(String s)
{
super();
this.name = s;
}
public OreDictResource(String s, float chance)
{
super(chance);
this.name = s;
}
public static class ItemStackResource extends Resource
{
public final ItemStack itemStack;
@Override
public boolean isEqual(ItemStack is)
{
return OreDictionary.getOres(this.name).contains(is);
}
public ItemStackResource(ItemStack is)
{
super();
this.itemStack = is;
}
@Override
public boolean isEqual(FluidStack fs)
{
return false;
}
}
public static class FluidStackResource extends Resource
{
public final FluidStack fluidStack;
public ItemStackResource(ItemStack is, float chance)
{
super(chance);
this.itemStack = is;
}
public FluidStackResource(FluidStack fs)
{
super();
this.fluidStack = fs;
}
public FluidStackResource(FluidStack fs, float chance)
{
super(chance);
this.fluidStack = fs;
}
@Override
public boolean isEqual(ItemStack is)
{
return false;
}
@Override
public boolean equals(Object obj)
{
return (obj instanceof ItemStack) ? ((ItemStack) obj).equals(this.itemStack) : false;
}
}
@Override
public boolean isEqual(FluidStack fs)
{
return fs.isFluidEqual(this.fluidStack);
}
}
public static class OreDictResource extends Resource
{
public final String name;
public OreDictResource(String s)
{
super();
this.name = s;
}
public OreDictResource(String s, float chance)
{
super(chance);
this.name = s;
}
@Override
public boolean equals(Object obj)
{
return (obj instanceof ItemStack) ? OreDictionary.getOres(this.name).contains(((ItemStack) obj)) : false;
}
}
public static class FluidStackResource extends Resource
{
public final FluidStack fluidStack;
public FluidStackResource(FluidStack fs)
{
super();
this.fluidStack = fs;
}
public FluidStackResource(FluidStack fs, float chance)
{
super(chance);
this.fluidStack = fs;
}
@Override
public boolean equals(Object obj)
{
return (obj instanceof FluidStack) ? ((FluidStack) obj).equals(obj) : false;
}
}
}

View file

@ -24,6 +24,8 @@ import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.OreDictionary.OreRegisterEvent;
import resonantinduction.ResonantInduction;
import resonantinduction.api.MachineRecipes;
import resonantinduction.api.MachineRecipes.RecipeType;
import resonantinduction.api.OreDetectionBlackList;
import resonantinduction.core.base.ItemBase;
import calclavia.lib.Calclavia;
@ -40,7 +42,7 @@ import cpw.mods.fml.relauncher.SideOnly;
*/
public class ItemDust extends ItemBase
{
public static final Set<String> ingotNames = new HashSet<String>();
public static final Set<String> materialNames = new HashSet<String>();
public static final Set<ItemStack> dusts = new HashSet<ItemStack>();
public static final HashMap<String, Integer> ingotColors = new HashMap<String, Integer>();
@ -66,11 +68,11 @@ public class ItemDust extends ItemBase
if (evt.Name.startsWith("ingot"))
{
String ingotName = evt.Name.replace("ingot", "");
if (OreDetectionBlackList.isIngotBlackListed("ingot" + ingotName) || OreDetectionBlackList.isOreBlackListed("ore" + ingotName))
return;
ingotNames.add(ingotName.toLowerCase());
materialNames.add(ingotName.toLowerCase());
}
}
@ -81,16 +83,23 @@ public class ItemDust extends ItemBase
computeColors();
}
public static void postInit()
{
for (String ingotName : ingotNames)
public static void generateDusts()
{
for (String materialName : materialNames)
{
String name = ingotName.substring(0, 1).toUpperCase() + ingotName.substring(1);
if (OreDictionary.getOres("dust" + name).size() == 0 && OreDictionary.getOres("ore" + name).size() > 0)
String name = materialName.substring(0, 1).toUpperCase() + materialName.substring(1);
if (OreDictionary.getOres("ore" + name).size() > 0)
{
dusts.add(getStackFromDust(ingotName));
OreDictionary.registerOre("dust" + name, getStackFromDust(ingotName));
if (OreDictionary.getOres("dust" + name).size() == 0)
{
dusts.add(getStackFromDust(materialName));
OreDictionary.registerOre("dust" + name, getStackFromDust(materialName));
}
// Add to machine recipes
MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER, "ore" + name, OreDictionary.getOres("dust" + name).get(0));
}
}
}
@ -98,7 +107,7 @@ public class ItemDust extends ItemBase
@SideOnly(Side.CLIENT)
public static void computeColors()
{
for (String ingotName : ingotNames)
for (String ingotName : materialNames)
{
LinkedList<Integer> colorCodes = new LinkedList<Integer>();
@ -114,14 +123,14 @@ public class ItemDust extends ItemBase
Method o = ReflectionHelper.findMethod(Item.class, theIngot, new String[] { "getIconString", "func_" + "111208_A" });
String iconString;
try
{
iconString = (String) o.invoke(theIngot);
}
catch (ReflectiveOperationException e1)
{
//e1.printStackTrace();
// e1.printStackTrace();
break;
}
@ -146,7 +155,7 @@ public class ItemDust extends ItemBase
}
catch (IOException e)
{
//e.printStackTrace();
// e.printStackTrace();
}
}
if (colorCodes.size() > 0)

View file

@ -59,7 +59,7 @@ public class TileGrinderWheel extends TileElectrical
}
public boolean canGrind(ItemStack itemStack)
{
{System.out.println(MachineRecipes.INSTANCE.getRecipe(RecipeType.GRINDER, itemStack));
return MachineRecipes.INSTANCE.getRecipe(RecipeType.GRINDER, itemStack) == null ? false : MachineRecipes.INSTANCE.getRecipe(RecipeType.GRINDER, itemStack).length > 0;
}