From 090eec55099add17395c5465e07afc88b6259c77 Mon Sep 17 00:00:00 2001 From: filoghost Date: Mon, 15 Jun 2020 17:40:40 +0200 Subject: [PATCH] Refactoring --- .../chestcommands/action/DragonBarAction.java | 7 ++++--- .../chestcommands/action/PlaySoundAction.java | 9 +++++---- .../chestcommands/config/AsciiPlaceholders.java | 7 ++++--- .../chestcommands/config/ConfigUtil.java | 10 +++++----- .../chestcommands/parser/EnchantmentParser.java | 3 ++- .../chestcommands/parser/ItemMetaParser.java | 6 ++++-- .../chestcommands/parser/ItemStackParser.java | 8 +++----- .../me/filoghost/chestcommands/util/Strings.java | 16 +++++++++++++++- 8 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java index 921d047..19bfbfa 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java @@ -21,6 +21,7 @@ import me.filoghost.chestcommands.bridge.BarAPIBridge; import me.filoghost.chestcommands.parser.ParseException; import me.filoghost.chestcommands.parser.NumberParser; import me.filoghost.chestcommands.util.FormatUtils; +import me.filoghost.chestcommands.util.Strings; import me.filoghost.chestcommands.variable.RelativeString; public class DragonBarAction extends Action { @@ -32,11 +33,11 @@ public class DragonBarAction extends Action { seconds = 1; String message = serialiazedAction; - String[] split = serialiazedAction.split("\\|", 2); // Max of 2 pieces + String[] split = Strings.trimmedSplit(serialiazedAction, "\\|", 2); // Max of 2 pieces if (split.length > 1) { try { - seconds = NumberParser.getStrictlyPositiveInteger(split[0].trim()); - message = split[1].trim(); + seconds = NumberParser.getStrictlyPositiveInteger(split[0]); + message = split[1]; } catch (ParseException ex) { disable(ChatColor.RED + "Invalid dragon bar time: " + split[0]); return; diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/PlaySoundAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/PlaySoundAction.java index ed85568..bb038b1 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/PlaySoundAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/PlaySoundAction.java @@ -19,6 +19,7 @@ import org.bukkit.Sound; import org.bukkit.entity.Player; import me.filoghost.chestcommands.util.Registry; +import me.filoghost.chestcommands.util.Strings; public class PlaySoundAction extends Action { @@ -32,24 +33,24 @@ public class PlaySoundAction extends Action { pitch = 1.0f; volume = 1.0f; - String[] split = serializedAction.split(","); + String[] split = Strings.trimmedSplit(serializedAction, ",", 3); sound = SOUNDS_REGISTRY.find(split[0]); if (sound == null) { - disable(ChatColor.RED + "Invalid sound \"" + split[0].trim() + "\"."); + disable(ChatColor.RED + "Invalid sound \"" + split[0] + "\"."); return; } if (split.length > 1) { try { - pitch = Float.parseFloat(split[1].trim()); + pitch = Float.parseFloat(split[1]); } catch (NumberFormatException e) { } } if (split.length > 2) { try { - volume = Float.parseFloat(split[2].trim()); + volume = Float.parseFloat(split[2]); } catch (NumberFormatException e) { } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/config/AsciiPlaceholders.java b/Plugin/src/main/java/me/filoghost/chestcommands/config/AsciiPlaceholders.java index 45f6572..6db3dc4 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/config/AsciiPlaceholders.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/config/AsciiPlaceholders.java @@ -28,6 +28,7 @@ import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.util.ErrorCollector; import me.filoghost.chestcommands.util.FileUtils; import me.filoghost.chestcommands.util.FormatUtils; +import me.filoghost.chestcommands.util.Strings; /** * This is not a real YAML file ;) @@ -59,9 +60,9 @@ public class AsciiPlaceholders { continue; } - int indexOf = line.indexOf(':'); - String placeholder = unquote(line.substring(0, indexOf).trim()); - String replacement = FormatUtils.addColors(StringEscapeUtils.unescapeJava(unquote(line.substring(indexOf + 1, line.length()).trim()))); + String[] parts = Strings.trimmedSplit(line, ":", 2); + String placeholder = unquote(parts[0]); + String replacement = FormatUtils.addColors(StringEscapeUtils.unescapeJava(unquote(parts[1]))); if (placeholder.length() == 0 || replacement.length() == 0) { errorCollector.addError("Unable to parse a line(" + line + ") from placeholders.yml: the placeholder and the replacement must have both at least 1 character."); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/config/ConfigUtil.java b/Plugin/src/main/java/me/filoghost/chestcommands/config/ConfigUtil.java index a5c513d..436714e 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/config/ConfigUtil.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/config/ConfigUtil.java @@ -21,6 +21,8 @@ import java.util.regex.Pattern; import org.bukkit.configuration.ConfigurationSection; +import me.filoghost.chestcommands.util.Strings; + public class ConfigUtil { public static String getAnyString(ConfigurationSection config, String... paths) { @@ -73,14 +75,12 @@ public class ConfigUtil { separator = ";"; } - String[] splitValues = input.split(Pattern.quote(separator)); + String[] splitValues = Strings.trimmedSplit(input, Pattern.quote(separator)); List values = new ArrayList<>(); for (String value : splitValues) { - String trimmedValue = value.trim(); - - if (!trimmedValue.isEmpty()) { - values.add(trimmedValue); + if (!value.isEmpty()) { + values.add(value); } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/parser/EnchantmentParser.java b/Plugin/src/main/java/me/filoghost/chestcommands/parser/EnchantmentParser.java index b21e387..6c710b7 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/parser/EnchantmentParser.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/parser/EnchantmentParser.java @@ -17,6 +17,7 @@ package me.filoghost.chestcommands.parser; import org.bukkit.enchantments.Enchantment; import me.filoghost.chestcommands.util.Registry; +import me.filoghost.chestcommands.util.Strings; import me.filoghost.chestcommands.util.ErrorCollector; public class EnchantmentParser { @@ -57,7 +58,7 @@ public class EnchantmentParser { int level = 1; if (input.contains(",")) { - String[] levelSplit = input.split(","); + String[] levelSplit = Strings.trimmedSplit(input, ",", 2); try { level = Integer.parseInt(levelSplit[1].trim()); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemMetaParser.java b/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemMetaParser.java index 676ebde..c98183c 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemMetaParser.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemMetaParser.java @@ -20,6 +20,8 @@ import org.bukkit.block.banner.Pattern; import org.bukkit.block.banner.PatternType; import me.filoghost.chestcommands.util.Registry; +import me.filoghost.chestcommands.util.Strings; + import java.util.ArrayList; import java.util.List; @@ -32,7 +34,7 @@ public final class ItemMetaParser { public static Color parseColor(String input) throws ParseException { - String[] split = input.replace(" ", "").split(","); + String[] split = Strings.trimmedSplit(input, ","); if (split.length != 3) { throw new ParseException("it must be in the format \"red, green, blue\"."); @@ -67,7 +69,7 @@ public final class ItemMetaParser { public static List parseBannerPatternList(List input) throws ParseException { List patterns = new ArrayList(); for (String str : input) { - String[] split = str.split(":"); + String[] split = Strings.trimmedSplit(str, ":"); if (split.length != 2) { throw new ParseException("it must be in the format \"pattern:color\"."); } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemStackParser.java b/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemStackParser.java index d6ee3bd..2ef0734 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemStackParser.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/parser/ItemStackParser.java @@ -19,6 +19,7 @@ import org.bukkit.inventory.ItemStack; import me.filoghost.chestcommands.util.MaterialsHelper; import me.filoghost.chestcommands.util.Preconditions; +import me.filoghost.chestcommands.util.Strings; public class ItemStackParser { @@ -35,12 +36,9 @@ public class ItemStackParser { public ItemStackParser(String input, boolean parseAmount) throws ParseException { Preconditions.notNull(input, "input"); - // Remove spaces, they're not needed - input = input.replace(" ", ""); - if (parseAmount) { // Read the optional amount - String[] splitAmount = input.split(","); + String[] splitAmount = Strings.trimmedSplit(input, ",", 2); if (splitAmount.length > 1) { this.amount = NumberParser.getStrictlyPositiveInteger(splitAmount[1], "invalid amount \"" + splitAmount[1] + "\""); @@ -52,7 +50,7 @@ public class ItemStackParser { // Read the optional durability - String[] splitByColons = input.split(":"); + String[] splitByColons = Strings.trimmedSplit(input, ":", 2); if (splitByColons.length > 1) { short durability = NumberParser.getPositiveShort(splitByColons[1], "invalid durability \"" + splitByColons[1] + "\""); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/util/Strings.java b/Plugin/src/main/java/me/filoghost/chestcommands/util/Strings.java index d5815c7..3a3fdc0 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/util/Strings.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/util/Strings.java @@ -17,7 +17,21 @@ package me.filoghost.chestcommands.util; public final class Strings { private Strings() {} - + + + public static String[] trimmedSplit(String input, String splitPattern) { + return trimmedSplit(input, splitPattern, 0); + } + + + public static String[] trimmedSplit(String input, String splitPattern, int splitLimit) { + String[] output = input.split(splitPattern, splitLimit); + for (int i = 0; i < output.length; i++) { + output[i] = output[i].trim(); + } + return output; + } + public static String stripChars(String input, char... removed) { if (input == null || input.isEmpty() || removed.length == 0) {