我正在尝试为Minecraft 1.7.10创建一个小实用程序mod,在其中将一个ID列表放在一个配置文件中,mod将从游戏中删除它们的食谱。我有配置文件工作,但我似乎无法删除配方。
我删除食谱的方法是我在其他地方的几个论坛帖子中看到过的,并稍加修改以使用我的配置文件(或者在我的情况下不起作用)。
以下是方法:
private void removeRecipes(String toDelete)
{
ArrayList<?> recipes = (ArrayList<?>) CraftingManager.getInstance().getRecipeList();
ItemStack recipeResult = null;
ItemStack resultItem = new ItemStack((Item)Item.itemRegistry.getObject(toDelete));
resultItem.stackSize = 1;
resultItem.setItemDamage(0);
for (int scan = 0; scan < recipes.size(); scan++)
{
IRecipe tmpRecipe = (IRecipe) recipes.get(scan);
if (tmpRecipe instanceof ShapedRecipes)
{
ShapedRecipes recipe = (ShapedRecipes)tmpRecipe;
recipeResult = recipe.getRecipeOutput();
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (tmpRecipe instanceof ShapelessRecipes)
{
ShapelessRecipes recipe = (ShapelessRecipes)tmpRecipe;
recipeResult = recipe.getRecipeOutput();
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (ItemStack.areItemStacksEqual(resultItem, recipeResult))
{
System.out.println("[RecipeRemover] Removed Recipe: " + recipes.get(scan) + " -> " + recipeResult);
recipes.remove(scan);
}
}
}
答案 0 :(得分:3)
我的代码显然有些不对劲,但这是一种更有效的方法(并且应该普遍适用):
@SuppressWarnings("unchecked")
private void removeRecipes(String toDelete)
{
ItemStack resultItem = new ItemStack((Item)Item.itemRegistry.getObject(toDelete));
resultItem.stackSize = 1;
resultItem.setItemDamage(0);
List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
for (int i = 0; i < recipes.size(); i++)
{
IRecipe tmpRecipe = recipes.get(i);
ItemStack recipeResult = tmpRecipe.getRecipeOutput();
if(recipeResult != null)
{
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (ItemStack.areItemStacksEqual(resultItem, recipeResult))
{
recipes.remove(i--);
}
}
}