From 22afa0d8286ec54a8f5a2214eee2802a3599d2f2 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Tue, 13 Feb 2024 17:53:15 -0600 Subject: [PATCH] - Fix NameValidation issues. --- Towny/pom.xml | 2 +- .../palmergames/bukkit/towny/command/NationCommand.java | 5 +++++ .../palmergames/bukkit/towny/command/TownCommand.java | 4 ++++ .../bukkit/towny/command/TownyAdminCommand.java | 4 ++++ .../java/com/palmergames/bukkit/util/NameValidation.java | 9 +-------- Towny/src/main/resources/ChangeLog.txt | 4 +++- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Towny/pom.xml b/Towny/pom.xml index f8ff5828b9..3086192bf0 100644 --- a/Towny/pom.xml +++ b/Towny/pom.xml @@ -13,7 +13,7 @@ towny jar - 0.100.1.12 + 0.100.1.13 diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java index b1abad608a..a9eafe3d0e 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java @@ -903,6 +903,8 @@ public static void newNation(CommandSender sender, String name, Town capitalTown throw new TownyException(Translatable.of("msg_err_already_nation")); String filteredName = NameValidation.checkAndFilterNationNameOrThrow(name); + if (TownyUniverse.getInstance().hasNation(filteredName)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", filteredName)); BukkitTools.ifCancelledThenThrow(new PreNewNationEvent(capitalTown, filteredName)); @@ -2104,6 +2106,9 @@ private static void nationSetName(CommandSender sender, Nation nation, String[] String name = String.join("_", StringMgmt.remFirstArg(split)); name = NameValidation.checkAndFilterGovernmentNameOrThrow(name, nation); + if (TownyUniverse.getInstance().hasNation(name)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", name)); + if (TownySettings.getTownAutomaticCapitalisationEnabled()) name = StringMgmt.capitalizeStrings(name); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownCommand.java index 2958772e66..6bd23f1395 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownCommand.java @@ -2129,6 +2129,8 @@ public static void townSetName(CommandSender sender, String[] split, Town town) if (TownySettings.getTownAutomaticCapitalisationEnabled()) name = StringMgmt.capitalizeStrings(name); name = NameValidation.checkAndFilterGovernmentNameOrThrow(name, town); + if (TownyUniverse.getInstance().hasTown(name)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", name)); if(TownyEconomyHandler.isActive() && TownySettings.getTownRenameCost() > 0) { if (!town.getAccount().canPayFromHoldings(TownySettings.getTownRenameCost())) @@ -2466,6 +2468,8 @@ public static void newTown(Player player, String name, Resident resident, boolea name = StringMgmt.capitalizeStrings(name); name = NameValidation.checkAndFilterTownNameOrThrow(name); + if (TownyUniverse.getInstance().hasTown(name)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", name)); if (resident.hasTown()) throw new TownyException(Translatable.of("msg_err_already_res", resident.getName())); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java index c63d6ecf43..ad407dc319 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java @@ -1225,6 +1225,8 @@ public void parseAdminTownCommand(CommandSender sender, String[] split) throws T if (split.length < 3) throw new TownyException(Translatable.of("msg_err_invalid_input", "/ta town TOWNNAME rename NEWNAME")); String name = NameValidation.checkAndFilterTownNameOrThrow(String.join("_", StringMgmt.remArgs(split, 2))); + if (TownyUniverse.getInstance().hasTown(name)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", name)); BukkitTools.ifCancelledThenThrow(new TownPreRenameEvent(town, name)); townyUniverse.getDataSource().renameTown(town, name); TownyMessaging.sendPrefixedTownMessage(town, Translatable.of("msg_town_set_name", getSenderFormatted(sender), town.getName())); @@ -1674,6 +1676,8 @@ public void parseAdminNationCommand(CommandSender sender, String[] split) throws case "rename": checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_NATION_RENAME.getNode()); String name = NameValidation.checkAndFilterNationNameOrThrow(String.join("_", StringMgmt.remArgs(split, 2))); + if (TownyUniverse.getInstance().hasNation(name)) + throw new TownyException(Translatable.of("msg_err_name_validation_name_already_in_use", name)); BukkitTools.ifCancelledThenThrow(new NationPreRenameEvent(nation, name)); townyUniverse.getDataSource().renameNation(nation, name); TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_nation_set_name", getSenderFormatted(sender), nation.getName())); diff --git a/Towny/src/main/java/com/palmergames/bukkit/util/NameValidation.java b/Towny/src/main/java/com/palmergames/bukkit/util/NameValidation.java index 6538d08351..36854dc020 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/util/NameValidation.java +++ b/Towny/src/main/java/com/palmergames/bukkit/util/NameValidation.java @@ -2,7 +2,6 @@ import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.TownySettings; -import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.towny.exceptions.InvalidNameException; import com.palmergames.bukkit.towny.exceptions.TownyException; import com.palmergames.bukkit.towny.object.Government; @@ -81,9 +80,6 @@ public static String checkAndFilterTownNameOrThrow(String name) throws InvalidNa if (out.startsWith(TownySettings.getTownAccountPrefix())) throw new InvalidNameException(Translatable.of("msg_err_name_validation_begins_with_eco_prefix", out)); - if (TownyUniverse.getInstance().hasTown(out)) - throw new InvalidNameException(Translatable.of("msg_err_name_validation_name_already_in_use", out)); - return out; } @@ -108,9 +104,6 @@ public static String checkAndFilterNationNameOrThrow(String name) throws Invalid if (out.startsWith(TownySettings.getNationAccountPrefix())) throw new InvalidNameException(Translatable.of("msg_err_name_validation_begins_with_eco_prefix", out)); - if (TownyUniverse.getInstance().hasNation(out)) - throw new InvalidNameException(Translatable.of("msg_err_name_validation_name_already_in_use", out)); - return out; } @@ -328,7 +321,7 @@ private static void testNameLength(String name) throws InvalidNameException { * @throws InvalidNameException thrown when numbers aren't allowed and they are present. */ private static void testForNumbers(String name) throws InvalidNameException { - if (TownySettings.areNumbersAllowedInNationNames() && numberPattern.matcher(name).find()) + if (!TownySettings.areNumbersAllowedInNationNames() && numberPattern.matcher(name).find()) throw new InvalidNameException(Translatable.of("msg_err_name_validation_contains_numbers", name)); } diff --git a/Towny/src/main/resources/ChangeLog.txt b/Towny/src/main/resources/ChangeLog.txt index 7a47c80c4e..c8dbb91166 100644 --- a/Towny/src/main/resources/ChangeLog.txt +++ b/Towny/src/main/resources/ChangeLog.txt @@ -9515,4 +9515,6 @@ v0.92.0.11: - Closes #7249. - Overhaul the NameValidation class. - Add the towny.command.resident.jail to the nomad section of the townyperms.yml file, so that players without a town can pay bail if they're jailed. - - Bump com.github.seeseemelk:MockBukkit-v1.20 from 3.65.0 to 3.72.0. \ No newline at end of file + - Bump com.github.seeseemelk:MockBukkit-v1.20 from 3.65.0 to 3.72.0. +0.100.1.13: + - Fix NameValidation issues. \ No newline at end of file