Hello, my name is Anon10W1z. I have been modding for just under a year and want to enlighten some of the modders here with the information I have gathered through my experience.
1. How to avoid enchantment ID conflicts
In 1.7, Forge had dropped the need to specify the ID of your item or block in its constructor. That was great! However, enchantment IDs still need to be specified in the constructor. I have developed a simple algorithm to combat this. Behold!
private static int findFreeEnchantID() {
int i;
for (i = 0; i < Enchantment.enchantmentsList.length; ++i)
if (Enchantment.enchantmentsList[i] == null)
break;
return i;
}
1.8 version:
private static int findFreeEnchantID() {
int i;
for (i = 0; i < 256; ++i)
if (Enchantment.getEnchantmentById(i) == null)
break;
return i;
}
It is a very simple yet effective algorithm that will only fail if there is no room for an enchantment at all, which is a problem that can only be solved by deleting the enchantment itself. It simply checks for an open space for an enchantment with the lowest enchantment ID it can find. All that is left to do is call findFreeEnchantID() in the enchantment's constructor.
Some other ideas to think about:
Give a reasonable crash report if no free enchantment ID is found.
Allow the user to pick whether or not to have the enchantment ID automatically selected for them. If not, allow them to pick the ID.
2. Remove a crafting recipe from the game
It is pretty simple to add a crafting recipe to Minecraft, but what if you want to remove a crafting recipe from Minecraft? Let me direct your attention to the following method:
private static void removeRecipe(ItemStack result) {
ItemStack recipeResult = null;
List recipes = craftingmanager.getRecipeList();
for (int scan = 0; scan < recipes.size(); scan++) {
IRecipe tmpRecipe = (IRecipe) recipes.get(scan);
recipeResult = tmpRecipe.getRecipeOutput();
if (recipeResult != null) {
if (recipeResult.getItem() == resultItem.getItem() && recipeResult.getItemDamage() == resultItem.getItemDamage()) {
recipes.remove(scan);
--scan;
}
}
}
}
Call this method with whatever ItemStack you wish. It is another simple method that will search through all recipes for results that match the given ItemStack. If it finds the desired item to remove, then it checks for matching metadata values. If that also matches, then the recipe is removed from the list of recipes.
More tutorials will come soon!