From fd0035a99c0327ffaf0e0f25fa6475ce716478f8 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 13 Jun 2022 22:52:26 +0100 Subject: [PATCH] Build against 1.19 and replace apache-commons-lang --- pom.xml | 6 +-- .../com/Acrobot/Breeze/Utils/StringUtil.java | 44 +++++++++++++++++-- .../ChestShop/Commands/AccessToggle.java | 4 +- .../Acrobot/ChestShop/Commands/Toggle.java | 4 +- .../ChestShop/Signs/ChestShopSign.java | 13 +++--- .../Acrobot/ChestShop/UUIDs/NameManager.java | 16 ++++--- 6 files changed, 63 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 8ddbaa0..33a4e9f 100644 --- a/pom.xml +++ b/pom.xml @@ -461,14 +461,14 @@ paper-repo - https://papermc.io/repo/repository/maven-public/ + https://repo.papermc.io/repository/maven-public/ io.papermc.paper paper-api - 1.17-R0.1-SNAPSHOT + 1.19-R0.1-SNAPSHOT provided @@ -522,7 +522,7 @@ paper-repo - https://papermc.io/repo/repository/maven-public/ + https://repo.papermc.io/repository/maven-public/ diff --git a/src/main/java/com/Acrobot/Breeze/Utils/StringUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/StringUtil.java index 302a515..f2cc327 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/StringUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/StringUtil.java @@ -1,10 +1,11 @@ package com.Acrobot.Breeze.Utils; -import org.apache.commons.lang.WordUtils; import org.bukkit.ChatColor; +import java.util.Arrays; import java.util.Collection; import java.util.Locale; +import java.util.stream.Collectors; /** * @author Acrobot @@ -22,9 +23,13 @@ public class StringUtil { if (string == null || string.isEmpty()) { return string; } - char[] separators = new char[]{separator}; - return WordUtils.capitalize(string.toLowerCase(Locale.ROOT), separators).replace(String.valueOf(separator), " "); + // Split into words + String[] words = string.toLowerCase(Locale.ROOT).split(String.valueOf(separator)); + // Capitalize every word and return joined string + return Arrays.stream(words) + .map(word -> word.substring(0, 1).toUpperCase(Locale.ROOT) + word.substring(1)) + .collect(Collectors.joining(" ")); } /** @@ -116,4 +121,37 @@ public class StringUtil { } return width; } + + /** + * Strip whitespace from the front and back + * @param string The string to strip + * @return The string with all whitespace from front and back stripped; returns null if input is null + */ + public static String strip(String string) { + if (string == null) + return null; + // The result stripped string + StringBuilder stripped = new StringBuilder(); + // The current white space which only gets added once we find a non-whitespace character + StringBuilder cachedWhitespace = new StringBuilder(); + // Check each code point (not characters to support UTF16 properly) + for (int codePoint : string.codePoints().toArray()) { + // Check if it's a whitespace, so we know if we should add it + if (!Character.isWhitespace(codePoint)) { + // Check if we have cached whitespace, if so append it first and reset the cache + if (cachedWhitespace.length() > 0) { + stripped.append(cachedWhitespace); + cachedWhitespace = new StringBuilder(); + } + // Append current code point + stripped.appendCodePoint(codePoint); + } else if (stripped.length() > 0) { + // If we already have some non-whitespace content in the final stripped + // then cache the current whitespace as it wasn't at the start + cachedWhitespace.appendCodePoint(codePoint); + } // Otherwise, this was the start, and we don't need the cached whitespace + } + // Return the stripped string, without any whitespace from the end left + return stripped.toString(); + } } diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/AccessToggle.java b/src/main/java/com/Acrobot/ChestShop/Commands/AccessToggle.java index 79769ef..7718f97 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/AccessToggle.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/AccessToggle.java @@ -1,7 +1,7 @@ package com.Acrobot.ChestShop.Commands; import com.Acrobot.ChestShop.Configuration.Messages; -import org.apache.commons.lang.Validate; +import com.google.common.base.Preconditions; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -43,7 +43,7 @@ public class AccessToggle implements CommandExecutor { } public static boolean setIgnoring(Player player, boolean ignoring) { - Validate.notNull(player); // Make sure the player instance is not null, in case there are any errors in the code + Preconditions.checkNotNull(player); // Make sure the player instance is not null, in case there are any errors in the code if (ignoring) { toggledPlayers.add(player.getUniqueId()); diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java index cb50706..70d8284 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java @@ -1,7 +1,7 @@ package com.Acrobot.ChestShop.Commands; import com.Acrobot.ChestShop.Configuration.Messages; -import org.apache.commons.lang.Validate; +import com.google.common.base.Preconditions; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -61,7 +61,7 @@ public class Toggle implements CommandExecutor { } public static boolean setIgnoring(Player player, boolean ignoring) { - Validate.notNull(player); // Make sure the player instance is not null, in case there are any errors in the code + Preconditions.checkNotNull(player); // Make sure the player instance is not null, in case there are any errors in the code if (ignoring) { toggledPlayers.add(player.getUniqueId()); diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 90fe468..31a24de 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -10,7 +10,6 @@ import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; -import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.block.BlockState; @@ -183,7 +182,7 @@ public class ChestShopSign { for (int i = 0; i < 3; i++) { boolean matches = false; for (Pattern pattern : SHOP_SIGN_PATTERN[i]) { - if (pattern.matcher(StringUtils.strip(StringUtil.stripColourCodes(lines[i+1]))).matches()) { + if (pattern.matcher(StringUtil.strip(StringUtil.stripColourCodes(lines[i+1]))).matches()) { matches = true; break; } @@ -213,7 +212,7 @@ public class ChestShopSign { * @return The owner string */ public static String getOwner(String[] lines) { - return StringUtil.stripColourCodes(StringUtils.strip(StringUtil.stripColourCodes(lines[NAME_LINE]))); + return StringUtil.stripColourCodes(StringUtil.strip(StringUtil.stripColourCodes(lines[NAME_LINE]))); } /** @@ -233,7 +232,7 @@ public class ChestShopSign { * @throws IllegalArgumentException Thrown when an invalid quantity is present */ public static String getQuantityLine(String[] lines) throws IllegalArgumentException { - return lines.length > QUANTITY_LINE ? StringUtils.strip(StringUtil.stripColourCodes(lines[QUANTITY_LINE])) : ""; + return lines.length > QUANTITY_LINE ? StringUtil.strip(StringUtil.stripColourCodes(lines[QUANTITY_LINE])) : ""; } /** @@ -262,7 +261,7 @@ public class ChestShopSign { * @return The price line */ public static String getPrice(Sign sign) { - return StringUtils.strip(StringUtil.stripColourCodes(sign.getLine(PRICE_LINE))); + return StringUtil.strip(StringUtil.stripColourCodes(sign.getLine(PRICE_LINE))); } /** @@ -271,7 +270,7 @@ public class ChestShopSign { * @return The price line */ public static String getPrice(String[] lines) { - return lines.length > PRICE_LINE ? StringUtils.strip(StringUtil.stripColourCodes(lines[PRICE_LINE])) : ""; + return lines.length > PRICE_LINE ? StringUtil.strip(StringUtil.stripColourCodes(lines[PRICE_LINE])) : ""; } /** @@ -289,6 +288,6 @@ public class ChestShopSign { * @return The item line */ public static String getItem(String[] lines) { - return lines.length > ITEM_LINE ? StringUtils.strip(StringUtil.stripColourCodes(lines[ITEM_LINE])) : ""; + return lines.length > ITEM_LINE ? StringUtil.strip(StringUtil.stripColourCodes(lines[ITEM_LINE])) : ""; } } diff --git a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java index b42daf1..4295852 100644 --- a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java +++ b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java @@ -12,10 +12,10 @@ import com.Acrobot.ChestShop.Events.AccountAccessEvent; import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.google.common.base.Preconditions; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.SelectArg; -import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -63,8 +63,8 @@ public class NameManager implements Listener { * @throws IllegalArgumentException when an invalid player object was passed */ public static Account getOrCreateAccount(OfflinePlayer player) { - Validate.notNull(player.getName(), "Name of player " + player.getUniqueId() + " is null?"); - Validate.isTrue(!(player instanceof Player) || !Properties.ENSURE_CORRECT_PLAYERID || uuidVersion < 0 || player.getUniqueId().version() == uuidVersion, + Preconditions.checkNotNull(player.getName(), "Name of player " + player.getUniqueId() + " is null?"); + Preconditions.checkArgument(!(player instanceof Player) || !Properties.ENSURE_CORRECT_PLAYERID || uuidVersion < 0 || player.getUniqueId().version() == uuidVersion, "Invalid OfflinePlayer! " + player.getUniqueId() + " has version " + player.getUniqueId().version() + " and not server version " + uuidVersion + ". " + "If you believe that is an error and your setup allows such UUIDs then set the ENSURE_CORRECT_PLAYERID config option to false."); return getOrCreateAccount(player.getUniqueId(), player.getName()); @@ -79,8 +79,8 @@ public class NameManager implements Listener { * @throws IllegalArgumentException when id or name are null */ public static Account getOrCreateAccount(UUID id, String name) { - Validate.notNull(id, "UUID of player is null?"); - Validate.notNull(name, "Name of player " + id + " is null?"); + Preconditions.checkNotNull(id, "UUID of player is null?"); + Preconditions.checkNotNull(name, "Name of player " + id + " is null?"); Account account = getAccount(id); if (account == null) { @@ -124,7 +124,8 @@ public class NameManager implements Listener { * @throws IllegalArgumentException if the username is empty or null */ public static Account getAccount(String fullName) { - Validate.notEmpty(fullName, "fullName cannot be null or empty!"); + Preconditions.checkNotNull(fullName, "fullName cannot be null!"); + Preconditions.checkArgument(!fullName.isEmpty(), "fullName cannot be empty!"); try { return usernameToAccount.get(fullName, () -> { try { @@ -161,7 +162,8 @@ public class NameManager implements Listener { */ @Deprecated public static Account getAccountFromShortName(String shortName) { - Validate.notEmpty(shortName, "shortName cannot be null or empty!"); + Preconditions.checkNotNull(shortName, "shortName cannot be null!"); + Preconditions.checkArgument(!shortName.isEmpty(), "shortName cannot be empty!"); Account account = null; try {