diff --git a/src/com/dre/brewery/recipe/BCauldronRecipe.java b/src/com/dre/brewery/recipe/BCauldronRecipe.java index 353c3b6..ffa9b2e 100644 --- a/src/com/dre/brewery/recipe/BCauldronRecipe.java +++ b/src/com/dre/brewery/recipe/BCauldronRecipe.java @@ -1,6 +1,7 @@ package com.dre.brewery.recipe; import com.dre.brewery.P; +import com.dre.brewery.utility.StringParser; import com.dre.brewery.utility.Tuple; import org.bukkit.Color; import org.bukkit.Material; @@ -102,7 +103,7 @@ public class BCauldronRecipe { } - List> lore = BRecipe.loadLore(cfg, id + ".lore"); + List> lore = BRecipe.loadQualityStringList(cfg, id + ".lore", StringParser.loreParser); if (lore != null && !lore.isEmpty()) { recipe.lore = lore.stream().map(Tuple::second).collect(Collectors.toList()); } diff --git a/src/com/dre/brewery/recipe/BRecipe.java b/src/com/dre/brewery/recipe/BRecipe.java index 0aea473..d76ea0b 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -5,6 +5,7 @@ import com.dre.brewery.Brew; import com.dre.brewery.P; import com.dre.brewery.filedata.BConfig; import com.dre.brewery.utility.BUtil; +import com.dre.brewery.utility.StringParser; import com.dre.brewery.utility.Tuple; import org.apache.commons.lang.NotImplementedException; import org.bukkit.Color; @@ -49,8 +50,8 @@ public class BRecipe { // drinking private List effects = new ArrayList<>(); // Special Effects when drinking - private List playercmds; // Commands executed as the player when drinking - private List servercmds; // Commands executed as the server when drinking + private @Nullable List> playercmds; // Commands executed as the player when drinking + private @Nullable List> servercmds; // Commands executed as the server when drinking private String drinkMsg; // Message when drinking private String drinkTitle; // Title to show when drinking @@ -127,27 +128,10 @@ public class BRecipe { return null; } - recipe.lore = loadLore(configSectionRecipes, recipeId + ".lore"); + recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.loreParser); - recipe.servercmds = BUtil.loadCfgStringList(configSectionRecipes, recipeId + ".servercommands"); - recipe.playercmds = BUtil.loadCfgStringList(configSectionRecipes, recipeId + ".playercommands"); - - if (recipe.servercmds != null && !recipe.servercmds.isEmpty()) { - for (ListIterator iter = recipe.servercmds.listIterator(); iter.hasNext(); ) { - String cmd = iter.next(); - if (cmd.startsWith("/")) { - iter.set(cmd.substring(1)); - } - } - } - if (recipe.playercmds != null && !recipe.playercmds.isEmpty()) { - for (ListIterator iter = recipe.playercmds.listIterator(); iter.hasNext(); ) { - String cmd = iter.next(); - if (cmd.startsWith("/")) { - iter.set(cmd.substring(1)); - } - } - } + recipe.servercmds = loadQualityStringList(configSectionRecipes, recipeId + ".servercommands", StringParser.cmdParser); + recipe.playercmds = loadQualityStringList(configSectionRecipes, recipeId + ".playercommands", StringParser.cmdParser); recipe.drinkMsg = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinkmessage")); recipe.drinkTitle = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinktitle")); @@ -307,33 +291,27 @@ public class BRecipe { return ingredients; } + /** + * Load a list of strings from a ConfigurationSection and parse it accordingly using a parser. + */ @Nullable - public static List> loadLore(ConfigurationSection cfg, String path) { + public static List> loadQualityStringList(ConfigurationSection cfg, String path, StringParser p) { List load = BUtil.loadCfgStringList(cfg, path); if (load != null) { - List> lore = new ArrayList<>(load.size()); - for (String line : load) { - line = P.p.color(line); - int plus = 0; - if (line.startsWith("+++")) { - plus = 3; - line = line.substring(3); - } else if (line.startsWith("++")) { - plus = 2; - line = line.substring(2); - } else if (line.startsWith("+")) { - plus = 1; - line = line.substring(1); - } - if (line.startsWith(" ")) { - line = line.substring(1); - } - if (!line.startsWith("§")) { - line = "§9" + line; - } - lore.add(new Tuple<>(plus, line)); + List> list = new ArrayList<>(load.size()); + // create fallback parser, so passing null will convert the String to a Touple without furter processing. + if (p == null){ + p = new StringParser() { + @Override + public Object parse(String line) { + return new Tuple(0, line); + } + }; } - return lore; + for (String line : load) { + list.add((Tuple) p.parse(line)); + } + return list; } return null; } @@ -450,12 +428,12 @@ public class BRecipe { public void applyDrinkFeatures(Player player, int quality) { if (playercmds != null && !playercmds.isEmpty()) { - for (String cmd : playercmds) { + for (String cmd : getPlayercmdsForQuality(quality)) { player.performCommand(BUtil.applyPlaceholders(cmd, player.getName(), quality)); } } if (servercmds != null && !servercmds.isEmpty()) { - for (String cmd : servercmds) { + for (String cmd : getServercmdsForQuality(quality)) { P.p.getServer().dispatchCommand(P.p.getServer().getConsoleSender(), BUtil.applyPlaceholders(cmd, player.getName(), quality)); } } @@ -628,7 +606,25 @@ public class BRecipe { @Nullable public List getLoreForQuality(int quality) { - if (lore == null) return null; + return getStringForQuality(quality, lore); + } + + @Nullable + public List getPlayercmdsForQuality(int quality) { + return getStringForQuality(quality, playercmds); + } + + @Nullable + public List getServercmdsForQuality(int quality) { + return getStringForQuality(quality, servercmds); + } + + /** + * Get a quality filtered list of supported attributes + */ + @Nullable + public List getStringForQuality(int quality, List> source) { + if (source == null) return null; int plus; if (quality <= 3) { plus = 1; @@ -637,8 +633,8 @@ public class BRecipe { } else { plus = 3; } - List list = new ArrayList<>(lore.size()); - for (Tuple line : lore) { + List list = new ArrayList<>(source.size()); + for (Tuple line : source) { if (line.first() == 0 || line.first() == plus) { list.add(line.second()); } @@ -653,11 +649,13 @@ public class BRecipe { return cmData; } - public List getPlayercmds() { + @Nullable + public List> getPlayercmds() { return playercmds; } - public List getServercmds() { + @Nullable + public List> getServercmds() { return servercmds; } @@ -880,10 +878,16 @@ public class BRecipe { * Add Commands that are executed by the player on drinking */ public Builder addPlayerCmds(String... cmds) { - if (recipe.playercmds == null) { - recipe.playercmds = new ArrayList<>(cmds.length); + ArrayList> playercmds = new ArrayList>(cmds.length); + + for (String cmd : cmds) { + playercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); + } + if (recipe.playercmds == null) { + recipe.playercmds = playercmds; + } else { + recipe.playercmds.addAll(playercmds); } - Collections.addAll(recipe.playercmds, cmds); return this; } @@ -891,10 +895,16 @@ public class BRecipe { * Add Commands that are executed by the server on drinking */ public Builder addServerCmds(String... cmds) { - if (recipe.servercmds == null) { - recipe.servercmds = new ArrayList<>(cmds.length); + ArrayList> servercmds = new ArrayList>(cmds.length); + + for (String cmd : cmds) { + servercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); + } + if (recipe.servercmds == null) { + recipe.servercmds = servercmds; + } else { + recipe.servercmds.addAll(servercmds); } - Collections.addAll(recipe.servercmds, cmds); return this; } diff --git a/src/com/dre/brewery/utility/StringParser.java b/src/com/dre/brewery/utility/StringParser.java new file mode 100644 index 0000000..c319957 --- /dev/null +++ b/src/com/dre/brewery/utility/StringParser.java @@ -0,0 +1,58 @@ +package com.dre.brewery.utility; + +import com.dre.brewery.P; + +public interface StringParser { + + public Object parse(String line); + + public static StringParser cmdParser = new StringParser() { + @Override + public Object parse(String line) { + line = P.p.color(line); + int plus = 0; + if (line.startsWith("+++")) { + plus = 3; + line = line.substring(3); + } else if (line.startsWith("++")) { + plus = 2; + line = line.substring(2); + } else if (line.startsWith("+")) { + plus = 1; + line = line.substring(1); + } + if (line.startsWith(" ")) { + line = line.substring(1); + } + if (line.startsWith("/")) { + line = line.substring(1); + } + return new Tuple(plus, line); + } + }; + + public static StringParser loreParser = new StringParser() { + @Override + public Object parse(String line) { + line = P.p.color(line); + int plus = 0; + if (line.startsWith("+++")) { + plus = 3; + line = line.substring(3); + } else if (line.startsWith("++")) { + plus = 2; + line = line.substring(2); + } else if (line.startsWith("+")) { + plus = 1; + line = line.substring(1); + } + if (line.startsWith(" ")) { + line = line.substring(1); + } + if (!line.startsWith("§")) { + line = "§9" + line; + } + return new Tuple(plus, line); + } + }; +}