From 697b349167b0748f29a8786015e19dc42c8e74fd Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 24 Jun 2016 18:38:05 +0100 Subject: [PATCH] Add primary groups --- .../java/me/lucko/luckperms/VaultHook.java | 12 +++-- .../luckperms/commands/CommandManager.java | 1 + .../user/subcommands/UserInfoCommand.java | 1 + .../subcommands/UserRemoveGroupCommand.java | 5 ++ .../UserSetPrimaryGroupCommand.java | 46 +++++++++++++++++++ .../data/methods/FlatfileDatastore.java | 17 +++++-- .../data/methods/MySQLDatastore.java | 2 +- .../luckperms/data/methods/SQLDatastore.java | 24 ++++++---- .../data/methods/SQLiteDatastore.java | 2 +- .../lucko/luckperms/groups/GroupManager.java | 2 +- .../java/me/lucko/luckperms/users/User.java | 7 +++ .../me/lucko/luckperms/users/UserManager.java | 3 +- 12 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/VaultHook.java b/bukkit/src/main/java/me/lucko/luckperms/VaultHook.java index 3d717740f..cc076fc52 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/VaultHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/VaultHook.java @@ -90,7 +90,10 @@ class VaultHook extends Permission { @Override public boolean playerInGroup(String world, String player, String group) { final User user = plugin.getUserManager().getUser(player); - return user != null && user.getGroupNames().contains(group); + if (user == null) return false; + + final Group group1 = plugin.getGroupManager().getGroup(group); + return group1 != null && user.isInGroup(group1); } @Override @@ -126,14 +129,13 @@ class VaultHook extends Permission { @Override public String[] getPlayerGroups(String world, String player) { final User user = plugin.getUserManager().getUser(player); - if (user == null) return new String[0]; - - return user.getGroupNames().toArray(new String[0]); + return (user == null) ? new String[0] : user.getGroupNames().toArray(new String[0]); } @Override public String getPrimaryGroup(String world, String player) { - throw new UnsupportedOperationException(); + final User user = plugin.getUserManager().getUser(player); + return (user == null) ? null : user.getPrimaryGroup(); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java index a1c0f64e2..e65bbf225 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java @@ -36,6 +36,7 @@ public class CommandManager { userCommand.registerSubCommand(new UserListNodesCommand()); userCommand.registerSubCommand(new UserRemoveGroupCommand()); userCommand.registerSubCommand(new UserSetPermissionCommand()); + userCommand.registerSubCommand(new UserSetPrimaryGroupCommand()); userCommand.registerSubCommand(new UserUnSetPermissionCommand()); GroupMainCommand groupCommand = new GroupMainCommand(); diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java index 15938bcb3..2c1e0feb6 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java @@ -21,6 +21,7 @@ public class UserInfoCommand extends UserSubCommand { prefix + "&d-> &eUUID: &6" + user.getUuid() + "\n" + prefix + "&d-> &eStatus: " + plugin.getPlayerStatus(user.getUuid()) + "\n" + prefix + "&d-> &eGroups: &6" + Util.listToCommaSep(user.getGroupNames()) + "\n" + + prefix + "&d-> &ePrimary Group: &6" + user.getPrimaryGroup() + "\n" + prefix + "&d-> &ePermissions: &6" + (user.getNodes().keySet().size() - user.getGroupNames().size()) + "\n" + prefix + "&d-> &bUse &a/perms user " + user.getName() + " listnodes &bto see all permissions."; diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java index 9fc07bfda..2f72babfc 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java @@ -33,6 +33,11 @@ public class UserRemoveGroupCommand extends UserSubCommand { return; } + if (server.equalsIgnoreCase("global") && user.getPrimaryGroup().equalsIgnoreCase(group1.getName())) { + Util.sendPluginMessage(sender, "You cannot remove a user from their primary group."); + return; + } + try { user.removeGroup(group1, server); Util.sendPluginMessage(sender, "&b" + user.getName() + "&a was removed from group &b" + group + "&a on server &b" + server + "&a."); diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java new file mode 100644 index 000000000..3f10abdca --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java @@ -0,0 +1,46 @@ +package me.lucko.luckperms.commands.user.subcommands; + +import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Sender; +import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.user.UserSubCommand; +import me.lucko.luckperms.groups.Group; +import me.lucko.luckperms.users.User; + +import java.util.List; + +public class UserSetPrimaryGroupCommand extends UserSubCommand { + public UserSetPrimaryGroupCommand() { + super("setprimarygroup", "Sets a users primary group", + "/perms user setprimarygroup ", "luckperms.user.setprimarygroup"); + } + + @Override + protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List args) { + Group group = plugin.getGroupManager().getGroup(args.get(0).toLowerCase()); + if (group == null) { + Util.sendPluginMessage(sender, "That group does not exist!"); + return; + } + + if (user.getPrimaryGroup().equalsIgnoreCase(group.getName())) { + Util.sendPluginMessage(sender, "The user already has this group set as their primary group."); + return; + } + + if (!user.isInGroup(group)) { + Util.sendPluginMessage(sender, "The user must be a member of the group first! Use &4/perms user addgroup "); + return; + } + + user.setPrimaryGroup(group.getName()); + Util.sendPluginMessage(sender, "&b" + user.getName() + "&a's primary group was set to &b" + group.getName() + "&a."); + + saveUser(user, sender, plugin); + } + + @Override + public boolean isArgLengthInvalid(int argLength) { + return argLength == 0; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java index 984ba7d0b..295cdb99f 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java @@ -120,9 +120,6 @@ public class FlatfileDatastore extends Datastore { @Override public boolean loadOrCreateUser(UUID uuid, String username) { User user = plugin.getUserManager().makeUser(uuid, username); - try { - user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); - } catch (ObjectAlreadyHasException ignored) {} File userFile = new File(usersDir, uuid.toString() + ".json"); if (!userFile.exists()) { @@ -133,10 +130,17 @@ public class FlatfileDatastore extends Datastore { return false; } + // Setup the new user with default values + try { + user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); + } catch (ObjectAlreadyHasException ignored) {} + user.setPrimaryGroup(plugin.getConfiguration().getDefaultGroupName()); + boolean success = doWrite(userFile, writer -> { writer.beginObject(); writer.name("uuid").value(user.getUuid().toString()); writer.name("name").value(user.getName()); + writer.name("primaryGroup").value(user.getPrimaryGroup()); writer.name("perms"); writer.beginObject(); for (Map.Entry e : user.getNodes().entrySet()) { @@ -156,6 +160,8 @@ public class FlatfileDatastore extends Datastore { reader.nextString(); // uuid reader.nextName(); // name record reader.nextString(); // name + reader.nextName(); // primaryGroup record + reader.nextString(); // primaryGroup reader.nextName(); //perms reader.beginObject(); while (reader.hasNext()) { @@ -189,7 +195,9 @@ public class FlatfileDatastore extends Datastore { reader.nextString(); // uuid reader.nextName(); // name record user.setName(reader.nextString()); // name - reader.nextName(); //perms + reader.nextName(); // primaryGroup record + user.setPrimaryGroup(reader.nextString()); // primaryGroup + reader.nextName(); // perms record reader.beginObject(); while (reader.hasNext()) { String node = reader.nextName(); @@ -223,6 +231,7 @@ public class FlatfileDatastore extends Datastore { writer.beginObject(); writer.name("uuid").value(user.getUuid().toString()); writer.name("name").value(user.getName()); + writer.name("primaryGroup").value(user.getPrimaryGroup()); writer.name("perms"); writer.beginObject(); for (Map.Entry e : user.getNodes().entrySet()) { diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java index b283e3039..c163acabe 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java @@ -10,7 +10,7 @@ import java.sql.SQLException; public class MySQLDatastore extends SQLDatastore { private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"; - private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"; + private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"; private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"; private final MySQLConfiguration configuration; diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java index 215f0f7da..1b1a8dc77 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java @@ -27,9 +27,9 @@ abstract class SQLDatastore extends Datastore { private static final Type NM_TYPE = new TypeToken>(){}.getType(); - private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?)"; + private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?, ?)"; private static final String USER_SELECT = "SELECT * FROM lp_users WHERE uuid=?"; - private static final String USER_UPDATE = "UPDATE lp_users SET name=?, perms=? WHERE uuid=?"; + private static final String USER_UPDATE = "UPDATE lp_users SET name=?, primary_group = ?, perms=? WHERE uuid=?"; private static final String GROUP_INSERT = "INSERT INTO lp_groups VALUES(?, ?)"; private static final String GROUP_SELECT = "SELECT perms FROM lp_groups WHERE name=?"; @@ -139,8 +139,9 @@ abstract class SQLDatastore extends Datastore { @Override boolean onResult(ResultSet resultSet) throws SQLException { if (resultSet.next()) { - user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE)); user.setName(resultSet.getString("name")); + user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE)); + user.setPrimaryGroup(resultSet.getString("primary_group")); return true; } return false; @@ -155,10 +156,6 @@ abstract class SQLDatastore extends Datastore { @Override public boolean loadOrCreateUser(UUID uuid, String username) { User user = plugin.getUserManager().makeUser(uuid, username); - try { - user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); - } catch (ObjectAlreadyHasException ignored) {} - boolean success = runQuery(new QueryRS(USER_SELECT) { @Override void onRun(PreparedStatement preparedStatement) throws SQLException { @@ -169,12 +166,20 @@ abstract class SQLDatastore extends Datastore { boolean onResult(ResultSet resultSet) throws SQLException { boolean success = true; if (!resultSet.next()) { + + // Setup the new user with default values + try { + user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); + } catch (ObjectAlreadyHasException ignored) {} + user.setPrimaryGroup(plugin.getConfiguration().getDefaultGroupName()); + success = runQuery(new QueryPS(USER_INSERT) { @Override void onRun(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setString(1, user.getUuid().toString()); preparedStatement.setString(2, user.getName()); - preparedStatement.setString(3, gson.toJson(user.getNodes())); + preparedStatement.setString(3, user.getPrimaryGroup()); + preparedStatement.setString(4, gson.toJson(user.getNodes())); } }); } else { @@ -196,7 +201,8 @@ abstract class SQLDatastore extends Datastore { void onRun(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setString(1, user.getName()); preparedStatement.setString(2, gson.toJson(user.getNodes())); - preparedStatement.setString(3, user.getUuid().toString()); + preparedStatement.setString(3, user.getPrimaryGroup()); + preparedStatement.setString(4, user.getUuid().toString()); } }); return success; diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java index c02f348a2..97c0152dd 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java @@ -10,7 +10,7 @@ import java.sql.SQLException; public class SQLiteDatastore extends SQLDatastore { private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`));"; - private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`));"; + private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`));"; private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`));"; private final File file; diff --git a/common/src/main/java/me/lucko/luckperms/groups/GroupManager.java b/common/src/main/java/me/lucko/luckperms/groups/GroupManager.java index 7afc150fc..b0d5ab007 100644 --- a/common/src/main/java/me/lucko/luckperms/groups/GroupManager.java +++ b/common/src/main/java/me/lucko/luckperms/groups/GroupManager.java @@ -42,7 +42,7 @@ public class GroupManager { * @param group The group to update or set */ public void updateOrSetGroup(Group group) { - if (!groups.containsKey(group.getName())) { + if (!isLoaded(group.getName())) { // The group isn't already loaded groups.put(group.getName(), group); } else { diff --git a/common/src/main/java/me/lucko/luckperms/users/User.java b/common/src/main/java/me/lucko/luckperms/users/User.java index e9f4fa19c..d9b0fbc46 100644 --- a/common/src/main/java/me/lucko/luckperms/users/User.java +++ b/common/src/main/java/me/lucko/luckperms/users/User.java @@ -26,6 +26,13 @@ public abstract class User extends PermissionObject { @Setter private String name; + /** + * The users primary group + */ + @Getter + @Setter + private String primaryGroup = null; + User(UUID uuid, LuckPermsPlugin plugin) { super(plugin, uuid.toString()); this.uuid = uuid; diff --git a/common/src/main/java/me/lucko/luckperms/users/UserManager.java b/common/src/main/java/me/lucko/luckperms/users/UserManager.java index 9ea369661..006779a12 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -57,7 +57,7 @@ public abstract class UserManager { * @param user The user to update or set */ public void updateOrSetUser(User user) { - if (!users.containsKey(user.getUuid())) { + if (!isLoaded(user.getUuid())) { // The user isn't already loaded, so we can just add users.put(user.getUuid(), user); // They're probably not online, but in case they are... @@ -65,6 +65,7 @@ public abstract class UserManager { } else { // Override the user's current loaded nodes, and force a refresh users.get(user.getUuid()).setNodes(user.getNodes()); + users.get(user.getUuid()).setPrimaryGroup(user.getPrimaryGroup()); users.get(user.getUuid()).refreshPermissions(); } }