Skip to content

Commit c750269

Browse files
committed
Improve recipe ingredient validation
1 parent 32f0cef commit c750269

File tree

9 files changed

+54
-23
lines changed

9 files changed

+54
-23
lines changed

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicBlastingRecipe.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ protected MagicBlastingRecipe(String key, MagicController controller) {
1313

1414
@Override
1515
protected Recipe createRecipe(ItemStack item) {
16-
return CompatibilityLib.getCompatibilityUtils().createBlastingRecipe(key, item, ingredient.getItemStack(1), ignoreDamage, experience, cookingTime);
16+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
17+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
18+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredient.getKey());
19+
return null;
20+
}
21+
return CompatibilityLib.getCompatibilityUtils().createBlastingRecipe(key, item, ingredientItem, ignoreDamage, experience, cookingTime);
1722
}
1823

1924
@Override

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicCampfireRecipe.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ protected MagicCampfireRecipe(String key, MagicController controller) {
1313

1414
@Override
1515
protected Recipe createRecipe(ItemStack item) {
16-
return CompatibilityLib.getCompatibilityUtils().createCampfireRecipe(key, item, ingredient.getItemStack(1), ignoreDamage, experience, cookingTime);
16+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
17+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
18+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredientKey);
19+
return null;
20+
}
21+
return CompatibilityLib.getCompatibilityUtils().createCampfireRecipe(key, item, ingredientItem, ignoreDamage, experience, cookingTime);
1722
}
1823

1924
@Override

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicCookingRecipe.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public abstract class MagicCookingRecipe extends MagicRecipe {
1212
protected Recipe recipe;
1313
protected ItemData ingredient;
14+
protected String ingredientKey;
1415
protected String group;
1516
protected float experience;
1617
protected int cookingTime;
@@ -27,10 +28,10 @@ public ItemStack load(ConfigurationSection configuration) {
2728
}
2829
cookingTime = configuration.getInt("cooking_time") / 50;
2930
experience = (float)configuration.getDouble("experience");
30-
String materialKey = configuration.getString("ingredient");
31-
ingredient = controller.getOrCreateItem(materialKey);
31+
ingredientKey = configuration.getString("ingredient");
32+
ingredient = controller.getOrCreateItem(ingredientKey);
3233
if (ingredient == null) {
33-
controller.getLogger().warning("Could not create " + getType() + " recipe ingredient: " + materialKey);
34+
controller.getLogger().warning("Could not create " + getType() + " recipe ingredient: " + ingredientKey);
3435
return null;
3536
}
3637
recipe = createRecipe(item);

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicFurnaceRecipe.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ protected MagicFurnaceRecipe(String key, MagicController controller) {
1313

1414
@Override
1515
protected Recipe createRecipe(ItemStack item) {
16-
return CompatibilityLib.getCompatibilityUtils().createFurnaceRecipe(key, item, ingredient.getItemStack(1), ignoreDamage, experience, cookingTime);
16+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
17+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
18+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredientKey);
19+
return null;
20+
}
21+
return CompatibilityLib.getCompatibilityUtils().createFurnaceRecipe(key, item, ingredientItem, ignoreDamage, experience, cookingTime);
1722
}
1823

