From 2c5f8389014141d49a2e22b9435a4d958d467491 Mon Sep 17 00:00:00 2001 From: WKEA <1605193340@qq.com> Date: Thu, 1 Aug 2024 12:24:35 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BD=BFwarp=E6=94=AF=E6=8C=81=E4=B8=AD?= =?UTF-8?q?=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/earth2me/essentials/utils/StringUtil.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java index d9d6602d7b1..8779c6a688c 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java @@ -10,10 +10,11 @@ import java.util.regex.Pattern; public final class StringUtil { - private static final Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9-]"); + private static final Pattern INVALIDFILECHARS = Pattern.compile("[^\\p{L}\\p{N}\\.\\-\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\p{IsHangul}]"); private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); @SuppressWarnings("CheckStyle") - private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]"); + private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"); + private StringUtil() { } From a0318caa6c9387ea6d9ed5656c36ba6b3f4ac188 Mon Sep 17 00:00:00 2001 From: WKEA <1605193340@qq.com> Date: Thu, 1 Aug 2024 14:19:09 +0800 Subject: [PATCH 2/5] Fix support for Chinese and other characters in setwarp and sethome filenames; ensure backward compatibility with old safe string names replaced by underscores his PR addresses the issue where and commands in the Essentials plugin do not support Chinese and other non-alphanumeric characters in filenames. The following changes have been made to ensure broader character support and maintain backward compatibility:setwarpsethome Support for Chinese and other characters: Updated the regular expression patterns to allow Chinese characters and other non-alphanumeric characters in and filenames.setwarpsethome Backward compatibility with old safe string names: When a player uses the command, the system will first attempt to match the provided with the new format./home homename --- .../src/main/java/com/earth2me/essentials/Worth.java | 1 + .../com/earth2me/essentials/commands/Commandhome.java | 9 ++++++++- .../java/com/earth2me/essentials/utils/StringUtil.java | 9 +++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/Worth.java b/Essentials/src/main/java/com/earth2me/essentials/Worth.java index d02d1853ca1..8e6c1505efe 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Worth.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Worth.java @@ -32,6 +32,7 @@ public Worth(final File dataFolder) { public BigDecimal getPrice(final IEssentials ess, final ItemStack itemStack) { BigDecimal result = BigDecimal.ONE.negate(); + final String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); if (VersionUtil.PRE_FLATTENING) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java index a8017dcbf8a..d5ed31465e5 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java @@ -8,6 +8,7 @@ import io.papermc.lib.PaperLib; import net.ess3.api.TranslatableException; import net.ess3.api.events.UserTeleportHomeEvent; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -15,8 +16,10 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.Optional; import java.util.concurrent.CompletableFuture; + public class Commandhome extends EssentialsCommand { public Commandhome() { super("home"); @@ -124,7 +127,10 @@ private void goHome(final User user, final User player, final String home, final if (home.length() < 1) { throw new NotEnoughArgumentsException(); } - final Location loc = player.getHome(home); + + final Location loc = Optional.ofNullable(player.getHome(home)) + .orElse(player.getHome(StringUtil.old_safeString(home))); + if (loc == null) { throw new NotEnoughArgumentsException(); } @@ -138,6 +144,7 @@ private void goHome(final User user, final User player, final String home, final future.thenAccept(success -> { if (success) { user.sendTl("teleportHome", home); + Bukkit.getLogger().info(home); } }); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java index 8779c6a688c..3cc665db705 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java @@ -10,7 +10,7 @@ import java.util.regex.Pattern; public final class StringUtil { - private static final Pattern INVALIDFILECHARS = Pattern.compile("[^\\p{L}\\p{N}\\.\\-\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\p{IsHangul}]"); + private static final Pattern INVALIDFILECHARS = Pattern.compile("[/\\\\:*?\"<>|\\x00-\\x1F]"); private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); @SuppressWarnings("CheckStyle") private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"); @@ -26,12 +26,17 @@ public static String sanitizeFileName(final String name) { //Used to clean strings/names before saving as filenames/permissions public static String safeString(final String string) { + if (string == null) { + return null; + } + return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + } + public static String old_safeString(final String string) { if (string == null) { return null; } return STRICTINVALIDCHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } - //Less restrictive string sanitizing, when not used as perm or filename public static String sanitizeString(final String string) { return INVALIDCHARS.matcher(string).replaceAll(""); From 2a3c92216aee6a388b8fe55198b635492fd18c5f Mon Sep 17 00:00:00 2001 From: WKEA <1605193340@qq.com> Date: Thu, 1 Aug 2024 14:50:31 +0800 Subject: [PATCH 3/5] This PR addresses the issue where and commands in the Essentials plugin do not support Chinese and other non-alphanumeric characters in filenames. The following changes have been made to ensure broader character support and maintain backward compatibility:setwarpsethome MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support for Chinese and other characters: Updated the regular expression patterns to allow Chinese characters and other non-alphanumeric characters in and filenames.setwarpsethome Backward compatibility with old safe string names: When a player uses the command, the system will first attempt to match the provided with the new format./home homename If no match is found, it will then attempt to match the old format where special characters were replaced by underscores, ensuring compatibility with existing configurations. These changes allow players to use a wider range of characters in their home and warp names while preserving the functionality for names created with the previous format. Example Usage New behavior: Players can now set home names like ./sethome 我的房子 Backward compatibility: If a player previously had a home named (converted from ), using will still work ./home 我的菜地 (/home ____) --- .../java/com/earth2me/essentials/commands/Commandhome.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java index d5ed31465e5..0a66876d38e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java @@ -8,7 +8,7 @@ import io.papermc.lib.PaperLib; import net.ess3.api.TranslatableException; import net.ess3.api.events.UserTeleportHomeEvent; -import org.bukkit.Bukkit; + import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -144,7 +144,6 @@ private void goHome(final User user, final User player, final String home, final future.thenAccept(success -> { if (success) { user.sendTl("teleportHome", home); - Bukkit.getLogger().info(home); } }); } From 9ce73218225dc397d13bd85403ab2fda6c2810cb Mon Sep 17 00:00:00 2001 From: WKEA <1605193340@qq.com> Date: Thu, 1 Aug 2024 14:50:31 +0800 Subject: [PATCH 4/5] This PR addresses the issue where and commands in the Essentials plugin do not support Chinese and other non-alphanumeric characters in filenames. The following changes have been made to ensure broader character support and maintain backward compatibility:setwarpsethome MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support for Chinese and other characters: Updated the regular expression patterns to allow Chinese characters and other non-alphanumeric characters in and filenames.setwarpsethome Backward compatibility with old safe string names: When a player uses the command, the system will first attempt to match the provided with the new format./home homename If no match is found, it will then attempt to match the old format where special characters were replaced by underscores, ensuring compatibility with existing configurations. These changes allow players to use a wider range of characters in their home and warp names while preserving the functionality for names created with the previous format. Example Usage New behavior: Players can now set home names like ./sethome 我的房子 Backward compatibility: If a player previously had a home named (converted from ), using will still work ./home 我的菜地 (/home ____) --- .../com/earth2me/essentials/commands/Commandhome.java | 3 +-- .../com/earth2me/essentials/utils/StringUtil.java | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java index d5ed31465e5..0a66876d38e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java @@ -8,7 +8,7 @@ import io.papermc.lib.PaperLib; import net.ess3.api.TranslatableException; import net.ess3.api.events.UserTeleportHomeEvent; -import org.bukkit.Bukkit; + import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -144,7 +144,6 @@ private void goHome(final User user, final User player, final String home, final future.thenAccept(success -> { if (success) { user.sendTl("teleportHome", home); - Bukkit.getLogger().info(home); } }); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java index 3cc665db705..72617d51306 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java @@ -10,8 +10,9 @@ import java.util.regex.Pattern; public final class StringUtil { - private static final Pattern INVALIDFILECHARS = Pattern.compile("[/\\\\:*?\"<>|\\x00-\\x1F]"); - private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); + private static final Pattern INVALIDCHARACTER = Pattern.compile("[/\\\\:*?\"<>|\\x00-\\x1F]"); + private static final Pattern OLD_INVALIDCHARACTER = Pattern.compile("[^a-z0-9-]"); + //private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); @SuppressWarnings("CheckStyle") private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"); @@ -21,7 +22,7 @@ private StringUtil() { //Used to clean file names before saving to disk public static String sanitizeFileName(final String name) { - return INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + return INVALIDCHARACTER.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } //Used to clean strings/names before saving as filenames/permissions @@ -29,13 +30,13 @@ public static String safeString(final String string) { if (string == null) { return null; } - return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + return INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } public static String old_safeString(final String string) { if (string == null) { return null; } - return STRICTINVALIDCHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + return OLD_INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } //Less restrictive string sanitizing, when not used as perm or filename public static String sanitizeString(final String string) { From ef5de49271d4ef63652ab91beeb88d8d97eb35d9 Mon Sep 17 00:00:00 2001 From: WKEA <1605193340@qq.com> Date: Thu, 1 Aug 2024 16:55:57 +0800 Subject: [PATCH 5/5] fix sethome delhome renamehome Avoid causing potential bugs --- .../com/earth2me/essentials/UserData.java | 26 ++++++++++++------- .../essentials/commands/Commandhome.java | 5 +--- .../earth2me/essentials/utils/StringUtil.java | 10 +++---- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/UserData.java b/Essentials/src/main/java/com/earth2me/essentials/UserData.java index ae71b0cabb1..47ea441f9b7 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/UserData.java +++ b/Essentials/src/main/java/com/earth2me/essentials/UserData.java @@ -143,10 +143,12 @@ private String getHomeName(String search) { } return search; } - + //Optimize character compatibility public Location getHome(final String name) { - final String search = getHomeName(name); - final LazyLocation loc = holder.homes().get(search); + LazyLocation loc = holder.homes().get(getHomeName(name)); + if (loc == null) { + loc = holder.homes().get(getHomeName(StringUtil.safeString(name))); + } return loc != null ? loc.location() : null; } @@ -179,28 +181,32 @@ public List getHomes() { public void setHome(String name, final Location loc) { //Invalid names will corrupt the yaml - name = StringUtil.safeString(name); + name = StringUtil.new_safeString(name); holder.homes().put(name, LazyLocation.fromLocation(loc)); config.save(); } public void delHome(final String name) throws Exception { String search = getHomeName(name); - if (!holder.homes().containsKey(search)) { - search = StringUtil.safeString(search); - } - if (holder.homes().containsKey(search)) { - holder.homes().remove(search); + String safeSearch = StringUtil.new_safeString(search); + String oldSafeSearch = StringUtil.safeString(search); + if (holder.homes().containsKey(safeSearch)) { + holder.homes().remove(safeSearch); + config.save(); + } else if (holder.homes().containsKey(oldSafeSearch)) { + holder.homes().remove(oldSafeSearch); config.save(); } else { throw new TranslatableException("invalidHome", search); } } + public void renameHome(final String name, final String newName) throws Exception { final LazyLocation location = holder.homes().remove(name); + //If possible, it is necessary to improve the compatibility here to make it support underscores if (location != null) { - holder.homes().put(StringUtil.safeString(newName), location); + holder.homes().put(StringUtil.new_safeString(newName), location); config.save(); } else { throw new TranslatableException("invalidHome", name); diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java index 0a66876d38e..078ba2f8316 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhome.java @@ -8,7 +8,6 @@ import io.papermc.lib.PaperLib; import net.ess3.api.TranslatableException; import net.ess3.api.events.UserTeleportHomeEvent; - import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -19,7 +18,6 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; - public class Commandhome extends EssentialsCommand { public Commandhome() { super("home"); @@ -128,8 +126,7 @@ private void goHome(final User user, final User player, final String home, final throw new NotEnoughArgumentsException(); } - final Location loc = Optional.ofNullable(player.getHome(home)) - .orElse(player.getHome(StringUtil.old_safeString(home))); + final Location loc = player.getHome(home); if (loc == null) { throw new NotEnoughArgumentsException(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java index 72617d51306..4d6132587a1 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java @@ -11,8 +11,7 @@ public final class StringUtil { private static final Pattern INVALIDCHARACTER = Pattern.compile("[/\\\\:*?\"<>|\\x00-\\x1F]"); - private static final Pattern OLD_INVALIDCHARACTER = Pattern.compile("[^a-z0-9-]"); - //private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); + private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); @SuppressWarnings("CheckStyle") private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"); @@ -30,14 +29,15 @@ public static String safeString(final String string) { if (string == null) { return null; } - return INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + return STRICTINVALIDCHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } - public static String old_safeString(final String string) { + public static String new_safeString(final String string) { if (string == null) { return null; } - return OLD_INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + return INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } + //Less restrictive string sanitizing, when not used as perm or filename public static String sanitizeString(final String string) { return INVALIDCHARS.matcher(string).replaceAll("");