diff --git a/common/src/main/java/me/lucko/luckperms/common/api/delegates/NodeFactoryDelegate.java b/common/src/main/java/me/lucko/luckperms/common/api/delegates/NodeFactoryDelegate.java index 8a8ea0a8..95dac0a2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/delegates/NodeFactoryDelegate.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/delegates/NodeFactoryDelegate.java @@ -32,7 +32,7 @@ public class NodeFactoryDelegate implements me.lucko.luckperms.api.NodeFactory { @Override public Node fromSerialisedNode(@NonNull String serialisedPermission, boolean value) { - return NodeFactory.fromSerialisedNode(serialisedPermission, value); + return NodeFactory.fromSerializedNode(serialisedPermission, value); } @Override @@ -47,7 +47,7 @@ public class NodeFactoryDelegate implements me.lucko.luckperms.api.NodeFactory { @Override public Node.Builder newBuilderFromSerialisedNode(@NonNull String serialisedPermission, boolean value) { - return NodeFactory.builderFromSerialisedNode(serialisedPermission, value); + return NodeFactory.builderFromSerializedNode(serialisedPermission, value); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/config/StaticContextsFile.java b/common/src/main/java/me/lucko/luckperms/common/config/StaticContextsFile.java index 710dd6e8..6d09d20b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/StaticContextsFile.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/StaticContextsFile.java @@ -3,14 +3,12 @@ package me.lucko.luckperms.common.config; import lombok.Getter; import lombok.RequiredArgsConstructor; -import com.google.common.collect.ImmutableSetMultimap; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.common.core.NodeModel; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -18,7 +16,6 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.Map; @RequiredArgsConstructor public class StaticContextsFile { @@ -49,21 +46,7 @@ public class StaticContextsFile { } JsonObject contexts = data.get("context").getAsJsonObject(); - ImmutableSetMultimap.Builder map = ImmutableSetMultimap.builder(); - - for (Map.Entry e : contexts.entrySet()) { - JsonElement val = e.getValue(); - if (val.isJsonArray()) { - JsonArray vals = val.getAsJsonArray(); - for (JsonElement element : vals) { - map.put(e.getKey(), element.getAsString()); - } - } else { - map.put(e.getKey(), val.getAsString()); - } - } - - contextSet = ImmutableContextSet.fromMultimap(map.build()); + contextSet = NodeModel.deserializeContextSet(contexts).makeImmutable(); } catch (IOException e) { e.printStackTrace(); } 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 08f2b9a4..865ee472 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 @@ -43,12 +43,12 @@ import java.util.Map; @UtilityClass public class NodeFactory { private static final LoadingCache CACHE = Caffeine.newBuilder() - .build(s -> builderFromSerialisedNode(s, true).build()); + .build(s -> builderFromSerializedNode(s, true).build()); private static final LoadingCache CACHE_NEGATED = Caffeine.newBuilder() - .build(s -> builderFromSerialisedNode(s, false).build()); + .build(s -> builderFromSerializedNode(s, false).build()); - public static Node fromSerialisedNode(String s, Boolean b) { + public static Node fromSerializedNode(String s, Boolean b) { try { return b ? CACHE.get(s) : CACHE_NEGATED.get(s); } catch (Exception e) { @@ -60,7 +60,7 @@ public class NodeFactory { return new NodeBuilder(s, false); } - public static Node.Builder builderFromSerialisedNode(String s, Boolean b) { + public static Node.Builder builderFromSerializedNode(String s, Boolean b) { // if contains / if (Patterns.compileDelimitedMatcher("/", "\\").matcher(s).find()) { List parts = Splitter.on(Patterns.compileDelimitedMatcher("/", "\\")).limit(2).splitToList(s); diff --git a/common/src/main/java/me/lucko/luckperms/common/core/NodeModel.java b/common/src/main/java/me/lucko/luckperms/common/core/NodeModel.java new file mode 100644 index 00000000..c133de61 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeModel.java @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2017 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 lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSetMultimap; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.api.context.MutableContextSet; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * An stripped down version of {@link Node}, without methods and cached values for handling permission lookups. + * + * All values are non-null. + */ +@Getter +@ToString +@EqualsAndHashCode +@AllArgsConstructor(staticName = "of") +public final class NodeModel { + private static final Gson GSON = new Gson(); + + public static NodeModel fromNode(Node node) { + return NodeModel.of( + node.getPermission(), + node.getValue(), + node.getServer().orElse("global"), + node.getWorld().orElse("global"), + node.isTemporary() ? node.getExpiryUnixTime() : 0L, + node.getContexts().makeImmutable() + ); + } + + public static NodeModel deserialize(String permission, boolean value, String server, String world, long expiry, String contexts) { + JsonObject context = GSON.fromJson(contexts, JsonObject.class); + return of(permission, value, server, world, expiry, deserializeContextSet(context).makeImmutable()); + } + + private final String permission; + private final boolean value; + private final String server; + private final String world; + private final long expiry; + private final ImmutableContextSet contexts; + + public String serializeContext() { + return GSON.toJson(getContextsAsJson()); + } + + public JsonObject getContextsAsJson() { + return serializeContextSet(contexts); + } + + public Node toNode() { + Node.Builder builder = NodeFactory.newBuilder(permission); + builder.setValue(value); + builder.setServer(server); + builder.setWorld(world); + builder.setExpiry(expiry); + builder.withExtraContext(contexts); + return builder.build(); + } + + public NodeModel setPermission(String permission) { + return of(permission, value, server, world, expiry, contexts); + } + + public NodeModel setValue(boolean value) { + return of(permission, value, server, world, expiry, contexts); + } + + public NodeModel setServer(String server) { + return of(permission, value, server, world, expiry, contexts); + } + + public NodeModel setWorld(String world) { + return of(permission, value, server, world, expiry, contexts); + } + + public NodeModel setExpiry(long expiry) { + return of(permission, value, server, world, expiry, contexts); + } + + public NodeModel setContexts(ImmutableContextSet contexts) { + return of(permission, value, server, world, expiry, contexts); + } + + public static JsonObject serializeContextSet(ContextSet contextSet) { + JsonObject data = new JsonObject(); + Map> map = contextSet.toMultimap().asMap(); + + map.forEach((k, v) -> { + List values = new ArrayList<>(v); + int size = values.size(); + + if (size == 1) { + data.addProperty(k, values.get(0)); + } else if (size > 1) { + JsonArray arr = new JsonArray(); + for (String s : values) { + arr.add(new JsonPrimitive(s)); + } + data.add(k, arr); + } + }); + + return data; + } + + public static MutableContextSet deserializeContextSet(JsonElement element) { + Preconditions.checkArgument(element.isJsonObject()); + JsonObject data = element.getAsJsonObject(); + + ImmutableSetMultimap.Builder map = ImmutableSetMultimap.builder(); + for (Map.Entry e : data.entrySet()) { + String k = e.getKey(); + JsonElement v = e.getValue(); + if (v.isJsonArray()) { + JsonArray values = v.getAsJsonArray(); + for (JsonElement value : values) { + map.put(k, value.getAsString()); + } + } else { + map.put(k, v.getAsString()); + } + } + + return MutableContextSet.fromMultimap(map.build()); + } + +} 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 18f9566b..4002a354 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 @@ -37,7 +37,7 @@ public class LogicParser { try { ScriptEngine engine = Scripting.getScriptEngine(); - String expression = generateExpression(s, s1 -> holder.hasPermission(NodeFactory.fromSerialisedNode(s1, true)) == tristate); + String expression = generateExpression(s, s1 -> holder.hasPermission(NodeFactory.fromSerializedNode(s1, true)) == tristate); if (engine == null) { throw new NullPointerException("script engine"); } 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 9dd04b56..78187bf8 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 @@ -89,11 +89,11 @@ public class Rule { // The holder meets all of the requirements of this rule. for (String s : toTake) { - user.unsetPermission(NodeFactory.fromSerialisedNode(s, true)); + user.unsetPermission(NodeFactory.fromSerializedNode(s, true)); } for (String s : toGive) { - user.setPermission(NodeFactory.fromSerialisedNode(s, true)); + user.setPermission(NodeFactory.fromSerializedNode(s, true)); } if (setPrimaryGroup != null) { 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 9e8ce4d9..9603e5ed 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 @@ -23,7 +23,6 @@ package me.lucko.luckperms.common.storage.backing; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Iterables; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -34,6 +33,8 @@ import com.google.gson.JsonPrimitive; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.core.PriorityComparator; import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.Group; @@ -41,7 +42,6 @@ import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; -import me.lucko.luckperms.common.storage.backing.utils.NodeDataHolder; import me.lucko.luckperms.common.storage.holder.NodeHeldPermission; import java.io.BufferedReader; @@ -51,7 +51,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -108,8 +107,8 @@ public class JSONBacking extends FlatfileBacking { String name = object.get("name").getAsString(); user.getPrimaryGroup().setStoredValue(object.get("primaryGroup").getAsString()); - Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); user.setNodes(nodes); boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -172,7 +171,7 @@ public class JSONBacking extends FlatfileBacking { data.addProperty("name", user.getName()); data.addProperty("primaryGroup", user.getPrimaryGroup().getStoredValue()); - Set nodes = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set nodes = user.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(userFile, data); @@ -191,7 +190,7 @@ public class JSONBacking extends FlatfileBacking { for (File file : files) { registerFileAction("users", file); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readObjectFromFile(file, object -> { nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); return true; @@ -199,7 +198,7 @@ public class JSONBacking extends FlatfileBacking { boolean shouldDelete = false; if (nodes.size() == 1) { - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { // There's only one shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); } @@ -224,14 +223,14 @@ public class JSONBacking extends FlatfileBacking { registerFileAction("users", file); UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 5)); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readObjectFromFile(file, object -> { nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); return true; }); - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { if (!e.getPermission().equalsIgnoreCase(permission)) { continue; } @@ -255,8 +254,8 @@ public class JSONBacking extends FlatfileBacking { if (groupFile.exists()) { return readObjectFromFile(groupFile, object -> { - Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setNodes(nodes); return true; }); @@ -271,7 +270,7 @@ public class JSONBacking extends FlatfileBacking { JsonObject data = new JsonObject(); data.addProperty("name", group.getName()); - Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set nodes = group.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(groupFile, data); @@ -292,8 +291,8 @@ public class JSONBacking extends FlatfileBacking { registerFileAction("groups", groupFile); return groupFile.exists() && readObjectFromFile(groupFile, object -> { - Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions(object.get("permissions").getAsJsonArray()); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setNodes(nodes); return true; }); @@ -322,7 +321,7 @@ public class JSONBacking extends FlatfileBacking { JsonObject data = new JsonObject(); data.addProperty("name", group.getName()); - Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set nodes = group.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(groupFile, data); }, false); @@ -342,13 +341,13 @@ public class JSONBacking extends FlatfileBacking { registerFileAction("groups", file); String holder = file.getName().substring(0, file.getName().length() - 5); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readObjectFromFile(file, element -> { nodes.addAll(deserializePermissions(element.get("permissions").getAsJsonArray())); return true; }); - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { if (!e.getPermission().equalsIgnoreCase(permission)) { continue; } @@ -459,8 +458,8 @@ public class JSONBacking extends FlatfileBacking { } } - public static Set deserializePermissions(JsonArray permissionsSection) { - Set nodes = new HashSet<>(); + public static Set deserializePermissions(JsonArray permissionsSection) { + Set nodes = new HashSet<>(); for (JsonElement ent : permissionsSection) { if (!ent.isJsonObject()) { @@ -481,7 +480,7 @@ public class JSONBacking extends FlatfileBacking { String server = "global"; String world = "global"; long expiry = 0L; - ImmutableSetMultimap context = ImmutableSetMultimap.of(); + ImmutableContextSet context = ImmutableContextSet.empty(); if (attributes.has("value")) { value = attributes.get("value").getAsBoolean(); @@ -498,33 +497,19 @@ public class JSONBacking extends FlatfileBacking { if (attributes.has("context") && attributes.get("context").isJsonObject()) { JsonObject contexts = attributes.get("context").getAsJsonObject(); - ImmutableSetMultimap.Builder map = ImmutableSetMultimap.builder(); - - for (Map.Entry e : contexts.entrySet()) { - JsonElement val = e.getValue(); - if (val.isJsonArray()) { - JsonArray vals = val.getAsJsonArray(); - for (JsonElement element : vals) { - map.put(e.getKey(), element.getAsString()); - } - } else { - map.put(e.getKey(), val.getAsString()); - } - } - - context = map.build(); + context = NodeModel.deserializeContextSet(contexts).makeImmutable(); } - nodes.add(NodeDataHolder.of(permission, value, server, world, expiry, context)); + nodes.add(NodeModel.of(permission, value, server, world, expiry, context)); } return nodes; } - public static JsonArray serializePermissions(Set nodes) { + public static JsonArray serializePermissions(Set nodes) { List data = new ArrayList<>(); - for (NodeDataHolder node : nodes) { + for (NodeModel node : nodes) { JsonObject attributes = new JsonObject(); attributes.addProperty("value", node.isValue()); @@ -541,25 +526,7 @@ public class JSONBacking extends FlatfileBacking { } if (!node.getContexts().isEmpty()) { - JsonObject context = new JsonObject(); - Map> map = node.getContexts().asMap(); - - for (Map.Entry> e : map.entrySet()) { - List vals = new ArrayList<>(e.getValue()); - int size = vals.size(); - - if (size == 1) { - context.addProperty(e.getKey(), vals.get(0)); - } else if (size > 1) { - JsonArray arr = new JsonArray(); - for (String s : vals) { - arr.add(new JsonPrimitive(s)); - } - context.add(e.getKey(), arr); - } - } - - attributes.add("context", context); + attributes.add("context", node.getContextsAsJson()); } JsonObject perm = new JsonObject(); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java index dffb04ba..e9ea757f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java @@ -237,7 +237,7 @@ public class MongoDBBacking extends AbstractBacking { // User exists, let's load. Document d = cursor.next(); user.setNodes(revert((Map) d.get("perms")).entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) .collect(Collectors.toSet()) ); user.getPrimaryGroup().setStoredValue(d.getString("primaryGroup")); @@ -341,7 +341,7 @@ public class MongoDBBacking extends AbstractBacking { Map perms = revert((Map) d.get("perms")); for (Map.Entry e : perms.entrySet()) { - Node node = NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()); + Node node = NodeFactory.fromSerializedNode(e.getKey(), e.getValue()); if (!node.getPermission().equalsIgnoreCase(permission)) { continue; } @@ -369,7 +369,7 @@ public class MongoDBBacking extends AbstractBacking { // Group exists, let's load. Document d = cursor.next(); group.setNodes(revert((Map) d.get("perms")).entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) .collect(Collectors.toSet()) ); } else { @@ -396,7 +396,7 @@ public class MongoDBBacking extends AbstractBacking { Document d = cursor.next(); group.setNodes(revert((Map) d.get("perms")).entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) .collect(Collectors.toSet()) ); return true; @@ -481,7 +481,7 @@ public class MongoDBBacking extends AbstractBacking { Map perms = revert((Map) d.get("perms")); for (Map.Entry e : perms.entrySet()) { - Node node = NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()); + Node node = NodeFactory.fromSerializedNode(e.getKey(), e.getValue()); if (!node.getPermission().equalsIgnoreCase(permission)) { continue; } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java index 65222100..53c835fc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java @@ -32,6 +32,7 @@ import com.google.gson.reflect.TypeToken; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Track; @@ -43,7 +44,6 @@ import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.backing.sqlprovider.SQLProvider; import me.lucko.luckperms.common.storage.backing.utils.LegacySQLSchemaMigration; -import me.lucko.luckperms.common.storage.backing.utils.NodeDataHolder; import me.lucko.luckperms.common.storage.holder.NodeHeldPermission; import java.io.BufferedReader; @@ -253,7 +253,7 @@ public class SQLBacking extends AbstractBacking { User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); user.getIoLock().lock(); try { - List data = new ArrayList<>(); + List data = new ArrayList<>(); AtomicReference primaryGroup = new AtomicReference<>(null); AtomicReference userName = new AtomicReference<>(null); @@ -270,7 +270,7 @@ public class SQLBacking extends AbstractBacking { String world = rs.getString("world"); long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - data.add(NodeDataHolder.of(permission, value, server, world, expiry, contexts)); + data.add(NodeModel.deserialize(permission, value, server, world, expiry, contexts)); } } } @@ -315,7 +315,7 @@ public class SQLBacking extends AbstractBacking { // If the user has any data in storage if (!data.isEmpty()) { - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); user.setNodes(nodes); // Save back to the store if data was changed @@ -364,7 +364,7 @@ public class SQLBacking extends AbstractBacking { } // Get a snapshot of current data. - Set remote = new HashSet<>(); + Set remote = new HashSet<>(); try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(USER_PERMISSIONS_SELECT))) { ps.setString(1, user.getUuid().toString()); @@ -377,7 +377,7 @@ public class SQLBacking extends AbstractBacking { String world = rs.getString("world"); long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - remote.add(NodeDataHolder.of(permission, value, server, world, expiry, contexts)); + remote.add(NodeModel.deserialize(permission, value, server, world, expiry, contexts)); } } } @@ -385,24 +385,24 @@ public class SQLBacking extends AbstractBacking { return false; } - Set local = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set local = user.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet()); - Map.Entry, Set> diff = compareSets(local, remote); + Map.Entry, Set> diff = compareSets(local, remote); - Set toAdd = diff.getKey(); - Set toRemove = diff.getValue(); + Set toAdd = diff.getKey(); + Set toRemove = diff.getValue(); if (!toRemove.isEmpty()) { try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(USER_PERMISSIONS_DELETE_SPECIFIC))) { - for (NodeDataHolder nd : toRemove) { + for (NodeModel nd : toRemove) { ps.setString(1, user.getUuid().toString()); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); @@ -416,14 +416,14 @@ public class SQLBacking extends AbstractBacking { if (!toAdd.isEmpty()) { try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(USER_PERMISSIONS_INSERT))) { - for (NodeDataHolder nd : toAdd) { + for (NodeModel nd : toAdd) { ps.setString(1, user.getUuid().toString()); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); @@ -490,7 +490,7 @@ public class SQLBacking extends AbstractBacking { long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - NodeDataHolder data = NodeDataHolder.of(permission, value, server, world, expiry, contexts); + NodeModel data = NodeModel.deserialize(permission, value, server, world, expiry, contexts); held.add(NodeHeldPermission.of(holder, data)); } } @@ -558,7 +558,7 @@ public class SQLBacking extends AbstractBacking { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock(); try { - List data = new ArrayList<>(); + List data = new ArrayList<>(); try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_PERMISSIONS_SELECT))) { @@ -572,7 +572,7 @@ public class SQLBacking extends AbstractBacking { String world = rs.getString("world"); long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - data.add(NodeDataHolder.of(permission, value, server, world, expiry, contexts)); + data.add(NodeModel.deserialize(permission, value, server, world, expiry, contexts)); } } } @@ -582,7 +582,7 @@ public class SQLBacking extends AbstractBacking { } if (!data.isEmpty()) { - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setNodes(nodes); } else { group.clearNodes(); @@ -646,7 +646,7 @@ public class SQLBacking extends AbstractBacking { } // Get a snapshot of current data - Set remote = new HashSet<>(); + Set remote = new HashSet<>(); try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_PERMISSIONS_SELECT))) { ps.setString(1, group.getName()); @@ -659,7 +659,7 @@ public class SQLBacking extends AbstractBacking { String world = rs.getString("world"); long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - remote.add(NodeDataHolder.of(permission, value, server, world, expiry, contexts)); + remote.add(NodeModel.deserialize(permission, value, server, world, expiry, contexts)); } } } @@ -668,24 +668,24 @@ public class SQLBacking extends AbstractBacking { return false; } - Set local = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set local = group.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet()); - Map.Entry, Set> diff = compareSets(local, remote); + Map.Entry, Set> diff = compareSets(local, remote); - Set toAdd = diff.getKey(); - Set toRemove = diff.getValue(); + Set toAdd = diff.getKey(); + Set toRemove = diff.getValue(); if (!toRemove.isEmpty()) { try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_PERMISSIONS_DELETE_SPECIFIC))) { - for (NodeDataHolder nd : toRemove) { + for (NodeModel nd : toRemove) { ps.setString(1, group.getName()); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); @@ -699,14 +699,14 @@ public class SQLBacking extends AbstractBacking { if (!toAdd.isEmpty()) { try (Connection c = provider.getConnection()) { try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_PERMISSIONS_INSERT))) { - for (NodeDataHolder nd : toAdd) { + for (NodeModel nd : toAdd) { ps.setString(1, group.getName()); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); @@ -764,7 +764,7 @@ public class SQLBacking extends AbstractBacking { long expiry = rs.getLong("expiry"); String contexts = rs.getString("contexts"); - NodeDataHolder data = NodeDataHolder.of(permission, value, server, world, expiry, contexts); + NodeModel data = NodeModel.deserialize(permission, value, server, world, expiry, contexts); held.add(NodeHeldPermission.of(holder, data)); } } @@ -1040,14 +1040,14 @@ public class SQLBacking extends AbstractBacking { * @param remote the remote set * @return the entries to add to remote, and the entries to remove from remote */ - private static Map.Entry, Set> compareSets(Set local, Set remote) { + private static Map.Entry, Set> compareSets(Set local, Set remote) { // entries in local but not remote need to be added // entries in remote but not local need to be removed - Set toAdd = new HashSet<>(local); + Set toAdd = new HashSet<>(local); toAdd.removeAll(remote); - Set toRemove = new HashSet<>(remote); + Set toRemove = new HashSet<>(remote); toRemove.removeAll(local); return Maps.immutableEntry(toAdd, toRemove); 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 930c681b..1d3529a0 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 @@ -28,13 +28,13 @@ import com.google.common.collect.Iterables; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; -import me.lucko.luckperms.common.storage.backing.utils.NodeDataHolder; import me.lucko.luckperms.common.storage.holder.NodeHeldPermission; import org.yaml.snakeyaml.DumperOptions; @@ -109,8 +109,8 @@ public class YAMLBacking extends FlatfileBacking { String name = (String) values.get("name"); user.getPrimaryGroup().setStoredValue((String) values.get("primary-group")); - Set data = deserializePermissions((List) values.get("permissions")); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions((List) values.get("permissions")); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); user.setNodes(nodes); boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false); @@ -171,7 +171,7 @@ public class YAMLBacking extends FlatfileBacking { values.put("name", user.getName()); values.put("primary-group", user.getPrimaryGroup().getStoredValue()); - Set data = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set data = user.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(userFile, values); @@ -190,7 +190,7 @@ public class YAMLBacking extends FlatfileBacking { for (File file : files) { registerFileAction("users", file); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readMapFromFile(file, values -> { nodes.addAll(deserializePermissions((List) values.get("permissions"))); return true; @@ -198,7 +198,7 @@ public class YAMLBacking extends FlatfileBacking { boolean shouldDelete = false; if (nodes.size() == 1) { - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { // There's only one shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); } @@ -223,13 +223,13 @@ public class YAMLBacking extends FlatfileBacking { registerFileAction("users", file); UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 4)); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readMapFromFile(file, values -> { nodes.addAll(deserializePermissions((List) values.get("permissions"))); return true; }); - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { if (!e.getPermission().equalsIgnoreCase(permission)) { continue; } @@ -253,8 +253,8 @@ public class YAMLBacking extends FlatfileBacking { if (groupFile.exists()) { return readMapFromFile(groupFile, values -> { - Set data = deserializePermissions((List) values.get("permissions")); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions((List) values.get("permissions")); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setNodes(nodes); return true; }); @@ -268,7 +268,7 @@ public class YAMLBacking extends FlatfileBacking { Map values = new LinkedHashMap<>(); values.put("name", group.getName()); - Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set data = group.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(groupFile, values); } @@ -288,8 +288,8 @@ public class YAMLBacking extends FlatfileBacking { registerFileAction("groups", groupFile); return groupFile.exists() && readMapFromFile(groupFile, values -> { - Set data = deserializePermissions((List) values.get("permissions")); - Set nodes = data.stream().map(NodeDataHolder::toNode).collect(Collectors.toSet()); + Set data = deserializePermissions((List) values.get("permissions")); + Set nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setNodes(nodes); return true; }); @@ -318,7 +318,7 @@ public class YAMLBacking extends FlatfileBacking { Map values = new LinkedHashMap<>(); values.put("name", group.getName()); - Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); + Set data = group.getNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(groupFile, values); }, false); @@ -338,13 +338,13 @@ public class YAMLBacking extends FlatfileBacking { registerFileAction("groups", file); String holder = file.getName().substring(0, file.getName().length() - 4); - Set nodes = new HashSet<>(); + Set nodes = new HashSet<>(); readMapFromFile(file, values -> { nodes.addAll(deserializePermissions((List) values.get("permissions"))); return true; }); - for (NodeDataHolder e : nodes) { + for (NodeModel e : nodes) { if (!e.getPermission().equalsIgnoreCase(permission)) { continue; } @@ -437,8 +437,8 @@ public class YAMLBacking extends FlatfileBacking { } } - public static Set deserializePermissions(List permissionsSection) { - Set nodes = new HashSet<>(); + public static Set deserializePermissions(List permissionsSection) { + Set nodes = new HashSet<>(); for (Object perm : permissionsSection) { @@ -498,17 +498,17 @@ public class YAMLBacking extends FlatfileBacking { context = map.build(); } - nodes.add(NodeDataHolder.of(permission, value, server, world, expiry, context)); + nodes.add(NodeModel.of(permission, value, server, world, expiry, context)); } } return nodes; } - public static List> serializePermissions(Set nodes) { + public static List> serializePermissions(Set nodes) { List> data = new ArrayList<>(); - for (NodeDataHolder node : nodes) { + for (NodeModel node : nodes) { Map attributes = new LinkedHashMap<>(); attributes.put("value", node.isValue()); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java index 7a89ad31..e12039d4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java @@ -28,6 +28,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.backing.JSONBacking; @@ -79,9 +80,9 @@ public class LegacyJSONSchemaMigration implements Runnable { return true; }); - Set nodes = perms.entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = perms.entrySet().stream() + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { @@ -133,9 +134,9 @@ public class LegacyJSONSchemaMigration implements Runnable { return true; }); - Set nodes = perms.entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = perms.entrySet().stream() + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacySQLSchemaMigration.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacySQLSchemaMigration.java index 6a26645a..f37d510f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacySQLSchemaMigration.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacySQLSchemaMigration.java @@ -28,6 +28,7 @@ import com.google.common.collect.Lists; import com.google.gson.reflect.TypeToken; import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.storage.backing.SQLBacking; import java.lang.reflect.Type; @@ -147,21 +148,21 @@ public class LegacySQLSchemaMigration implements Runnable { continue; } - Set nodes = convertedPerms.entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = convertedPerms.entrySet().stream() + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toSet()); try (Connection c = backing.getProvider().getConnection()) { try (PreparedStatement ps = c.prepareStatement(backing.getPrefix().apply("INSERT INTO {prefix}user_permissions(uuid, permission, value, server, world, expiry, contexts) VALUES(?, ?, ?, ?, ?, ?, ?)"))) { - for (NodeDataHolder nd : nodes) { + for (NodeModel nd : nodes) { ps.setString(1, uuid.toString()); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); @@ -226,21 +227,21 @@ public class LegacySQLSchemaMigration implements Runnable { continue; } - Set nodes = convertedPerms.entrySet().stream() - .map(ent -> NodeFactory.fromSerialisedNode(ent.getKey(), ent.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = convertedPerms.entrySet().stream() + .map(ent -> NodeFactory.fromSerializedNode(ent.getKey(), ent.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toSet()); try (Connection c = backing.getProvider().getConnection()) { try (PreparedStatement ps = c.prepareStatement(backing.getPrefix().apply("INSERT INTO {prefix}group_permissions(name, permission, value, server, world, expiry, contexts) VALUES(?, ?, ?, ?, ?, ?, ?)"))) { - for (NodeDataHolder nd : nodes) { + for (NodeModel nd : nodes) { ps.setString(1, name); ps.setString(2, nd.getPermission()); ps.setBoolean(3, nd.isValue()); ps.setString(4, nd.getServer()); ps.setString(5, nd.getWorld()); ps.setLong(6, nd.getExpiry()); - ps.setString(7, nd.serialiseContext()); + ps.setString(7, nd.serializeContext()); ps.addBatch(); } ps.executeBatch(); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java index 18a87244..3bfc6961 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.storage.backing.utils; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.backing.YAMLBacking; @@ -74,9 +75,9 @@ public class LegacyYAMLSchemaMigration implements Runnable { return true; }); - Set nodes = perms.entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = perms.entrySet().stream() + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { @@ -125,9 +126,9 @@ public class LegacyYAMLSchemaMigration implements Runnable { return true; }); - Set nodes = perms.entrySet().stream() - .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) - .map(NodeDataHolder::fromNode) + Set nodes = perms.entrySet().stream() + .map(e -> NodeFactory.fromSerializedNode(e.getKey(), e.getValue())) + .map(NodeModel::fromNode) .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java deleted file mode 100644 index d3221aee..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2016 Lucko (Luck) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.common.storage.backing.utils; - -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.ToString; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSetMultimap; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.common.core.NodeFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -@Getter -@ToString -@EqualsAndHashCode -@AllArgsConstructor(staticName = "of") -public final class NodeDataHolder { - private static final Gson GSON = new Gson(); - - public static NodeDataHolder fromNode(Node node) { - return NodeDataHolder.of( - node.getPermission(), - node.getValue(), - node.getServer().orElse("global"), - node.getWorld().orElse("global"), - node.isTemporary() ? node.getExpiryUnixTime() : 0L, - ImmutableSetMultimap.copyOf(node.getContexts().toMultimap()) - ); - } - - public static NodeDataHolder of(String permission, boolean value, String server, String world, long expiry, String contexts) { - JsonObject context = GSON.fromJson(contexts, JsonObject.class); - ImmutableSetMultimap.Builder map = ImmutableSetMultimap.builder(); - for (Map.Entry e : context.entrySet()) { - JsonElement val = e.getValue(); - if (val.isJsonArray()) { - JsonArray vals = val.getAsJsonArray(); - for (JsonElement element : vals) { - map.put(e.getKey(), element.getAsString()); - } - } else { - map.put(e.getKey(), val.getAsString()); - } - } - return new NodeDataHolder(permission, value, server, world, expiry, map.build()); - } - - private final String permission; - private final boolean value; - private final String server; - private final String world; - private final long expiry; - private final ImmutableSetMultimap contexts; - - public String serialiseContext() { - JsonObject context = new JsonObject(); - ImmutableMap> map = getContexts().asMap(); - - map.forEach((key, value) -> { - List vals = new ArrayList<>(value); - int size = vals.size(); - - if (size == 1) { - context.addProperty(key, vals.get(0)); - } else if (size > 1) { - JsonArray arr = new JsonArray(); - for (String s : vals) { - arr.add(new JsonPrimitive(s)); - } - context.add(key, arr); - } - }); - - return GSON.toJson(context); - } - - public Node toNode() { - Node.Builder builder = NodeFactory.newBuilder(permission); - builder.setValue(value); - builder.setServer(server); - builder.setWorld(world); - builder.setExpiry(expiry); - - for (Map.Entry e : contexts.entries()) { - builder.withExtraContext(e); - } - - return builder.build(); - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/holder/NodeHeldPermission.java b/common/src/main/java/me/lucko/luckperms/common/storage/holder/NodeHeldPermission.java index d9420914..1f679e5b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/holder/NodeHeldPermission.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/holder/NodeHeldPermission.java @@ -30,7 +30,7 @@ import com.google.common.collect.Multimap; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.common.storage.backing.utils.NodeDataHolder; +import me.lucko.luckperms.common.core.NodeModel; import java.util.Optional; import java.util.OptionalLong; @@ -39,8 +39,8 @@ import java.util.OptionalLong; @EqualsAndHashCode @AllArgsConstructor(staticName = "of") public final class NodeHeldPermission implements HeldPermission { - public static NodeHeldPermission of(T holder, NodeDataHolder nodeDataHolder) { - return of(holder, nodeDataHolder.toNode()); + public static NodeHeldPermission of(T holder, NodeModel nodeModel) { + return of(holder, nodeModel.toNode()); } private final T holder; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java index 1957f0a1..5e255068 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java @@ -30,18 +30,15 @@ import com.google.common.collect.ImmutableMap; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; -import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.core.ContextSetComparator; +import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.core.PriorityComparator; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.references.SubjectReference; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Map; @@ -100,7 +97,7 @@ public class SubjectStorageModel { JsonObject context = section.get("context").getAsJsonObject(); JsonObject data = section.get("data").getAsJsonObject(); - ImmutableContextSet contextSet = contextsFromJson(context); + ImmutableContextSet contextSet = NodeModel.deserializeContextSet(context).makeImmutable(); ImmutableMap.Builder perms = ImmutableMap.builder(); for (Map.Entry perm : data.entrySet()) { perms.put(perm.getKey(), perm.getValue().getAsBoolean()); @@ -123,7 +120,7 @@ public class SubjectStorageModel { JsonObject context = section.get("context").getAsJsonObject(); JsonObject data = section.get("data").getAsJsonObject(); - ImmutableContextSet contextSet = contextsFromJson(context); + ImmutableContextSet contextSet = NodeModel.deserializeContextSet(context).makeImmutable(); ImmutableMap.Builder opts = ImmutableMap.builder(); for (Map.Entry opt : data.entrySet()) { opts.put(opt.getKey(), opt.getValue().getAsString()); @@ -146,7 +143,7 @@ public class SubjectStorageModel { JsonObject context = section.get("context").getAsJsonObject(); JsonArray data = section.get("data").getAsJsonArray(); - ImmutableContextSet contextSet = contextsFromJson(context); + ImmutableContextSet contextSet = NodeModel.deserializeContextSet(context).makeImmutable(); ImmutableList.Builder pars = ImmutableList.builder(); for (JsonElement p : data) { if (!p.isJsonObject()) { @@ -182,7 +179,7 @@ public class SubjectStorageModel { } JsonObject section = new JsonObject(); - section.add("context", contextsToJson(e.getKey())); + section.add("context", NodeModel.serializeContextSet(e.getKey())); JsonObject data = new JsonObject(); @@ -206,7 +203,7 @@ public class SubjectStorageModel { } JsonObject section = new JsonObject(); - section.add("context", contextsToJson(e.getKey())); + section.add("context", NodeModel.serializeContextSet(e.getKey())); JsonObject data = new JsonObject(); @@ -230,7 +227,7 @@ public class SubjectStorageModel { } JsonObject section = new JsonObject(); - section.add("context", contextsToJson(e.getKey())); + section.add("context", NodeModel.serializeContextSet(e.getKey())); JsonArray data = new JsonArray(); for (SubjectReference ref : e.getValue()) { @@ -253,40 +250,4 @@ public class SubjectStorageModel { subjectData.replaceOptions(options); subjectData.replaceParents(parents); } - - private static ImmutableContextSet contextsFromJson(JsonObject contexts) { - MutableContextSet ret = MutableContextSet.create(); - for (Map.Entry e : contexts.entrySet()) { - String key = e.getKey(); - - if (e.getValue().isJsonArray()) { - JsonArray values = e.getValue().getAsJsonArray(); - for (JsonElement value : values) { - ret.add(key, value.getAsString()); - } - } else { - ret.add(key, e.getValue().getAsString()); - } - } - return ret.makeImmutable(); - } - - private static JsonObject contextsToJson(ContextSet contexts) { - JsonObject ret = new JsonObject(); - for (Map.Entry> e : contexts.toMultimap().asMap().entrySet()) { - String key = e.getKey(); - List values = new ArrayList<>(e.getValue()); - - if (values.size() == 1) { - ret.addProperty(key, values.get(0)); - } else if (values.size() > 1) { - JsonArray arr = new JsonArray(); - for (String s : values) { - arr.add(new JsonPrimitive(s)); - } - ret.add(key, arr); - } - } - return ret; - } }