/*
 * This file is part of LuckPerms, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  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.primarygroup;

import lombok.NonNull;

import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;

import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;

public class AllParentsByWeightHolder extends StoredHolder {

    private String cachedValue = null;
    private boolean useCached = false;

    public AllParentsByWeightHolder(@NonNull User user) {
        super(user);
        user.getStateListeners().add(() -> useCached = false);
    }

    @Override
    public String getValue() {
        if (useCached) {
            return cachedValue;
        }

        Contexts contexts = user.getPlugin().getContextForUser(user);
        ContextSet contextSet = contexts != null ? contexts.getContexts() : user.getPlugin().getContextManager().getStaticContexts();
        cachedValue = user.resolveInheritancesAlmostEqual(ExtractedContexts.generate(contextSet)).stream()
                .filter(Node::isGroupNode)
                .filter(Node::getValue)
                .map(n -> Optional.ofNullable(user.getPlugin().getGroupManager().getIfLoaded(n.getGroupName())))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .sorted(Collections.reverseOrder(Comparator.comparingInt(o -> o.getWeight().orElse(0))))
                .findFirst()
                .map(Group::getName)
                .orElse(null);

        useCached = true;
        return cachedValue;
    }
}

@@ -1418,30 +1418,32 @@
 rms.
-api.context.ContextSet
+common.config.ConfigKeys
 ;%0Aim
@@ -2172,133 +2172,620 @@
     
-C
+if (c
 ontext
-Set contextSet = contexts != null ? contexts.getContexts() : user.getPlugin().getContextManager().getStaticContexts();
+s == null) %7B%0A            contexts = new Contexts(%0A                    user.getPlugin().getContextManager().getStaticContexts(),%0A                    user.getPlugin().getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),%0A                    user.getPlugin().getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),%0A                    true,%0A                    user.getPlugin().getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),%0A                    user.getPlugin().getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),%0A                    false%0A            );%0A        %7D%0A
 %0A   
@@ -2877,11 +2877,9 @@
 text
-Set
+s
 )).s

Fix NPE caused by AllParentsByWeight primary group selector - closes #252