2012-08-06 00:59:53 +00:00
|
|
|
package net.minecraftforge.oredict;
|
2012-08-01 23:35:35 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2012-12-13 07:27:57 +00:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.CraftingManager;
|
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
|
|
import net.minecraft.item.crafting.ShapedRecipes;
|
|
|
|
import net.minecraft.item.crafting.ShapelessRecipes;
|
2012-08-06 00:59:53 +00:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.Event;
|
2012-08-01 23:35:35 +00:00
|
|
|
|
|
|
|
public class OreDictionary
|
|
|
|
{
|
2012-11-13 01:07:33 +00:00
|
|
|
private static boolean hasInit = false;
|
2012-08-01 23:35:35 +00:00
|
|
|
private static int maxID = 0;
|
|
|
|
private static HashMap<String, Integer> oreIDs = new HashMap<String, Integer>();
|
|
|
|
private static HashMap<Integer, ArrayList<ItemStack>> oreStacks = new HashMap<Integer, ArrayList<ItemStack>>();
|
2013-03-11 21:43:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minecraft changed from -1 to Short.MAX_VALUE in 1.5 release for the "block wildcard". Use this in case it
|
|
|
|
* changes again.
|
|
|
|
*/
|
|
|
|
public static final int WILDCARD_VALUE = Short.MAX_VALUE;
|
|
|
|
|
2012-10-28 14:24:01 +00:00
|
|
|
static {
|
|
|
|
initVanillaEntries();
|
|
|
|
}
|
|
|
|
|
2012-11-06 00:15:43 +00:00
|
|
|
public static void initVanillaEntries()
|
|
|
|
{
|
2012-11-13 01:07:33 +00:00
|
|
|
if (!hasInit)
|
|
|
|
{
|
2013-03-11 21:43:23 +00:00
|
|
|
registerOre("logWood", new ItemStack(Block.wood, 1, WILDCARD_VALUE));
|
|
|
|
registerOre("plankWood", new ItemStack(Block.planks, 1, WILDCARD_VALUE));
|
|
|
|
registerOre("slabWood", new ItemStack(Block.woodSingleSlab, 1, WILDCARD_VALUE));
|
2013-03-20 01:09:34 +00:00
|
|
|
registerOre("stairWood", Block.stairsWoodOak);
|
2012-11-13 01:07:33 +00:00
|
|
|
registerOre("stairWood", Block.stairsWoodBirch);
|
|
|
|
registerOre("stairWood", Block.stairsWoodJungle);
|
|
|
|
registerOre("stairWood", Block.stairsWoodSpruce);
|
|
|
|
registerOre("stickWood", Item.stick);
|
2013-03-11 21:43:23 +00:00
|
|
|
registerOre("treeSapling", new ItemStack(Block.sapling, 1, WILDCARD_VALUE));
|
|
|
|
registerOre("treeLeaves", new ItemStack(Block.leaves, 1, WILDCARD_VALUE));
|
2013-09-30 21:58:03 +00:00
|
|
|
registerOre("oreGold", Block.oreGold);
|
|
|
|
registerOre("oreIron", Block.oreIron);
|
|
|
|
registerOre("oreLapis", Block.oreLapis);
|
|
|
|
registerOre("oreDiamond", Block.oreDiamond);
|
2013-04-26 01:36:51 +00:00
|
|
|
registerOre("oreRedstone", Block.oreRedstone);
|
2013-09-30 21:58:03 +00:00
|
|
|
registerOre("oreEmerald", Block.oreEmerald);
|
|
|
|
registerOre("oreQuartz", Block.oreNetherQuartz);
|
|
|
|
registerOre("stone", Block.stone);
|
2013-06-12 02:58:05 +00:00
|
|
|
registerOre("cobblestone", Block.cobblestone);
|
2013-09-30 21:58:03 +00:00
|
|
|
registerOre("record", Item.record13);
|
|
|
|
registerOre("record", Item.recordCat);
|
|
|
|
registerOre("record", Item.recordBlocks);
|
|
|
|
registerOre("record", Item.recordChirp);
|
|
|
|
registerOre("record", Item.recordFar);
|
|
|
|
registerOre("record", Item.recordMall);
|
|
|
|
registerOre("record", Item.recordMellohi);
|
|
|
|
registerOre("record", Item.recordStal);
|
|
|
|
registerOre("record", Item.recordStrad);
|
|
|
|
registerOre("record", Item.recordWard);
|
|
|
|
registerOre("record", Item.record11);
|
|
|
|
registerOre("record", Item.recordWait);
|
2012-11-13 01:07:33 +00:00
|
|
|
}
|
2012-11-06 00:15:43 +00:00
|
|
|
|
|
|
|
// Build our list of items to replace with ore tags
|
|
|
|
Map<ItemStack, String> replacements = new HashMap<ItemStack, String>();
|
|
|
|
replacements.put(new ItemStack(Item.stick), "stickWood");
|
2013-10-19 01:58:31 +00:00
|
|
|
replacements.put(new ItemStack(Block.planks), "plankWood");
|
|
|
|
replacements.put(new ItemStack(Block.planks, 1, WILDCARD_VALUE), "plankWood");
|
2013-06-12 02:58:05 +00:00
|
|
|
replacements.put(new ItemStack(Block.stone), "stone");
|
2013-10-19 01:58:31 +00:00
|
|
|
replacements.put(new ItemStack(Block.stone, 1, WILDCARD_VALUE), "stone");
|
2013-06-12 02:58:05 +00:00
|
|
|
replacements.put(new ItemStack(Block.cobblestone), "cobblestone");
|
2013-10-19 01:58:31 +00:00
|
|
|
replacements.put(new ItemStack(Block.cobblestone, 1, WILDCARD_VALUE), "cobblestone");
|
2012-11-06 00:15:43 +00:00
|
|
|
|
|
|
|
// Register dyes
|
2013-03-11 21:43:23 +00:00
|
|
|
String[] dyes =
|
2012-11-06 00:15:43 +00:00
|
|
|
{
|
|
|
|
"dyeBlack",
|
|
|
|
"dyeRed",
|
|
|
|
"dyeGreen",
|
|
|
|
"dyeBrown",
|
|
|
|
"dyeBlue",
|
|
|
|
"dyePurple",
|
|
|
|
"dyeCyan",
|
|
|
|
"dyeLightGray",
|
|
|
|
"dyeGray",
|
|
|
|
"dyePink",
|
|
|
|
"dyeLime",
|
|
|
|
"dyeYellow",
|
|
|
|
"dyeLightBlue",
|
|
|
|
"dyeMagenta",
|
|
|
|
"dyeOrange",
|
|
|
|
"dyeWhite"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
ItemStack dye = new ItemStack(Item.dyePowder, 1, i);
|
2012-11-13 01:07:33 +00:00
|
|
|
if (!hasInit)
|
|
|
|
{
|
|
|
|
registerOre(dyes[i], dye);
|
|
|
|
}
|
2012-11-06 00:15:43 +00:00
|
|
|
replacements.put(dye, dyes[i]);
|
|
|
|
}
|
2012-11-13 01:07:33 +00:00
|
|
|
hasInit = true;
|
2012-11-06 00:15:43 +00:00
|
|
|
|
2013-01-02 04:57:45 +00:00
|
|
|
ItemStack[] replaceStacks = replacements.keySet().toArray(new ItemStack[replacements.keySet().size()]);
|
2012-11-06 00:15:43 +00:00
|
|
|
|
|
|
|
// Ignore recipes for the following items
|
|
|
|
ItemStack[] exclusions = new ItemStack[]
|
|
|
|
{
|
|
|
|
new ItemStack(Block.blockLapis),
|
|
|
|
new ItemStack(Item.cookie),
|
2013-06-17 22:09:40 +00:00
|
|
|
new ItemStack(Block.stoneBrick),
|
|
|
|
new ItemStack(Block.stoneSingleSlab),
|
|
|
|
new ItemStack(Block.stairsCobblestone),
|
|
|
|
new ItemStack(Block.cobblestoneWall)
|
2012-11-06 00:15:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List recipes = CraftingManager.getInstance().getRecipeList();
|
|
|
|
List<IRecipe> recipesToRemove = new ArrayList<IRecipe>();
|
|
|
|
List<IRecipe> recipesToAdd = new ArrayList<IRecipe>();
|
|
|
|
|
|
|
|
// Search vanilla recipes for recipes to replace
|
|
|
|
for(Object obj : recipes)
|
|
|
|
{
|
|
|
|
if(obj instanceof ShapedRecipes)
|
|
|
|
{
|
|
|
|
ShapedRecipes recipe = (ShapedRecipes)obj;
|
|
|
|
ItemStack output = recipe.getRecipeOutput();
|
|
|
|
if (output != null && containsMatch(false, exclusions, output))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(containsMatch(true, recipe.recipeItems, replaceStacks))
|
|
|
|
{
|
|
|
|
recipesToRemove.add(recipe);
|
|
|
|
recipesToAdd.add(new ShapedOreRecipe(recipe, replacements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(obj instanceof ShapelessRecipes)
|
|
|
|
{
|
|
|
|
ShapelessRecipes recipe = (ShapelessRecipes)obj;
|
|
|
|
ItemStack output = recipe.getRecipeOutput();
|
|
|
|
if (output != null && containsMatch(false, exclusions, output))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-02 04:57:45 +00:00
|
|
|
if(containsMatch(true, (ItemStack[])recipe.recipeItems.toArray(new ItemStack[recipe.recipeItems.size()]), replaceStacks))
|
2012-11-06 00:15:43 +00:00
|
|
|
{
|
|
|
|
recipesToRemove.add((IRecipe)obj);
|
|
|
|
IRecipe newRecipe = new ShapelessOreRecipe(recipe, replacements);
|
|
|
|
recipesToAdd.add(newRecipe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipes.removeAll(recipesToRemove);
|
|
|
|
recipes.addAll(recipesToAdd);
|
2012-11-13 01:07:33 +00:00
|
|
|
if (recipesToRemove.size() > 0)
|
|
|
|
{
|
|
|
|
System.out.println("Replaced " + recipesToRemove.size() + " ore recipies");
|
|
|
|
}
|
2012-10-28 14:24:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
/**
|
2013-03-11 21:43:23 +00:00
|
|
|
* Gets the integer ID for the specified ore name.
|
2012-08-01 23:35:35 +00:00
|
|
|
* If the name does not have a ID it assigns it a new one.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-08-01 23:35:35 +00:00
|
|
|
* @param name The unique name for this ore 'oreIron', 'ingotIron', etc..
|
|
|
|
* @return A number representing the ID for this ore type
|
|
|
|
*/
|
|
|
|
public static int getOreID(String name)
|
|
|
|
{
|
|
|
|
Integer val = oreIDs.get(name);
|
|
|
|
if (val == null)
|
|
|
|
{
|
|
|
|
val = maxID++;
|
|
|
|
oreIDs.put(name, val);
|
|
|
|
oreStacks.put(val, new ArrayList<ItemStack>());
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
/**
|
|
|
|
* Reverse of getOreID, will not create new entries.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-08-01 23:35:35 +00:00
|
|
|
* @param id The ID to translate to a string
|
|
|
|
* @return The String name, or "Unknown" if not found.
|
|
|
|
*/
|
|
|
|
public static String getOreName(int id)
|
|
|
|
{
|
|
|
|
for (Map.Entry<String, Integer> entry : oreIDs.entrySet())
|
|
|
|
{
|
|
|
|
if (id == entry.getValue())
|
|
|
|
{
|
|
|
|
return entry.getKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-11-27 03:22:11 +00:00
|
|
|
/**
|
|
|
|
* Gets the integer ID for the specified item stack.
|
|
|
|
* If the item stack is not linked to any ore, this will return -1 and no new entry will be created.
|
|
|
|
*
|
|
|
|
* @param itemStack The item stack of the ore.
|
|
|
|
* @return A number representing the ID for this ore type, or -1 if couldn't find it.
|
|
|
|
*/
|
|
|
|
public static int getOreID(ItemStack itemStack)
|
|
|
|
{
|
2013-02-23 19:22:01 +00:00
|
|
|
if (itemStack == null)
|
|
|
|
{
|
2012-11-27 03:22:11 +00:00
|
|
|
return -1;
|
2013-02-23 19:22:01 +00:00
|
|
|
}
|
2012-11-27 03:22:11 +00:00
|
|
|
|
2013-02-23 19:22:01 +00:00
|
|
|
for(Entry<Integer, ArrayList<ItemStack>> ore : oreStacks.entrySet())
|
2012-11-27 03:22:11 +00:00
|
|
|
{
|
2013-02-23 19:22:01 +00:00
|
|
|
for(ItemStack target : ore.getValue())
|
2012-11-27 03:22:11 +00:00
|
|
|
{
|
2013-03-11 21:43:23 +00:00
|
|
|
if(itemStack.itemID == target.itemID && (target.getItemDamage() == WILDCARD_VALUE || itemStack.getItemDamage() == target.getItemDamage()))
|
2013-02-23 19:22:01 +00:00
|
|
|
{
|
|
|
|
return ore.getKey();
|
|
|
|
}
|
2012-11-27 03:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1; // didn't find it.
|
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the ArrayList of items that are registered to this ore type.
|
|
|
|
* Creates the list as empty if it did not exist.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-09-11 20:11:17 +00:00
|
|
|
* @param name The ore name, directly calls getOreID
|
2012-08-01 23:35:35 +00:00
|
|
|
* @return An arrayList containing ItemStacks registered for this ore
|
|
|
|
*/
|
|
|
|
public static ArrayList<ItemStack> getOres(String name)
|
|
|
|
{
|
|
|
|
return getOres(getOreID(name));
|
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-06 00:59:53 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a list of all unique ore names that are already registered.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-08-06 00:59:53 +00:00
|
|
|
* @return All unique ore names that are currently registered.
|
|
|
|
*/
|
|
|
|
public static String[] getOreNames()
|
|
|
|
{
|
2013-01-02 04:57:45 +00:00
|
|
|
return oreIDs.keySet().toArray(new String[oreIDs.keySet().size()]);
|
2012-08-06 00:59:53 +00:00
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the ArrayList of items that are registered to this ore type.
|
|
|
|
* Creates the list as empty if it did not exist.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-08-01 23:35:35 +00:00
|
|
|
* @param id The ore ID, see getOreID
|
|
|
|
* @return An arrayList containing ItemStacks registered for this ore
|
|
|
|
*/
|
|
|
|
public static ArrayList<ItemStack> getOres(Integer id)
|
|
|
|
{
|
|
|
|
ArrayList<ItemStack> val = oreStacks.get(id);
|
|
|
|
if (val == null)
|
|
|
|
{
|
|
|
|
val = new ArrayList<ItemStack>();
|
|
|
|
oreStacks.put(id, val);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2012-11-06 00:15:43 +00:00
|
|
|
|
|
|
|
private static boolean containsMatch(boolean strict, ItemStack[] inputs, ItemStack... targets)
|
|
|
|
{
|
|
|
|
for (ItemStack input : inputs)
|
|
|
|
{
|
|
|
|
for (ItemStack target : targets)
|
|
|
|
{
|
|
|
|
if (itemMatches(target, input, strict))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean itemMatches(ItemStack target, ItemStack input, boolean strict)
|
|
|
|
{
|
|
|
|
if (input == null && target != null || input != null && target == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
return (target.itemID == input.itemID && ((target.getItemDamage() == WILDCARD_VALUE && !strict) || target.getItemDamage() == input.getItemDamage()));
|
2012-11-06 00:15:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
//Convenience functions that make for cleaner code mod side. They all drill down to registerOre(String, int, ItemStack)
|
|
|
|
public static void registerOre(String name, Item ore){ registerOre(name, new ItemStack(ore)); }
|
|
|
|
public static void registerOre(String name, Block ore){ registerOre(name, new ItemStack(ore)); }
|
|
|
|
public static void registerOre(String name, ItemStack ore){ registerOre(name, getOreID(name), ore); }
|
|
|
|
public static void registerOre(int id, Item ore){ registerOre(id, new ItemStack(ore)); }
|
|
|
|
public static void registerOre(int id, Block ore){ registerOre(id, new ItemStack(ore)); }
|
|
|
|
public static void registerOre(int id, ItemStack ore){ registerOre(getOreName(id), id, ore); }
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-01 23:35:35 +00:00
|
|
|
/**
|
|
|
|
* Registers a ore item into the dictionary.
|
|
|
|
* Raises the registerOre function in all registered handlers.
|
2013-03-11 21:43:23 +00:00
|
|
|
*
|
2012-08-01 23:35:35 +00:00
|
|
|
* @param name The name of the ore
|
|
|
|
* @param id The ID of the ore
|
|
|
|
* @param ore The ore's ItemStack
|
|
|
|
*/
|
|
|
|
private static void registerOre(String name, int id, ItemStack ore)
|
|
|
|
{
|
|
|
|
ArrayList<ItemStack> ores = getOres(id);
|
|
|
|
ore = ore.copy();
|
|
|
|
ores.add(ore);
|
2012-08-06 02:14:02 +00:00
|
|
|
MinecraftForge.EVENT_BUS.post(new OreRegisterEvent(name, ore));
|
2012-08-06 00:59:53 +00:00
|
|
|
}
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-06 00:59:53 +00:00
|
|
|
public static class OreRegisterEvent extends Event
|
|
|
|
{
|
|
|
|
public final String Name;
|
|
|
|
public final ItemStack Ore;
|
2013-03-11 21:43:23 +00:00
|
|
|
|
2012-08-06 00:59:53 +00:00
|
|
|
public OreRegisterEvent(String name, ItemStack ore)
|
2012-08-01 23:35:35 +00:00
|
|
|
{
|
2012-08-06 00:59:53 +00:00
|
|
|
this.Name = name;
|
|
|
|
this.Ore = ore;
|
2012-08-01 23:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|