1924
@Override

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicShapedRecipe.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,13 @@ public ItemStack load(ConfigurationSection configuration) {
7070
input = new ItemStack(Material.AIR);
7171
}
7272
ItemData ingredient = controller.createItemData(input);
73+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
74+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
75+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredientKey);
76+
return null;
77+
}
7378
ingredients.put(ingredientKey, ingredient);
74-
if (!CompatibilityLib.getCompatibilityUtils().setRecipeIngredient(this.recipe, ingredientKey, ingredient.getItemStack(1), ignoreDamage)) {
79+
if (!CompatibilityLib.getCompatibilityUtils().setRecipeIngredient(this.recipe, ingredientKey, ingredientItem, ignoreDamage)) {
7580
controller.getLogger().warning("Unable to set recipe ingredient from vanilla ingredient: " + input);
7681
return null;
7782
}
@@ -111,11 +116,12 @@ public ItemStack load(ConfigurationSection configuration) {
111116
continue;
112117
}
113118
ItemData ingredient = controller.getOrCreateItem(materialKey);
114-
if (ingredient == null) {
115-
controller.getLogger().warning("Invalid recipe ingredient " + materialKey);
119+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
120+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
121+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + materialKey);
116122
return null;
117123
}
118-
if (!CompatibilityLib.getCompatibilityUtils().setRecipeIngredient(shaped, key.charAt(0), ingredient.getItemStack(1), ignoreDamage)) {
124+
if (!CompatibilityLib.getCompatibilityUtils().setRecipeIngredient(shaped, key.charAt(0), ingredientItem, ignoreDamage)) {
119125
controller.getLogger().warning("Unable to set recipe ingredient " + materialKey);
120126
return null;
121127
}

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicShapelessRecipe.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ public ItemStack load(ConfigurationSection configuration) {
3232
List<ItemStack> ingredients = new ArrayList<>();
3333
Collection<String> ingredientKeys = ConfigurationUtils.getStringList(configuration, "ingredients");
3434
for (String ingredientKey : ingredientKeys) {
35-
ItemData ingredientItem = controller.getOrCreateItem(ingredientKey);
36-
if (ingredientItem == null) {
37-
controller.getLogger().warning("Could not create " + getType() + " recipe ingredient: " + ingredientKey);
35+
ItemData ingredient = controller.getOrCreateItem(ingredientKey);
36+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
37+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
38+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredientKey);
3839
return null;
3940
}
40-
ingredients.add(ingredientItem.getItemStack());
41+
ingredients.add(ingredientItem);
4142
}
4243
recipe = CompatibilityLib.getCompatibilityUtils().createShapelessRecipe(key, item, ingredients, ignoreDamage);
4344
if (recipe != null && group != null && !group.isEmpty()) {

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicSmithingRecipe.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ public ItemStack load(ConfigurationSection configuration) {
2626
}
2727
String materialKey = configuration.getString("ingredient");
2828
ingredient = controller.getOrCreateItem(materialKey);
29-
if (ingredient == null) {
30-
controller.getLogger().warning("Could not create smithing recipe ingredient: " + materialKey);
29+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
30+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
31+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + materialKey);
3132
return null;
3233
}
3334
materialKey = configuration.getString("addition");
3435
addition = controller.getOrCreateItem(materialKey);
35-
if (addition == null) {
36-
controller.getLogger().warning("Could not create smithing recipe addition: " + materialKey);
36+
ItemStack additionItem = addition == null ? null : ingredient.getItemStack();
37+
if (CompatibilityLib.getItemUtils().isEmpty(additionItem)) {
38+
controller.getLogger().warning("Invalid " + getType() + " recipe addition ingredient " + materialKey);
3739
return null;
3840
}
39-
recipe = CompatibilityLib.getCompatibilityUtils().createSmithingRecipe(key, item, ingredient.getItemStack(1), addition.getItemStack(1));
41+
recipe = CompatibilityLib.getCompatibilityUtils().createSmithingRecipe(key, item, ingredientItem, additionItem);
4042
if (recipe != null && group != null && !group.isEmpty()) {
4143
CompatibilityLib.getCompatibilityUtils().setRecipeGroup(recipe, group);
4244
}

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicSmokingRecipe.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ protected MagicSmokingRecipe(String key, MagicController controller) {
1313

1414
@Override
1515
protected Recipe createRecipe(ItemStack item) {
16-
return CompatibilityLib.getCompatibilityUtils().createSmokingRecipe(key, item, ingredient.getItemStack(1), ignoreDamage, experience, cookingTime);
16+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
17+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
18+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + ingredientKey);
19+
return null;
20+
}
21+
return CompatibilityLib.getCompatibilityUtils().createSmokingRecipe(key, item, ingredientItem, ignoreDamage, experience, cookingTime);
1722
}
1823

1924
@Override

Magic/src/main/java/com/elmakers/mine/bukkit/crafting/MagicStonecuttingRecipe.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public ItemStack load(ConfigurationSection configuration) {
2525
}
2626
String materialKey = configuration.getString("ingredient");
2727
ingredient = controller.getOrCreateItem(materialKey);
28-
if (ingredient == null) {
29-
controller.getLogger().warning("Could not create stonecutting recipe ingredient: " + materialKey);
28+
ItemStack ingredientItem = ingredient == null ? null : ingredient.getItemStack();
29+
if (CompatibilityLib.getItemUtils().isEmpty(ingredientItem)) {
30+
controller.getLogger().warning("Invalid " + getType() + " recipe ingredient " + materialKey);
3031
return null;
3132
}
32-
recipe = CompatibilityLib.getCompatibilityUtils().createStonecuttingRecipe(key, item, ingredient.getItemStack(1), ignoreDamage);
33+
recipe = CompatibilityLib.getCompatibilityUtils().createStonecuttingRecipe(key, item, ingredientItem, ignoreDamage);
3334
if (recipe != null && group != null && !group.isEmpty()) {
3435
CompatibilityLib.getCompatibilityUtils().setRecipeGroup(recipe, group);
3536
}

0 commit comments

Comments
 (0)