mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Separate prefix/suffix/meta nodes into their own section within yaml/json/hocon storage files
This commit is contained in:
parent
966bf8bf51
commit
2889c28815
@ -98,6 +98,10 @@ public final class NodeFactory {
|
|||||||
return GROUP_NODE_MARKER + groupName;
|
return GROUP_NODE_MARKER + groupName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String chatMetaNode(ChatMetaType type, int priority, String value) {
|
||||||
|
return type == ChatMetaType.PREFIX ? prefixNode(priority, value) : suffixNode(priority, value);
|
||||||
|
}
|
||||||
|
|
||||||
public static String prefixNode(int priority, String prefix) {
|
public static String prefixNode(int priority, String prefix) {
|
||||||
return PREFIX_NODE_MARKER + priority + "." + LegacyNodeFactory.escapeCharacters(prefix);
|
return PREFIX_NODE_MARKER + priority + "." + LegacyNodeFactory.escapeCharacters(prefix);
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,10 @@ public final class NodeModel {
|
|||||||
return new NodeModel(permission, value, server, world, expiry, contexts);
|
return new NodeModel(permission, value, server, world, expiry, contexts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NodeModel of(String permission) {
|
||||||
|
return of(permission, true, "global", "global", 0L, ImmutableContextSet.empty());
|
||||||
|
}
|
||||||
|
|
||||||
private final String permission;
|
private final String permission;
|
||||||
private final boolean value;
|
private final boolean value;
|
||||||
private final String server;
|
private final String server;
|
||||||
|
@ -26,7 +26,9 @@
|
|||||||
package me.lucko.luckperms.common.storage.dao.file;
|
package me.lucko.luckperms.common.storage.dao.file;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
import me.lucko.luckperms.api.ChatMetaType;
|
||||||
import me.lucko.luckperms.api.HeldPermission;
|
import me.lucko.luckperms.api.HeldPermission;
|
||||||
import me.lucko.luckperms.api.LogEntry;
|
import me.lucko.luckperms.api.LogEntry;
|
||||||
import me.lucko.luckperms.api.Node;
|
import me.lucko.luckperms.api.Node;
|
||||||
@ -39,6 +41,7 @@ import me.lucko.luckperms.common.managers.track.TrackManager;
|
|||||||
import me.lucko.luckperms.common.model.Group;
|
import me.lucko.luckperms.common.model.Group;
|
||||||
import me.lucko.luckperms.common.model.Track;
|
import me.lucko.luckperms.common.model.Track;
|
||||||
import me.lucko.luckperms.common.model.User;
|
import me.lucko.luckperms.common.model.User;
|
||||||
|
import me.lucko.luckperms.common.node.MetaType;
|
||||||
import me.lucko.luckperms.common.node.NodeFactory;
|
import me.lucko.luckperms.common.node.NodeFactory;
|
||||||
import me.lucko.luckperms.common.node.NodeHeldPermission;
|
import me.lucko.luckperms.common.node.NodeHeldPermission;
|
||||||
import me.lucko.luckperms.common.node.NodeModel;
|
import me.lucko.luckperms.common.node.NodeModel;
|
||||||
@ -69,6 +72,7 @@ import java.util.Objects;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public abstract class ConfigurateDao extends AbstractDao {
|
public abstract class ConfigurateDao extends AbstractDao {
|
||||||
@ -680,35 +684,35 @@ public abstract class ConfigurateDao extends AbstractDao {
|
|||||||
return this.uuidCache.lookupUsername(uuid);
|
return this.uuidCache.lookupUsername(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Collection<NodeModel> readAttributes(ConfigurationNode entry, String permission) {
|
private static NodeModel readAttributes(ConfigurationNode attributes, Function<ConfigurationNode, String> permissionFunction) {
|
||||||
Map<Object, ? extends ConfigurationNode> attributes = entry.getChildrenMap();
|
boolean value = attributes.getNode("value").getBoolean(true);
|
||||||
|
String server = attributes.getNode("server").getString("global");
|
||||||
|
String world = attributes.getNode("world").getString("global");
|
||||||
|
long expiry = attributes.getNode("expiry").getLong(0L);
|
||||||
|
|
||||||
boolean value = true;
|
|
||||||
String server = "global";
|
|
||||||
String world = "global";
|
|
||||||
long expiry = 0L;
|
|
||||||
ImmutableContextSet context = ImmutableContextSet.empty();
|
ImmutableContextSet context = ImmutableContextSet.empty();
|
||||||
|
ConfigurationNode contextMap = attributes.getNode("context");
|
||||||
if (attributes.containsKey("value")) {
|
if (!contextMap.isVirtual() && contextMap.hasMapChildren()) {
|
||||||
value = attributes.get("value").getBoolean();
|
context = ContextSetConfigurateSerializer.deserializeContextSet(contextMap).makeImmutable();
|
||||||
}
|
|
||||||
if (attributes.containsKey("server")) {
|
|
||||||
server = attributes.get("server").getString();
|
|
||||||
}
|
|
||||||
if (attributes.containsKey("world")) {
|
|
||||||
world = attributes.get("world").getString();
|
|
||||||
}
|
|
||||||
if (attributes.containsKey("expiry")) {
|
|
||||||
expiry = attributes.get("expiry").getLong();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attributes.containsKey("context") && attributes.get("context").hasMapChildren()) {
|
return NodeModel.of(permissionFunction.apply(attributes), value, server, world, expiry, context);
|
||||||
ConfigurationNode contexts = attributes.get("context");
|
|
||||||
context = ContextSetConfigurateSerializer.deserializeContextSet(contexts).makeImmutable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigurationNode batchAttribute = attributes.get("permissions");
|
private static Collection<NodeModel> readAttributes(ConfigurationNode attributes, String permission) {
|
||||||
if (permission.startsWith("luckperms.batch") && batchAttribute != null && batchAttribute.hasListChildren()) {
|
boolean value = attributes.getNode("value").getBoolean(true);
|
||||||
|
String server = attributes.getNode("server").getString("global");
|
||||||
|
String world = attributes.getNode("world").getString("global");
|
||||||
|
long expiry = attributes.getNode("expiry").getLong(0L);
|
||||||
|
|
||||||
|
ImmutableContextSet context = ImmutableContextSet.empty();
|
||||||
|
ConfigurationNode contextMap = attributes.getNode("context");
|
||||||
|
if (!contextMap.isVirtual() && contextMap.hasMapChildren()) {
|
||||||
|
context = ContextSetConfigurateSerializer.deserializeContextSet(contextMap).makeImmutable();
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigurationNode batchAttribute = attributes.getNode("permissions");
|
||||||
|
if (permission.startsWith("luckperms.batch") && !batchAttribute.isVirtual() && batchAttribute.hasListChildren()) {
|
||||||
List<NodeModel> nodes = new ArrayList<>();
|
List<NodeModel> nodes = new ArrayList<>();
|
||||||
for (ConfigurationNode element : batchAttribute.getChildrenList()) {
|
for (ConfigurationNode element : batchAttribute.getChildrenList()) {
|
||||||
nodes.add(NodeModel.of(element.getString(), value, server, world, expiry, context));
|
nodes.add(NodeModel.of(element.getString(), value, server, world, expiry, context));
|
||||||
@ -719,63 +723,87 @@ public abstract class ConfigurateDao extends AbstractDao {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Map.Entry<String, ConfigurationNode> parseEntry(ConfigurationNode appended) {
|
||||||
|
if (!appended.hasMapChildren()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map.Entry<Object, ? extends ConfigurationNode> entry = Iterables.getFirst(appended.getChildrenMap().entrySet(), null);
|
||||||
|
if (entry == null || !entry.getValue().hasMapChildren()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Maps.immutableEntry(entry.getKey().toString(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
private static Set<NodeModel> readNodes(ConfigurationNode data) {
|
private static Set<NodeModel> readNodes(ConfigurationNode data) {
|
||||||
Set<NodeModel> nodes = new HashSet<>();
|
Set<NodeModel> nodes = new HashSet<>();
|
||||||
|
|
||||||
if (data.getNode("permissions").hasListChildren()) {
|
if (data.getNode("permissions").hasListChildren()) {
|
||||||
List<? extends ConfigurationNode> parts = data.getNode("permissions").getChildrenList();
|
List<? extends ConfigurationNode> children = data.getNode("permissions").getChildrenList();
|
||||||
|
for (ConfigurationNode appended : children) {
|
||||||
for (ConfigurationNode ent : parts) {
|
String plainValue = appended.getValue(Types::strictAsString);
|
||||||
String stringValue = ent.getValue(Types::strictAsString);
|
if (plainValue != null) {
|
||||||
if (stringValue != null) {
|
nodes.add(NodeModel.of(plainValue));
|
||||||
nodes.add(NodeModel.of(stringValue, true, "global", "global", 0L, ImmutableContextSet.empty()));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ent.hasMapChildren()) {
|
Map.Entry<String, ConfigurationNode> entry = parseEntry(appended);
|
||||||
|
if (entry == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
nodes.addAll(readAttributes(entry.getValue(), entry.getKey()));
|
||||||
Map.Entry<Object, ? extends ConfigurationNode> entry = Iterables.getFirst(ent.getChildrenMap().entrySet(), null);
|
|
||||||
if (entry == null || !entry.getValue().hasMapChildren()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String permission = entry.getKey().toString();
|
|
||||||
nodes.addAll(readAttributes(entry.getValue(), permission));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.getNode("parents").hasListChildren()) {
|
if (data.getNode("parents").hasListChildren()) {
|
||||||
List<? extends ConfigurationNode> parts = data.getNode("parents").getChildrenList();
|
List<? extends ConfigurationNode> children = data.getNode("parents").getChildrenList();
|
||||||
|
for (ConfigurationNode appended : children) {
|
||||||
for (ConfigurationNode ent : parts) {
|
String stringValue = appended.getValue(Types::strictAsString);
|
||||||
String stringValue = ent.getValue(Types::strictAsString);
|
|
||||||
if (stringValue != null) {
|
if (stringValue != null) {
|
||||||
nodes.add(NodeModel.of(NodeFactory.groupNode(stringValue), true, "global", "global", 0L, ImmutableContextSet.empty()));
|
nodes.add(NodeModel.of(NodeFactory.groupNode(stringValue)));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ent.hasMapChildren()) {
|
Map.Entry<String, ConfigurationNode> entry = parseEntry(appended);
|
||||||
|
if (entry == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
nodes.add(readAttributes(entry.getValue(), c -> NodeFactory.groupNode(entry.getKey())));
|
||||||
Map.Entry<Object, ? extends ConfigurationNode> entry = Iterables.getFirst(ent.getChildrenMap().entrySet(), null);
|
}
|
||||||
if (entry == null || !entry.getValue().hasMapChildren()) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String permission = NodeFactory.groupNode(entry.getKey().toString());
|
for (ChatMetaType chatMetaType : ChatMetaType.values()) {
|
||||||
nodes.addAll(readAttributes(entry.getValue(), permission));
|
String keyName = chatMetaType.toString() + "es";
|
||||||
|
if (data.getNode(keyName).hasListChildren()) {
|
||||||
|
List<? extends ConfigurationNode> children = data.getNode(keyName).getChildrenList();
|
||||||
|
for (ConfigurationNode appended : children) {
|
||||||
|
Map.Entry<String, ConfigurationNode> entry = parseEntry(appended);
|
||||||
|
if (entry == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nodes.add(readAttributes(entry.getValue(), c -> NodeFactory.chatMetaNode(chatMetaType, c.getNode("priority").getInt(0), entry.getKey())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.getNode("meta").hasListChildren()) {
|
||||||
|
List<? extends ConfigurationNode> children = data.getNode("meta").getChildrenList();
|
||||||
|
for (ConfigurationNode appended : children) {
|
||||||
|
Map.Entry<String, ConfigurationNode> entry = parseEntry(appended);
|
||||||
|
if (entry == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nodes.add(readAttributes(entry.getValue(), c -> NodeFactory.metaNode(entry.getKey(), entry.getValue().getNode("value").getString("null"))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConfigurationNode writeAttributes(NodeModel node) {
|
private static void writeAttributesTo(ConfigurationNode attributes, NodeModel node, boolean writeValue) {
|
||||||
ConfigurationNode attributes = SimpleConfigurationNode.root();
|
if (writeValue) {
|
||||||
attributes.getNode("value").setValue(node.getValue());
|
attributes.getNode("value").setValue(node.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
if (!node.getServer().equals("global")) {
|
if (!node.getServer().equals("global")) {
|
||||||
attributes.getNode("server").setValue(node.getServer());
|
attributes.getNode("server").setValue(node.getServer());
|
||||||
@ -792,52 +820,108 @@ public abstract class ConfigurateDao extends AbstractDao {
|
|||||||
if (!node.getContexts().isEmpty()) {
|
if (!node.getContexts().isEmpty()) {
|
||||||
attributes.getNode("context").setValue(ContextSetConfigurateSerializer.serializeContextSet(node.getContexts()));
|
attributes.getNode("context").setValue(ContextSetConfigurateSerializer.serializeContextSet(node.getContexts()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return attributes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeNodes(ConfigurationNode to, Set<NodeModel> nodes) {
|
private static boolean isPlain(NodeModel node) {
|
||||||
ConfigurationNode permsSection = SimpleConfigurationNode.root();
|
return node.getValue() &&
|
||||||
ConfigurationNode parentsSection = SimpleConfigurationNode.root();
|
|
||||||
|
|
||||||
for (NodeModel node : nodes) {
|
|
||||||
|
|
||||||
// just a raw, default node.
|
|
||||||
boolean single = node.getValue() &&
|
|
||||||
node.getServer().equalsIgnoreCase("global") &&
|
node.getServer().equalsIgnoreCase("global") &&
|
||||||
node.getWorld().equalsIgnoreCase("global") &&
|
node.getWorld().equalsIgnoreCase("global") &&
|
||||||
node.getExpiry() == 0L &&
|
node.getExpiry() == 0L &&
|
||||||
node.getContexts().isEmpty();
|
node.getContexts().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
// try to parse out the group
|
private static void writeNodes(ConfigurationNode to, Set<NodeModel> nodes) {
|
||||||
String group = node.toNode().isGroupNode() ? node.toNode().getGroupName() : null;
|
ConfigurationNode permissionsSection = SimpleConfigurationNode.root();
|
||||||
|
ConfigurationNode parentsSection = SimpleConfigurationNode.root();
|
||||||
|
ConfigurationNode prefixesSection = SimpleConfigurationNode.root();
|
||||||
|
ConfigurationNode suffixesSection = SimpleConfigurationNode.root();
|
||||||
|
ConfigurationNode metaSection = SimpleConfigurationNode.root();
|
||||||
|
|
||||||
|
for (NodeModel node : nodes) {
|
||||||
|
Node n = node.toNode();
|
||||||
|
|
||||||
// just add a string to the list.
|
// just add a string to the list.
|
||||||
if (single) {
|
if (isPlain(node)) {
|
||||||
|
if (n.isGroupNode()) {
|
||||||
if (group != null) {
|
parentsSection.getAppendedNode().setValue(n.getGroupName());
|
||||||
parentsSection.getAppendedNode().setValue(group);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!MetaType.ANY.matches(n)) {
|
||||||
permsSection.getAppendedNode().setValue(node.getPermission());
|
permissionsSection.getAppendedNode().setValue(node.getPermission());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (group != null) {
|
|
||||||
ConfigurationNode ent = SimpleConfigurationNode.root();
|
|
||||||
ent.getNode(group).setValue(writeAttributes(node));
|
|
||||||
parentsSection.getAppendedNode().setValue(ent);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigurationNode ent = SimpleConfigurationNode.root();
|
ChatMetaType chatMetaType = ChatMetaType.ofNode(n).orElse(null);
|
||||||
ent.getNode(node.getPermission()).setValue(writeAttributes(node));
|
if (chatMetaType != null && n.getValuePrimitive()) {
|
||||||
permsSection.getAppendedNode().setValue(ent);
|
// handle prefixes / suffixes
|
||||||
|
Map.Entry<Integer, String> entry = chatMetaType.getEntry(n);
|
||||||
|
|
||||||
|
ConfigurationNode attributes = SimpleConfigurationNode.root();
|
||||||
|
attributes.getNode("priority").setValue(entry.getKey());
|
||||||
|
writeAttributesTo(attributes, node, false);
|
||||||
|
|
||||||
|
ConfigurationNode appended = SimpleConfigurationNode.root();
|
||||||
|
appended.getNode(entry.getValue()).setValue(attributes);
|
||||||
|
|
||||||
|
switch (chatMetaType) {
|
||||||
|
case PREFIX:
|
||||||
|
prefixesSection.getAppendedNode().setValue(appended);
|
||||||
|
break;
|
||||||
|
case SUFFIX:
|
||||||
|
suffixesSection.getAppendedNode().setValue(appended);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
} else if (n.isMeta() && n.getValuePrimitive()) {
|
||||||
|
// handle meta nodes
|
||||||
|
Map.Entry<String, String> meta = n.getMeta();
|
||||||
|
|
||||||
|
ConfigurationNode attributes = SimpleConfigurationNode.root();
|
||||||
|
attributes.getNode("value").setValue(meta.getValue());
|
||||||
|
writeAttributesTo(attributes, node, false);
|
||||||
|
|
||||||
|
ConfigurationNode appended = SimpleConfigurationNode.root();
|
||||||
|
appended.getNode(meta.getKey()).setValue(attributes);
|
||||||
|
|
||||||
|
metaSection.getAppendedNode().setValue(appended);
|
||||||
|
} else if (n.isGroupNode() && n.getValuePrimitive()) {
|
||||||
|
// handle group nodes
|
||||||
|
ConfigurationNode attributes = SimpleConfigurationNode.root();
|
||||||
|
writeAttributesTo(attributes, node, false);
|
||||||
|
|
||||||
|
ConfigurationNode appended = SimpleConfigurationNode.root();
|
||||||
|
appended.getNode(n.getGroupName()).setValue(attributes);
|
||||||
|
|
||||||
|
parentsSection.getAppendedNode().setValue(appended);
|
||||||
|
} else {
|
||||||
|
// handle regular permissions and negated meta+prefixes+suffixes
|
||||||
|
ConfigurationNode attributes = SimpleConfigurationNode.root();
|
||||||
|
writeAttributesTo(attributes, node, true);
|
||||||
|
|
||||||
|
ConfigurationNode appended = SimpleConfigurationNode.root();
|
||||||
|
appended.getNode(n.getPermission()).setValue(attributes);
|
||||||
|
|
||||||
|
permissionsSection.getAppendedNode().setValue(appended);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
to.getNode("permissions").setValue(permsSection);
|
if (permissionsSection.hasListChildren()) {
|
||||||
|
to.getNode("permissions").setValue(permissionsSection);
|
||||||
|
}
|
||||||
|
if (parentsSection.hasListChildren()) {
|
||||||
to.getNode("parents").setValue(parentsSection);
|
to.getNode("parents").setValue(parentsSection);
|
||||||
}
|
}
|
||||||
|
if (prefixesSection.hasListChildren()) {
|
||||||
|
to.getNode("prefixes").setValue(prefixesSection);
|
||||||
|
}
|
||||||
|
if (suffixesSection.hasListChildren()) {
|
||||||
|
to.getNode("suffixes").setValue(suffixesSection);
|
||||||
|
}
|
||||||
|
if (metaSection.hasListChildren()) {
|
||||||
|
to.getNode("meta").setValue(metaSection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user