From d4c335e33edb2eeaec8bed5318d4b94f434608af Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 28 Jun 2016 17:21:41 +0100 Subject: [PATCH] Fix clear command not resetting primary group --- .../luckperms/users/BukkitUserManager.java | 1 + .../luckperms/users/BungeeUserManager.java | 1 + .../user/subcommands/UserClearCommand.java | 2 +- .../data/methods/FlatfileDatastore.java | 7 +----- .../luckperms/data/methods/SQLDatastore.java | 8 +----- .../me/lucko/luckperms/users/UserManager.java | 25 +++++++++++++++++-- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java index 4c693679c..c65740513 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java @@ -10,6 +10,7 @@ public class BukkitUserManager extends UserManager { private final LPBukkitPlugin plugin; public BukkitUserManager(LPBukkitPlugin plugin) { + super(plugin); this.plugin = plugin; } diff --git a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java index 97b23950d..c8ba620a1 100644 --- a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java +++ b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java @@ -9,6 +9,7 @@ public class BungeeUserManager extends UserManager { private final LPBungeePlugin plugin; public BungeeUserManager(LPBungeePlugin plugin) { + super(plugin); this.plugin = plugin; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java index f657bedf3..b1b96fa8b 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java @@ -17,7 +17,7 @@ public class UserClearCommand extends UserSubCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List args) { user.clearNodes(); - user.getNodes().put(plugin.getConfiguration().getDefaultGroupNode(), true); + plugin.getUserManager().giveDefaults(user); Util.sendPluginMessage(sender, "&b" + user.getName() + "&a's permissions were cleared."); saveUser(user, sender, plugin); 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 078e90acb..6476662ad 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 @@ -4,7 +4,6 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.data.Datastore; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.users.User; @@ -130,11 +129,7 @@ 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()); + plugin.getUserManager().giveDefaults(user); boolean success = doWrite(userFile, writer -> { writer.beginObject(); 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 ad6130291..bc8486f29 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 @@ -6,7 +6,6 @@ import lombok.AllArgsConstructor; import lombok.Getter; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.data.Datastore; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.users.User; @@ -165,12 +164,7 @@ 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()); + plugin.getUserManager().giveDefaults(user); success = runQuery(new QueryPS(USER_INSERT) { @Override 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 006779a12..39a76d4f3 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -1,14 +1,14 @@ package me.lucko.luckperms.users; import lombok.Getter; -import lombok.NoArgsConstructor; +import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.data.Datastore; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -@NoArgsConstructor public abstract class UserManager { /** @@ -17,6 +17,15 @@ public abstract class UserManager { @Getter private final Map users = new ConcurrentHashMap<>(); + /** + * Reference to main plugin instance + */ + private final LuckPermsPlugin plugin; + + public UserManager(LuckPermsPlugin plugin) { + this.plugin = plugin; + } + /** * Get a user object by UUID * @param uuid The uuid to search by @@ -80,6 +89,18 @@ public abstract class UserManager { datastore.saveUser(user, success -> {}); } + /** + * Set a user to the default group + * @param user the user to give to + */ + public void giveDefaults(User user) { + // Setup the new user with default values + try { + user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); + } catch (ObjectAlreadyHasException ignored) {} + user.setPrimaryGroup(plugin.getConfiguration().getDefaultGroupName()); + } + /** * Check to see if a user is loaded or not * @param uuid the UUID of the user