From 696cd235fd53c8822729a2d5063ebc3ca6cec0af Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 13 Nov 2016 11:09:57 +0000 Subject: [PATCH] Fix meta serialization. Resolves #41 --- .../luckperms/bukkit/vault/VaultChatHook.java | 8 ++-- .../common/commands/generic/meta/MetaSet.java | 10 ++--- .../commands/generic/meta/MetaSetTemp.java | 10 ++--- .../commands/generic/meta/MetaUnset.java | 3 +- .../commands/generic/meta/MetaUnsetTemp.java | 3 +- .../me/lucko/luckperms/common/core/Node.java | 7 ++-- .../luckperms/common/core/NodeFactory.java | 13 ++++++ .../sponge/migration/MigrationUtils.java | 11 +++-- .../sponge/service/LuckPermsSubjectData.java | 41 ++++++++++--------- .../sponge/service/LuckPermsUserSubject.java | 3 ++ 10 files changed, 62 insertions(+), 47 deletions(-) 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 97c284a6..43d5e0df 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 @@ -28,6 +28,7 @@ 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.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; @@ -83,11 +84,8 @@ public class VaultChatHook extends Chat { perms.log("Setting meta: '" + node + "' for " + holder.getObjectName() + " on world " + world + ", server " + perms.getServer()); perms.getScheduler().scheduleTask(() -> { - String k = escapeCharacters(node); - String v = escapeCharacters(value); - List toRemove = holder.getNodes().stream() - .filter(n -> n.isMeta() && n.getMeta().getKey().equals(k)) + .filter(n -> n.isMeta() && n.getMeta().getKey().equals(node)) .collect(Collectors.toList()); toRemove.forEach(n -> { @@ -96,7 +94,7 @@ public class VaultChatHook extends Chat { } catch (ObjectLacksException ignored) {} }); - Node.Builder metaNode = new NodeBuilder("meta." + k + "." + v).setValue(true); + Node.Builder metaNode = NodeFactory.makeMetaNode(node, value).setValue(true); if (!perms.getServer().equalsIgnoreCase("global")) { metaNode.setServer(perms.getServer()); } 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 2aa71d74..c39b5587 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 @@ -22,7 +22,6 @@ 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; @@ -34,7 +33,7 @@ import me.lucko.luckperms.common.commands.utils.ArgumentUtils; import me.lucko.luckperms.common.commands.utils.ContextHelper; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.NodeBuilder; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.utils.Predicates; @@ -57,14 +56,13 @@ public class MetaSet extends SharedSubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { - String key = MetaUtils.escapeCharacters(args.get(0)); - String value = MetaUtils.escapeCharacters(args.get(1)); + String key = args.get(0); + String value = args.get(1); - String node = "meta." + key + "." + value; String server = ArgumentUtils.handleServer(2, args); String world = ArgumentUtils.handleWorld(3, args); - Node n = new NodeBuilder(node).setServer(server).setWorld(world).build(); + Node n = NodeFactory.makeMetaNode(key, value).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 f52c5a9d..277c69a9 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 @@ -22,7 +22,6 @@ 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; @@ -34,7 +33,7 @@ import me.lucko.luckperms.common.commands.utils.ArgumentUtils; import me.lucko.luckperms.common.commands.utils.ContextHelper; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; -import me.lucko.luckperms.common.core.NodeBuilder; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.utils.DateUtil; @@ -59,14 +58,13 @@ public class MetaSetTemp extends SharedSubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { - String key = MetaUtils.escapeCharacters(args.get(0)); - String value = MetaUtils.escapeCharacters(args.get(1)); + String key = args.get(0); + String value = args.get(1); long duration = ArgumentUtils.handleDuration(2, args); - String node = "meta." + key + "." + value; String server = ArgumentUtils.handleServer(3, args); String world = ArgumentUtils.handleWorld(4, args); - Node n = new NodeBuilder(node).setServer(server).setWorld(world).setExpiry(duration).build(); + Node n = NodeFactory.makeMetaNode(key, value).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/generic/meta/MetaUnset.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java index e78cdb57..4e2c9600 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java @@ -22,7 +22,6 @@ package me.lucko.luckperms.common.commands.generic.meta; -import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandException; @@ -54,7 +53,7 @@ public class MetaUnset extends SharedSubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { - String key = MetaUtils.escapeCharacters(args.get(0)); + String key = args.get(0); String server = ArgumentUtils.handleServer(1, args); String world = ArgumentUtils.handleWorld(2, args); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java index 1eaa002b..64854959 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java @@ -22,7 +22,6 @@ package me.lucko.luckperms.common.commands.generic.meta; -import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandException; @@ -54,7 +53,7 @@ public class MetaUnsetTemp extends SharedSubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { - String key = MetaUtils.escapeCharacters(args.get(0)); + String key = args.get(0); String server = ArgumentUtils.handleServer(1, args); String world = ArgumentUtils.handleWorld(2, args); 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 40662ccf..a530798c 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 @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; +import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.constants.Patterns; @@ -132,21 +133,21 @@ public class Node implements me.lucko.luckperms.api.Node { 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)); + meta = Maps.immutableEntry(MetaUtils.unescapeCharacters(metaPart.get(0)), MetaUtils.unescapeCharacters(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)); + prefix = Maps.immutableEntry(i, MetaUtils.unescapeCharacters(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)); + suffix = Maps.immutableEntry(i, MetaUtils.unescapeCharacters(suffixPart.get(1))); } resolvedShorthand = calculateShorthand(); 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 index 7ff7479a..2949d281 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java @@ -27,6 +27,7 @@ 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.MetaUtils; import me.lucko.luckperms.api.Node; import java.util.List; @@ -101,4 +102,16 @@ public class NodeFactory { public static Node.Builder builderFromExisting(Node other) { return new NodeBuilder(other); } + + public static NodeBuilder makeMetaNode(String key, String value) { + return new NodeBuilder("meta." + MetaUtils.escapeCharacters(key) + "." + MetaUtils.escapeCharacters(value)); + } + + public static NodeBuilder makePrefixNode(int priority, String prefix) { + return new NodeBuilder("prefix." + priority + "." + MetaUtils.escapeCharacters(prefix)); + } + + public static NodeBuilder makeSuffixNode(int priority, String suffix) { + return new NodeBuilder("suffix." + priority + "." + MetaUtils.escapeCharacters(suffix)); + } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java index f0855f56..81813269 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java @@ -26,6 +26,7 @@ import lombok.experimental.UtilityClass; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.core.NodeBuilder; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.sponge.service.LuckPermsService; @@ -72,13 +73,17 @@ public class MigrationUtils { contexts.removeAll("world"); for (Map.Entry opt : e.getValue().entrySet()) { - if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("suffix")) { + if (opt.getKey().equalsIgnoreCase("prefix")) { try { - holder.setPermission(new NodeBuilder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermission(NodeFactory.makePrefixNode(100, opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + } catch (ObjectAlreadyHasException ignored) {} + } else if (opt.getKey().equalsIgnoreCase("suffix")) { + try { + holder.setPermission(NodeFactory.makeSuffixNode(100, opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } catch (ObjectAlreadyHasException ignored) {} } else { try { - holder.setPermission(new NodeBuilder("meta." + opt.getKey() + "." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermission(NodeFactory.makeMetaNode(opt.getKey(), opt.getValue()).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 91d34fa9..b08330d0 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 @@ -32,6 +32,7 @@ import lombok.NonNull; 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.NodeFactory; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; @@ -46,8 +47,6 @@ import org.spongepowered.api.util.Tristate; import java.util.*; import java.util.stream.Collectors; -import static me.lucko.luckperms.api.MetaUtils.escapeCharacters; - @SuppressWarnings({"OptionalGetWithoutIsPresent", "unused"}) @AllArgsConstructor public class LuckPermsSubjectData implements SubjectData { @@ -364,16 +363,15 @@ public class LuckPermsSubjectData implements SubjectData { public Map, Map> getAllOptions() { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_OPTIONS)) { Map, Map> options = new HashMap<>(); - - int prefixPriority = Integer.MIN_VALUE; - int suffixPriority = Integer.MIN_VALUE; + Map, Integer> minPrefixPriority = new HashMap<>(); + Map, Integer> minSuffixPriority = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { if (!n.getValue()) { continue; } - if (!n.isMeta() || !n.isPrefix() || n.isSuffix()) { + if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) { continue; } @@ -389,22 +387,24 @@ public class LuckPermsSubjectData implements SubjectData { if (!options.containsKey(contexts)) { options.put(contexts, new HashMap<>()); + minPrefixPriority.put(contexts, Integer.MIN_VALUE); + minSuffixPriority.put(contexts, Integer.MIN_VALUE); } if (n.isPrefix()) { Map.Entry value = n.getPrefix(); - if (value.getKey() > prefixPriority) { + if (value.getKey() > minPrefixPriority.get(contexts)) { options.get(contexts).put("prefix", value.getValue()); - prefixPriority = value.getKey(); + minPrefixPriority.put(contexts, value.getKey()); } continue; } if (n.isSuffix()) { Map.Entry value = n.getSuffix(); - if (value.getKey() > suffixPriority) { + if (value.getKey() > minSuffixPriority.get(contexts)) { options.get(contexts).put("suffix", value.getValue()); - suffixPriority = value.getKey(); + minSuffixPriority.put(contexts, value.getKey()); } continue; } @@ -433,20 +433,21 @@ public class LuckPermsSubjectData implements SubjectData { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) { ContextSet context = LuckPermsService.convertContexts(set); - key = escapeCharacters(key); - value = escapeCharacters(value); + List toRemove = holder.getNodes().stream() + .filter(n -> n.isMeta() && n.getMeta().getKey().equals(key)) + .collect(Collectors.toList()); + + toRemove.forEach(n -> { + try { + holder.unsetPermission(n); + } catch (ObjectLacksException ignored) {} + }); try { if (enduring) { - holder.setPermission(new NodeBuilder("meta." + key + "." + value) - .withExtraContext(context) - .build() - ); + holder.setPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); } else { - holder.setTransientPermission(new NodeBuilder("meta." + key + "." + value) - .withExtraContext(context) - .build() - ); + holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); } } catch (ObjectAlreadyHasException ignored) {} objectSave(holder); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java index 6df07f0e..8cd06825 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java @@ -135,6 +135,8 @@ public class LuckPermsUserSubject extends LuckPermsSubject { @Override public Optional getOption(ContextSet contexts, String s) { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.USER_GET_OPTION)) { + service.getPlugin().getLog().warn("#getOption: " + contexts + " - " + s); + if (hasData()) { MetaData data = user.getUserData().getMetaData(service.calculateContexts(contexts)); if (s.equalsIgnoreCase("prefix")) { @@ -149,6 +151,7 @@ public class LuckPermsUserSubject extends LuckPermsSubject { } } + service.getPlugin().getLog().info(data.getMeta().toString()); if (data.getMeta().containsKey(s)) { return Optional.of(data.getMeta().get(s)); }