Permalink
Browse files

Potentially fix bug resulting in 'a' being placed into CustomCrafting…

… shape.
  • Loading branch information...
me4502 committed Mar 6, 2017
1 parent ae5b5a8 commit aa049d900dea1776e0b3d0dc6c92889e849cabe9
Showing with 17 additions and 49 deletions.
  1. +17 −49 src/main/java/com/sk89q/craftbook/mechanics/crafting/RecipeManager.java
@@ -19,14 +19,12 @@
protected YAMLProcessor config;
public RecipeManager(YAMLProcessor config) {
-
INSTANCE = this;
this.config = config;
load();
}
public void load() {
-
recipes = new LinkedHashSet<Recipe>();
if (config == null) {
CraftBookPlugin.logger().severe("Failure loading recipes! Config is null!");
@@ -60,7 +58,6 @@ public void load() {
}
public void save() {
-
if (config == null) {
CraftBookPlugin.logger().severe("Failure saving recipes! Config is null!");
return; // If the config is null, it can't continue.
@@ -85,18 +82,15 @@ public void save() {
}
public Collection<Recipe> getRecipes() {
-
return recipes;
}
public void addRecipe(Recipe rec) {
-
recipes.add(rec);
save();
}
public boolean removeRecipe(String name) {
-
Iterator<Recipe> recs = recipes.iterator();
while(recs.hasNext()) {
@@ -112,7 +106,6 @@ public boolean removeRecipe(String name) {
}
public final class Recipe {
-
private final String id;
private RecipeType type;
@@ -131,10 +124,8 @@ public boolean equals(Object o) {
if(ingredients != null) {
if(ingredients.size() != ((Recipe)o).ingredients.size())
return false;
- List<CraftingItemStack> stacks = new ArrayList<CraftingItemStack>();
- stacks.addAll(ingredients);
+ List<CraftingItemStack> stacks = new ArrayList<CraftingItemStack>(ingredients);
for(CraftingItemStack st : ((Recipe)o).ingredients) {
-
if(stacks.size() <= 0)
return false;
Iterator<CraftingItemStack> it = stacks.iterator();
@@ -154,10 +145,8 @@ public boolean equals(Object o) {
if(items.size() != ((Recipe)o).items.size())
return false;
- List<CraftingItemStack> stacks = new ArrayList<CraftingItemStack>();
- stacks.addAll(items.keySet());
+ List<CraftingItemStack> stacks = new ArrayList<CraftingItemStack>(items.keySet());
for(CraftingItemStack st : ((Recipe)o).items.keySet()) {
-
if(stacks.size() <= 0)
return false;
Iterator<CraftingItemStack> it = stacks.iterator();
@@ -176,7 +165,7 @@ public boolean equals(Object o) {
if(advancedData != null)
if(advancedData.size() != ((Recipe)o).advancedData.size())
return false;
- return ((Recipe) o).getId().equals(id) && type == ((Recipe)o).type && result.equals(((Recipe)o).result);
+ return ((Recipe) o).id.equals(id) && type == ((Recipe)o).type && result.equals(((Recipe)o).result);
}
else
return false;
@@ -212,15 +201,13 @@ public boolean hasAdvancedData() {
}
private Recipe(String id, YAMLProcessor config) throws InvalidCraftingException {
-
this.id = id;
ingredients = new ArrayList<CraftingItemStack>();
items = new LinkedHashMap<CraftingItemStack, Character>();
load();
}
public Recipe(String id, RecipeType type, LinkedHashMap<CraftingItemStack, Character> items, List<String> shape, CraftingItemStack result, HashMap<String, Object> advancedData) throws InvalidCraftingException {
-
this.id = id;
this.type = type;
this.items = items;
@@ -230,7 +217,6 @@ public Recipe(String id, RecipeType type, LinkedHashMap<CraftingItemStack, Chara
}
public Recipe(String id, RecipeType type, List<CraftingItemStack> ingredients, CraftingItemStack result, HashMap<String, Object> advancedData) throws InvalidCraftingException {
-
this.id = id;
this.type = type;
this.ingredients = ingredients;
@@ -239,7 +225,6 @@ public Recipe(String id, RecipeType type, List<CraftingItemStack> ingredients, C
}
private void load() throws InvalidCraftingException {
-
type = RecipeType.getTypeFromName(config.getString("crafting-recipes." + id + ".type"));
if (type != RecipeType.SHAPED) {
ingredients = getItems("crafting-recipes." + id + ".ingredients");
@@ -283,30 +268,29 @@ else if(s.equalsIgnoreCase("commands-player"))
@SuppressWarnings("unchecked")
public void save() {
-
config.addNode("crafting-recipes." + id);
config.setProperty("crafting-recipes." + id + ".type", type.name);
if(type != RecipeType.SHAPED) {
LinkedHashMap<String, Integer> resz = new LinkedHashMap<String, Integer>();
- for(CraftingItemStack stack : ingredients)
- resz.put(stack.toString() + " ", stack.getItemStack().getAmount());
+ for(CraftingItemStack stack : ingredients) {
+ resz.put(stack.toString(), stack.getItemStack().getAmount());
+ }
config.setProperty("crafting-recipes." + id + ".ingredients", resz);
} else {
LinkedHashMap<String, Character> resz = new LinkedHashMap<String, Character>();
for(Map.Entry<CraftingItemStack, Character> craftingItemStackCharacterEntry : items.entrySet())
- resz.put(craftingItemStackCharacterEntry.getKey().toString() + " ", craftingItemStackCharacterEntry.getValue());
+ resz.put(craftingItemStackCharacterEntry.getKey().toString() + ' ', craftingItemStackCharacterEntry.getValue());
config.setProperty("crafting-recipes." + id + ".ingredients", resz);
config.setProperty("crafting-recipes." + id + ".shape", shape);
}
LinkedHashMap<String, Integer> resz = new LinkedHashMap<String, Integer>();
- resz.put(result.toString() + " ", result.getItemStack().getAmount());
+ resz.put(result.toString() + ' ', result.getItemStack().getAmount());
if(hasAdvancedData("extra-results")) {
- ArrayList<CraftingItemStack> extraResults = new ArrayList<CraftingItemStack>();
- extraResults.addAll((Collection<? extends CraftingItemStack>) getAdvancedData("extra-results"));
+ ArrayList<CraftingItemStack> extraResults = new ArrayList<CraftingItemStack>((Collection<? extends CraftingItemStack>) getAdvancedData("extra-results"));
for(CraftingItemStack s : extraResults)
- resz.put(s.toString() + " ", s.getItemStack().getAmount());
+ resz.put(s.toString() + ' ', s.getItemStack().getAmount());
}
config.setProperty("crafting-recipes." + id + ".results", resz);
if(hasAdvancedData("permission-node"))
@@ -323,22 +307,17 @@ public void save() {
}
private LinkedHashMap<CraftingItemStack, Character> getShapeIngredients(String path) {
-
LinkedHashMap<CraftingItemStack, Character> items = new LinkedHashMap<CraftingItemStack, Character>();
try {
- for (Object oitem : config.getKeys(path)) {
- String okey = String.valueOf(oitem);
- String item = okey.trim();
-
- ItemStack stack = ItemUtil.makeItemValid(ItemSyntax.getItem(RegexUtil.PERCENT_PATTERN.split(item)[0]));
+ for (String item : config.getKeys(path)) {
+ ItemStack stack = ItemUtil.makeItemValid(ItemSyntax.getItem(RegexUtil.PERCENT_PATTERN.split(item.trim())[0]));
if (stack != null) {
-
stack.setAmount(1);
CraftingItemStack itemStack = new CraftingItemStack(stack);
if(RegexUtil.PERCENT_PATTERN.split(item).length > 1)
- itemStack.addAdvancedData("chance", Double.parseDouble(RegexUtil.PERCENT_PATTERN.split(item)[1]));
- items.put(itemStack, config.getString(path + "." + okey, "a").charAt(0));
+ itemStack.addAdvancedData("chance", Double.parseDouble(RegexUtil.PERCENT_PATTERN.split(item.trim())[1]));
+ items.put(itemStack, config.getString(path + '.' + item, "a").charAt(0));
}
}
} catch (Exception e) {
@@ -349,7 +328,6 @@ public void save() {
}
private List<CraftingItemStack> getItems(String path) {
-
List<CraftingItemStack> items = new ArrayList<CraftingItemStack>();
try {
for (Object oitem : config.getKeys(path)) {
@@ -359,8 +337,7 @@ public void save() {
ItemStack stack = ItemUtil.makeItemValid(ItemSyntax.getItem(RegexUtil.PERCENT_PATTERN.split(item)[0]));
if (stack != null) {
-
- stack.setAmount(config.getInt(path + "." + okey, 1));
+ stack.setAmount(config.getInt(path + '.' + okey, 1));
CraftingItemStack itemStack = new CraftingItemStack(stack);
if(RegexUtil.PERCENT_PATTERN.split(item).length > 1)
itemStack.addAdvancedData("chance", Double.parseDouble(RegexUtil.PERCENT_PATTERN.split(item)[1]));
@@ -375,32 +352,26 @@ public void save() {
}
public String getId() {
-
return id;
}
public RecipeType getType() {
-
return type;
}
public List<CraftingItemStack> getIngredients() {
-
return ingredients;
}
public String[] getShape() {
-
return shape.toArray(new String[shape.size()]);
}
public LinkedHashMap<CraftingItemStack, Character> getShapedIngredients() {
-
return items;
}
public CraftingItemStack getResult() {
-
return result;
}
@@ -430,25 +401,22 @@ public void addAdvancedData(String key, Object data) {
private String name;
- private RecipeType(String name) {
-
+ RecipeType(String name) {
this.name = name;
}
public String getName() {
-
return name;
}
public static RecipeType getTypeFromName(String name) {
-
if(name.equalsIgnoreCase("Shaped2x2") || name.equalsIgnoreCase("Shaped3x3")) {
CraftBookPlugin.logger().warning("You are using deprecated recipe type '" + name + "', we recommend you change it to 'shaped'!");
return SHAPED;
}
for (RecipeType t : RecipeType.values()) {
- if (t.getName().equalsIgnoreCase(name))
+ if (t.name.equalsIgnoreCase(name))
return t;
}
return SHAPELESS; // Default to shapeless

0 comments on commit aa049d9

Please sign in to comment.