From 0d358989dcc257bcde3961e28d72459c4f5e54a2 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 22 Mar 2023 00:31:02 +0000 Subject: [PATCH] Add more unit tests --- .../huskhomes/BukkitPluginTests.java | 350 +++++++++++++++++- .../huskhomes/command/EditWarpCommand.java | 2 +- .../william278/huskhomes/command/Node.java | 2 +- .../huskhomes/command/SpawnCommand.java | 2 +- .../huskhomes/command/WarpCommand.java | 2 +- .../william278/huskhomes/manager/Manager.java | 12 +- .../huskhomes/manager/WarpsManager.java | 6 +- .../william278/huskhomes/util/Validator.java | 10 +- 8 files changed, 368 insertions(+), 18 deletions(-) diff --git a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java index 1ce2e9f6..e0d3bb20 100644 --- a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java +++ b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java @@ -7,14 +7,17 @@ import de.themoep.minedown.adventure.MineDown; import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.Command; import net.william278.huskhomes.command.RtpCommand; +import net.william278.huskhomes.position.*; import net.william278.huskhomes.user.BukkitUser; import net.william278.huskhomes.user.ConsoleUser; import net.william278.huskhomes.user.OnlineUser; +import net.william278.huskhomes.util.BukkitAdapter; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import java.util.List; import java.util.Map; @@ -165,4 +168,349 @@ public class BukkitPluginTests { } -} + @Nested + @DisplayName("Validator Tests") + public class ValidatorTests { + + @DisplayName("Test Validator Accepts Valid Names") + @ParameterizedTest(name = "Valid Name: \"{0}\"") + @ValueSource(strings = {"ValidName", "Valid_Name", "Valid-Name", "Valid.Name", "ValidName123", "ValidName-123", "ValidName_123", "V"}) + public void testValidNameIsValid(@NotNull String name) { + Assertions.assertTrue(plugin.getValidator().isValidName(name)); + } + + @DisplayName("Test Validator Rejects Invalid Names") + @ParameterizedTest(name = "Invalid Name: \"{0}\"") + @ValueSource(strings = {"Invalid Name", "•♣♠", "Invali♣Name", "", " ", "\0", "InvalidName12345678901234567890"}) + public void testInvalidNameIsInvalid(@NotNull String name) { + Assertions.assertFalse(plugin.getValidator().isValidName(name)); + } + + // test descriptions + @DisplayName("Test Validator Accepts Valid Descriptions") + @ParameterizedTest(name = "Valid Description: \"{0}\"") + @ValueSource(strings = { + "Lorem ipsum text", "Lorem ipsum text with special characters •♣♠", + "Lorem ipsum text with special characters •♣♠ and numbers 1234567890", + "Lorem ipsum text with special characters •♣♠ and numbers 1234567890 and whitespace", + "Lorem ipsum text with special characters •♣♠ and numbers 1234567890 and whitespace and a very long" + + " description that is 255 characters long and should be accepted by the validator" + }) + public void testValidDescriptionIsValid(@NotNull String description) { + Assertions.assertTrue(plugin.getValidator().isValidDescription(description)); + } + + @DisplayName("Test Validator Rejects Invalid Descriptions") + @ParameterizedTest(name = "Invalid Description: \"{0}\"") + @ValueSource(strings = {"Lorem ipsum text with special characters •♣♠ and numbers 1234567890 and whitespace and" + + "a very long description that is more than 256 characters long and should be rejected " + + "by the validator because it is far, far too long and thus exceeds the maximum length " + + "of 255 characters that are allowed for a description of a home or a warp."}) + public void testInvalidDescriptionIsInvalid(@NotNull String description) { + Assertions.assertFalse(plugin.getValidator().isValidDescription(description)); + } + + } + + @Nested + @DisplayName("Warp Tests") + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) + public class WarpTests { + private static final List WARP_NAMES = List.of("warple", "example", "exmaination", "123234__44", "a", "1"); + + @DisplayName("Test Warp Creation") + @ParameterizedTest(name = "Warp Name: \"{0}\"") + @MethodSource("provideWarpData") + @Order(1) + public void testWarpCreation(@NotNull String name, @NotNull Position position) { + plugin.getManager().warps().createWarp(name, position); + Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent()); + Assertions.assertTrue(plugin.getManager().warps() + .getWarps().stream() + .anyMatch(warp -> warp.equals(name))); + } + + // test warp renaming + @DisplayName("Test Warp Renaming") + @ParameterizedTest(name = "Rename: \"{0}\" > \"{0}2\"") + @MethodSource("provideWarpData") + @Order(2) + public void testWarpRenaming(@NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + final String newName = (name + "2"); + plugin.getManager().warps().setWarpName(name, newName); + Assertions.assertTrue(plugin.getDatabase().getWarp(newName).isPresent()); + Assertions.assertTrue(plugin.getManager().warps() + .getWarps().stream() + .anyMatch(warp -> warp.equals(newName))); + + // Rename back to original name + plugin.getManager().warps().setWarpName(newName, name); + Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent()); + Assertions.assertTrue(plugin.getManager().warps() + .getWarps().stream() + .anyMatch(warp -> warp.equals(name))); + } + + @DisplayName("Test Warp Edit Description") + @ParameterizedTest(name = "Edit Description: \"{0}\"") + @MethodSource("provideWarpData") + @Order(3) + public void testWarpChangingDescription(@NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + final String description = "This is a test description for " + name + "."; + plugin.getManager().warps().setWarpDescription(name, description); + final Optional warpDescription = plugin.getDatabase().getWarp(name) + .map(Warp::getMeta) + .map(PositionMeta::getDescription); + Assertions.assertTrue(warpDescription.isPresent()); + Assertions.assertEquals(description, warpDescription.get()); + } + + @DisplayName("Test Warp Relocate") + @ParameterizedTest(name = "Relocate: \"{0}\"") + @MethodSource("provideWarpData") + @Order(4) + public void testWarpRelocation(@NotNull String name, @NotNull Position position) { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + 0, 0, world, plugin.getServerName()); + plugin.getManager().warps().relocateWarp(name, newPosition); + + final Optional warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp); + Assertions.assertTrue(warpPosition.isPresent()); + Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX()); + Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY()); + Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ()); + } + + @DisplayName("Test Warp Overwrite") + @ParameterizedTest(name = "Overwrite: \"{0}\"") + @MethodSource("provideWarpData") + @Order(5) + public void testWarpOverwrite(@NotNull String name, @NotNull Position position) { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + 0, 0, world, plugin.getServerName()); + plugin.getManager().warps().createWarp(name, newPosition, true); + + final Optional warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp); + Assertions.assertTrue(warpPosition.isPresent()); + Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX()); + Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY()); + Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ()); + } + + @DisplayName("Test Warp Deletion") + @ParameterizedTest(name = "Delete Warp: \"{0}\"") + @MethodSource("provideWarpData") + @Order(6) + public void testWarpDeletion(@NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + plugin.getManager().warps().deleteWarp(name); + Assertions.assertFalse(plugin.getDatabase().getWarp(name).isPresent()); + Assertions.assertFalse(plugin.getManager().warps() + .getWarps().stream() + .anyMatch(warp -> warp.equals(name))); + + plugin.getManager().warps().createWarp(name, position); + Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent()); + } + + @DisplayName("Test Deleting All Warps") + @Order(7) + @Test + public void testWarpDeleteAll() { + final int deleted = plugin.getManager().warps().deleteAllWarps(); + Assertions.assertTrue(plugin.getManager().warps().getWarps().isEmpty()); + Assertions.assertTrue(plugin.getDatabase().getWarps().isEmpty()); + Assertions.assertEquals(WARP_NAMES.size(), deleted); + } + + @NotNull + public static Stream provideWarpData() { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position position = new Position(0, 0, 0, 0, 0, world, plugin.getServerName()); + return WARP_NAMES.stream() + .flatMap(name -> Stream.of(Arguments.of(name, position))); + } + + } + + // home tests, like warps but with a user parameter (owner) as well as name and position and a test for changing home privacy + @Nested + @DisplayName("Home Tests") + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) + public class HomeTests { + private static final List HOME_NAMES = List.of("mr_home", "homble", "seaside", "hovel", "tokyo-3", "testington", "h"); + private static BukkitUser homeOwner; + + @DisplayName("Ensure User Data") + @BeforeAll + public static void createHomeUser() { + homeOwner = BukkitUser.adapt(server.addPlayer("TestUser278")); + plugin.getDatabase().ensureUser(homeOwner); + Assertions.assertTrue(plugin.getDatabase().getUserData(homeOwner.getUuid()).isPresent()); + } + + @DisplayName("Test Home Creation") + @ParameterizedTest(name = "Home Name: \"{1}\"") + @MethodSource("provideHomeData") + @Order(1) + public void testHomeCreation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) { + plugin.getManager().homes().createHome(owner, name, position); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + Assertions.assertTrue(plugin.getManager().homes() + .getUserHomes() + .get(owner.getUsername()).stream() + .anyMatch(home -> home.equals(name))); + } + + // rename + @DisplayName("Test Home Renaming") + @ParameterizedTest(name = "Rename: \"{1}\" > \"{1}2\"") + @MethodSource("provideHomeData") + @Order(2) + public void testHomeRenaming(@NotNull OnlineUser owner, @NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + final String newName = "new_" + name; + plugin.getManager().homes().setHomeName(owner, name, newName); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, newName).isPresent()); + Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent()); + Assertions.assertTrue(plugin.getManager().homes() + .getUserHomes() + .get(owner.getUsername()).stream() + .anyMatch(home -> home.equals(newName))); + Assertions.assertFalse(plugin.getManager().homes() + .getUserHomes() + .get(owner.getUsername()).stream() + .anyMatch(home -> home.equals(name))); + + // Rename back to original name + plugin.getManager().homes().setHomeName(owner, newName, name); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + Assertions.assertTrue(plugin.getManager().homes() + .getUserHomes() + .get(owner.getUsername()).stream() + .anyMatch(home -> home.equals(name))); + } + + // description + @DisplayName("Test Home Description") + @ParameterizedTest(name = "Edit Description: \"{1}\"") + @MethodSource("provideHomeData") + @Order(3) + public void testHomeDescription(@NotNull OnlineUser owner, @NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + final String description = "This is a test description for the home " + name + "!"; + plugin.getManager().homes().setHomeDescription(owner, name, description); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + + final Optional homeDescription = plugin.getDatabase().getHome(owner, name) + .map(Home::getMeta) + .map(PositionMeta::getDescription); + Assertions.assertTrue(homeDescription.isPresent()); + Assertions.assertEquals(description, homeDescription.get()); + } + + // relocate + @DisplayName("Test Home Relocation") + @ParameterizedTest(name = "Relocate: \"{1}\"") + @MethodSource("provideHomeData") + @Order(4) + public void testHomeRelocation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + 0, 0, world, plugin.getServerName()); + plugin.getManager().homes().setHomePosition(owner, name, newPosition); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + + final Optional homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home); + Assertions.assertTrue(homePosition.isPresent()); + Assertions.assertEquals(newPosition.getX(), homePosition.get().getX()); + Assertions.assertEquals(newPosition.getY(), homePosition.get().getY()); + Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ()); + } + + // overwrite + @DisplayName("Test Home Overwrite") + @ParameterizedTest(name = "Overwrite: \"{1}\"") + @MethodSource("provideHomeData") + @Order(5) + public void testHomeOverwrite(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + 0, 0, world, plugin.getServerName()); + plugin.getManager().homes().setHomePosition(owner, name, newPosition); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + + final Optional homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home); + Assertions.assertTrue(homePosition.isPresent()); + Assertions.assertEquals(newPosition.getX(), homePosition.get().getX()); + Assertions.assertEquals(newPosition.getY(), homePosition.get().getY()); + Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ()); + } + + // make public + @DisplayName("Test Making Home Public") + @ParameterizedTest(name = "Make Public: \"{1}\"") + @MethodSource("provideHomeData") + @Order(6) + public void testHomeMakePublic(@NotNull OnlineUser owner, @NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + plugin.getManager().homes().setHomePrivacy(owner, name, true); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + + final Optional homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic); + Assertions.assertTrue(homePrivacy.isPresent()); + Assertions.assertTrue(homePrivacy.get()); + Assertions.assertTrue(plugin.getManager().homes().getPublicHomes().get(owner.getUsername()).contains(name)); + } + + // make private + @DisplayName("Test Making Home Private") + @ParameterizedTest(name = "Make Private: \"{1}\"") + @MethodSource("provideHomeData") + @Order(7) + public void testHomeMakePrivate(@NotNull OnlineUser owner, @NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + plugin.getManager().homes().setHomePrivacy(owner, name, false); + Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); + + final Optional homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic); + Assertions.assertTrue(homePrivacy.isPresent()); + Assertions.assertFalse(homePrivacy.get()); + Assertions.assertFalse(plugin.getManager().homes().getPublicHomes() + .getOrDefault(owner.getUsername(), List.of()) + .contains(name)); + } + + // delete + @DisplayName("Test Home Deletion") + @ParameterizedTest(name = "Delete: \"{1}\"") + @MethodSource("provideHomeData") + @Order(8) + public void testHomeDeletion(@NotNull OnlineUser owner, @NotNull String name, @SuppressWarnings("unused") @NotNull Position position) { + plugin.getManager().homes().deleteHome(owner, name); + Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent()); + Assertions.assertFalse(plugin.getManager().homes().getUserHomes() + .getOrDefault(owner.getUsername(), List.of()) + .contains(name)); + + plugin.getManager().homes().createHome(owner, name, position); + } + + // delete all + @DisplayName("Test Deleting All Homes") + @Order(9) + @Test + public void testDeleteAllHomes() { + final int deleted = plugin.getManager().homes().deleteAllHomes(homeOwner); + Assertions.assertTrue(plugin.getDatabase().getHomes(homeOwner).isEmpty()); + Assertions.assertTrue(plugin.getManager().homes().getUserHomes().get(homeOwner.getUsername()).isEmpty()); + Assertions.assertEquals(HOME_NAMES.size(), deleted); + } + + @NotNull + public static Stream provideHomeData() { + final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); + final Position position = new Position(0, 0, 0, 0, 0, world, plugin.getServerName()); + return HOME_NAMES.stream() + .flatMap(name -> Stream.of(Arguments.of(homeOwner, name, position))); + } + + } +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java index da2716a3..83a12392 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java @@ -55,7 +55,7 @@ public class EditWarpCommand extends SavedPositionCommand { warp.getMeta().setName(optionalName.get()); plugin.fireEvent(plugin.getWarpEditEvent(warp, executor), (event) -> { try { - plugin.getManager().warps().renameWarp(warp, warp.getName()); + plugin.getManager().warps().setWarpName(warp, warp.getName()); } catch (ValidationException e) { e.dispatchWarpError(executor, plugin, warp.getName()); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/Node.java b/common/src/main/java/net/william278/huskhomes/command/Node.java index 26e3a3a5..a6120c76 100644 --- a/common/src/main/java/net/william278/huskhomes/command/Node.java +++ b/common/src/main/java/net/william278/huskhomes/command/Node.java @@ -170,7 +170,7 @@ public abstract class Node implements Executable { } - protected Optional resolveTeleportable(@NotNull CommandUser executor, @NotNull String[] args) { + protected Optional resolveTeleporter(@NotNull CommandUser executor, @NotNull String[] args) { return parseStringArg(args, 0).map(Teleportable::username) .or(() -> executor instanceof Teleportable ? Optional.of((Teleportable) executor) : Optional.empty()); } diff --git a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java index 0e78e423..c8e8ee9c 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java @@ -27,7 +27,7 @@ public class SpawnCommand extends Command { return; } - final Optional optionalTeleporter = resolveTeleportable(executor, args); + final Optional optionalTeleporter = resolveTeleporter(executor, args); if (optionalTeleporter.isEmpty()) { plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) .ifPresent(executor::sendMessage); diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index af7dc9e6..bc08c3ee 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -36,7 +36,7 @@ public class WarpCommand extends SavedPositionCommand { } } - final Optional optionalTeleporter = resolveTeleportable(executor, args); + final Optional optionalTeleporter = resolveTeleporter(executor, args); if (optionalTeleporter.isEmpty()) { plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) .ifPresent(executor::sendMessage); diff --git a/common/src/main/java/net/william278/huskhomes/manager/Manager.java b/common/src/main/java/net/william278/huskhomes/manager/Manager.java index ba5d86f6..713027e7 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/Manager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/Manager.java @@ -35,10 +35,12 @@ public class Manager { // Update caches on all servers protected void propagateCacheUpdate() { - plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder() - .type(Message.Type.UPDATE_CACHES) - .scope(Message.Scope.SERVER) - .target(Message.TARGET_ALL) - .build().send(plugin.getMessenger(), user)); + if (plugin.getSettings().isCrossServer()) { + plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder() + .type(Message.Type.UPDATE_CACHES) + .scope(Message.Scope.SERVER) + .target(Message.TARGET_ALL) + .build().send(plugin.getMessenger(), user)); + } } } diff --git a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java index 1cf6d46b..76fae83c 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java @@ -137,16 +137,16 @@ public class WarpsManager { this.cacheWarp(warp, true); } - public void renameWarp(@NotNull String name, @NotNull String newName) throws ValidationException { + public void setWarpName(@NotNull String name, @NotNull String newName) throws ValidationException { final Optional optionalWarp = plugin.getDatabase().getWarp(name); if (optionalWarp.isEmpty()) { throw new ValidationException(ValidationException.Type.NOT_FOUND); } - this.renameWarp(optionalWarp.get(), newName); + this.setWarpName(optionalWarp.get(), newName); } - public void renameWarp(@NotNull Warp warp, @NotNull String newName) throws ValidationException { + public void setWarpName(@NotNull Warp warp, @NotNull String newName) throws ValidationException { if (!plugin.getValidator().isValidName(newName)) { throw new ValidationException(ValidationException.Type.NAME_INVALID); } diff --git a/common/src/main/java/net/william278/huskhomes/util/Validator.java b/common/src/main/java/net/william278/huskhomes/util/Validator.java index 168afc0f..b42b8e8f 100644 --- a/common/src/main/java/net/william278/huskhomes/util/Validator.java +++ b/common/src/main/java/net/william278/huskhomes/util/Validator.java @@ -31,17 +31,17 @@ public class Validator { /** * Validate home and warp descriptions * - * @param meta The meta to validate + * @param description The meta to validate * @return Whether the meta is valid against the plugin settings */ - public boolean isValidDescription(@NotNull String meta) { - return (isAsciiOnly(meta) || plugin.getSettings().isAllowUnicodeDescriptions()) - && meta.length() <= MAX_DESCRIPTION_LENGTH; + public boolean isValidDescription(@NotNull String description) { + return (isAsciiOnly(description) || plugin.getSettings().isAllowUnicodeDescriptions()) + && description.length() <= MAX_DESCRIPTION_LENGTH; } // Check if a string contains only ASCII characters private static boolean isAsciiOnly(@NotNull String string) { - return string.matches("\\A\\p{ASCII}*\\z"); + return string.matches("\\A\\p{ASCII}*\\z") && !string.contains("\u0000"); } // Check if a string contains whitespace