From 7305c6c54b2a8cc2c85d4af47a9a86e923ebcf13 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 11 Mar 2017 18:24:25 +0000 Subject: [PATCH] Migrate group weightings onto the group itself as well as to the chat meta held by it --- .../migration/MigrationPermissionsEx.java | 4 ++++ .../migration/MigrationPowerfulPerms.java | 3 +++ .../common/core/model/PermissionHolder.java | 19 +++++++++++++++++++ .../sponge/migration/MigrationUtils.java | 19 +++++++------------ 4 files changed, 33 insertions(+), 12 deletions(-) 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 e949b96b..d57618ee 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 @@ -29,6 +29,7 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -103,6 +104,9 @@ public class MigrationPermissionsEx extends SubCommand { plugin.getStorage().createAndLoadGroup(name, CreationCause.INTERNAL).join(); Group lpGroup = plugin.getGroupManager().getIfLoaded(name); + lpGroup.removeIf(n -> n.getPermission().startsWith("weight.")); + lpGroup.setPermissionUnchecked(NodeFactory.make("weight." + groupWeight, true)); + try { for (String node : group.getOwnPermissions(null)) { boolean value = true; 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 8646c40d..4e46b0ae 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 @@ -172,6 +172,9 @@ public class MigrationPowerfulPerms extends SubCommand { plugin.getStorage().createAndLoadGroup(name, CreationCause.INTERNAL).join(); final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(name); + group.removeIf(n -> n.getPermission().startsWith("weight.")); + group.setPermissionUnchecked(NodeFactory.make("weight." + g.getRank(), true)); + for (Permission p : g.getOwnPermissions()) { applyPerm(group, p); } 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 5c599edb..3af11358 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 @@ -76,6 +76,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Predicate; import java.util.stream.Collectors; /** @@ -403,6 +404,24 @@ public abstract class PermissionHolder { invalidateCache(true); } + public boolean removeIf(Predicate predicate) { + boolean result; + ImmutableSet before = ImmutableSet.copyOf(getNodes()); + + synchronized (nodes) { + result = nodes.removeIf(predicate); + } + + if (!result) { + return false; + } + + invalidateCache(true); + ImmutableSet after = ImmutableSet.copyOf(getNodes()); + plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + return true; + } + public Set getTransientNodes() { return transientCache.get(); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java index d5a1dd4d..74c5468f 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java @@ -47,6 +47,9 @@ public class MigrationUtils { public static void migrateSubject(Subject subject, PermissionHolder holder, int priority) { + holder.removeIf(n -> n.getPermission().startsWith("weight.")); + holder.setPermissionUnchecked(NodeFactory.make("weight." + priority, true)); + // Migrate permissions Map, Map> perms = subject.getSubjectData().getAllPermissions(); for (Map.Entry, Map> e : perms.entrySet()) { @@ -77,17 +80,11 @@ public class MigrationUtils { for (Map.Entry opt : e.getValue().entrySet()) { if (opt.getKey().equalsIgnoreCase("prefix")) { - try { - holder.setPermission(NodeFactory.makePrefixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(NodeFactory.makePrefixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } else if (opt.getKey().equalsIgnoreCase("suffix")) { - try { - holder.setPermission(NodeFactory.makeSuffixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(NodeFactory.makeSuffixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } else { - try { - holder.setPermission(NodeFactory.makeMetaNode(opt.getKey(), opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(NodeFactory.makeMetaNode(opt.getKey(), opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } } } @@ -110,9 +107,7 @@ public class MigrationUtils { continue; // LuckPerms does not support persisting other subject types. } - try { - holder.setPermission(new NodeBuilder("group." + convertName(s.getIdentifier())).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(new NodeBuilder("group." + convertName(s.getIdentifier())).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } } }