mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-12-29 12:37:40 +01:00
Refactor Node class
This commit is contained in:
parent
a58cc6bdf3
commit
e2cebfbd16
@ -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<Object> {
|
||||
// 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<Object> {
|
||||
// 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);
|
||||
|
@ -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());
|
||||
|
@ -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")
|
||||
|
@ -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());
|
||||
|
@ -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());
|
||||
|
@ -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<Group> {
|
||||
}
|
||||
|
||||
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<Group> {
|
||||
}
|
||||
|
||||
toRemove.add(element);
|
||||
toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build());
|
||||
toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Group> {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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<Group> {
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -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<User> {
|
||||
}
|
||||
|
||||
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<User> {
|
||||
}
|
||||
|
||||
toRemove.add(element);
|
||||
toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build());
|
||||
toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<User> {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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<User> {
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -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<Datastore> {
|
||||
}
|
||||
|
||||
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<Datastore> {
|
||||
}
|
||||
|
||||
toRemove.add(element);
|
||||
toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build());
|
||||
toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Datastore> {
|
||||
}
|
||||
|
||||
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<Datastore> {
|
||||
}
|
||||
|
||||
toRemove.add(element);
|
||||
toAdd.add(me.lucko.luckperms.common.core.Node.builderFromExisting(element).setServer(to).build());
|
||||
toAdd.add(NodeFactory.builderFromExisting(element).setServer(to).build());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<String, String> meta;
|
||||
|
||||
private final boolean isPrefix;
|
||||
private Map.Entry<Integer, String> prefix;
|
||||
|
||||
private final boolean isSuffix;
|
||||
private Map.Entry<Integer, String> suffix;
|
||||
|
||||
private final List<String> 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<String> 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<String> 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<String> 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<String, String> getMeta() {
|
||||
if (!isMeta()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
List<String> 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<Integer, String> getPrefix() {
|
||||
if (!isPrefix()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
List<String> 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<Integer, String> getSuffix() {
|
||||
if (!isSuffix()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
List<String> 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<String> resolveShorthand() {
|
||||
return resolvedShorthand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSerializedNode() {
|
||||
return serializedNode;
|
||||
}
|
||||
|
||||
private List<String> calculateShorthand() {
|
||||
if (!Patterns.SHORTHAND_NODE.matcher(getPermission()).find()) {
|
||||
return Collections.emptyList();
|
||||
return ImmutableList.of();
|
||||
}
|
||||
|
||||
if (!getPermission().contains(".")) {
|
||||
return Collections.emptyList();
|
||||
return ImmutableList.of();
|
||||
}
|
||||
|
||||
Iterable<String> 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<String, me.lucko.luckperms.api.Node> CACHE = new ConcurrentHashMap<>();
|
||||
private static final Map<String, me.lucko.luckperms.api.Node> 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<String> parts = Splitter.on('/').limit(2).splitToList(s);
|
||||
// 0=server(+world) 1=node
|
||||
|
||||
// WORLD SPECIFIC
|
||||
if (parts.get(0).contains("-")) {
|
||||
List<String> serverParts = Splitter.on('-').limit(2).splitToList(parts.get(0));
|
||||
// 0=server 1=world
|
||||
|
||||
if (parts.get(1).contains("$")) {
|
||||
List<String> 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<String> 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<String> 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<String> 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<String, String> entry) {
|
||||
withExtraContext(entry.getKey(), entry.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public me.lucko.luckperms.api.Node.Builder withExtraContext(Map<String, String> map) {
|
||||
withExtraContext(ContextSet.fromMap(map));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public me.lucko.luckperms.api.Node.Builder withExtraContext(Set<Map.Entry<String, String>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* 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<String> 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<String, String> entry) {
|
||||
withExtraContext(entry.getKey(), entry.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public me.lucko.luckperms.api.Node.Builder withExtraContext(Map<String, String> map) {
|
||||
withExtraContext(ContextSet.fromMap(map));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public me.lucko.luckperms.api.Node.Builder withExtraContext(Set<Map.Entry<String, String>> 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* 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<String, Node> CACHE = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<String, Node>() {
|
||||
@Override
|
||||
public Node load(String s) {
|
||||
return builderFromSerialisedNode(s, true).build();
|
||||
}
|
||||
});
|
||||
|
||||
private static final LoadingCache<String, Node> CACHE_NEGATED = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<String, Node>() {
|
||||
@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<String> parts = Splitter.on('/').limit(2).splitToList(s);
|
||||
// 0=server(+world) 1=node
|
||||
|
||||
// WORLD SPECIFIC
|
||||
if (parts.get(0).contains("-")) {
|
||||
List<String> serverParts = Splitter.on('-').limit(2).splitToList(parts.get(0));
|
||||
// 0=server 1=world
|
||||
|
||||
if (parts.get(1).contains("$")) {
|
||||
List<String> 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<String> 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<String> 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);
|
||||
}
|
||||
}
|
@ -119,6 +119,11 @@ public abstract class PermissionHolder {
|
||||
* @return the holders transient and permanent nodes
|
||||
*/
|
||||
public SortedSet<LocalizedNode> getPermissions(boolean mergeTemp) {
|
||||
Optional<ImmutableSortedSet<LocalizedNode>> opt = mergeTemp ? mergedCache.getIfPresent() : cache.getIfPresent();
|
||||
if (opt.isPresent()) {
|
||||
return opt.get();
|
||||
}
|
||||
|
||||
Supplier<ImmutableSortedSet<LocalizedNode>> supplier = () -> {
|
||||
// Create sorted set
|
||||
TreeSet<LocalizedNode> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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")) {
|
||||
|
@ -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) {}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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<String, Boolean> perms = (Map<String, Boolean>) values.get("perms");
|
||||
for (Map.Entry<String, Boolean> 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<String, Boolean> perms = (Map<String, Boolean>) values.get("perms");
|
||||
for (Map.Entry<String, Boolean> 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<String, Boolean> perms = (Map<String, Boolean>) values.get("perms");
|
||||
for (Map.Entry<String, Boolean> e : perms.entrySet()) {
|
||||
group.addNodeUnchecked(Node.fromSerialisedNode(e.getKey(), e.getValue()));
|
||||
group.addNodeUnchecked(NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
@ -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<Object> {
|
||||
|
||||
for (Map.Entry<String, Boolean> 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<Object> {
|
||||
for (Map.Entry<String, String> 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<Object> {
|
||||
}
|
||||
|
||||
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) {}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user