From d3da0dbe14557231ae9fdcbbc5b1d1a2143d4586 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 12:17:58 +0200 Subject: [PATCH 1/6] change loadLore() to a more generic loadQualityStringList() --- src/com/dre/brewery/recipe/BCauldronRecipe.java | 2 +- src/com/dre/brewery/recipe/BRecipe.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/com/dre/brewery/recipe/BCauldronRecipe.java b/src/com/dre/brewery/recipe/BCauldronRecipe.java index 11cefdc..b6afa15 100644 --- a/src/com/dre/brewery/recipe/BCauldronRecipe.java +++ b/src/com/dre/brewery/recipe/BCauldronRecipe.java @@ -79,7 +79,7 @@ public class BCauldronRecipe { } - List> lore = BRecipe.loadLore(cfg, id + ".lore"); + List> lore = BRecipe.loadQualityStringList(cfg, id + ".lore"); 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..6a3ab19 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -127,7 +127,7 @@ public class BRecipe { return null; } - recipe.lore = loadLore(configSectionRecipes, recipeId + ".lore"); + recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore"); recipe.servercmds = BUtil.loadCfgStringList(configSectionRecipes, recipeId + ".servercommands"); recipe.playercmds = BUtil.loadCfgStringList(configSectionRecipes, recipeId + ".playercommands"); @@ -307,11 +307,14 @@ public class BRecipe { return ingredients; } + /** + * Load a list of strings from a ConfigurationSection and parse preceded pluses. + */ @Nullable - public static List> loadLore(ConfigurationSection cfg, String path) { + public static List> loadQualityStringList(ConfigurationSection cfg, String path) { List load = BUtil.loadCfgStringList(cfg, path); if (load != null) { - List> lore = new ArrayList<>(load.size()); + List> list = new ArrayList<>(load.size()); for (String line : load) { line = P.p.color(line); int plus = 0; @@ -331,9 +334,9 @@ public class BRecipe { if (!line.startsWith("§")) { line = "§9" + line; } - lore.add(new Tuple<>(plus, line)); + list.add(new Tuple<>(plus, line)); } - return lore; + return list; } return null; } From adaeeed88cc568edfb18aa9b0cb4129d4f5b5a56 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 12:33:51 +0200 Subject: [PATCH 2/6] rework getLoreForQuality() to extract a more generic getStringForQuality() --- src/com/dre/brewery/recipe/BRecipe.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/com/dre/brewery/recipe/BRecipe.java b/src/com/dre/brewery/recipe/BRecipe.java index 6a3ab19..7d608c1 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -631,7 +631,15 @@ public class BRecipe { @Nullable public List getLoreForQuality(int quality) { - if (lore == null) return null; + return getStringForQuality(quality, lore); + } + + /** + * 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; @@ -640,8 +648,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()); } From aab880e43722b754244226afb30e644d5a1337b2 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 15:19:26 +0200 Subject: [PATCH 3/6] 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 | 122 +++++++++++------- src/com/dre/brewery/utility/StringParser.java | 33 +++++ 2 files changed, 107 insertions(+), 48 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) { - if (recipe.playercmds == null) { - recipe.playercmds = new ArrayList<>(cmds.length); + 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 = 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) { - if (recipe.servercmds == null) { - recipe.servercmds = new ArrayList<>(cmds.length); + 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 = 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); + } + }; + } + +} From dda1c6fb0af32a6559f7a4b6c5578d12e5a202c3 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 15:48:36 +0200 Subject: [PATCH 4/6] Fix git omitting half of the changes --- src/com/dre/brewery/recipe/BRecipe.java | 45 +++---------- src/com/dre/brewery/utility/StringParser.java | 66 ++++++++++++------- 2 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/com/dre/brewery/recipe/BRecipe.java b/src/com/dre/brewery/recipe/BRecipe.java index d17639d..d76ea0b 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -128,36 +128,10 @@ public class BRecipe { return null; } - // 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 (line.startsWith(" ")) { - line = line.substring(1); - } - if (!line.startsWith("§")) { - line = "§9" + line; - } - return new Tuple<>(plus, line); - } - }; - recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", loreParser); + recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.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.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")); @@ -318,13 +292,14 @@ public class BRecipe { } /** - * Load a list of strings from a ConfigurationSection and parse preceded pluses. + * Load a list of strings from a ConfigurationSection and parse it accordingly using a parser. */ @Nullable 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()); + // create fallback parser, so passing null will convert the String to a Touple without furter processing. if (p == null){ p = new StringParser() { @Override @@ -453,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)); } } @@ -905,9 +880,8 @@ public class BRecipe { 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)); + playercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); } if (recipe.playercmds == null) { recipe.playercmds = playercmds; @@ -923,9 +897,8 @@ public class BRecipe { 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)); + servercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); } if (recipe.servercmds == null) { recipe.servercmds = servercmds; diff --git a/src/com/dre/brewery/utility/StringParser.java b/src/com/dre/brewery/utility/StringParser.java index 62f36f5..e37b285 100644 --- a/src/com/dre/brewery/utility/StringParser.java +++ b/src/com/dre/brewery/utility/StringParser.java @@ -6,28 +6,50 @@ 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); + 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); + } + 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); + } + }; } From 51cd582045069fa8d32ca42f9c3511e376ca68b2 Mon Sep 17 00:00:00 2001 From: Sebi Date: Fri, 8 May 2020 16:21:16 +0200 Subject: [PATCH 5/6] Fix for commands having a space after '+' like ' - ++ weather' --- src/com/dre/brewery/utility/StringParser.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/com/dre/brewery/utility/StringParser.java b/src/com/dre/brewery/utility/StringParser.java index e37b285..c319957 100644 --- a/src/com/dre/brewery/utility/StringParser.java +++ b/src/com/dre/brewery/utility/StringParser.java @@ -21,6 +21,9 @@ public interface StringParser { plus = 1; line = line.substring(1); } + if (line.startsWith(" ")) { + line = line.substring(1); + } if (line.startsWith("/")) { line = line.substring(1); } From 54a939edad21f3c0f2de495439c6b393e263cd1e Mon Sep 17 00:00:00 2001 From: Sebi Date: Sat, 9 May 2020 11:27:50 +0200 Subject: [PATCH 6/6] Fix missing param in BCauldronRecipe.java --- src/com/dre/brewery/recipe/BCauldronRecipe.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/dre/brewery/recipe/BCauldronRecipe.java b/src/com/dre/brewery/recipe/BCauldronRecipe.java index b6afa15..e1c8888 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; @@ -79,7 +80,7 @@ public class BCauldronRecipe { } - List> lore = BRecipe.loadQualityStringList(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()); }