From 3ed1cc7868babaaab2a3d1f45ceb85cbd61263f9 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 25 Feb 2018 22:01:18 -0500 Subject: [PATCH] Validate names against unloaded communities --- .../core/communities/CommunityManager.java | 5 ++++ .../commands/CommunityCreateCommand.java | 24 ++++++++--------- .../commands/CommunityRenameCommand.java | 27 ++++++++++--------- .../storage/CommunityRepository.java | 21 +++++++++++++++ 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index f0c0cfc4d..192795043 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -261,6 +261,11 @@ public class CommunityManager extends MiniDbClientPlugin PermissionGroup.ADMIN.setPermission(Perm.COMMUNITY_UNINVITE_STAFF_COMMAND, true, true); } + public void communityExists(String name, Runnable onTrue, Runnable onFalse) + { + _repo.communityExists(name, onTrue, onFalse); + } + public boolean ownsCommunity(UUID uuid) { return _loadedCommunities.values().stream() diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java index 60e5ad93a..93d69633c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java @@ -29,12 +29,6 @@ public class CommunityCreateCommand extends CommandBase UtilPlayer.message(caller, F.help("/com create ", "Creates a new community", ChatColor.DARK_AQUA)); return; } - Community c = Plugin.getLoadedCommunity(args[0]); - if (c != null) - { - UtilPlayer.message(caller, F.main(Plugin.getName(), "A community already exists with that name!")); - return; - } if (Plugin.Get(caller).ownsCommunity()) { UtilPlayer.message(caller, F.main(Plugin.getName(), "You already own a community!")); @@ -54,14 +48,20 @@ public class CommunityCreateCommand extends CommandBase final String senderName = Managers.get(CoreClientManager.class).Get(caller).getName(); Plugin.runAsync(() -> { - if (Managers.get(Chat.class).getFilteredMessage(caller, args[0]).contains("*")) + Plugin.communityExists(args[0], () -> // onTrue { - UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); - } - else + UtilPlayer.message(caller, F.main(Plugin.getName(), "A community with that name already exists!")); + }, () -> // onFalse { - Plugin.runSync(() -> Plugin.handleCreate(caller, senderName, accountId, args[0])); - } + if (Managers.get(Chat.class).getFilteredMessage(caller, args[0]).contains("*")) + { + UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); + } + else + { + Plugin.runSync(() -> Plugin.handleCreate(caller, senderName, accountId, args[0])); + } + }); }); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java index 6383e5369..642a0b3c8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java @@ -46,11 +46,6 @@ public class CommunityRenameCommand extends CommandBase return; } } - if (Plugin.getLoadedCommunity(newName) != null) - { - UtilPlayer.message(caller, F.main(Plugin.getName(), "A community already exists with that name!")); - return; - } if (newName.length() > 15 || Plugin.ALPHA_NUMERIC_PATTERN.matcher(newName).find()) { UtilPlayer.message(caller, F.main(Plugin.getName(), "A community name cannot be longer than 15 characters and must be alphanumeric!")); @@ -63,18 +58,24 @@ public class CommunityRenameCommand extends CommandBase } Plugin.runAsync(() -> { - if (Managers.get(Chat.class).getFilteredMessage(caller, newName).contains("*")) + Plugin.communityExists(newName, () -> // onTrue - community already exists { - UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); - } - else + UtilPlayer.message(caller, F.main(Plugin.getName(), "A community with that name already exists!")); + }, () -> // onFalse - we're good { - if (!c.getMembers().containsKey(caller.getUniqueId())) + if (Managers.get(Chat.class).getFilteredMessage(caller, newName).contains("*")) { - UtilPlayer.message(caller, F.main(Plugin.getName(), "You have changed the name of " + F.name(c.getName()) + " to " + F.name(newName) + "!")); + UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); } - Plugin.handleNameUpdate(caller, c, newName); - } + else + { + if (!c.getMembers().containsKey(caller.getUniqueId())) + { + UtilPlayer.message(caller, F.main(Plugin.getName(), "You have changed the name of " + F.name(c.getName()) + " to " + F.name(newName) + "!")); + } + Plugin.handleNameUpdate(caller, c, newName); + } + }); }); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 5207eb246..75ba9e3f6 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -27,6 +27,7 @@ import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.data.PlayerStatus; import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.RepositoryBase; +import mineplex.serverdata.database.column.Column; import mineplex.serverdata.database.column.ColumnBoolean; import mineplex.serverdata.database.column.ColumnInt; import mineplex.serverdata.database.column.ColumnVarChar; @@ -63,6 +64,26 @@ public class CommunityRepository extends RepositoryBase _repo = statusRepo; _us = us; } + + public void communityExists(String name, Runnable onTrue, Runnable onFalse) + { + try (Connection connection = getConnection()) + { + executeQuery("SELECT name FROM communities WHERE name=?", resultSet -> + { + if (resultSet.next()) + { + onTrue.run(); + } else + { + onFalse.run(); + } + }, new ColumnVarChar("name", 15, name)); + } catch (Exception ex) + { + throw new RuntimeException("Failed to determine if community exists", ex); + } + } /** * Loads all communities that are eligible to be shown in the browser.