diff --git a/src/com/dre/brewery/api/BreweryApi.java b/src/com/dre/brewery/api/BreweryApi.java index b6c8494..6b0c3a4 100644 --- a/src/com/dre/brewery/api/BreweryApi.java +++ b/src/com/dre/brewery/api/BreweryApi.java @@ -253,6 +253,16 @@ public class BreweryApi { return BRecipe.get(name); } + /** + * Get a BRecipe by _one of_ its names. + *

May be any of the quality names, or the optional config id. + *

Returns null if recipe with that name does not exist + */ + @Nullable + public static BRecipe getRecipeMatch(String name) { + return BRecipe.getMatching(name); + } + /** * Add a New Recipe. *

Brews can be made out of this Recipe. diff --git a/src/com/dre/brewery/listeners/CommandListener.java b/src/com/dre/brewery/listeners/CommandListener.java index 59f5c66..b68a183 100644 --- a/src/com/dre/brewery/listeners/CommandListener.java +++ b/src/com/dre/brewery/listeners/CommandListener.java @@ -492,7 +492,7 @@ public class CommandListener implements CommandExecutor { BRecipe nonDistill = ingredients.getBestRecipe(brew.getWood(), brew.getAgeTime(), false); P.p.log("&lWould prefer Recipe: " + (nonDistill == null ? "none" : nonDistill.getRecipeName()) + " and Distill-Recipe: " + (distill == null ? "none" : distill.getRecipeName())); } else { - BRecipe recipe = BRecipe.get(recipeName); + BRecipe recipe = BRecipe.getMatching(recipeName); if (recipe == null) { P.p.msg(player, "Could not find Recipe " + recipeName); return; @@ -665,19 +665,14 @@ public class CommandListener implements CommandExecutor { } else { name = args[1]; } + name = name.replaceAll("\"", ""); if (player.getInventory().firstEmpty() == -1) { p.msg(sender, p.languageReader.get("CMD_Copy_Error", "1")); return; } - BRecipe recipe = null; - for (BRecipe r : BRecipe.getAllRecipes()) { - if (r.hasName(name)) { - recipe = r; - break; - } - } + BRecipe recipe = BRecipe.getMatching(name); if (recipe != null) { ItemStack item = recipe.create(quality); if (item != null) { diff --git a/src/com/dre/brewery/recipe/BRecipe.java b/src/com/dre/brewery/recipe/BRecipe.java index d0e151e..2259775 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -32,6 +32,7 @@ public class BRecipe { // info private String[] name; private boolean saveInData; // If this recipe should be saved in data and loaded again when the server restarts. Applicable to non-config recipes + private String optionalID; // ID that might be given by the config // brewing private List ingredients = new ArrayList<>(); // Items and amounts @@ -85,6 +86,7 @@ public class BRecipe { @Nullable public static BRecipe fromConfig(ConfigurationSection configSectionRecipes, String recipeId) { BRecipe recipe = new BRecipe(); + recipe.optionalID = recipeId; String nameList = configSectionRecipes.getString(recipeId + ".name"); if (nameList != null) { String[] name = nameList.split("/"); @@ -542,6 +544,11 @@ public class BRecipe { return false; } + @Nullable + public String getOptionalID() { + return optionalID; + } + public List getIngredients() { return ingredients; } @@ -749,6 +756,34 @@ public class BRecipe { return recipes; } + /** + * Get the BRecipe that has the given name as one of its quality names. + */ + @Nullable + public static BRecipe getMatching(String name) { + BRecipe mainNameRecipe = get(name); + if (mainNameRecipe != null) { + return mainNameRecipe; + } + for (BRecipe recipe : recipes) { + if (recipe.getName(1).equalsIgnoreCase(name)) { + return recipe; + } else if (recipe.getName(10).equalsIgnoreCase(name)) { + return recipe; + } + } + for (BRecipe recipe : recipes) { + if (recipe.getOptionalID().equalsIgnoreCase(name)) { + return recipe; + } + } + return null; + } + + /** + * Get the BRecipe that has that name as its name + */ + @Nullable public static BRecipe get(String name) { for (BRecipe recipe : recipes) { if (recipe.getRecipeName().equalsIgnoreCase(name)) { @@ -865,7 +900,7 @@ public class BRecipe { * Add Commands that are executed by the player on drinking */ public Builder addPlayerCmds(String... cmds) { - ArrayList> playercmds = new ArrayList>(cmds.length); + ArrayList> playercmds = new ArrayList<>(cmds.length); for (String cmd : cmds) { playercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.CMD)); @@ -882,7 +917,7 @@ public class BRecipe { * Add Commands that are executed by the server on drinking */ public Builder addServerCmds(String... cmds) { - ArrayList> servercmds = new ArrayList>(cmds.length); + ArrayList> servercmds = new ArrayList<>(cmds.length); for (String cmd : cmds) { servercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.CMD)); @@ -911,6 +946,14 @@ public class BRecipe { return this; } + /** + * Set the Optional ID of this recipe + */ + public Builder setID(String id) { + recipe.optionalID = id; + return this; + } + /** * Add Custom Model Data for each Quality */