From af68dbcf8baaf02c7dd7a0c063369bac67b5d85d Mon Sep 17 00:00:00 2001 From: extendedclip Date: Fri, 23 Mar 2018 17:03:56 -0400 Subject: [PATCH] Little refactoring... --- .../clip/placeholderapi/PlaceholderAPI.java | 101 +++++++++--------- .../commands/PlaceholderAPICommands.java | 67 ++++++------ .../bukkit/ExpansionCloudCommands.java | 46 ++++---- .../spigot/ExpansionCloudCommands.java | 39 ++++--- .../java/me/clip/placeholderapi/util/Msg.java | 9 +- 5 files changed, 130 insertions(+), 132 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java index e759270..8088871 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java @@ -142,42 +142,9 @@ public class PlaceholderAPI { * @return modified list with all placeholders set to the corresponding values */ public static List setBracketPlaceholders(Player p, List text) { - if (text == null) return null; - List temp = new ArrayList<>(); - text.forEach(line -> { - temp.add(setBracketPlaceholders(p, line)); - }); - return temp; + return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN); } - - /** - * set placeholders in the text specified - * placeholders are matched with the pattern {} when set with this method - * @param player Player to parse the placeholders for - * @param text text to parse the placeholder values to - * @return modified text with all placeholders set to the corresponding values - */ - public static String setBracketPlaceholders(Player player, String text) { - if (text == null) return null; - if (placeholders.isEmpty()) return colorize(text); - Matcher placeholderMatcher = BRACKET_PLACEHOLDER_PATTERN.matcher(text); - Map hooks = getPlaceholders(); - while (placeholderMatcher.find()) { - String format = placeholderMatcher.group(1); - int index = format.indexOf("_"); - if (index == -1 || index >= format.length()) continue; - String identifier = format.substring(0, index).toLowerCase(); - String params = format.substring(index+1); - if (hooks.containsKey(identifier)) { - String value = hooks.get(identifier).onPlaceholderRequest(player, params); - if (value != null) { - text = text.replaceAll("\\{"+format+"\\}", Matcher.quoteReplacement(value)); - } - } - } - return colorize(text); - } - + /** * set placeholders in the list text provided * placeholders are matched with the pattern %(identifier)_(params)>% when set with this method @@ -186,13 +153,35 @@ public class PlaceholderAPI { * @return modified list with all placeholders set to the corresponding values */ public static List setPlaceholders(Player p, List text) { - if (text == null) return null; - List temp = new ArrayList<>(); - text.forEach(line -> { - temp.add(setPlaceholders(p, line)); - }); - return temp; + return setPlaceholders(p, text, PLACEHOLDER_PATTERN); } + + /** + * set placeholders in the list text provided + * placeholders are matched with the pattern %(identifier)_(params)>% when set with this method + * @param p Player to parse the placeholders for + * @param text text to parse the placeholder values in + * @return modified list with all placeholders set to the corresponding values + */ + public static List setPlaceholders(Player p, List text, Pattern pattern) { + if (text == null) return null; + List temp = new ArrayList<>(); + text.forEach(line -> { + temp.add(setPlaceholders(p, line, pattern)); + }); + return temp; + } + + /** + * set placeholders in the text specified + * placeholders are matched with the pattern {} when set with this method + * @param player Player to parse the placeholders for + * @param text text to parse the placeholder values to + * @return modified text with all placeholders set to the corresponding values + */ + public static String setBracketPlaceholders(Player player, String text) { + return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN); + } /** * set placeholders in the text specified @@ -202,22 +191,34 @@ public class PlaceholderAPI { * @return text with all placeholders set to the corresponding values */ public static String setPlaceholders(Player player, String text) { + return setPlaceholders(player, text, PLACEHOLDER_PATTERN); + } + + /** + * set placeholders in the text specified + * placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method + * @param player Player to parse the placeholders for + * @param text text to parse the placeholder values to + * @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an underscore separating the identifier from the params + * @return text with all placeholders set to the corresponding values + */ + public static String setPlaceholders(Player player, String text, Pattern placeholderPattern) { if (text == null) return null; if (placeholders.isEmpty()) return colorize(text); - Matcher m = PLACEHOLDER_PATTERN.matcher(text); + Matcher m = placeholderPattern.matcher(text); Map hooks = getPlaceholders(); while (m.find()) { String format = m.group(1); int index = format.indexOf("_"); - if (index <= 0 || index >= format.length()) continue; - String identifier = format.substring(0, index).toLowerCase(); - String params = format.substring(index+1); - if (hooks.containsKey(identifier)) { - String value = hooks.get(identifier).onPlaceholderRequest(player, params); + if (index <= 0 || index >= format.length()) continue; + String identifier = format.substring(0, index).toLowerCase(); + String params = format.substring(index+1); + if (hooks.containsKey(identifier)) { + String value = hooks.get(identifier).onPlaceholderRequest(player, params); if (value != null) { - text = text.replace("%"+format+"%", Matcher.quoteReplacement(value)); + text = text.replaceAll(m.group(), Matcher.quoteReplacement(value)); } - } + } } return ChatColor.translateAlternateColorCodes('&', text); } @@ -269,7 +270,7 @@ public class PlaceholderAPI { String value = rel.onPlaceholderRequest(one, two, params); if (value != null) { - text = text.replace("%rel_"+format+"%", Matcher.quoteReplacement(value)); + text = text.replaceAll(m.group(), Matcher.quoteReplacement(value)); } } } diff --git a/src/main/java/me/clip/placeholderapi/commands/PlaceholderAPICommands.java b/src/main/java/me/clip/placeholderapi/commands/PlaceholderAPICommands.java index ac297cd..8b4cf6d 100644 --- a/src/main/java/me/clip/placeholderapi/commands/PlaceholderAPICommands.java +++ b/src/main/java/me/clip/placeholderapi/commands/PlaceholderAPICommands.java @@ -53,46 +53,44 @@ public class PlaceholderAPICommands implements CommandExecutor { public boolean onCommand(CommandSender s, Command c, String label, String[] args) { if (args.length == 0) { - Msg.msg(s, "PlaceholderAPI &7version &b&o"+plugin.getDescription().getVersion()); - Msg.msg(s, "&fCreated by&7: &bextended_clip"); + Msg.msg(s, "PlaceholderAPI &7version &b&o" + plugin.getDescription().getVersion(), + "&fCreated by&7: &bextended_clip"); return true; } else { if (args[0].equalsIgnoreCase("help")) { - Msg.msg(s, "PlaceholderAPI &aHelp &e(&f" + plugin.getDescription().getVersion() + "&e)"); - Msg.msg(s, "&b/papi"); - Msg.msg(s, "&fView plugin info/version info"); - Msg.msg(s, "&b/papi list"); - Msg.msg(s, "&fList all placeholder expansions that are currently active"); - Msg.msg(s, "&b/papi info "); - Msg.msg(s, "&fView information for a specific expansion"); - Msg.msg(s, "&b/papi parse <...args>"); - Msg.msg(s, "&fParse a String with placeholders"); - Msg.msg(s, "&b/papi parserel <...args>"); - Msg.msg(s, "&fParse a String with relational placeholders"); - Msg.msg(s, "&b/papi reload"); - Msg.msg(s, "&fReload the config settings"); - - boolean enabled = plugin.getExpansionCloud() != null; + Msg.msg(s, "PlaceholderAPI &aHelp &e(&f" + plugin.getDescription().getVersion() + "&e)", + "&b/papi", + "&fView plugin info/version info", + "&b/papi list", + "&fList all placeholder expansions that are currently active", + "&b/papi info ", + "&fView information for a specific expansion", + "&b/papi parse <...args>", + "&fParse a String with placeholders", + "&b/papi parserel <...args>", + "&fParse a String with relational placeholders", + "&b/papi reload", + "&fReload the config settings"); if (s.isOp()) { - if (!enabled) { - Msg.msg(s, "&b/papi enablecloud"); - Msg.msg(s, "&fEnable the expansion cloud"); + if (plugin.getExpansionCloud() == null) { + Msg.msg(s, "&b/papi enablecloud", + "&fEnable the expansion cloud"); } else { - Msg.msg(s, "&b/papi disablecloud"); - Msg.msg(s, "&fDisable the expansion cloud"); - Msg.msg(s, "&b/papi ecloud"); - Msg.msg(s, "&fView information about the PlaceholderAPI expansion cloud"); - Msg.msg(s, "&b/papi ecloud status"); - Msg.msg(s, "&fView status of the PlaceholderAPI expansion cloud"); - Msg.msg(s, "&b/papi ecloud list "); - Msg.msg(s, "&fList all available expansions"); - Msg.msg(s, "&b/papi ecloud info "); - Msg.msg(s, "&fView information about a specific expansion on the cloud"); - Msg.msg(s, "&b/papi ecloud download "); - Msg.msg(s, "&fDownload a specific expansion from the cloud"); + Msg.msg(s, "&b/papi disablecloud", + "&fDisable the expansion cloud", + "&b/papi ecloud", + "&fView information about the PlaceholderAPI expansion cloud", + "&b/papi ecloud status", + "&fView status of the PlaceholderAPI expansion cloud", + "&b/papi ecloud list ", + "&fList all available expansions", + "&b/papi ecloud info ", + "&fView information about a specific expansion on the cloud", + "&b/papi ecloud download ", + "&fDownload a specific expansion from the cloud"); } } @@ -112,7 +110,6 @@ public class PlaceholderAPICommands implements CommandExecutor { return eCloud.onCommand(s, c, label, args); } else if (args[0].equalsIgnoreCase("enablecloud")) { - if (!s.isOp()) { Msg.msg(s, "&cYou don't have permission to do that!"); return true; @@ -182,7 +179,7 @@ public class PlaceholderAPICommands implements CommandExecutor { if (ex.getPlugin() != null) { Msg.msg(s, "&7Requires plugin: &f" + ex.getPlugin()); } - + if (ex.getPlaceholders() != null) { Msg.msg(s, "&8&m-- &r&7Placeholders &8&m--"); for (String placeholder : ex.getPlaceholders()) { @@ -274,7 +271,7 @@ public class PlaceholderAPICommands implements CommandExecutor { Msg.msg(s, "&cIncorrect usage! &7/papi help"); } } - + return true; } diff --git a/src/main/java/me/clip/placeholderapi/commands/bukkit/ExpansionCloudCommands.java b/src/main/java/me/clip/placeholderapi/commands/bukkit/ExpansionCloudCommands.java index 89ba2c3..405dbd9 100644 --- a/src/main/java/me/clip/placeholderapi/commands/bukkit/ExpansionCloudCommands.java +++ b/src/main/java/me/clip/placeholderapi/commands/bukkit/ExpansionCloudCommands.java @@ -45,20 +45,20 @@ public class ExpansionCloudCommands implements CommandExecutor { public boolean onCommand(CommandSender s, Command c, String label, String[] args) { if (args.length == 1) { - Msg.msg(s, "&bExpansion cloud commands"); - Msg.msg(s, " "); - Msg.msg(s, "&b/papi ecloud status"); - Msg.msg(s, "&fView status of the cloud"); - Msg.msg(s, "&b/papi ecloud list (page)"); - Msg.msg(s, "&fList all/author specific available expansions"); - Msg.msg(s, "&b/papi ecloud info "); - Msg.msg(s, "&fView information about a specific expansion available on the cloud"); - Msg.msg(s, "&b/papi ecloud download "); - Msg.msg(s, "&fDownload a specific expansion from the cloud"); - Msg.msg(s, "&b/papi ecloud refresh"); - Msg.msg(s, "&fFetch the most up to date list of expansions available."); - Msg.msg(s, "&b/papi ecloud clear"); - Msg.msg(s, "&fClear the expansion cloud cache."); + Msg.msg(s, "&bExpansion cloud commands", + " ", + "&b/papi ecloud status", + "&fView status of the cloud", + "&b/papi ecloud list (page)", + "&fList all/author specific available expansions", + "&b/papi ecloud info ", + "&fView information about a specific expansion available on the cloud", + "&b/papi ecloud download ", + "&fDownload a specific expansion from the cloud", + "&b/papi ecloud refresh", + "&fFetch the most up to date list of expansions available.", + "&b/papi ecloud clear", + "&fClear the expansion cloud cache."); return true; } @@ -111,10 +111,8 @@ public class ExpansionCloudCommands implements CommandExecutor { if (args[1].equalsIgnoreCase("status")) { - Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() - + " &bcloud expansions available to download on demand."); - Msg.msg(s, "&bA total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() - + " &bauthors have contributed to the expansion cloud."); + Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bcloud expansions available to download", + "&bA total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() + " &bauthors have contributed."); return true; } else if (args[1].equalsIgnoreCase("info")) { @@ -143,12 +141,12 @@ public class ExpansionCloudCommands implements CommandExecutor { Msg.msg(s, "&aExpansion: &f" + expansion.getName()); if (enabled) { - Msg.msg(s, "&aThis expansion is currently enabled!"); - Msg.msg(s, "&bYour version&7: &f" + version); + Msg.msg(s, "&aThis expansion is currently enabled!", + "&bYour version&7: &f" + version); } - Msg.msg(s, "&bCloud version&7: &f" + expansion.getVersion()); - Msg.msg(s, "&bAuthor&7: &f" + expansion.getAuthor()); + Msg.msg(s, "&bCloud version&7: &f" + expansion.getVersion(), + "&bAuthor&7: &f" + expansion.getAuthor()); String desc = expansion.getVersion(); @@ -223,8 +221,8 @@ public class ExpansionCloudCommands implements CommandExecutor { for (Entry expansion : ex.entrySet()) { Msg.msg(s, "&b" + (expansion.getKey()+1) + "&7: &f" + expansion.getValue().getName() + " &8&m-- &r" + expansion.getValue().getLink()); } - Msg.msg(s, "&bDownload an expansion with &7/papi ecloud download "); - Msg.msg(s, "&bView more info on an expansion with &7/papi ecloud info "); + Msg.msg(s, "&bDownload an expansion with &7/papi ecloud download ", + "&bView more info on an expansion with &7/papi ecloud info "); return true; } diff --git a/src/main/java/me/clip/placeholderapi/commands/spigot/ExpansionCloudCommands.java b/src/main/java/me/clip/placeholderapi/commands/spigot/ExpansionCloudCommands.java index 766d70c..9c992a6 100644 --- a/src/main/java/me/clip/placeholderapi/commands/spigot/ExpansionCloudCommands.java +++ b/src/main/java/me/clip/placeholderapi/commands/spigot/ExpansionCloudCommands.java @@ -50,20 +50,20 @@ public class ExpansionCloudCommands implements CommandExecutor { public boolean onCommand(CommandSender s, Command c, String label, String[] args) { if (args.length == 1) { - Msg.msg(s, "&bExpansion cloud commands"); - Msg.msg(s, " "); - Msg.msg(s, "&b/papi ecloud status"); - Msg.msg(s, "&fView status of the cloud"); - Msg.msg(s, "&b/papi ecloud list (page)"); - Msg.msg(s, "&fList all/author specific available expansions"); - Msg.msg(s, "&b/papi ecloud info "); - Msg.msg(s, "&fView information about a specific expansion available on the cloud"); - Msg.msg(s, "&b/papi ecloud download "); - Msg.msg(s, "&fDownload a specific expansion from the cloud"); - Msg.msg(s, "&b/papi ecloud refresh"); - Msg.msg(s, "&fFetch the most up to date list of expansions available."); - Msg.msg(s, "&b/papi ecloud clear"); - Msg.msg(s, "&fClear the expansion cloud cache."); + Msg.msg(s, "&bExpansion cloud commands", + " ", + "&b/papi ecloud status", + "&fView status of the cloud", + "&b/papi ecloud list (page)", + "&fList all/author specific available expansions", + "&b/papi ecloud info ", + "&fView information about a specific expansion available on the cloud", + "&b/papi ecloud download ", + "&fDownload a specific expansion from the cloud", + "&b/papi ecloud refresh", + "&fFetch the most up to date list of expansions available.", + "&b/papi ecloud clear", + "&fClear the expansion cloud cache."); return true; } @@ -87,9 +87,8 @@ public class ExpansionCloudCommands implements CommandExecutor { if (args[1].equalsIgnoreCase("status")) { - Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bexpansions available on the cloud."); - Msg.msg(s, "&7A total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() - + " &7authors have contributed to the expansion cloud."); + Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bexpansions available on the cloud.", + "&7A total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() + " &7authors have contributed to the expansion cloud."); if (plugin.getExpansionCloud().getToUpdateCount() > 0) { Msg.msg(s, "&eYou have &f" + plugin.getExpansionCloud().getToUpdateCount() + " &eexpansions installed that have updates available."); @@ -306,13 +305,13 @@ public class ExpansionCloudCommands implements CommandExecutor { return true; } - private void sms(Player p, String text, String hover, String link) { + private void sms(Player p, String text, String hover, String name) { TextComponent message = new TextComponent( ChatColor.translateAlternateColorCodes('&', text) ); if (hover != null) { message.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', hover)).create() ) ); } - if (link != null) { - message.setClickEvent( new ClickEvent( ClickEvent.Action.SUGGEST_COMMAND, "/papi ecloud download " + link) ); + if (name != null) { + message.setClickEvent( new ClickEvent( ClickEvent.Action.SUGGEST_COMMAND, "/papi ecloud download " + name) ); } p.spigot().sendMessage( message ); } diff --git a/src/main/java/me/clip/placeholderapi/util/Msg.java b/src/main/java/me/clip/placeholderapi/util/Msg.java index 1afee16..97d87e3 100644 --- a/src/main/java/me/clip/placeholderapi/util/Msg.java +++ b/src/main/java/me/clip/placeholderapi/util/Msg.java @@ -23,10 +23,13 @@ package me.clip.placeholderapi.util; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.Arrays; + public class Msg { - public static void msg(CommandSender s, String msg) { - s.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + public static void msg(CommandSender s, String... msg) { + Arrays.stream(msg).forEach(text -> + s.sendMessage(ChatColor.translateAlternateColorCodes('&', text))); } - + }