From 18dd59ed0fd394e5ba973933d269ffe98d8e5013 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 25 Nov 2016 20:59:28 +0000 Subject: [PATCH] Refactor Sponge subject handling --- .../migration/MigrationBPermissions.java | 2 +- .../migration/MigrationGroupManager.java | 4 +- .../migration/MigrationPermissionsEx.java | 2 +- .../migration/MigrationPowerfulPerms.java | 2 +- .../migration/MigrationZPermissions.java | 4 +- .../luckperms/bukkit/vault/VaultChatHook.java | 48 ++-- .../bukkit/vault/VaultPermissionHook.java | 26 +- .../migration/MigrationBungeePerms.java | 2 +- .../luckperms/common/api/ApiProvider.java | 6 +- .../commands/generic/parent/ParentAdd.java | 2 +- .../generic/parent/ParentAddTemp.java | 2 +- .../commands/generic/parent/ParentSet.java | 2 +- .../common/commands/group/DeleteGroup.java | 2 +- .../common/commands/group/GroupClone.java | 2 +- .../commands/group/GroupMainCommand.java | 2 +- .../common/commands/group/GroupRename.java | 2 +- .../common/commands/track/DeleteTrack.java | 2 +- .../common/commands/track/TrackAppend.java | 2 +- .../common/commands/track/TrackClone.java | 2 +- .../common/commands/track/TrackInsert.java | 2 +- .../commands/track/TrackMainCommand.java | 2 +- .../common/commands/track/TrackRename.java | 2 +- .../common/commands/user/UserDemote.java | 4 +- .../common/commands/user/UserPromote.java | 4 +- .../commands/user/UserSwitchPrimaryGroup.java | 2 +- .../luckperms/common/core/model/Group.java | 2 +- .../common/core/model/PermissionHolder.java | 4 +- .../luckperms/common/core/model/Track.java | 2 +- .../common/managers/AbstractManager.java | 21 +- .../luckperms/common/managers/Manager.java | 4 +- .../common/managers/UserManager.java | 9 +- .../managers/impl/GenericUserManager.java | 73 +++--- .../common/storage/backing/JSONBacking.java | 5 +- .../storage/backing/MongoDBBacking.java | 5 +- .../common/storage/backing/SQLBacking.java | 5 +- .../common/storage/backing/YAMLBacking.java | 5 +- .../luckperms/common/utils/Identifiable.java | 9 + .../luckperms/sponge/LPSpongePlugin.java | 14 +- .../luckperms/sponge/SpongeListener.java | 1 - .../SpongeGroupManager.java} | 131 +++++++--- .../sponge/managers/SpongeUserManager.java | 245 ++++++++++++++++++ .../migration/MigrationPermissionManager.java | 2 +- .../migration/MigrationPermissionsEx.java | 2 +- .../luckperms/sponge/model/SpongeGroup.java | 216 +++++++++++++++ .../luckperms/sponge/model/SpongeUser.java | 188 ++++++++++++++ .../sponge/service/LuckPermsGroupSubject.java | 208 --------------- .../sponge/service/LuckPermsService.java | 25 +- .../sponge/service/LuckPermsSubjectData.java | 17 +- .../sponge/service/LuckPermsUserSubject.java | 172 ------------ .../service/collections/UserCollection.java | 163 ------------ 50 files changed, 919 insertions(+), 741 deletions(-) rename sponge/src/main/java/me/lucko/luckperms/sponge/{service/collections/GroupCollection.java => managers/SpongeGroupManager.java} (51%) create mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java create mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java create mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java delete mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java delete mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java delete mode 100644 sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/UserCollection.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java index 1283c1f0e..554efa297 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java @@ -124,7 +124,7 @@ public class MigrationBPermissions extends SubCommand { // Make a LuckPerms group for the one being migrated. plugin.getStorage().createAndLoadGroup(groupName).join(); - me.lucko.luckperms.common.core.model.Group lpGroup = plugin.getGroupManager().get(groupName); + me.lucko.luckperms.common.core.model.Group lpGroup = plugin.getGroupManager().getIfLoaded(groupName); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java index b75f90177..aeddc9584 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java @@ -73,7 +73,7 @@ public class MigrationGroupManager extends SubCommand { for (Group g : gg.getGroupList()) { plugin.getStorage().createAndLoadGroup(g.getName().toLowerCase()).join(); - me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().get(g.getName().toLowerCase()); + me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(g.getName().toLowerCase()); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) @@ -196,7 +196,7 @@ public class MigrationGroupManager extends SubCommand { for (Map.Entry, Boolean>> e : groups.entrySet()) { plugin.getStorage().createAndLoadGroup(e.getKey()).join(); - me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().get(e.getKey()); + me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(e.getKey()); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java index ae979a68d..02644e0c9 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java @@ -101,7 +101,7 @@ public class MigrationPermissionsEx extends SubCommand { final String name = group.getName().toLowerCase(); plugin.getStorage().createAndLoadGroup(name).join(); - Group lpGroup = plugin.getGroupManager().get(name); + Group lpGroup = plugin.getGroupManager().getIfLoaded(name); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java index 9873c90c5..eb014a325 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java @@ -252,7 +252,7 @@ public class MigrationPowerfulPerms extends SubCommand { Map groups = pm.getGroups(); // All versions for (Group g : groups.values()) { plugin.getStorage().createAndLoadGroup(g.getName().toLowerCase()).join(); - final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().get(g.getName().toLowerCase()); + final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(g.getName().toLowerCase()); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java index f63a544c5..2c7b85da7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java @@ -81,7 +81,7 @@ public class MigrationZPermissions extends SubCommand { log.info("zPermissions Migration: Starting group migration."); for (String g : service.getAllGroups()) { plugin.getStorage().createAndLoadGroup(g.toLowerCase()).join(); - Group group = plugin.getGroupManager().get(g.toLowerCase()); + Group group = plugin.getGroupManager().getIfLoaded(g.toLowerCase()); PermissionEntity entity = internalService.getEntity(g, null, true); migrateEntity(group, entity); @@ -93,7 +93,7 @@ public class MigrationZPermissions extends SubCommand { log.info("zPermissions Migration: Starting track migration."); for (String t : service.getAllTracks()) { plugin.getStorage().createAndLoadTrack(t.toLowerCase()).join(); - Track track = plugin.getTrackManager().get(t.toLowerCase()); + Track track = plugin.getTrackManager().getIfLoaded(t.toLowerCase()); track.setGroups(service.getTrackGroups(t)); plugin.getStorage().saveTrack(track); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java index f18ac84fa..a7598c8e8 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java @@ -244,47 +244,47 @@ public class VaultChatHook extends Chat { } public String getPlayerPrefix(String world, @NonNull String player) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); return getUserChatMeta(true, user, world); } public void setPlayerPrefix(String world, @NonNull String player, @NonNull String prefix) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setChatMeta(true, user, prefix, world); } public String getPlayerSuffix(String world, @NonNull String player) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); return getUserChatMeta(false, user, world); } public void setPlayerSuffix(String world, @NonNull String player, @NonNull String suffix) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setChatMeta(false, user, suffix, world); } public String getGroupPrefix(String world, @NonNull String group) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); return getGroupChatMeta(false, g, world); } public void setGroupPrefix(String world, @NonNull String group, @NonNull String prefix) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setChatMeta(true, g, prefix, world); } public String getGroupSuffix(String world, @NonNull String group) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); return getGroupChatMeta(false, g, world); } public void setGroupSuffix(String world, @NonNull String group, @NonNull String suffix) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setChatMeta(false, g, suffix, world); } public int getPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int defaultValue) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); try { return Integer.parseInt(getUserMeta(user, world, node, String.valueOf(defaultValue))); } catch (NumberFormatException e) { @@ -293,12 +293,12 @@ public class VaultChatHook extends Chat { } public void setPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int value) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setMeta(user, world, node, String.valueOf(value)); } public int getGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int defaultValue) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); try { return Integer.parseInt(getGroupMeta(g, world, node, String.valueOf(defaultValue))); } catch (NumberFormatException e) { @@ -307,12 +307,12 @@ public class VaultChatHook extends Chat { } public void setGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int value) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setMeta(g, world, node, String.valueOf(value)); } public double getPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double defaultValue) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); try { return Double.parseDouble(getUserMeta(user, world, node, String.valueOf(defaultValue))); } catch (NumberFormatException e) { @@ -321,12 +321,12 @@ public class VaultChatHook extends Chat { } public void setPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double value) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setMeta(user, world, node, String.valueOf(value)); } public double getGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double defaultValue) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); try { return Double.parseDouble(getGroupMeta(g, world, node, String.valueOf(defaultValue))); } catch (NumberFormatException e) { @@ -335,12 +335,12 @@ public class VaultChatHook extends Chat { } public void setGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double value) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setMeta(g, world, node, String.valueOf(value)); } public boolean getPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean defaultValue) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); String s = getUserMeta(user, world, node, String.valueOf(defaultValue)); if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) { return defaultValue; @@ -349,12 +349,12 @@ public class VaultChatHook extends Chat { } public void setPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean value) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setMeta(user, world, node, String.valueOf(value)); } public boolean getGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean defaultValue) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); String s = getGroupMeta(g, world, node, String.valueOf(defaultValue)); if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) { return defaultValue; @@ -363,27 +363,27 @@ public class VaultChatHook extends Chat { } public void setGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean value) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setMeta(g, world, node, String.valueOf(value)); } public String getPlayerInfoString(String world, @NonNull String player, @NonNull String node, String defaultValue) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); return getUserMeta(user, world, node, defaultValue); } public void setPlayerInfoString(String world, @NonNull String player, @NonNull String node, String value) { - final User user = perms.getPlugin().getUserManager().get(player); + final User user = perms.getPlugin().getUserManager().getByUsername(player); setMeta(user, world, node, value); } public String getGroupInfoString(String world, @NonNull String group, @NonNull String node, String defaultValue) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); return getGroupMeta(g, world, node, defaultValue); } public void setGroupInfoString(String world, @NonNull String group, @NonNull String node, String value) { - final Group g = perms.getPlugin().getGroupManager().get(group); + final Group g = perms.getPlugin().getGroupManager().getIfLoaded(group); setMeta(g, world, node, value); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java index dbfebcdf2..4f8468217 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java @@ -155,7 +155,7 @@ public class VaultPermissionHook extends Permission { world = ignoreWorld ? null : world; // Correct world value log("Checking if player " + player + " has permission: " + permission + " on world " + world + ", server " + server); - User user = plugin.getUserManager().get(player); + User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; if (user.getUserData() == null) { @@ -171,7 +171,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Adding permission to player " + player + ": '" + permission + "' on world " + finalWorld + ", server " + server); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; scheduler.scheduleTask(() -> add(finalWorld, user, permission)); @@ -183,7 +183,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Removing permission from player " + player + ": '" + permission + "' on world " + finalWorld + ", server " + server); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; scheduler.scheduleTask(() -> remove(finalWorld, user, permission)); @@ -195,7 +195,7 @@ public class VaultPermissionHook extends Permission { world = ignoreWorld ? null : world; // Correct world value log("Checking if group " + groupName + " has permission: " + permission + " on world " + world + ", server " + server); - final Group group = plugin.getGroupManager().get(groupName); + final Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) return false; // This is a nasty call. Groups aren't cached. :( @@ -209,7 +209,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Adding permission to group " + groupName + ": '" + permission + "' on world " + finalWorld + ", server " + server); - final Group group = plugin.getGroupManager().get(groupName); + final Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) return false; scheduler.scheduleTask(() -> add(finalWorld, group, permission)); @@ -221,7 +221,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Removing permission from group " + groupName + ": '" + permission + "' on world " + finalWorld + ", server " + server); - final Group group = plugin.getGroupManager().get(groupName); + final Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) return false; scheduler.scheduleTask(() -> remove(finalWorld, group, permission)); @@ -233,7 +233,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Checking if player " + player + " is in group: " + group + " on world " + finalWorld + ", server " + server); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; return user.getNodes().stream() @@ -250,10 +250,10 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Adding player " + player + " to group: '" + groupName + "' on world " + finalWorld + ", server " + server); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; - final Group group = plugin.getGroupManager().get(groupName); + final Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) return false; scheduler.scheduleTask(() -> { @@ -274,10 +274,10 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Removing player " + player + " from group: '" + groupName + "' on world " + finalWorld + ", server " + server); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) return false; - final Group group = plugin.getGroupManager().get(groupName); + final Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) return false; scheduler.scheduleTask(() -> { @@ -298,7 +298,7 @@ public class VaultPermissionHook extends Permission { String finalWorld = ignoreWorld ? null : world; // Correct world value log("Getting groups of player: " + player + ", on world " + finalWorld + ", server " + server); - User user = plugin.getUserManager().get(player); + User user = plugin.getUserManager().getByUsername(player); if (user == null) return new String[0]; return user.getNodes().stream() @@ -313,7 +313,7 @@ public class VaultPermissionHook extends Permission { public String getPrimaryGroup(String world, @NonNull String player) { world = ignoreWorld ? null : world; // Correct world value log("Getting primary group of player: " + player); - final User user = plugin.getUserManager().get(player); + final User user = plugin.getUserManager().getByUsername(player); if (user == null) { return null; diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java index 2bcb7cb16..03374e254 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java @@ -65,7 +65,7 @@ public class MigrationBungeePerms extends SubCommand { // Make a LuckPerms group for the one being migrated plugin.getStorage().createAndLoadGroup(g.getName().toLowerCase()).join(); - me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().get(g.getName().toLowerCase()); + me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(g.getName().toLowerCase()); try { LogEntry.build() .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) diff --git a/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java b/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java index ad50d5691..988f6bf9a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java @@ -138,7 +138,7 @@ public class ApiProvider implements LuckPermsApi { @Override public User getUser(@NonNull String name) { - final me.lucko.luckperms.common.core.model.User user = plugin.getUserManager().get(name); + final me.lucko.luckperms.common.core.model.User user = plugin.getUserManager().getByUsername(name); return user == null ? null : new UserLink(user); } @@ -165,7 +165,7 @@ public class ApiProvider implements LuckPermsApi { @Override public Group getGroup(@NonNull String name) { - final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().get(name); + final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(name); return group == null ? null : new GroupLink(group); } @@ -186,7 +186,7 @@ public class ApiProvider implements LuckPermsApi { @Override public Track getTrack(@NonNull String name) { - final me.lucko.luckperms.common.core.model.Track track = plugin.getTrackManager().get(name); + final me.lucko.luckperms.common.core.model.Track track = plugin.getTrackManager().getIfLoaded(name); return track == null ? null : new TrackLink(track); } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java index 9e41c2171..fec33a2f5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java @@ -66,7 +66,7 @@ public class ParentAdd extends SharedSubCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java index 6df3b398f..d17444917 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java @@ -69,7 +69,7 @@ public class ParentAddTemp extends SharedSubCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.INVALID_ARGS; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java index 15369fa2a..9ede9eb83 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java @@ -67,7 +67,7 @@ public class ParentSet extends SharedSubCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/DeleteGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/DeleteGroup.java index 4a96ae986..32c7d08a4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/DeleteGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/DeleteGroup.java @@ -66,7 +66,7 @@ public class DeleteGroup extends SingleCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupClone.java index 2618f8fb6..0b9b6e714 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupClone.java @@ -62,7 +62,7 @@ public class GroupClone extends SubCommand { return CommandResult.FAILURE; } - Group newGroup = plugin.getGroupManager().get(newGroupName); + Group newGroup = plugin.getGroupManager().getIfLoaded(newGroupName); if (newGroup == null) { Message.GROUP_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupMainCommand.java index 6e8754d85..974ad4b82 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupMainCommand.java @@ -60,7 +60,7 @@ public class GroupMainCommand extends MainCommand { return null; } - Group group = plugin.getGroupManager().get(target); + Group group = plugin.getGroupManager().getIfLoaded(target); if (group == null) { Message.GROUP_NOT_FOUND.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java index 70a448537..f4f6bbc4f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java @@ -62,7 +62,7 @@ public class GroupRename extends SubCommand { return CommandResult.FAILURE; } - Group newGroup = plugin.getGroupManager().get(newGroupName); + Group newGroup = plugin.getGroupManager().getIfLoaded(newGroupName); if (newGroup == null) { Message.GROUP_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/DeleteTrack.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/DeleteTrack.java index 4b3147f73..0aaef62d0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/DeleteTrack.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/DeleteTrack.java @@ -60,7 +60,7 @@ public class DeleteTrack extends SingleCommand { return CommandResult.INVALID_ARGS; } - Track track = plugin.getTrackManager().get(trackName); + Track track = plugin.getTrackManager().getIfLoaded(trackName); if (track == null) { Message.TRACK_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackAppend.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackAppend.java index bed8ca976..4590d3898 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackAppend.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackAppend.java @@ -61,7 +61,7 @@ public class TrackAppend extends SubCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackClone.java index 32320c250..b2ef11e87 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackClone.java @@ -62,7 +62,7 @@ public class TrackClone extends SubCommand { return CommandResult.FAILURE; } - Track newTrack = plugin.getTrackManager().get(newTrackName); + Track newTrack = plugin.getTrackManager().getIfLoaded(newTrackName); if (newTrack == null) { Message.TRACK_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackInsert.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackInsert.java index 413a83cdc..f7dce66ba 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackInsert.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackInsert.java @@ -72,7 +72,7 @@ public class TrackInsert extends SubCommand { return CommandResult.INVALID_ARGS; } - Group group = plugin.getGroupManager().get(groupName); + Group group = plugin.getGroupManager().getIfLoaded(groupName); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackMainCommand.java index 974967eff..ffede87bf 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackMainCommand.java @@ -54,7 +54,7 @@ public class TrackMainCommand extends MainCommand { return null; } - Track track = plugin.getTrackManager().get(target); + Track track = plugin.getTrackManager().getIfLoaded(target); if (track == null) { Message.TRACK_NOT_FOUND.send(sender); return null; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackRename.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackRename.java index de393de50..0501a22ea 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackRename.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/TrackRename.java @@ -62,7 +62,7 @@ public class TrackRename extends SubCommand { return CommandResult.FAILURE; } - Track newTrack = plugin.getTrackManager().get(newTrackName); + Track newTrack = plugin.getTrackManager().getIfLoaded(newTrackName); if (newTrack == null) { Message.TRACK_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java index f8d140402..9b03d013e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java @@ -77,7 +77,7 @@ public class UserDemote extends SubCommand { return CommandResult.INVALID_ARGS; } - Track track = plugin.getTrackManager().get(trackName); + Track track = plugin.getTrackManager().getIfLoaded(trackName); if (track == null) { Message.TRACK_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; @@ -152,7 +152,7 @@ public class UserDemote extends SubCommand { return CommandResult.STATE_ERROR; } - Group previousGroup = plugin.getGroupManager().get(previous); + Group previousGroup = plugin.getGroupManager().getIfLoaded(previous); if (previousGroup == null) { Message.USER_DEMOTE_ERROR_MALFORMED.send(sender, previous); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java index 096985d74..f57a02e83 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java @@ -77,7 +77,7 @@ public class UserPromote extends SubCommand { return CommandResult.INVALID_ARGS; } - Track track = plugin.getTrackManager().get(trackName); + Track track = plugin.getTrackManager().getIfLoaded(trackName); if (track == null) { Message.TRACK_DOES_NOT_EXIST.send(sender); return CommandResult.LOADING_ERROR; @@ -152,7 +152,7 @@ public class UserPromote extends SubCommand { return CommandResult.STATE_ERROR; } - Group nextGroup = plugin.getGroupManager().get(next); + Group nextGroup = plugin.getGroupManager().getIfLoaded(next); if (nextGroup == null) { Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserSwitchPrimaryGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserSwitchPrimaryGroup.java index d11f02981..826cc41d4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserSwitchPrimaryGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserSwitchPrimaryGroup.java @@ -47,7 +47,7 @@ public class UserSwitchPrimaryGroup extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { - Group group = plugin.getGroupManager().get(args.get(0).toLowerCase()); + Group group = plugin.getGroupManager().getIfLoaded(args.get(0).toLowerCase()); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); return CommandResult.INVALID_ARGS; diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/Group.java b/common/src/main/java/me/lucko/luckperms/common/core/model/Group.java index 3ed8e83ad..cfdbbb63b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/Group.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/Group.java @@ -45,7 +45,7 @@ public class Group extends PermissionHolder implements Identifiable { @Override public String getId() { - return name; + return name.toLowerCase(); } public String getRawDisplayName() { diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java index 4fb3251cb..94710f2d5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java @@ -285,7 +285,7 @@ public abstract class PermissionHolder { for (Map.Entry e : sortedParents) { Node parent = e.getValue(); - Group group = plugin.getGroupManager().get(parent.getGroupName()); + Group group = plugin.getGroupManager().getIfLoaded(parent.getGroupName()); if (group == null) { continue; } @@ -361,7 +361,7 @@ public abstract class PermissionHolder { for (Map.Entry e : sortedParents) { Node parent = e.getValue(); - Group group = plugin.getGroupManager().get(parent.getGroupName()); + Group group = plugin.getGroupManager().getIfLoaded(parent.getGroupName()); if (group == null) { continue; } diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/Track.java b/common/src/main/java/me/lucko/luckperms/common/core/model/Track.java index 1026adc3f..d0a82610b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/Track.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/Track.java @@ -58,7 +58,7 @@ public class Track implements Identifiable { @Override public String getId() { - return name; + return name.toLowerCase(); } /** diff --git a/common/src/main/java/me/lucko/luckperms/common/managers/AbstractManager.java b/common/src/main/java/me/lucko/luckperms/common/managers/AbstractManager.java index a13c8c8ec..fe0a89733 100644 --- a/common/src/main/java/me/lucko/luckperms/common/managers/AbstractManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/managers/AbstractManager.java @@ -59,17 +59,30 @@ public abstract class AbstractManager> implements M @Override public T getOrMake(I id) { - return objects.getUnchecked(id); + if (id instanceof String) { + return objects.getUnchecked((I) ((String) id).toLowerCase()); + } else { + return objects.getUnchecked(id); + } + } @Override - public T get(I id) { - return objects.getIfPresent(id); + public T getIfLoaded(I id) { + if (id instanceof String) { + return objects.getIfPresent(((String) id).toLowerCase()); + } else { + return objects.getIfPresent(id); + } } @Override public boolean isLoaded(I id) { - return objects.asMap().containsKey(id); + if (id instanceof String) { + return objects.asMap().containsKey(((String) id).toLowerCase()); + } else { + return objects.asMap().containsKey(id); + } } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/managers/Manager.java b/common/src/main/java/me/lucko/luckperms/common/managers/Manager.java index fff83b50c..bbb7f79fd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/managers/Manager.java +++ b/common/src/main/java/me/lucko/luckperms/common/managers/Manager.java @@ -38,7 +38,7 @@ public interface Manager> extends Function { * Gets a map containing all cached instances held by this manager. * @return all instances held in this manager */ - Map getAll(); + Map getAll(); /** * Gets or creates an object by id @@ -52,7 +52,7 @@ public interface Manager> extends Function { * @param id The id to search by * @return a {@link T} object if the object is loaded, returns null if the object is not loaded */ - T get(I id); + T getIfLoaded(I id); /** * Check to see if a object is loaded or not diff --git a/common/src/main/java/me/lucko/luckperms/common/managers/UserManager.java b/common/src/main/java/me/lucko/luckperms/common/managers/UserManager.java index aeb487831..487d0a713 100644 --- a/common/src/main/java/me/lucko/luckperms/common/managers/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/managers/UserManager.java @@ -35,7 +35,7 @@ public interface UserManager extends Manager { * @param name The name to search by * @return a {@link User} object if the user is loaded, returns null if the user is not loaded */ - User get(String name); + User getByUsername(String name); /** * Get a user object by uuid @@ -50,13 +50,6 @@ public interface UserManager extends Manager { */ boolean giveDefaultIfNeeded(User user, boolean save); - /** - * Check whether the user's state indicates that they should be persisted to storage. - * @param user the user to check - * @return true if the user should be saved - */ - boolean shouldSave(User user); - /** * Checks to see if the user is online, and if they are not, runs {@link #unload(Identifiable)} * @param user The user to be cleaned up diff --git a/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java b/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java index 224f84ef4..8e6b67b20 100644 --- a/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java @@ -29,7 +29,6 @@ import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.managers.AbstractManager; import me.lucko.luckperms.common.managers.UserManager; -import me.lucko.luckperms.common.utils.Identifiable; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.Set; @@ -46,12 +45,8 @@ public class GenericUserManager extends AbstractManager im new User(id.getUuid(), id.getUsername(), plugin); } - /** - * Get a user object by name - * @param name The name to search by - * @return a {@link User} object if the user is loaded, returns null if the user is not loaded - */ - public User get(String name) { + @Override + public User getByUsername(String name) { for (User user : getAll().values()) { if (user.getName().equalsIgnoreCase(name)) { return user; @@ -60,15 +55,37 @@ public class GenericUserManager extends AbstractManager im return null; } + @Override public User get(UUID uuid) { - return get(UserIdentifier.of(uuid, null)); + return getIfLoaded(UserIdentifier.of(uuid, null)); } - /** - * Set a user to the default group - * @param user the user to give to - */ + @Override public boolean giveDefaultIfNeeded(User user, boolean save) { + return giveDefaultIfNeeded(user, save, plugin); + } + + @Override + public void cleanup(User user) { + if (!plugin.isOnline(plugin.getUuidCache().getExternalUUID(user.getUuid()))) { + unload(user); + } + } + + @Override + public void updateAllUsers() { + plugin.doSync(() -> { + Set players = plugin.getOnlinePlayers(); + plugin.doAsync(() -> { + for (UUID uuid : players) { + UUID internal = plugin.getUuidCache().getUUID(uuid); + plugin.getStorage().loadUser(internal, "null").join(); + } + }); + }); + } + + public static boolean giveDefaultIfNeeded(User user, boolean save, LuckPermsPlugin plugin) { boolean hasGroup = false; if (user.getPrimaryGroup() != null && !user.getPrimaryGroup().isEmpty()) { @@ -96,7 +113,12 @@ public class GenericUserManager extends AbstractManager im return true; } - public boolean shouldSave(User user) { + /** + * Check whether the user's state indicates that they should be persisted to storage. + * @param user the user to check + * @return true if the user should be saved + */ + public static boolean shouldSave(User user) { if (user.getNodes().size() != 1) { return true; } @@ -123,29 +145,4 @@ public class GenericUserManager extends AbstractManager im return false; } - - /** - * Checks to see if the user is online, and if they are not, runs {@link #unload(Identifiable)} - * @param user The user to be cleaned up - */ - public void cleanup(User user) { - if (!plugin.isOnline(plugin.getUuidCache().getExternalUUID(user.getUuid()))) { - unload(user); - } - } - - /** - * Reloads the data of all online users - */ - public void updateAllUsers() { - plugin.doSync(() -> { - Set players = plugin.getOnlinePlayers(); - plugin.doAsync(() -> { - for (UUID uuid : players) { - UUID internal = plugin.getUuidCache().getUUID(uuid); - plugin.getStorage().loadUser(internal, "null").join(); - } - }); - }); - } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java index 1ba8c3a4b..7ff6a849a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java @@ -32,6 +32,7 @@ import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; +import me.lucko.luckperms.common.managers.impl.GenericUserManager; import java.io.*; import java.util.*; @@ -131,7 +132,7 @@ public class JSONBacking extends FlatfileBacking { return true; }); } else { - if (plugin.getUserManager().shouldSave(user)) { + if (GenericUserManager.shouldSave(user)) { user.clearNodes(); user.setPrimaryGroup(null); plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -151,7 +152,7 @@ public class JSONBacking extends FlatfileBacking { try { return call(() -> { File userFile = new File(usersDir, user.getUuid().toString() + ".json"); - if (!plugin.getUserManager().shouldSave(user)) { + if (!GenericUserManager.shouldSave(user)) { if (userFile.exists()) { userFile.delete(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java index 265895be1..c57e94185 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java @@ -38,6 +38,7 @@ import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.data.Log; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; +import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.storage.DatastoreConfiguration; import org.bson.Document; @@ -167,7 +168,7 @@ public class MongoDBBacking extends AbstractBacking { c.replaceOne(new Document("_id", user.getUuid()), fromUser(user)); } } else { - if (plugin.getUserManager().shouldSave(user)) { + if (GenericUserManager.shouldSave(user)) { user.clearNodes(); user.setPrimaryGroup(null); plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -184,7 +185,7 @@ public class MongoDBBacking extends AbstractBacking { @Override public boolean saveUser(User user) { - if (!plugin.getUserManager().shouldSave(user)) { + if (!GenericUserManager.shouldSave(user)) { user.getIoLock().lock(); try { return call(() -> { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java index 9e3b4ae1c..dc4913fc2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java @@ -33,6 +33,7 @@ import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.data.Log; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; +import me.lucko.luckperms.common.managers.impl.GenericUserManager; import java.lang.reflect.Type; import java.sql.Connection; @@ -205,7 +206,7 @@ abstract class SQLBacking extends AbstractBacking { } } else { - if (plugin.getUserManager().shouldSave(user)) { + if (GenericUserManager.shouldSave(user)) { user.clearNodes(); user.setPrimaryGroup(null); plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -221,7 +222,7 @@ abstract class SQLBacking extends AbstractBacking { @Override public boolean saveUser(User user) { - if (!plugin.getUserManager().shouldSave(user)) { + if (!GenericUserManager.shouldSave(user)) { user.getIoLock().lock(); try { return runQuery(USER_DELETE, preparedStatement -> { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index 35eceefa5..ae46178a6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -30,6 +30,7 @@ import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; +import me.lucko.luckperms.common.managers.impl.GenericUserManager; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -113,7 +114,7 @@ public class YAMLBacking extends FlatfileBacking { return true; }); } else { - if (plugin.getUserManager().shouldSave(user)) { + if (GenericUserManager.shouldSave(user)) { user.clearNodes(); user.setPrimaryGroup(null); plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -133,7 +134,7 @@ public class YAMLBacking extends FlatfileBacking { try { return call(() -> { File userFile = new File(usersDir, user.getUuid().toString() + ".yml"); - if (!plugin.getUserManager().shouldSave(user)) { + if (!GenericUserManager.shouldSave(user)) { if (userFile.exists()) { userFile.delete(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/Identifiable.java b/common/src/main/java/me/lucko/luckperms/common/utils/Identifiable.java index bcb31dfc5..aa27c088c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/Identifiable.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/Identifiable.java @@ -22,8 +22,17 @@ package me.lucko.luckperms.common.utils; +/** + * Interface to represent an identifiable object + * @param + */ public interface Identifiable { + /** + * Returns the objects identifier. + * If the type is a {@link String}, this method must return a {@link String#toLowerCase()} representation + * @return the identifier + */ T getId(); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 9fa27738c..8dbcc95a3 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -41,12 +41,8 @@ import me.lucko.luckperms.common.contexts.ServerCalculator; import me.lucko.luckperms.common.core.UuidCache; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.data.Importer; -import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; -import me.lucko.luckperms.common.managers.UserManager; -import me.lucko.luckperms.common.managers.impl.GenericGroupManager; import me.lucko.luckperms.common.managers.impl.GenericTrackManager; -import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.StorageFactory; @@ -55,6 +51,8 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.utils.*; import me.lucko.luckperms.sponge.commands.SpongeMainCommand; import me.lucko.luckperms.sponge.contexts.WorldCalculator; +import me.lucko.luckperms.sponge.managers.SpongeGroupManager; +import me.lucko.luckperms.sponge.managers.SpongeUserManager; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.timings.LPTimings; import me.lucko.luckperms.sponge.utils.VersionData; @@ -119,8 +117,8 @@ public class LPSpongePlugin implements LuckPermsPlugin { private final Set ignoringLogs = ConcurrentHashMap.newKeySet(); private LPConfiguration configuration; - private UserManager userManager; - private GroupManager groupManager; + private SpongeUserManager userManager; + private SpongeGroupManager groupManager; private TrackManager trackManager; private Storage storage; private RedisMessaging redisMessaging = null; @@ -199,8 +197,8 @@ public class LPSpongePlugin implements LuckPermsPlugin { // load internal managers getLog().info("Loading internal permission managers..."); uuidCache = new UuidCache(getConfiguration().isOnlineMode()); - userManager = new GenericUserManager(this); - groupManager = new GenericGroupManager(this); + userManager = new SpongeUserManager(this); + groupManager = new SpongeGroupManager(this); trackManager = new GenericTrackManager(); importer = new Importer(commandManager); consecutiveExecutor = new ConsecutiveExecutor(commandManager); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeListener.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeListener.java index 588262422..84d4f765f 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeListener.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeListener.java @@ -110,7 +110,6 @@ public class SpongeListener extends AbstractListener { public void onClientLeave(ClientConnectionEvent.Disconnect e) { try (Timing ignored = plugin.getTimings().time(LPTiming.ON_CLIENT_LEAVE)) { onLeave(e.getTargetEntity().getUniqueId()); - plugin.getService().getUserSubjects().unload(plugin.getUuidCache().getUUID(e.getTargetEntity().getUniqueId())); } } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/GroupCollection.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java similarity index 51% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/GroupCollection.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java index 13911d486..7e9ac74ef 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/GroupCollection.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java @@ -20,22 +20,23 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.collections; +package me.lucko.luckperms.sponge.managers; import co.aikar.timings.Timing; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.UncheckedExecutionException; import lombok.NonNull; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.LuckPermsGroupSubject; +import me.lucko.luckperms.sponge.LPSpongePlugin; +import me.lucko.luckperms.sponge.model.SpongeGroup; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.simple.SimpleCollection; import me.lucko.luckperms.sponge.timings.LPTiming; @@ -48,37 +49,74 @@ import org.spongepowered.api.util.Tristate; import java.util.Map; import java.util.Set; -import java.util.concurrent.ExecutionException; -public class GroupCollection implements SubjectCollection { - private final LuckPermsService service; - private final GroupManager manager; +public class SpongeGroupManager implements GroupManager, SubjectCollection { + private final LPSpongePlugin plugin; private final SimpleCollection fallback; - private final LoadingCache groups = CacheBuilder.newBuilder() - .build(new CacheLoader() { + private final LoadingCache objects = CacheBuilder.newBuilder() + .build(new CacheLoader() { @Override - public LuckPermsGroupSubject load(String id) throws Exception { - Group group = manager.get(id); - if (group == null) { - throw new IllegalStateException("User not loaded"); - } - - return LuckPermsGroupSubject.wrapGroup(group, service); + public SpongeGroup load(String i) { + return apply(i); } @Override - public ListenableFuture reload(String str, LuckPermsGroupSubject s) { - return Futures.immediateFuture(s); // Never needs to be refreshed. + public ListenableFuture reload(String i, SpongeGroup t) { + return Futures.immediateFuture(t); // Never needs to be refreshed. } }); - public GroupCollection(LuckPermsService service, GroupManager manager) { - this.service = service; - this.manager = manager; - this.fallback = new SimpleCollection(service, "fallback-groups"); + public SpongeGroupManager(LPSpongePlugin plugin) { + this.plugin = plugin; + this.fallback = plugin.getService().getFallbackGroupSubjects(); } + @Override + public SpongeGroup apply(String name) { + return new SpongeGroup(name, plugin); + } + + /* ------------------------------------------ + * Manager methods + * ------------------------------------------ */ + + @Override + public Map getAll() { + return ImmutableMap.copyOf(objects.asMap()); + } + + @Override + public SpongeGroup getOrMake(String id) { + return objects.getUnchecked(id); + } + + @Override + public SpongeGroup getIfLoaded(String id) { + return objects.getIfPresent(id); + } + + @Override + public boolean isLoaded(String id) { + return objects.asMap().containsKey(id); + } + + @Override + public void unload(Group t) { + if (t != null) { + objects.invalidate(t.getId()); + } + } + + @Override + public void unloadAll() { + objects.invalidateAll(); + } + + /* ------------------------------------------ + * SubjectCollection methods + * ------------------------------------------ */ + @Override public String getIdentifier() { return PermissionService.SUBJECTS_GROUP; @@ -86,27 +124,33 @@ public class GroupCollection implements SubjectCollection { @Override public Subject get(@NonNull String id) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_COLLECTION_GET)) { + // Special Sponge method. This call will actually load the group from the datastore if not already present. + + try (Timing ignored = plugin.getTimings().time(LPTiming.GROUP_COLLECTION_GET)) { id = id.toLowerCase(); if (ArgumentChecker.checkName(id)) { - service.getPlugin().getLog().warn("Couldn't get group subject for id: " + id + " (invalid name)"); + plugin.getLog().warn("Couldn't get group subject for id: " + id + " (invalid name)"); return fallback.get(id); // fallback to transient collection } - // check if the user is loaded in memory. hopefully this call is not on the main thread. :( - if (!manager.isLoaded(id)) { - service.getPlugin().getLog().warn("Group Subject '" + id + "' was requested, but is not loaded in memory. Loading it from storage now."); - long startTime = System.currentTimeMillis(); - service.getPlugin().getStorage().createAndLoadGroup(id).join(); - service.getPlugin().getLog().warn("Loading '" + id + "' took " + (System.currentTimeMillis() - startTime) + " ms."); - } + // check if the group is loaded in memory. + if (isLoaded(id)) { + return getIfLoaded(id).getSpongeData(); + } else { - try { - return groups.get(id); - } catch (ExecutionException | UncheckedExecutionException e) { - service.getPlugin().getLog().warn("Unable to get group subject '" + id + "' from memory."); - e.printStackTrace(); - return fallback.get(id); + // Group isn't already loaded. hopefully this call is not on the main thread. :( + plugin.getLog().warn("Group Subject '" + id + "' was requested, but is not loaded in memory. Loading it from storage now."); + long startTime = System.currentTimeMillis(); + plugin.getStorage().createAndLoadGroup(id).join(); + SpongeGroup group = getIfLoaded(id); + + if (group == null) { + plugin.getLog().severe("Error whilst loading group '" + id + "'."); + return fallback.get(id); + } + + plugin.getLog().warn("Loading '" + id + "' took " + (System.currentTimeMillis() - startTime) + " ms."); + return group.getSpongeData(); } } } @@ -114,29 +158,30 @@ public class GroupCollection implements SubjectCollection { @Override public boolean hasRegistered(@NonNull String id) { id = id.toLowerCase(); - return !ArgumentChecker.checkName(id) && (groups.asMap().containsKey(id) || manager.isLoaded(id)); + return !ArgumentChecker.checkName(id) && isLoaded(id); } @Override public Iterable getAllSubjects() { - return groups.asMap().values().stream().collect(ImmutableCollectors.toImmutableList()); + return objects.asMap().values().stream().map(SpongeGroup::getSpongeData).collect(ImmutableCollectors.toImmutableList()); } @Override - public Map getAllWithPermission(@NonNull String node) { - return getAllWithPermission(SubjectData.GLOBAL_CONTEXT, node); + public Map getAllWithPermission(@NonNull String id) { + return getAllWithPermission(SubjectData.GLOBAL_CONTEXT, id); } @Override public Map getAllWithPermission(@NonNull Set contexts, @NonNull String node) { ContextSet cs = LuckPermsService.convertContexts(contexts); - return groups.asMap().values().stream() + return objects.asMap().values().stream() + .map(SpongeGroup::getSpongeData) .filter(sub -> sub.getPermissionValue(cs, node) != Tristate.UNDEFINED) .collect(ImmutableCollectors.toImmutableMap(sub -> sub, sub -> sub.getPermissionValue(cs, node).asBoolean())); } @Override public Subject getDefaults() { - return service.getDefaultSubjects().get(getIdentifier()); + return plugin.getService().getDefaultSubjects().get(getIdentifier()); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java new file mode 100644 index 000000000..edb52e75a --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.sponge.managers; + +import co.aikar.timings.Timing; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import lombok.NonNull; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.core.UserIdentifier; +import me.lucko.luckperms.common.core.model.User; +import me.lucko.luckperms.common.managers.UserManager; +import me.lucko.luckperms.common.managers.impl.GenericUserManager; +import me.lucko.luckperms.common.utils.ImmutableCollectors; +import me.lucko.luckperms.sponge.LPSpongePlugin; +import me.lucko.luckperms.sponge.model.SpongeUser; +import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.simple.SimpleCollection; +import me.lucko.luckperms.sponge.timings.LPTiming; +import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.PermissionService; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.service.permission.SubjectData; +import org.spongepowered.api.util.Tristate; + +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +public class SpongeUserManager implements UserManager, SubjectCollection { + private final LPSpongePlugin plugin; + private final SimpleCollection fallback; + + private final LoadingCache objects = CacheBuilder.newBuilder() + .build(new CacheLoader() { + @Override + public SpongeUser load(UserIdentifier i) { + return apply(i); + } + + @Override + public ListenableFuture reload(UserIdentifier i, SpongeUser t) { + return Futures.immediateFuture(t); // Never needs to be refreshed. + } + }); + + public SpongeUserManager(LPSpongePlugin plugin) { + this.plugin = plugin; + this.fallback = plugin.getService().getFallbackUserSubjects(); + } + + @Override + public SpongeUser apply(UserIdentifier id) { + return id.getUsername() == null ? + new SpongeUser(id.getUuid(), plugin) : + new SpongeUser(id.getUuid(), id.getUsername(), plugin); + } + + /* ------------------------------------------ + * Manager methods + * ------------------------------------------ */ + + @Override + public Map getAll() { + return ImmutableMap.copyOf(objects.asMap()); + } + + @Override + public SpongeUser getOrMake(UserIdentifier id) { + return objects.getUnchecked(id); + } + + @Override + public SpongeUser getIfLoaded(UserIdentifier id) { + return objects.getIfPresent(id); + } + + @Override + public boolean isLoaded(UserIdentifier id) { + return objects.asMap().containsKey(id); + } + + @Override + public void unload(User t) { + // TODO override + if (t != null) { + objects.invalidate(t.getId()); + } + } + + @Override + public void unloadAll() { + objects.invalidateAll(); + } + + /* ------------------------------------------ + * UserManager methods + * ------------------------------------------ */ + + @Override + public SpongeUser getByUsername(String name) { + for (SpongeUser user : getAll().values()) { + if (user.getName().equalsIgnoreCase(name)) { + return user; + } + } + return null; + } + + @Override + public SpongeUser get(UUID uuid) { + return getIfLoaded(UserIdentifier.of(uuid, null)); + } + + @Override + public boolean giveDefaultIfNeeded(User user, boolean save) { + return GenericUserManager.giveDefaultIfNeeded(user, save, plugin); + } + + @Override + public void cleanup(User user) { + if (!plugin.isOnline(plugin.getUuidCache().getExternalUUID(user.getUuid()))) { + unload(user); + } + } + + @Override + public void updateAllUsers() { + plugin.doSync(() -> { + Set players = plugin.getOnlinePlayers(); + plugin.doAsync(() -> { + for (UUID uuid : players) { + UUID internal = plugin.getUuidCache().getUUID(uuid); + plugin.getStorage().loadUser(internal, "null").join(); + } + }); + }); + } + + /* ------------------------------------------ + * SubjectCollection methods + * ------------------------------------------ */ + + @Override + public String getIdentifier() { + return PermissionService.SUBJECTS_USER; + } + + @Override + public Subject get(@NonNull String id) { + // Special Sponge method. This call will actually load the user from the datastore if not already present. + + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_COLLECTION_GET)) { + UUID uuid = Util.parseUuid(id); + if (uuid == null) { + plugin.getLog().warn("Couldn't get user subject for id: " + id + " (not a uuid)"); + return fallback.get(id); // fallback to the transient collection + } + + UUID u = plugin.getUuidCache().getUUID(uuid); + + // check if the user is loaded in memory. + if (isLoaded(UserIdentifier.of(u, null))) { + return get(u).getSpongeData(); + } else { + + // User isn't already loaded. hopefully this call is not on the main thread. :( + plugin.getLog().warn("User Subject '" + u + "' was requested, but is not loaded in memory. Loading them from storage now."); + long startTime = System.currentTimeMillis(); + plugin.getStorage().loadUser(u, "null").join(); + SpongeUser user = get(u); + + if (user == null) { + plugin.getLog().severe("Error whilst loading user '" + u + "'."); + return fallback.get(u.toString()); + } + + user.setupData(false); + plugin.getLog().warn("Loading '" + u + "' took " + (System.currentTimeMillis() - startTime) + " ms."); + return user.getSpongeData(); + } + } + } + + @Override + public boolean hasRegistered(@NonNull String id) { + UUID uuid = Util.parseUuid(id); + if (uuid == null) { + return false; + } + + UUID internal = plugin.getUuidCache().getUUID(uuid); + return isLoaded(UserIdentifier.of(internal, null)); + } + + @Override + public Iterable getAllSubjects() { + return objects.asMap().values().stream().map(SpongeUser::getSpongeData).collect(ImmutableCollectors.toImmutableList()); + } + + @Override + public Map getAllWithPermission(@NonNull String id) { + return getAllWithPermission(SubjectData.GLOBAL_CONTEXT, id); + } + + @Override + public Map getAllWithPermission(@NonNull Set contexts, @NonNull String node) { + ContextSet cs = LuckPermsService.convertContexts(contexts); + return objects.asMap().values().stream() + .map(SpongeUser::getSpongeData) + .filter(sub -> sub.getPermissionValue(cs, node) != Tristate.UNDEFINED) + .collect(ImmutableCollectors.toImmutableMap(sub -> sub, sub -> sub.getPermissionValue(cs, node).asBoolean())); + } + + @Override + public Subject getDefaults() { + return plugin.getService().getDefaultSubjects().get(getIdentifier()); + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java index 4d4a2c4db..5f32f794a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java @@ -90,7 +90,7 @@ public class MigrationPermissionManager extends SubCommand { // Make a LuckPerms group for the one being migrated plugin.getStorage().createAndLoadGroup(pmGroup.getIdentifier().toLowerCase()).join(); - Group group = plugin.getGroupManager().get(pmGroup.getIdentifier().toLowerCase()); + Group group = plugin.getGroupManager().getIfLoaded(pmGroup.getIdentifier().toLowerCase()); migrateSubject(pmGroup, group); plugin.getStorage().saveGroup(group); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java index 2b94d4a35..2f3052af6 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java @@ -84,7 +84,7 @@ public class MigrationPermissionsEx extends SubCommand { // Make a LuckPerms group for the one being migrated plugin.getStorage().createAndLoadGroup(pexGroup.getIdentifier().toLowerCase()).join(); - Group group = plugin.getGroupManager().get(pexGroup.getIdentifier().toLowerCase()); + Group group = plugin.getGroupManager().getIfLoaded(pexGroup.getIdentifier().toLowerCase()); migrateSubject(pexGroup, group); plugin.getStorage().saveGroup(group); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java new file mode 100644 index 000000000..59e33be2b --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.sponge.model; + +import co.aikar.timings.Timing; +import com.google.common.collect.ImmutableList; +import lombok.Getter; +import me.lucko.luckperms.api.LocalizedNode; +import me.lucko.luckperms.api.MetaUtils; +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.core.model.Group; +import me.lucko.luckperms.common.utils.ExtractedContexts; +import me.lucko.luckperms.sponge.LPSpongePlugin; +import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.LuckPermsSubject; +import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; +import me.lucko.luckperms.sponge.timings.LPTiming; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.service.permission.NodeTree; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.util.Tristate; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +public class SpongeGroup extends Group { + + @Getter + private final GroupSubject spongeData; + + public SpongeGroup(String name, LPSpongePlugin plugin) { + super(name, plugin); + this.spongeData = new GroupSubject(plugin, this); + } + + public static class GroupSubject extends LuckPermsSubject { + private final SpongeGroup parent; + private final LPSpongePlugin plugin; + + @Getter + private final LuckPermsSubjectData subjectData; + + @Getter + private final LuckPermsSubjectData transientSubjectData; + + private GroupSubject(LPSpongePlugin plugin, SpongeGroup parent) { + this.parent = parent; + this.plugin = plugin; + this.subjectData = new LuckPermsSubjectData(true, plugin.getService(), parent); + this.transientSubjectData = new LuckPermsSubjectData(false, plugin.getService(), parent); + } + + @Override + public String getIdentifier() { + return parent.getObjectName(); + } + + @Override + public Optional getCommandSource() { + return Optional.empty(); + } + + @Override + public SubjectCollection getContainingCollection() { + return plugin.getService().getGroupSubjects(); + } + + @Override + public Tristate getPermissionValue(ContextSet contexts, String permission) { + try (Timing ignored = plugin.getTimings().time(LPTiming.GROUP_GET_PERMISSION_VALUE)) { + Map permissions = parent.getAllNodesFiltered(ExtractedContexts.generate(plugin.getService().calculateContexts(contexts))).stream() + .map(LocalizedNode::getNode) + .collect(Collectors.toMap(Node::getPermission, Node::getValue)); + + Tristate t = NodeTree.of(permissions).get(permission); + if (t != Tristate.UNDEFINED) { + return t; + } + + t = plugin.getService().getGroupSubjects().getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission); + if (t != Tristate.UNDEFINED) { + return t; + } + + t = plugin.getService().getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission); + return t; + } + } + + @Override + public boolean isChildOf(ContextSet contexts, Subject parent) { + try (Timing ignored = plugin.getTimings().time(LPTiming.GROUP_IS_CHILD_OF)) { + return parent instanceof SpongeGroup && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean(); + } + } + + @Override + public List getParents(ContextSet contexts) { + try (Timing ignored = plugin.getTimings().time(LPTiming.GROUP_GET_PARENTS)) { + List subjects = parent.getAllNodesFiltered(ExtractedContexts.generate(plugin.getService().calculateContexts(contexts))).stream() + .map(LocalizedNode::getNode) + .filter(Node::isGroupNode) + .map(Node::getGroupName) + .map(s -> plugin.getService().getGroupSubjects().get(s)) + .collect(Collectors.toList()); + + subjects.addAll(plugin.getService().getGroupSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); + subjects.addAll(plugin.getService().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); + + return ImmutableList.copyOf(subjects); + } + } + + @Override + public Optional getOption(ContextSet contexts, String s) { + try (Timing ignored = plugin.getService().getPlugin().getTimings().time(LPTiming.GROUP_GET_OPTION)) { + Optional option; + if (s.equalsIgnoreCase("prefix")) { + option = getChatMeta(contexts, true); + + } else if (s.equalsIgnoreCase("suffix")) { + option = getChatMeta(contexts, false); + + } else { + option = getMeta(contexts, s); + } + + if (option.isPresent()) { + return option; + } + + option = plugin.getService().getGroupSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); + if (option.isPresent()) { + return option; + } + + return plugin.getService().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); + } + } + + @Override + public ContextSet getActiveContextSet() { + try (Timing ignored = plugin.getTimings().time(LPTiming.GROUP_GET_ACTIVE_CONTEXTS)) { + return plugin.getContextManager().getApplicableContext(this); + } + } + + private Optional getChatMeta(ContextSet contexts, boolean prefix) { + int priority = Integer.MIN_VALUE; + String meta = null; + + for (Node n : parent.getAllNodesFiltered(ExtractedContexts.generate(plugin.getService().calculateContexts(contexts)))) { + if (!n.getValue()) { + continue; + } + + if (prefix ? !n.isPrefix() : !n.isSuffix()) { + continue; + } + + Map.Entry value = prefix ? n.getPrefix() : n.getSuffix(); + if (value.getKey() > priority) { + meta = value.getValue(); + priority = value.getKey(); + } + } + + return meta == null ? Optional.empty() : Optional.of(MetaUtils.unescapeCharacters(meta)); + } + + private Optional getMeta(ContextSet contexts, String key) { + for (Node n : parent.getAllNodesFiltered(ExtractedContexts.generate(plugin.getService().calculateContexts(contexts)))) { + if (!n.getValue()) { + continue; + } + + if (!n.isMeta()) { + continue; + } + + Map.Entry m = n.getMeta(); + if (!m.getKey().equalsIgnoreCase(key)) { + continue; + } + + return Optional.of(MetaUtils.unescapeCharacters(m.getValue())); + } + + return Optional.empty(); + } + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java new file mode 100644 index 000000000..346b6de11 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.sponge.model; + +import co.aikar.timings.Timing; +import com.google.common.collect.ImmutableList; +import lombok.Getter; +import me.lucko.luckperms.api.caching.MetaData; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.core.model.User; +import me.lucko.luckperms.sponge.LPSpongePlugin; +import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.LuckPermsSubject; +import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; +import me.lucko.luckperms.sponge.timings.LPTiming; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.util.Tristate; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public class SpongeUser extends User { + + @Getter + private final UserSubject spongeData; + + public SpongeUser(UUID uuid, LPSpongePlugin plugin) { + super(uuid, plugin); + this.spongeData = new UserSubject(plugin, this); + } + + public SpongeUser(UUID uuid, String name, LPSpongePlugin plugin) { + super(uuid, name, plugin); + this.spongeData = new UserSubject(plugin, this); + } + + public static class UserSubject extends LuckPermsSubject { + private final SpongeUser parent; + private final LPSpongePlugin plugin; + + @Getter + private final LuckPermsSubjectData subjectData; + + @Getter + private final LuckPermsSubjectData transientSubjectData; + + private UserSubject(LPSpongePlugin plugin, SpongeUser parent) { + this.parent = parent; + this.plugin = plugin; + this.subjectData = new LuckPermsSubjectData(true, plugin.getService(), parent); + this.transientSubjectData = new LuckPermsSubjectData(false, plugin.getService(), parent); + } + + private boolean hasData() { + return parent.getUserData() != null; + } + + @Override + public String getIdentifier() { + return plugin.getUuidCache().getExternalUUID(parent.getUuid()).toString(); + } + + @Override + public Optional getCommandSource() { + final UUID uuid = plugin.getUuidCache().getExternalUUID(parent.getUuid()); + + Optional p = Sponge.getServer().getPlayer(uuid); + if (p.isPresent()) { + return Optional.of(p.get()); + } + + return Optional.empty(); + } + + @Override + public SubjectCollection getContainingCollection() { + return plugin.getService().getUserSubjects(); + } + + @Override + public Tristate getPermissionValue(ContextSet contexts, String permission) { + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PERMISSION_VALUE)) { + if (!hasData()) { + return Tristate.UNDEFINED; + } + + return LuckPermsService.convertTristate(parent.getUserData().getPermissionData(plugin.getService().calculateContexts(contexts)).getPermissionValue(permission)); + } + } + + @Override + public boolean isChildOf(ContextSet contexts, Subject parent) { + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_IS_CHILD_OF)) { + return parent instanceof SpongeGroup && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean(); + } + } + + @Override + public List getParents(ContextSet contexts) { + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PARENTS)) { + ImmutableList.Builder subjects = ImmutableList.builder(); + + if (hasData()) { + for (String perm : parent.getUserData().getPermissionData(plugin.getService().calculateContexts(contexts)).getImmutableBacking().keySet()) { + if (!perm.startsWith("group.")) { + continue; + } + + String groupName = perm.substring("group.".length()); + if (plugin.getGroupManager().isLoaded(groupName)) { + subjects.add(plugin.getService().getGroupSubjects().get(groupName)); + } + } + } + + subjects.addAll(plugin.getService().getUserSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); + subjects.addAll(plugin.getService().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); + + return subjects.build(); + } + } + + @Override + public Optional getOption(ContextSet contexts, String s) { + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_OPTION)) { + if (hasData()) { + MetaData data = parent.getUserData().getMetaData(plugin.getService().calculateContexts(contexts)); + if (s.equalsIgnoreCase("prefix")) { + if (data.getPrefix() != null) { + return Optional.of(data.getPrefix()); + } + } + + if (s.equalsIgnoreCase("suffix")) { + if (data.getSuffix() != null) { + return Optional.of(data.getSuffix()); + } + } + + if (data.getMeta().containsKey(s)) { + return Optional.of(data.getMeta().get(s)); + } + } + + Optional v = plugin.getService().getUserSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); + if (v.isPresent()) { + return v; + } + + return plugin.getService().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); + } + } + + @Override + public ContextSet getActiveContextSet() { + try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_ACTIVE_CONTEXTS)) { + return plugin.getContextManager().getApplicableContext(this); + } + } + } + + +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java deleted file mode 100644 index 5f1c3cdc8..000000000 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (c) 2016 Lucko (Luck) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.sponge.service; - -import co.aikar.timings.Timing; -import com.google.common.collect.ImmutableList; -import lombok.AccessLevel; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import me.lucko.luckperms.api.LocalizedNode; -import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.api.context.ContextSet; -import me.lucko.luckperms.common.core.model.Group; -import me.lucko.luckperms.common.utils.ExtractedContexts; -import me.lucko.luckperms.sponge.timings.LPTiming; -import org.spongepowered.api.command.CommandSource; -import org.spongepowered.api.service.permission.NodeTree; -import org.spongepowered.api.service.permission.Subject; -import org.spongepowered.api.service.permission.SubjectCollection; -import org.spongepowered.api.util.Tristate; - -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -import static me.lucko.luckperms.api.MetaUtils.unescapeCharacters; - -@Getter -@EqualsAndHashCode(of = "group", callSuper = false) -public class LuckPermsGroupSubject extends LuckPermsSubject { - public static LuckPermsGroupSubject wrapGroup(Group group, LuckPermsService service) { - return new LuckPermsGroupSubject(group, service); - } - - @Getter(value = AccessLevel.NONE) - private LuckPermsService service; - - private Group group; - private LuckPermsSubjectData subjectData; - private LuckPermsSubjectData transientSubjectData; - - private LuckPermsGroupSubject(Group group, LuckPermsService service) { - this.group = group; - this.subjectData = new LuckPermsSubjectData(true, service, group); - this.transientSubjectData = new LuckPermsSubjectData(false, service, group); - this.service = service; - } - - @Override - public String getIdentifier() { - return group.getObjectName(); - } - - @Override - public Optional getCommandSource() { - return Optional.empty(); - } - - @Override - public SubjectCollection getContainingCollection() { - return service.getGroupSubjects(); - } - - @Override - public Tristate getPermissionValue(ContextSet contexts, String permission) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_PERMISSION_VALUE)) { - Map permissions = group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts))).stream() - .map(LocalizedNode::getNode) - .collect(Collectors.toMap(Node::getPermission, Node::getValue)); - - Tristate t = NodeTree.of(permissions).get(permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - t = service.getGroupSubjects().getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - t = service.getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission); - return t; - } - } - - @Override - public boolean isChildOf(ContextSet contexts, Subject parent) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_IS_CHILD_OF)) { - return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean(); - } - } - - @Override - public List getParents(ContextSet contexts) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_PARENTS)) { - List subjects = group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts))).stream() - .map(LocalizedNode::getNode) - .filter(Node::isGroupNode) - .map(Node::getGroupName) - .map(s -> service.getGroupSubjects().get(s)) - .collect(Collectors.toList()); - - subjects.addAll(service.getGroupSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); - subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts))); - - return ImmutableList.copyOf(subjects); - } - } - - @Override - public Optional getOption(ContextSet contexts, String s) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_OPTION)) { - Optional option; - if (s.equalsIgnoreCase("prefix")) { - option = getChatMeta(contexts, true); - - } else if (s.equalsIgnoreCase("suffix")) { - option = getChatMeta(contexts, false); - - } else { - option = getMeta(contexts, s); - } - - if (option.isPresent()) { - return option; - } - - option = service.getGroupSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); - if (option.isPresent()) { - return option; - } - - return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); - } - } - - @Override - public ContextSet getActiveContextSet() { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_ACTIVE_CONTEXTS)) { - return service.getPlugin().getContextManager().getApplicableContext(this); - } - } - - private Optional getChatMeta(ContextSet contexts, boolean prefix) { - int priority = Integer.MIN_VALUE; - String meta = null; - - for (Node n : group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts)))) { - if (!n.getValue()) { - continue; - } - - if (prefix ? !n.isPrefix() : !n.isSuffix()) { - continue; - } - - Map.Entry value = prefix ? n.getPrefix() : n.getSuffix(); - if (value.getKey() > priority) { - meta = value.getValue(); - priority = value.getKey(); - } - } - - return meta == null ? Optional.empty() : Optional.of(unescapeCharacters(meta)); - } - - private Optional getMeta(ContextSet contexts, String key) { - for (Node n : group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts)))) { - if (!n.getValue()) { - continue; - } - - if (!n.isMeta()) { - continue; - } - - Map.Entry m = n.getMeta(); - if (!m.getKey().equalsIgnoreCase(key)) { - continue; - } - - return Optional.of(m.getValue()); - } - - return Optional.empty(); - } -} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java index e96346d3f..88f1254ad 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java @@ -29,13 +29,15 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import lombok.*; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.contexts.SpongeCalculatorLink; -import me.lucko.luckperms.sponge.service.collections.GroupCollection; -import me.lucko.luckperms.sponge.service.collections.UserCollection; +import me.lucko.luckperms.sponge.managers.SpongeGroupManager; +import me.lucko.luckperms.sponge.managers.SpongeUserManager; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; import me.lucko.luckperms.sponge.service.persisted.SubjectStorage; import me.lucko.luckperms.sponge.service.simple.SimpleCollection; @@ -61,8 +63,10 @@ public class LuckPermsService implements PermissionService { private final LPSpongePlugin plugin; private final SubjectStorage storage; - private final UserCollection userSubjects; - private final GroupCollection groupSubjects; + private final SpongeUserManager userSubjects; + private final SimpleCollection fallbackUserSubjects; + private final SpongeGroupManager groupSubjects; + private final SimpleCollection fallbackGroupSubjects; private final PersistedCollection defaultSubjects; private final Set descriptionSet; @@ -73,6 +77,11 @@ public class LuckPermsService implements PermissionService { public SubjectCollection load(String s) { return new SimpleCollection(LuckPermsService.this, s); } + + @Override + public ListenableFuture reload(String s, SubjectCollection collection) { + return Futures.immediateFuture(collection); // Never needs to be refreshed. + } }); public LuckPermsService(LPSpongePlugin plugin) { @@ -80,13 +89,17 @@ public class LuckPermsService implements PermissionService { storage = new SubjectStorage(new File(plugin.getDataFolder(), "local")); - userSubjects = new UserCollection(this, plugin.getUserManager()); - groupSubjects = new GroupCollection(this, plugin.getGroupManager()); + userSubjects = plugin.getUserManager(); + fallbackUserSubjects = new SimpleCollection(this, "fallback-users"); + groupSubjects = plugin.getGroupManager(); + fallbackGroupSubjects = new SimpleCollection(this, "fallback-groups"); defaultSubjects = new PersistedCollection(this, "defaults"); defaultSubjects.loadAll(); collections.put(PermissionService.SUBJECTS_USER, userSubjects); + collections.put("fallback-users", fallbackUserSubjects); collections.put(PermissionService.SUBJECTS_GROUP, groupSubjects); + collections.put("fallback-groups", fallbackGroupSubjects); collections.put("defaults", defaultSubjects); descriptionSet = ConcurrentHashMap.newKeySet(); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java index d652a5bbd..6af7b1bbf 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java @@ -38,6 +38,7 @@ import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; +import me.lucko.luckperms.sponge.model.SpongeGroup; import me.lucko.luckperms.sponge.timings.LPTiming; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; @@ -252,17 +253,17 @@ public class LuckPermsSubjectData implements SubjectData { @Override public boolean addParent(@NonNull Set set, @NonNull Subject subject) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_ADD_PARENT)) { - if (subject instanceof LuckPermsGroupSubject) { - LuckPermsGroupSubject permsSubject = ((LuckPermsGroupSubject) subject); + if (subject instanceof SpongeGroup) { + SpongeGroup permsSubject = ((SpongeGroup) subject); ContextSet contexts = LuckPermsService.convertContexts(set); try { if (enduring) { - holder.setPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) + holder.setPermission(new NodeBuilder("group." + permsSubject.getName()) .withExtraContext(contexts) .build()); } else { - holder.setTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) + holder.setTransientPermission(new NodeBuilder("group." + permsSubject.getName()) .withExtraContext(contexts) .build()); } @@ -278,17 +279,17 @@ public class LuckPermsSubjectData implements SubjectData { @Override public boolean removeParent(@NonNull Set set, @NonNull Subject subject) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_REMOVE_PARENT)) { - if (subject instanceof LuckPermsGroupSubject) { - LuckPermsGroupSubject permsSubject = ((LuckPermsGroupSubject) subject); + if (subject instanceof SpongeGroup) { + SpongeGroup permsSubject = ((SpongeGroup) subject); ContextSet contexts = LuckPermsService.convertContexts(set); try { if (enduring) { - holder.unsetPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) + holder.unsetPermission(new NodeBuilder("group." + permsSubject.getName()) .withExtraContext(contexts) .build()); } else { - holder.unsetTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) + holder.unsetTransientPermission(new NodeBuilder("group." + permsSubject.getName()) .withExtraContext(contexts) .build()); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java deleted file mode 100644 index 19785395b..000000000 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2016 Lucko (Luck) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.sponge.service; - -import co.aikar.timings.Timing; -import com.google.common.collect.ImmutableList; -import lombok.AccessLevel; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import me.lucko.luckperms.api.caching.MetaData; -import me.lucko.luckperms.api.context.ContextSet; -import me.lucko.luckperms.common.core.model.User; -import me.lucko.luckperms.sponge.timings.LPTiming; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.command.CommandSource; -import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.service.permission.Subject; -import org.spongepowered.api.service.permission.SubjectCollection; -import org.spongepowered.api.util.Tristate; - -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -@Getter -@EqualsAndHashCode(of = "user", callSuper = false) -public class LuckPermsUserSubject extends LuckPermsSubject { - public static LuckPermsUserSubject wrapUser(User user, LuckPermsService service) { - return new LuckPermsUserSubject(user, service); - } - - @Getter(value = AccessLevel.NONE) - private LuckPermsService service; - - private User user; - private LuckPermsSubjectData subjectData; - private LuckPermsSubjectData transientSubjectData; - - private LuckPermsUserSubject(User user, LuckPermsService service) { - this.user = user; - this.service = service; - this.subjectData = new LuckPermsSubjectData(true, service, user); - this.transientSubjectData = new LuckPermsSubjectData(false, service, user); - } - - private boolean hasData() { - return user.getUserData() != null; - } - - @Override - public String getIdentifier() { - return service.getPlugin().getUuidCache().getExternalUUID(user.getUuid()).toString(); - } - - @Override - public Optional getCommandSource() { - final UUID uuid = service.getPlugin().getUuidCache().getExternalUUID(user.getUuid()); - - Optional p = Sponge.getServer().getPlayer(uuid); - if (p.isPresent()) { - return Optional.of(p.get()); - } - - return Optional.empty(); - } - - @Override - public SubjectCollection getContainingCollection() { - return service.getUserSubjects(); - } - - @Override - public Tristate getPermissionValue(ContextSet contexts, String permission) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_GET_PERMISSION_VALUE)) { - return !hasData() ? - Tristate.UNDEFINED : - LuckPermsService.convertTristate( - user.getUserData().getPermissionData(service.calculateContexts(contexts)).getPermissionValue(permission) - ); - } - } - - @Override - public boolean isChildOf(ContextSet contexts, Subject parent) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_IS_CHILD_OF)) { - return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean(); - } - } - - @Override - public List getParents(ContextSet contexts) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_GET_PARENTS)) { - ImmutableList.Builder subjects = ImmutableList.builder(); - - if (hasData()) { - for (String perm : user.getUserData().getPermissionData(service.calculateContexts(contexts)).getImmutableBacking().keySet()) { - if (!perm.startsWith("group.")) { - continue; - } - - String groupName = perm.substring("group.".length()); - if (service.getPlugin().getGroupManager().isLoaded(groupName)) { - subjects.add(service.getGroupSubjects().get(groupName)); - } - } - } - - subjects.addAll(service.getUserSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts))); - subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts))); - - return subjects.build(); - } - } - - @Override - public Optional getOption(ContextSet contexts, String s) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_GET_OPTION)) { - if (hasData()) { - MetaData data = user.getUserData().getMetaData(service.calculateContexts(contexts)); - if (s.equalsIgnoreCase("prefix")) { - if (data.getPrefix() != null) { - return Optional.of(data.getPrefix()); - } - } - - if (s.equalsIgnoreCase("suffix")) { - if (data.getSuffix() != null) { - return Optional.of(data.getSuffix()); - } - } - - if (data.getMeta().containsKey(s)) { - return Optional.of(data.getMeta().get(s)); - } - } - - Optional v = service.getUserSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); - if (v.isPresent()) { - return v; - } - - return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s); - } - } - - @Override - public ContextSet getActiveContextSet() { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_GET_ACTIVE_CONTEXTS)) { - return service.getPlugin().getContextManager().getApplicableContext(this); - } - } -} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/UserCollection.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/UserCollection.java deleted file mode 100644 index ee1462a5e..000000000 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/collections/UserCollection.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (c) 2016 Lucko (Luck) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.sponge.service.collections; - -import co.aikar.timings.Timing; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.UncheckedExecutionException; -import lombok.NonNull; -import me.lucko.luckperms.api.context.ContextSet; -import me.lucko.luckperms.common.commands.utils.Util; -import me.lucko.luckperms.common.core.UserIdentifier; -import me.lucko.luckperms.common.core.model.User; -import me.lucko.luckperms.common.managers.UserManager; -import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.LuckPermsUserSubject; -import me.lucko.luckperms.sponge.service.simple.SimpleCollection; -import me.lucko.luckperms.sponge.timings.LPTiming; -import org.spongepowered.api.service.context.Context; -import org.spongepowered.api.service.permission.PermissionService; -import org.spongepowered.api.service.permission.Subject; -import org.spongepowered.api.service.permission.SubjectCollection; -import org.spongepowered.api.service.permission.SubjectData; -import org.spongepowered.api.util.Tristate; - -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.ExecutionException; - -/** - * Manages low level Subject instances for the PermissionService. - * Most calls are cached. - */ -public class UserCollection implements SubjectCollection { - private final LuckPermsService service; - private final UserManager manager; - private final SimpleCollection fallback; - - private final LoadingCache users = CacheBuilder.newBuilder() - .build(new CacheLoader() { - @Override - public LuckPermsUserSubject load(UUID uuid) throws Exception { - User user = manager.get(uuid); - if (user == null) { - throw new IllegalStateException("User not loaded"); - } - - return LuckPermsUserSubject.wrapUser(user, service); - } - - @Override - public ListenableFuture reload(UUID uuid, LuckPermsUserSubject s) { - return Futures.immediateFuture(s); // Never needs to be refreshed. - } - }); - - public UserCollection(LuckPermsService service, UserManager manager) { - this.service = service; - this.manager = manager; - this.fallback = new SimpleCollection(service, "fallback-users"); - } - - @Override - public String getIdentifier() { - return PermissionService.SUBJECTS_USER; - } - - public void unload(UUID uuid) { - users.invalidate(uuid); - } - - @Override - public Subject get(@NonNull String id) { - try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_COLLECTION_GET)) { - UUID uuid = Util.parseUuid(id); - if (uuid == null) { - service.getPlugin().getLog().warn("Couldn't get user subject for id: " + id + " (not a uuid)"); - return fallback.get(id); // fallback to the transient collection - } - - UUID u = service.getPlugin().getUuidCache().getUUID(uuid); - - // check if the user is loaded in memory. hopefully this call is not on the main thread. :( - if (!manager.isLoaded(UserIdentifier.of(u, null))) { - service.getPlugin().getLog().warn("User Subject '" + u + "' was requested, but is not loaded in memory. Loading them from storage now."); - long startTime = System.currentTimeMillis(); - service.getPlugin().getStorage().loadUser(u, "null").join(); - User user = service.getPlugin().getUserManager().get(u); - if (user != null) { - user.setupData(false); - } - service.getPlugin().getLog().warn("Loading '" + u + "' took " + (System.currentTimeMillis() - startTime) + " ms."); - } - - try { - return users.get(u); - } catch (ExecutionException | UncheckedExecutionException e) { - service.getPlugin().getLog().warn("Unable to get user subject '" + u + "' from memory."); - e.printStackTrace(); - return fallback.get(u.toString()); - } - } - } - - @Override - public boolean hasRegistered(@NonNull String id) { - UUID uuid = Util.parseUuid(id); - if (uuid == null) { - return false; - } - - UUID internal = service.getPlugin().getUuidCache().getUUID(uuid); - return users.asMap().containsKey(internal) || manager.isLoaded(UserIdentifier.of(internal, null)); - } - - @Override - public Iterable getAllSubjects() { - return users.asMap().values().stream().collect(ImmutableCollectors.toImmutableList()); - } - - @Override - public Map getAllWithPermission(@NonNull String id) { - return getAllWithPermission(SubjectData.GLOBAL_CONTEXT, id); - } - - @Override - public Map getAllWithPermission(@NonNull Set contexts, @NonNull String node) { - ContextSet cs = LuckPermsService.convertContexts(contexts); - return users.asMap().values().stream() - .filter(sub -> sub.getPermissionValue(cs, node) != Tristate.UNDEFINED) - .collect(ImmutableCollectors.toImmutableMap(sub -> sub, sub -> sub.getPermissionValue(cs, node).asBoolean())); - } - - @Override - public Subject getDefaults() { - return service.getDefaultSubjects().get(getIdentifier()); - } -}