diff --git a/src/com/dre/brewery/listeners/WorldListener.java b/src/com/dre/brewery/listeners/WorldListener.java index 15f669e..e9988ec 100644 --- a/src/com/dre/brewery/listeners/WorldListener.java +++ b/src/com/dre/brewery/listeners/WorldListener.java @@ -47,7 +47,6 @@ public class WorldListener implements Listener { public void onWorldUnload(WorldUnloadEvent event) { String worldName = event.getWorld().getName(); if (Barrel.hasDataInWorld(worldName) || BCauldron.hasDataInWorld(worldName)) { - P.p.log("Saving due to data in unloading world"); DataSave.save(true); Barrel.onUnload(worldName); BCauldron.onUnload(worldName); diff --git a/src/com/dre/brewery/recipe/BCauldronRecipe.java b/src/com/dre/brewery/recipe/BCauldronRecipe.java index ffa9b2e..52a8994 100644 --- a/src/com/dre/brewery/recipe/BCauldronRecipe.java +++ b/src/com/dre/brewery/recipe/BCauldronRecipe.java @@ -103,7 +103,7 @@ public class BCauldronRecipe { } - List> lore = BRecipe.loadQualityStringList(cfg, id + ".lore", StringParser.loreParser); + List> lore = BRecipe.loadQualityStringList(cfg, id + ".lore", StringParser.ParseType.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 d76ea0b..d0e151e 100644 --- a/src/com/dre/brewery/recipe/BRecipe.java +++ b/src/com/dre/brewery/recipe/BRecipe.java @@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.ListIterator; +import java.util.stream.Collectors; /** * A Recipe used to Brew a Brewery Potion. @@ -128,10 +128,10 @@ public class BRecipe { return null; } - recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.loreParser); + recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.ParseType.LORE); - recipe.servercmds = loadQualityStringList(configSectionRecipes, recipeId + ".servercommands", StringParser.cmdParser); - recipe.playercmds = loadQualityStringList(configSectionRecipes, recipeId + ".playercommands", StringParser.cmdParser); + recipe.servercmds = loadQualityStringList(configSectionRecipes, recipeId + ".servercommands", StringParser.ParseType.CMD); + recipe.playercmds = loadQualityStringList(configSectionRecipes, recipeId + ".playercommands", StringParser.ParseType.CMD); recipe.drinkMsg = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinkmessage")); recipe.drinkTitle = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinktitle")); @@ -292,26 +292,13 @@ public class BRecipe { } /** - * Load a list of strings from a ConfigurationSection and parse it accordingly using a parser. + * Load a list of strings from a ConfigurationSection and parse the quality */ @Nullable - public static List> loadQualityStringList(ConfigurationSection cfg, String path, StringParser p) { + public static List> loadQualityStringList(ConfigurationSection cfg, String path, StringParser.ParseType parseType) { 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 - public Object parse(String line) { - return new Tuple(0, line); - } - }; - } - for (String line : load) { - list.add((Tuple) p.parse(line)); - } - return list; + return load.stream().map(line -> StringParser.parseQuality(line, parseType)).collect(Collectors.toList()); } return null; } @@ -606,24 +593,24 @@ public class BRecipe { @Nullable public List getLoreForQuality(int quality) { - return getStringForQuality(quality, lore); + return getStringsForQuality(quality, lore); } @Nullable public List getPlayercmdsForQuality(int quality) { - return getStringForQuality(quality, playercmds); + return getStringsForQuality(quality, playercmds); } @Nullable public List getServercmdsForQuality(int quality) { - return getStringForQuality(quality, servercmds); + return getStringsForQuality(quality, servercmds); } /** * Get a quality filtered list of supported attributes */ @Nullable - public List getStringForQuality(int quality, List> source) { + public List getStringsForQuality(int quality, List> source) { if (source == null) return null; int plus; if (quality <= 3) { @@ -881,7 +868,7 @@ public class BRecipe { ArrayList> playercmds = new ArrayList>(cmds.length); for (String cmd : cmds) { - playercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); + playercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.CMD)); } if (recipe.playercmds == null) { recipe.playercmds = playercmds; @@ -898,7 +885,7 @@ public class BRecipe { ArrayList> servercmds = new ArrayList>(cmds.length); for (String cmd : cmds) { - servercmds.add((Tuple) StringParser.cmdParser.parse(cmd)); + servercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.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 c319957..5824e7c 100644 --- a/src/com/dre/brewery/utility/StringParser.java +++ b/src/com/dre/brewery/utility/StringParser.java @@ -2,57 +2,38 @@ package com.dre.brewery.utility; import com.dre.brewery.P; -public interface StringParser { +public class 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 Tuple parseQuality(String line, ParseType type) { + 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); } - }; - - 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); + if (line.startsWith(" ")) { + line = line.substring(1); } - }; + + if (type == ParseType.CMD && line.startsWith("/")) { + line = line.substring(1); + } + + if (type == ParseType.LORE && !line.startsWith("§")) { + line = "§9" + line; + } + return new Tuple(plus, line); + } + + public enum ParseType { + LORE, + CMD, + OTHER + } }