Search in sources :

Example 26 with ImmutableContextSet

use of me.lucko.luckperms.api.context.ImmutableContextSet in project LuckPerms by lucko.

the class SubjectDataContainer method serialize.

/**
 * Converts this container to json
 *
 * @return a json serialisation of the data in this container
 */
public JsonObject serialize() {
    JsonObject root = new JsonObject();
    JsonArray permissions = new JsonArray();
    for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : sortContextMap(this.permissions)) {
        if (e.getValue().isEmpty()) {
            continue;
        }
        JsonObject section = new JsonObject();
        section.add("context", ContextSetJsonSerializer.serializeContextSet(e.getKey()));
        JsonObject data = new JsonObject();
        // sort alphabetically.
        List<Map.Entry<String, Boolean>> perms = new ArrayList<>(e.getValue().entrySet());
        perms.sort(Map.Entry.comparingByKey(CollationKeyCache.comparator()));
        for (Map.Entry<String, Boolean> ent : perms) {
            data.addProperty(ent.getKey(), ent.getValue());
        }
        section.add("data", data);
        permissions.add(section);
    }
    root.add("permissions", permissions);
    JsonArray options = new JsonArray();
    for (Map.Entry<ImmutableContextSet, Map<String, String>> e : sortContextMap(this.options)) {
        if (e.getValue().isEmpty()) {
            continue;
        }
        JsonObject section = new JsonObject();
        section.add("context", ContextSetJsonSerializer.serializeContextSet(e.getKey()));
        JsonObject data = new JsonObject();
        // sort alphabetically.
        List<Map.Entry<String, String>> opts = new ArrayList<>(e.getValue().entrySet());
        opts.sort(Map.Entry.comparingByKey(CollationKeyCache.comparator()));
        for (Map.Entry<String, String> ent : opts) {
            data.addProperty(ent.getKey(), ent.getValue());
        }
        section.add("data", data);
        options.add(section);
    }
    root.add("options", options);
    JsonArray parents = new JsonArray();
    for (Map.Entry<ImmutableContextSet, List<LPSubjectReference>> e : sortContextMap(this.parents)) {
        if (e.getValue().isEmpty()) {
            continue;
        }
        JsonObject section = new JsonObject();
        section.add("context", ContextSetJsonSerializer.serializeContextSet(e.getKey()));
        JsonArray data = new JsonArray();
        for (LPSubjectReference ref : e.getValue()) {
            JsonObject parent = new JsonObject();
            parent.addProperty("collection", ref.getCollectionIdentifier());
            parent.addProperty("subject", ref.getSubjectIdentifier());
            data.add(parent);
        }
        section.add("data", data);
        options.add(section);
    }
    root.add("parents", parents);
    return root;
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) JsonArray(com.google.gson.JsonArray) LPSubjectReference(me.lucko.luckperms.sponge.service.model.LPSubjectReference) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 27 with ImmutableContextSet

use of me.lucko.luckperms.api.context.ImmutableContextSet in project LuckPerms by lucko.

the class CalculatedSubjectData method resolveParents.

public Set<LPSubjectReference> resolveParents(ContextSet filter) {
    // get relevant entries
    SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse());
    for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> entry : this.parents.entrySet()) {
        if (!entry.getKey().isSatisfiedBy(filter)) {
            continue;
        }
        sorted.put(entry.getKey(), entry.getValue());
    }
    // flatten
    Set<LPSubjectReference> result = new LinkedHashSet<>();
    for (Set<LPSubjectReference> set : sorted.values()) {
        for (LPSubjectReference e : set) {
            if (!result.contains(e)) {
                result.add(e);
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) ContextSet(me.lucko.luckperms.api.context.ContextSet) LinkedHashSet(java.util.LinkedHashSet) LPSubjectReference(me.lucko.luckperms.sponge.service.model.LPSubjectReference) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) TreeMap(java.util.TreeMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Aggregations

ImmutableContextSet (me.lucko.luckperms.api.context.ImmutableContextSet)27 Map (java.util.Map)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 HashMap (java.util.HashMap)6 LPSubjectReference (me.lucko.luckperms.sponge.service.model.LPSubjectReference)6 Node (me.lucko.luckperms.api.Node)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 List (java.util.List)3 LPSubject (me.lucko.luckperms.sponge.service.model.LPSubject)3 JsonObject (com.google.gson.JsonObject)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Tristate (me.lucko.luckperms.api.Tristate)2 ContextSet (me.lucko.luckperms.api.context.ContextSet)2 NodeModel (me.lucko.luckperms.common.node.NodeModel)2