Validate names against unloaded communities

This commit is contained in:
Dan Mulloy 2018-02-25 22:01:18 -05:00 committed by Alexander Meech
parent 11d80037c8
commit 3ed1cc7868
4 changed files with 52 additions and 25 deletions

View File

@ -261,6 +261,11 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
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()

View File

@ -29,12 +29,6 @@ public class CommunityCreateCommand extends CommandBase<CommunityManager>
UtilPlayer.message(caller, F.help("/com create <name>", "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<CommunityManager>
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]));
}
});
});
}
}

View File

@ -46,11 +46,6 @@ public class CommunityRenameCommand extends CommandBase<CommunityManager>
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<CommunityManager>
}
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);
}
});
});
}
}

View File

@ -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.