From aab880e43722b754244226afb30e644d5a1337b2 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 15:19:26 +0200 Subject: [PATCH] further abstraction and implementing quality on player and server commands Add StringParser to parse a String to other formats on the fly or on a predefined base. Rework Builder methods for server and player commands to allow quality. Add getServercmdsForQuality() and getPlayercmdsForQuality(). Rework loadQualityStringList() to accept the new StringParser. Add cmdParser and loreParser to the StringParser. --- src/com/dre/brewery/recipe/BRecipe.java | 118 +++++++++++------- src/com/dre/brewery/utility/StringParser.java | 33 +++++ 2 files changed, 105 insertions(+), 46 deletions(-) create mode 100644 src/com/dre/brewery/utility/StringParser.java diff --git a/src/com/dre/brewery/recipe/BRecipe.java b/src/com/dre/brewery/recipe/BRecipe.java index 7d608c1..d17639d 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,36 @@ public class BRecipe { return null; } - recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore"); - - 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)); + // parsers can be created inline: + 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 (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)); + if (line.startsWith(" ")) { + line = line.substring(1); } + if (!line.startsWith("§")) { + line = "§9" + line; + } + return new Tuple<>(plus, line); } - } + }; + recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", loreParser); + + // or parsers can be predefined in the interface itself: + 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")); @@ -311,30 +321,20 @@ public class BRecipe { * Load a list of strings from a ConfigurationSection and parse preceded pluses. */ @Nullable - public static List> loadQualityStringList(ConfigurationSection cfg, String path) { + public static List> loadQualityStringList(ConfigurationSection cfg, String path, StringParser p) { List load = BUtil.loadCfgStringList(cfg, path); if (load != null) { List> list = new ArrayList<>(load.size()); + if (p == null){ + p = new StringParser() { + @Override + public Object parse(String line) { + return new Tuple(0, line); + } + }; + } 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; - } - list.add(new Tuple<>(plus, line)); + list.add((Tuple) p.parse(line)); } return list; } @@ -634,6 +634,16 @@ public class BRecipe { 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 */ @@ -664,11 +674,13 @@ public class BRecipe { return cmData; } - public List getPlayercmds() { + @Nullable + public List> getPlayercmds() { return playercmds; } - public List getServercmds() { + @Nullable + public List> getServercmds() { return servercmds; } @@ -891,10 +903,17 @@ public class BRecipe { * Add Commands that are executed by the player on drinking */ public Builder addPlayerCmds(String... cmds) { + ArrayList> playercmds = new ArrayList>(cmds.length); + + StringParser p = StringParser.cmdParser(); + for (String cmd : cmds) { + playercmds.add((Tuple) p.parse(cmd)); + } if (recipe.playercmds == null) { - recipe.playercmds = new ArrayList<>(cmds.length); + recipe.playercmds = playercmds; + } else { + recipe.playercmds.addAll(playercmds); } - Collections.addAll(recipe.playercmds, cmds); return this; } @@ -902,10 +921,17 @@ public class BRecipe { * Add Commands that are executed by the server on drinking */ public Builder addServerCmds(String... cmds) { + ArrayList> servercmds = new ArrayList>(cmds.length); + + StringParser p = StringParser.cmdParser(); + for (String cmd : cmds) { + servercmds.add((Tuple) p.parse(cmd)); + } if (recipe.servercmds == null) { - recipe.servercmds = new ArrayList<>(cmds.length); + 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..62f36f5 --- /dev/null +++ b/src/com/dre/brewery/utility/StringParser.java @@ -0,0 +1,33 @@ +package com.dre.brewery.utility; + +import com.dre.brewery.P; + +public interface StringParser { + + public Object parse(String line); + + public static StringParser cmdParser() { + return 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); + } + return new Tuple<>(plus, line); + } + }; + } + +}