From e2cebfbd166ae3401d95990e0c661f2d23c8212d Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 31 Oct 2016 18:18:31 +0000 Subject: [PATCH] Refactor Node class --- .../migration/MigrationGroupManager.java | 6 +- .../luckperms/bukkit/vault/VaultChatHook.java | 5 +- .../luckperms/common/api/ApiProvider.java | 3 +- .../common/commands/generic/meta/MetaSet.java | 5 +- .../commands/generic/meta/MetaSetTemp.java | 5 +- .../group/subcommands/GroupBulkChange.java | 5 +- .../group/subcommands/GroupHasPerm.java | 8 +- .../group/subcommands/GroupInheritsPerm.java | 8 +- .../user/subcommands/UserBulkChange.java | 5 +- .../user/subcommands/UserHasPerm.java | 8 +- .../user/subcommands/UserInheritsPerm.java | 8 +- .../subcommands/BulkEditGroup.java | 5 +- .../subcommands/BulkEditPermission.java | 5 +- .../me/lucko/luckperms/common/core/Node.java | 355 +++++------------- .../luckperms/common/core/NodeBuilder.java | 170 +++++++++ .../luckperms/common/core/NodeFactory.java | 100 +++++ .../common/core/PermissionHolder.java | 9 +- .../common/defaults/LogicParser.java | 4 +- .../lucko/luckperms/common/defaults/Rule.java | 6 +- .../common/storage/backing/JSONBacking.java | 8 +- .../common/storage/backing/YAMLBacking.java | 8 +- .../migration/MigrationPermissionsEx.java | 10 +- .../sponge/service/LuckPermsSubjectData.java | 18 +- 23 files changed, 439 insertions(+), 325 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java 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 4e7688831..7c2776727 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 @@ -27,7 +27,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -207,7 +207,7 @@ public class MigrationGroupManager extends SubCommand { // n.key.value = node // n.value = true/false try { - group.setPermission(Node.fromSerialisedNode("global-" + n.getKey().getKey() + "/" + n.getKey().getValue(), n.getValue())); + group.setPermission(NodeFactory.fromSerialisedNode("global-" + n.getKey().getKey() + "/" + n.getKey().getValue(), n.getValue())); if (n.getKey().getValue().startsWith("group.")) { String groupName = n.getKey().getValue().substring(6); @@ -241,7 +241,7 @@ public class MigrationGroupManager extends SubCommand { // n.key.value = node // n.value = true/false try { - user.setPermission(Node.fromSerialisedNode("global-" + n.getKey().getKey() + "/" + n.getKey().getValue(), n.getValue())); + user.setPermission(NodeFactory.fromSerialisedNode("global-" + n.getKey().getKey() + "/" + n.getKey().getValue(), n.getValue())); if (n.getKey().getValue().startsWith("group.")) { String group = n.getKey().getValue().substring(6); 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 120dec5f0..97c284a6c 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 @@ -27,6 +27,7 @@ import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.caching.MetaData; import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; @@ -95,7 +96,7 @@ public class VaultChatHook extends Chat { } catch (ObjectLacksException ignored) {} }); - Node.Builder metaNode = new me.lucko.luckperms.common.core.Node.Builder("meta." + k + "." + v).setValue(true); + Node.Builder metaNode = new NodeBuilder("meta." + k + "." + v).setValue(true); if (!perms.getServer().equalsIgnoreCase("global")) { metaNode.setServer(perms.getServer()); } @@ -119,7 +120,7 @@ public class VaultChatHook extends Chat { perms.log("Setting " + (prefix ? "prefix" : "suffix") + " for " + holder.getObjectName() + " on world " + world + ", server " + perms.getServer()); perms.getScheduler().scheduleTask(() -> { - Node.Builder node = new me.lucko.luckperms.common.core.Node.Builder(prefix ? "prefix" : "suffix" + ".1000." + escapeCharacters(value)); + Node.Builder node = new NodeBuilder(prefix ? "prefix" : "suffix" + ".1000." + escapeCharacters(value)); node.setValue(true); if (!perms.getServer().equalsIgnoreCase("global")) { node.setServer(perms.getServer()); 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 6b7abf7fc..04afe15fb 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 @@ -31,6 +31,7 @@ import me.lucko.luckperms.api.context.IContextCalculator; import me.lucko.luckperms.api.event.LPEvent; import me.lucko.luckperms.api.event.LPListener; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.users.UserIdentifier; import java.util.Optional; @@ -210,7 +211,7 @@ public class ApiProvider implements LuckPermsApi { @Override public Node.Builder buildNode(@NonNull String permission) throws IllegalArgumentException { - return new me.lucko.luckperms.common.core.Node.Builder(checkNode(permission)); + return new NodeBuilder(checkNode(permission)); } @SuppressWarnings("unchecked") diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java index 806bd9d67..39880581e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; +import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandResult; @@ -31,7 +32,7 @@ import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -72,7 +73,7 @@ public class MetaSet extends SecondarySubCommand { } } - me.lucko.luckperms.api.Node n = new Node.Builder(node).setServer(server).setWorld(world).build(); + Node n = new NodeBuilder(node).setServer(server).setWorld(world).build(); if (holder.hasPermission(n).asBoolean()) { Message.ALREADY_HAS_META.send(sender, holder.getFriendlyName()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java index 83cedce2b..e0ba7f16d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; +import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandResult; @@ -31,7 +32,7 @@ import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -91,7 +92,7 @@ public class MetaSetTemp extends SecondarySubCommand { } } - me.lucko.luckperms.api.Node n = new Node.Builder(node).setServer(server).setWorld(world).setExpiry(duration).build(); + Node n = new NodeBuilder(node).setServer(server).setWorld(world).setExpiry(duration).build(); if (holder.hasPermission(n).asBoolean()) { Message.ALREADY_HAS_META.send(sender, holder.getFriendlyName()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java index 8f926a261..74adc5199 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -74,7 +75,7 @@ public class GroupBulkChange extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setWorld(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setWorld(to).build()); } } else { while (iterator.hasNext()) { @@ -85,7 +86,7 @@ public class GroupBulkChange extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupHasPerm.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupHasPerm.java index 18ce17978..9c81c7243 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupHasPerm.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupHasPerm.java @@ -26,7 +26,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -53,13 +53,13 @@ public class GroupHasPerm extends SubCommand { } if (args.size() == 2) { - Util.sendTristate(sender, args.get(0), group.hasPermission(new Node.Builder(args.get(0)).setServer(args.get(1)).build())); + Util.sendTristate(sender, args.get(0), group.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).build())); } else { - Util.sendTristate(sender, args.get(0), group.hasPermission(new Node.Builder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build())); + Util.sendTristate(sender, args.get(0), group.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build())); } } else { - Util.sendTristate(sender, args.get(0), group.hasPermission(new Node.Builder(args.get(0)).build())); + Util.sendTristate(sender, args.get(0), group.hasPermission(new NodeBuilder(args.get(0)).build())); } return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInheritsPerm.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInheritsPerm.java index 6504c1339..0458083ce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInheritsPerm.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInheritsPerm.java @@ -27,7 +27,7 @@ import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.InheritanceInfo; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -55,13 +55,13 @@ public class GroupInheritsPerm extends SubCommand { } if (args.size() == 2) { - result = group.inheritsPermissionInfo(new Node.Builder(args.get(0)).setServer(args.get(1)).build()); + result = group.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).build()); } else { - result = group.inheritsPermissionInfo(new Node.Builder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build()); + result = group.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build()); } } else { - result = group.inheritsPermissionInfo(new Node.Builder(args.get(0)).build()); + result = group.inheritsPermissionInfo(new NodeBuilder(args.get(0)).build()); } String location = null; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java index 509e3a612..2335d0776 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -79,7 +80,7 @@ public class UserBulkChange extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setWorld(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setWorld(to).build()); } } else { while (iterator.hasNext()) { @@ -95,7 +96,7 @@ public class UserBulkChange extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserHasPerm.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserHasPerm.java index ed76bf55d..2673c556f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserHasPerm.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserHasPerm.java @@ -26,7 +26,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -53,13 +53,13 @@ public class UserHasPerm extends SubCommand { } if (args.size() == 2) { - Util.sendTristate(sender, args.get(0), user.hasPermission(new Node.Builder(args.get(0)).setServer(args.get(1)).build())); + Util.sendTristate(sender, args.get(0), user.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).build())); } else { - Util.sendTristate(sender, args.get(0), user.hasPermission(new Node.Builder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build())); + Util.sendTristate(sender, args.get(0), user.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build())); } } else { - Util.sendTristate(sender, args.get(0), user.hasPermission(new Node.Builder(args.get(0)).build())); + Util.sendTristate(sender, args.get(0), user.hasPermission(new NodeBuilder(args.get(0)).build())); } return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInheritsPerm.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInheritsPerm.java index e27cb7cc2..6048295a3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInheritsPerm.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInheritsPerm.java @@ -27,7 +27,7 @@ import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.InheritanceInfo; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.utils.ArgumentChecker; @@ -55,13 +55,13 @@ public class UserInheritsPerm extends SubCommand { } if (args.size() == 2) { - result = user.inheritsPermissionInfo(new Node.Builder(args.get(0)).setServer(args.get(1)).build()); + result = user.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).build()); } else { - result = user.inheritsPermissionInfo(new Node.Builder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build()); + result = user.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build()); } } else { - result = user.inheritsPermissionInfo(new Node.Builder(args.get(0)).build()); + result = user.inheritsPermissionInfo(new NodeBuilder(args.get(0)).build()); } String location = null; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java index b62a3b2b1..5f98e092e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.storage.Datastore; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -97,7 +98,7 @@ public class BulkEditGroup extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setWorld(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setWorld(to).build()); } } else { while (iterator.hasNext()) { @@ -121,7 +122,7 @@ public class BulkEditGroup extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java index ed104f92e..451f59b7e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.storage.Datastore; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -91,7 +92,7 @@ public class BulkEditPermission extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setWorld(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setWorld(to).build()); } } else { while (iterator.hasNext()) { @@ -111,7 +112,7 @@ public class BulkEditPermission extends SubCommand { } toRemove.add(element); - toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build()); + toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/core/Node.java b/common/src/main/java/me/lucko/luckperms/common/core/Node.java index 36632ac30..40662ccfb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/Node.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/Node.java @@ -22,16 +22,18 @@ package me.lucko.luckperms.common.core; +import com.google.common.base.Preconditions; import com.google.common.base.Splitter; -import lombok.*; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Maps; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; -import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.constants.Patterns; -import me.lucko.luckperms.common.utils.ArgumentChecker; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -39,9 +41,9 @@ import java.util.stream.IntStream; /** * An immutable permission node */ -@SuppressWarnings({"WeakerAccess", "unused"}) -@ToString(exclude = {"isPrefix", "isSuffix", "isMeta"}) -@EqualsAndHashCode(exclude = {"isPrefix", "isSuffix", "isMeta"}) +@SuppressWarnings("OptionalGetWithoutIsPresent") +@ToString(of = {"permission", "value", "override", "server", "world", "expireAt", "contexts"}) +@EqualsAndHashCode(of = {"permission", "value", "override", "server", "world", "expireAt", "contexts"}) public class Node implements me.lucko.luckperms.api.Node { private static final Pattern PREFIX_PATTERN = Pattern.compile("(?i)prefix\\.-?\\d+\\..*"); private static final Pattern SUFFIX_PATTERN = Pattern.compile("(?i)suffix\\.-?\\d+\\..*"); @@ -64,10 +66,25 @@ public class Node implements me.lucko.luckperms.api.Node { @Getter private final ContextSet contexts; - // Cache the state - private Tristate isPrefix = Tristate.UNDEFINED; - private Tristate isSuffix = Tristate.UNDEFINED; - private Tristate isMeta = Tristate.UNDEFINED; + // Cached state + private final boolean isGroup; + private String groupName; + + private final boolean isWildcard; + private final int wildcardLevel; + + private final boolean isMeta; + private Map.Entry meta; + + private final boolean isPrefix; + private Map.Entry prefix; + + private final boolean isSuffix; + private Map.Entry suffix; + + private final List resolvedShorthand; + + private final String serializedNode; /** * Make an immutable node instance @@ -102,6 +119,38 @@ public class Node implements me.lucko.luckperms.api.Node { this.server = server; this.world = world; this.contexts = contexts == null ? ContextSet.empty() : contexts.makeImmutable(); + + // Setup state + isGroup = permission.toLowerCase().startsWith("group."); + if (isGroup) { + groupName = permission.substring("group.".length()); + } + + isWildcard = permission.endsWith(".*"); + wildcardLevel = (int) permission.chars().filter(num -> num == Character.getNumericValue('.')).count(); + + isMeta = META_PATTERN.matcher(permission).matches(); + if (isMeta) { + List metaPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("meta.".length())); + meta = Maps.immutableEntry(metaPart.get(0), metaPart.get(1)); + } + + isPrefix = PREFIX_PATTERN.matcher(permission).matches(); + if (isPrefix) { + List prefixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("prefix.".length())); + Integer i = Integer.parseInt(prefixPart.get(0)); + prefix = Maps.immutableEntry(i, prefixPart.get(1)); + } + + isSuffix = SUFFIX_PATTERN.matcher(permission).matches(); + if (isSuffix) { + List suffixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("suffix.".length())); + Integer i = Integer.parseInt(suffixPart.get(0)); + suffix = Maps.immutableEntry(i, suffixPart.get(1)); + } + + resolvedShorthand = calculateShorthand(); + serializedNode = calculateSerializedNode(); } @Override @@ -146,25 +195,19 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public long getExpiryUnixTime(){ - if (!isTemporary()) { - throw new IllegalStateException("Node does not have an expiry time."); - } + Preconditions.checkState(isTemporary(), "Node does not have an expiry time."); return expireAt; } @Override public Date getExpiry() { - if (!isTemporary()) { - throw new IllegalStateException("Node does not have an expiry time."); - } + Preconditions.checkState(isTemporary(), "Node does not have an expiry time."); return new Date(expireAt * 1000L); } @Override public long getSecondsTilExpiry() { - if (!isTemporary()) { - throw new IllegalStateException("Node does not have an expiry time."); - } + Preconditions.checkState(isTemporary(), "Node does not have an expiry time."); return expireAt - (System.currentTimeMillis() / 1000L); } @@ -175,85 +218,56 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public boolean isGroupNode() { - return getPermission().toLowerCase().startsWith("group."); + return isGroup; } @Override public String getGroupName() { - if (!isGroupNode()) { - throw new IllegalStateException("Node is not a group node"); - } - - return getPermission().substring("group.".length()); + Preconditions.checkState(isGroupNode(), "Node is not a group node"); + return groupName; } @Override public boolean isWildcard() { - return getPermission().endsWith(".*"); + return isWildcard; } @Override public int getWildcardLevel() { - return (int) getPermission().chars().filter(num -> num == Character.getNumericValue('.')).count(); + return wildcardLevel; } @Override public boolean isMeta() { - if (isMeta == Tristate.UNDEFINED) { - isMeta = Tristate.fromBoolean(META_PATTERN.matcher(getPermission()).matches()); - } - - return isMeta.asBoolean(); + return isMeta; } @Override public Map.Entry getMeta() { - if (!isMeta()) { - throw new IllegalStateException(); - } - - List metaPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("meta.".length())); - return new AbstractMap.SimpleEntry<>(metaPart.get(0), metaPart.get(1)); + Preconditions.checkState(isMeta(), "Node is not a meta node"); + return meta; } @Override public boolean isPrefix() { - if (isPrefix == Tristate.UNDEFINED) { - isPrefix = Tristate.fromBoolean(PREFIX_PATTERN.matcher(getPermission()).matches()); - } - - return isPrefix.asBoolean(); + return isPrefix; } @Override public Map.Entry getPrefix() { - if (!isPrefix()) { - throw new IllegalStateException(); - } - - List prefixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("prefix.".length())); - Integer i = Integer.parseInt(prefixPart.get(0)); - return new AbstractMap.SimpleEntry<>(i, prefixPart.get(1)); + Preconditions.checkState(isPrefix(), "Node is not a prefix node"); + return prefix; } @Override public boolean isSuffix() { - if (isSuffix == Tristate.UNDEFINED) { - isSuffix = Tristate.fromBoolean(SUFFIX_PATTERN.matcher(getPermission()).matches()); - } - - return isSuffix.asBoolean(); + return isSuffix; } @Override public Map.Entry getSuffix() { - if (!isSuffix()) { - throw new IllegalStateException(); - } - - List suffixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("suffix.".length())); - Integer i = Integer.parseInt(suffixPart.get(0)); - return new AbstractMap.SimpleEntry<>(i, suffixPart.get(1)); + Preconditions.checkState(isSuffix(), "Node is not a suffix node"); + return suffix; } @Override @@ -262,11 +276,7 @@ public class Node implements me.lucko.luckperms.api.Node { return !isServerSpecific(); } - if (isServerSpecific()) { - return shouldApply(server, applyRegex, this.server); - } else { - return includeGlobal; - } + return isServerSpecific() ? shouldApply(server, applyRegex, this.server) : includeGlobal; } @Override @@ -275,11 +285,7 @@ public class Node implements me.lucko.luckperms.api.Node { return !isWorldSpecific(); } - if (isWorldSpecific()) { - return shouldApply(world, applyRegex, this.world); - } else { - return includeGlobal; - } + return isWorldSpecific() ? shouldApply(world, applyRegex, this.world) : includeGlobal; } private static boolean shouldApply(String world, boolean applyRegex, String thisWorld) { @@ -398,12 +404,21 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public List resolveShorthand() { + return resolvedShorthand; + } + + @Override + public String toSerializedNode() { + return serializedNode; + } + + private List calculateShorthand() { if (!Patterns.SHORTHAND_NODE.matcher(getPermission()).find()) { - return Collections.emptyList(); + return ImmutableList.of(); } if (!getPermission().contains(".")) { - return Collections.emptyList(); + return ImmutableList.of(); } Iterable parts = Splitter.on('.').split(getPermission()); @@ -448,10 +463,10 @@ public class Node implements me.lucko.luckperms.api.Node { nodes = newNodes; } - return new ArrayList<>(nodes); + return ImmutableList.copyOf(nodes); } - public String toSerializedNode() { + private String calculateSerializedNode() { StringBuilder builder = new StringBuilder(); if (server != null) { @@ -608,190 +623,4 @@ public class Node implements me.lucko.luckperms.api.Node { public String getKey() { return getPermission(); } - - private static final Map CACHE = new ConcurrentHashMap<>(); - private static final Map CACHE_NEGATED = new ConcurrentHashMap<>(); - - public static me.lucko.luckperms.api.Node fromSerialisedNode(String s, Boolean b) { - if (b) { - return CACHE.computeIfAbsent(s, s1 -> builderFromSerialisedNode(s1, true).build()); - } else { - return CACHE_NEGATED.computeIfAbsent(s, s1 -> builderFromSerialisedNode(s1, false).build()); - } - } - - public static me.lucko.luckperms.api.Node.Builder builderFromSerialisedNode(String s, Boolean b) { - if (s.contains("/")) { - List parts = Splitter.on('/').limit(2).splitToList(s); - // 0=server(+world) 1=node - - // WORLD SPECIFIC - if (parts.get(0).contains("-")) { - List serverParts = Splitter.on('-').limit(2).splitToList(parts.get(0)); - // 0=server 1=world - - if (parts.get(1).contains("$")) { - List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); - return new Node.Builder(tempParts.get(0), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)) - .setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); - } else { - return new Node.Builder(parts.get(1), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)).setValue(b); - } - - } else { - // SERVER BUT NOT WORLD SPECIFIC - if (parts.get(1).contains("$")) { - List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); - return new Node.Builder(tempParts.get(0), true).setServerRaw(parts.get(0)).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); - } else { - return new Node.Builder(parts.get(1), true).setServerRaw(parts.get(0)).setValue(b); - } - } - } else { - // NOT SERVER SPECIFIC - if (s.contains("$")) { - List tempParts = Splitter.on('$').limit(2).splitToList(s); - return new Node.Builder(tempParts.get(0), true).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); - } else { - return new Node.Builder(s, true).setValue(b); - } - } - } - - public static me.lucko.luckperms.api.Node.Builder builderFromExisting(me.lucko.luckperms.api.Node other) { - return new Builder(other); - } - - @RequiredArgsConstructor - public static class Builder implements me.lucko.luckperms.api.Node.Builder { - private final String permission; - private Boolean value = true; - private boolean override = false; - private String server = null; - private String world = null; - private long expireAt = 0L; - private final MutableContextSet extraContexts = new MutableContextSet(); - - Builder(String permission, boolean shouldConvertContexts) { - if (!shouldConvertContexts) { - this.permission = permission; - } else { - if (!Patterns.NODE_CONTEXTS.matcher(permission).matches()) { - this.permission = permission; - } else { - List contextParts = Splitter.on(')').limit(2).splitToList(permission.substring(1)); - // 0 = context, 1 = node - - this.permission = contextParts.get(1); - try { - extraContexts.addAll(Splitter.on(',').withKeyValueSeparator('=').split(contextParts.get(0))); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } - } - } - } - - Builder(me.lucko.luckperms.api.Node other) { - this.permission = other.getPermission(); - this.value = other.getValue(); - this.override = other.isOverride(); - this.server = other.getServer().orElse(null); - this.world = other.getWorld().orElse(null); - this.expireAt = other.isPermanent() ? 0L : other.getExpiryUnixTime(); - this.extraContexts.addAll(other.getContexts()); - } - - @Override - public me.lucko.luckperms.api.Node.Builder setNegated(boolean negated) { - value = !negated; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder setValue(boolean value) { - this.value = value; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder setOverride(boolean override) { - this.override = override; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder setExpiry(long expireAt) { - this.expireAt = expireAt; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder setWorld(String world) { - this.world = world; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder setServer(String server) { - if (server != null && ArgumentChecker.checkServer(server)) { - throw new IllegalArgumentException("Server name invalid."); - } - - this.server = server; - return this; - } - - public me.lucko.luckperms.api.Node.Builder setServerRaw(String server) { - this.server = server; - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder withExtraContext(@NonNull String key, @NonNull String value) { - switch (key.toLowerCase()) { - case "server": - setServer(value); - break; - case "world": - setWorld(value); - break; - default: - this.extraContexts.add(key, value); - break; - } - - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder withExtraContext(Map.Entry entry) { - withExtraContext(entry.getKey(), entry.getValue()); - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder withExtraContext(Map map) { - withExtraContext(ContextSet.fromMap(map)); - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder withExtraContext(Set> context) { - withExtraContext(ContextSet.fromEntries(context)); - return this; - } - - @Override - public me.lucko.luckperms.api.Node.Builder withExtraContext(ContextSet set) { - set.toSet().forEach(this::withExtraContext); - return this; - } - - @Override - public me.lucko.luckperms.api.Node build() { - return new Node(permission, value, override, expireAt, server, world, extraContexts); - } - } - } diff --git a/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java b/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java new file mode 100644 index 000000000..20b0829ab --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java @@ -0,0 +1,170 @@ +/* + * 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.common.core; + +import com.google.common.base.Splitter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.constants.Patterns; +import me.lucko.luckperms.common.utils.ArgumentChecker; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Builds Nodes + */ +@RequiredArgsConstructor +public class NodeBuilder implements me.lucko.luckperms.api.Node.Builder { + private final String permission; + private Boolean value = true; + private boolean override = false; + private String server = null; + private String world = null; + private long expireAt = 0L; + private final MutableContextSet extraContexts = new MutableContextSet(); + + NodeBuilder(String permission, boolean shouldConvertContexts) { + if (!shouldConvertContexts) { + this.permission = permission; + } else { + if (!Patterns.NODE_CONTEXTS.matcher(permission).matches()) { + this.permission = permission; + } else { + List contextParts = Splitter.on(')').limit(2).splitToList(permission.substring(1)); + // 0 = context, 1 = node + + this.permission = contextParts.get(1); + try { + extraContexts.addAll(Splitter.on(',').withKeyValueSeparator('=').split(contextParts.get(0))); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + } + } + + NodeBuilder(me.lucko.luckperms.api.Node other) { + this.permission = other.getPermission(); + this.value = other.getValue(); + this.override = other.isOverride(); + this.server = other.getServer().orElse(null); + this.world = other.getWorld().orElse(null); + this.expireAt = other.isPermanent() ? 0L : other.getExpiryUnixTime(); + this.extraContexts.addAll(other.getContexts()); + } + + @Override + public me.lucko.luckperms.api.Node.Builder setNegated(boolean negated) { + value = !negated; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder setValue(boolean value) { + this.value = value; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder setOverride(boolean override) { + this.override = override; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder setExpiry(long expireAt) { + this.expireAt = expireAt; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder setWorld(String world) { + this.world = world; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder setServer(String server) { + if (server != null && ArgumentChecker.checkServer(server)) { + throw new IllegalArgumentException("Server name invalid."); + } + + this.server = server; + return this; + } + + public me.lucko.luckperms.api.Node.Builder setServerRaw(String server) { + this.server = server; + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder withExtraContext(@NonNull String key, @NonNull String value) { + switch (key.toLowerCase()) { + case "server": + setServer(value); + break; + case "world": + setWorld(value); + break; + default: + this.extraContexts.add(key, value); + break; + } + + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder withExtraContext(Map.Entry entry) { + withExtraContext(entry.getKey(), entry.getValue()); + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder withExtraContext(Map map) { + withExtraContext(ContextSet.fromMap(map)); + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder withExtraContext(Set> context) { + withExtraContext(ContextSet.fromEntries(context)); + return this; + } + + @Override + public me.lucko.luckperms.api.Node.Builder withExtraContext(ContextSet set) { + set.toSet().forEach(this::withExtraContext); + return this; + } + + @Override + public me.lucko.luckperms.api.Node build() { + return new Node(permission, value, override, expireAt, server, world, extraContexts); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java b/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java new file mode 100644 index 000000000..c0e9f8e43 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java @@ -0,0 +1,100 @@ +/* + * 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.common.core; + +import com.google.common.base.Splitter; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import lombok.experimental.UtilityClass; +import me.lucko.luckperms.api.Node; + +import java.util.List; + +/** + * Utility class to make Node(Builder) instances from serialised strings or existing Nodes + */ +@UtilityClass +public class NodeFactory { + private static final LoadingCache CACHE = CacheBuilder.newBuilder() + .build(new CacheLoader() { + @Override + public Node load(String s) { + return builderFromSerialisedNode(s, true).build(); + } + }); + + private static final LoadingCache CACHE_NEGATED = CacheBuilder.newBuilder() + .build(new CacheLoader() { + @Override + public Node load(String s) { + return builderFromSerialisedNode(s, false).build(); + } + }); + + public static Node fromSerialisedNode(String s, Boolean b) { + return b ? CACHE.getUnchecked(s) : CACHE_NEGATED.getUnchecked(s); + } + + public static Node.Builder builderFromSerialisedNode(String s, Boolean b) { + if (s.contains("/")) { + List parts = Splitter.on('/').limit(2).splitToList(s); + // 0=server(+world) 1=node + + // WORLD SPECIFIC + if (parts.get(0).contains("-")) { + List serverParts = Splitter.on('-').limit(2).splitToList(parts.get(0)); + // 0=server 1=world + + if (parts.get(1).contains("$")) { + List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); + return new NodeBuilder(tempParts.get(0), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)) + .setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); + } else { + return new NodeBuilder(parts.get(1), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)).setValue(b); + } + + } else { + // SERVER BUT NOT WORLD SPECIFIC + if (parts.get(1).contains("$")) { + List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); + return new NodeBuilder(tempParts.get(0), true).setServerRaw(parts.get(0)).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); + } else { + return new NodeBuilder(parts.get(1), true).setServerRaw(parts.get(0)).setValue(b); + } + } + } else { + // NOT SERVER SPECIFIC + if (s.contains("$")) { + List tempParts = Splitter.on('$').limit(2).splitToList(s); + return new NodeBuilder(tempParts.get(0), true).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); + } else { + return new NodeBuilder(s, true).setValue(b); + } + } + } + + public static Node.Builder builderFromExisting(Node other) { + return new NodeBuilder(other); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java index 41935944a..f02cec631 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java @@ -119,6 +119,11 @@ public abstract class PermissionHolder { * @return the holders transient and permanent nodes */ public SortedSet getPermissions(boolean mergeTemp) { + Optional> opt = mergeTemp ? mergedCache.getIfPresent() : cache.getIfPresent(); + if (opt.isPresent()) { + return opt.get(); + } + Supplier> supplier = () -> { // Create sorted set TreeSet combined = new TreeSet<>(PriorityComparator.reverse()); @@ -879,7 +884,7 @@ public abstract class PermissionHolder { } private static Node.Builder buildNode(String permission) { - return new me.lucko.luckperms.common.core.Node.Builder(permission); + return new NodeBuilder(permission); } private static me.lucko.luckperms.common.utils.LocalizedNode makeLocal(Node node, String location) { @@ -887,6 +892,6 @@ public abstract class PermissionHolder { } private static Node makeNode(String s, Boolean b) { - return me.lucko.luckperms.common.core.Node.fromSerialisedNode(s, b); + return NodeFactory.fromSerialisedNode(s, b); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/defaults/LogicParser.java b/common/src/main/java/me/lucko/luckperms/common/defaults/LogicParser.java index c20f61cbf..bc9b36b80 100644 --- a/common/src/main/java/me/lucko/luckperms/common/defaults/LogicParser.java +++ b/common/src/main/java/me/lucko/luckperms/common/defaults/LogicParser.java @@ -23,7 +23,7 @@ package me.lucko.luckperms.common.defaults; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import javax.script.ScriptEngine; @@ -36,7 +36,7 @@ public class LogicParser { public static boolean parse(String s, PermissionHolder holder, Tristate tristate) throws IllegalArgumentException { try { - String expression = generateExpression(s, s1 -> holder.hasPermission(Node.fromSerialisedNode(s1, true)) == tristate); + String expression = generateExpression(s, s1 -> holder.hasPermission(NodeFactory.fromSerialisedNode(s1, true)) == tristate); String result = SCRIPT_ENGINE.eval(expression).toString(); if (!result.equals("true") && !result.equals("false")) { diff --git a/common/src/main/java/me/lucko/luckperms/common/defaults/Rule.java b/common/src/main/java/me/lucko/luckperms/common/defaults/Rule.java index 5bcfe0aa1..281eec7bf 100644 --- a/common/src/main/java/me/lucko/luckperms/common/defaults/Rule.java +++ b/common/src/main/java/me/lucko/luckperms/common/defaults/Rule.java @@ -26,7 +26,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.ToString; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -91,13 +91,13 @@ public class Rule { // The holder meets all of the requirements of this rule. for (String s : toTake) { try { - user.unsetPermission(Node.fromSerialisedNode(s, true)); + user.unsetPermission(NodeFactory.fromSerialisedNode(s, true)); } catch (ObjectLacksException ignored) {} } for (String s : toGive) { try { - user.setPermission(Node.fromSerialisedNode(s, true)); + user.setPermission(NodeFactory.fromSerialisedNode(s, true)); } catch (ObjectAlreadyHasException ignored) {} } 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 430ae3977..889aed651 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 @@ -26,7 +26,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import lombok.Cleanup; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.groups.GroupManager; import me.lucko.luckperms.common.tracks.Track; @@ -96,7 +96,7 @@ public class JSONBacking extends FlatfileBacking { while (reader.hasNext()) { String node = reader.nextName(); boolean b = reader.nextBoolean(); - user.addNodeUnchecked(Node.fromSerialisedNode(node, b)); + user.addNodeUnchecked(NodeFactory.fromSerialisedNode(node, b)); } reader.endObject(); reader.endObject(); @@ -258,7 +258,7 @@ public class JSONBacking extends FlatfileBacking { while (reader.hasNext()) { String node = reader.nextName(); boolean b = reader.nextBoolean(); - group.addNodeUnchecked(Node.fromSerialisedNode(node, b)); + group.addNodeUnchecked(NodeFactory.fromSerialisedNode(node, b)); } reader.endObject(); @@ -308,7 +308,7 @@ public class JSONBacking extends FlatfileBacking { while (reader.hasNext()) { String node = reader.nextName(); boolean b = reader.nextBoolean(); - group.addNodeUnchecked(Node.fromSerialisedNode(node, b)); + group.addNodeUnchecked(NodeFactory.fromSerialisedNode(node, b)); } reader.endObject(); reader.endObject(); 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 319caa76e..d874dc465 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 @@ -24,7 +24,7 @@ package me.lucko.luckperms.common.storage.backing; import lombok.Cleanup; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.groups.GroupManager; import me.lucko.luckperms.common.tracks.Track; @@ -92,7 +92,7 @@ public class YAMLBacking extends FlatfileBacking { user.setPrimaryGroup((String) values.get("primary-group")); Map perms = (Map) values.get("perms"); for (Map.Entry e : perms.entrySet()) { - user.addNodeUnchecked(Node.fromSerialisedNode(e.getKey(), e.getValue())); + user.addNodeUnchecked(NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())); } boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -215,7 +215,7 @@ public class YAMLBacking extends FlatfileBacking { return doRead(groupFile, values -> { Map perms = (Map) values.get("perms"); for (Map.Entry e : perms.entrySet()) { - group.addNodeUnchecked(Node.fromSerialisedNode(e.getKey(), e.getValue())); + group.addNodeUnchecked(NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())); } return true; }); @@ -248,7 +248,7 @@ public class YAMLBacking extends FlatfileBacking { return groupFile.exists() && doRead(groupFile, values -> { Map perms = (Map) values.get("perms"); for (Map.Entry e : perms.entrySet()) { - group.addNodeUnchecked(Node.fromSerialisedNode(e.getKey(), e.getValue())); + group.addNodeUnchecked(NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())); } return true; }); 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 b33da81d7..91b0821a0 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 @@ -28,7 +28,7 @@ import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.Node; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; @@ -112,7 +112,7 @@ public class MigrationPermissionsEx extends SubCommand { for (Map.Entry perm : e.getValue().entrySet()) { try { - holder.setPermission(new Node.Builder(perm.getKey()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(perm.getValue()).build()); + holder.setPermission(new NodeBuilder(perm.getKey()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(perm.getValue()).build()); } catch (ObjectAlreadyHasException ignored) {} } } @@ -131,11 +131,11 @@ public class MigrationPermissionsEx extends SubCommand { for (Map.Entry opt : e.getValue().entrySet()) { if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("suffix")) { try { - holder.setPermission(new Node.Builder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermission(new NodeBuilder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } catch (ObjectAlreadyHasException ignored) {} } else { try { - holder.setPermission(new Node.Builder("meta." + opt.getKey() + "." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermission(new NodeBuilder("meta." + opt.getKey() + "." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } catch (ObjectAlreadyHasException ignored) {} } } @@ -158,7 +158,7 @@ public class MigrationPermissionsEx extends SubCommand { } try { - holder.setPermission(new Node.Builder("group." + s.getIdentifier().toLowerCase()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermission(new NodeBuilder("group." + s.getIdentifier().toLowerCase()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } catch (ObjectAlreadyHasException ignored) {} } } 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 ece61df83..6e8727e74 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 @@ -30,6 +30,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; @@ -46,6 +47,7 @@ import java.util.stream.Collectors; import static me.lucko.luckperms.api.MetaUtils.escapeCharacters; +@SuppressWarnings({"OptionalGetWithoutIsPresent", "unused"}) @AllArgsConstructor public class LuckPermsSubjectData implements SubjectData { private final boolean enduring; @@ -104,7 +106,7 @@ public class LuckPermsSubjectData implements SubjectData { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_PERMISSION)) { if (tristate == Tristate.UNDEFINED) { // Unset - Node.Builder builder = new me.lucko.luckperms.common.core.Node.Builder(permission); + Node.Builder builder = new NodeBuilder(permission); for (Context ct : contexts) { builder.withExtraContext(ct.getKey(), ct.getValue()); @@ -122,7 +124,7 @@ public class LuckPermsSubjectData implements SubjectData { return true; } - Node.Builder builder = new me.lucko.luckperms.common.core.Node.Builder(permission) + Node.Builder builder = new NodeBuilder(permission) .setValue(tristate.asBoolean()); for (Context ct : contexts) { @@ -243,11 +245,11 @@ public class LuckPermsSubjectData implements SubjectData { try { if (enduring) { - holder.setPermission(new me.lucko.luckperms.common.core.Node.Builder("group." + permsSubject.getIdentifier()) + holder.setPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) .withExtraContext(contexts) .build()); } else { - holder.setTransientPermission(new me.lucko.luckperms.common.core.Node.Builder("group." + permsSubject.getIdentifier()) + holder.setTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) .withExtraContext(contexts) .build()); } @@ -269,11 +271,11 @@ public class LuckPermsSubjectData implements SubjectData { try { if (enduring) { - holder.unsetPermission(new me.lucko.luckperms.common.core.Node.Builder("group." + permsSubject.getIdentifier()) + holder.unsetPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) .withExtraContext(contexts) .build()); } else { - holder.unsetTransientPermission(new me.lucko.luckperms.common.core.Node.Builder("group." + permsSubject.getIdentifier()) + holder.unsetTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) .withExtraContext(contexts) .build()); } @@ -433,12 +435,12 @@ public class LuckPermsSubjectData implements SubjectData { try { if (enduring) { - holder.setPermission(new me.lucko.luckperms.common.core.Node.Builder("meta." + key + "." + value) + holder.setPermission(new NodeBuilder("meta." + key + "." + value) .withExtraContext(context) .build() ); } else { - holder.setTransientPermission(new me.lucko.luckperms.common.core.Node.Builder("meta." + key + "." + value) + holder.setTransientPermission(new NodeBuilder("meta." + key + "." + value) .withExtraContext(context) .build() );