Refactoring

This commit is contained in:
filoghost 2020-06-15 17:40:40 +02:00
parent 3aa6bcf7c6
commit 090eec5509
8 changed files with 42 additions and 24 deletions

View File

@ -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;

View File

@ -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) {
}
}

View File

@ -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.");

View File

@ -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<String> values = new ArrayList<>();
for (String value : splitValues) {
String trimmedValue = value.trim();
if (!trimmedValue.isEmpty()) {
values.add(trimmedValue);
if (!value.isEmpty()) {
values.add(value);
}
}

View File

@ -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());

View File

@ -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<Pattern> parseBannerPatternList(List<String> input) throws ParseException {
List<Pattern> patterns = new ArrayList<Pattern>();
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\".");
}

View File

@ -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] + "\"");

View File

@ -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) {