/*
 * 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.processors;

import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.node.ImmutableNode;

public class WildcardProcessor extends AbstractPermissionProcessor implements PermissionProcessor {
    public static final String WILDCARD_SUFFIX = ".*";
    private static final String GLOBAL_WILDCARD_1 = "*";
    private static final String GLOBAL_WILDCARD_2 = "'*'";

    @Override
    public Tristate hasPermission(String permission) {
        String node = permission;

        while (true) {
            int endIndex = node.lastIndexOf(ImmutableNode.NODE_SEPARATOR);
            if (endIndex == -1) {
                break;
            }

            node = node.substring(0, endIndex);
            if (!node.isEmpty()) {
                Tristate t = Tristate.fromNullableBoolean(this.sourceMap.get(node + WILDCARD_SUFFIX));
                if (t != Tristate.UNDEFINED) {
                    return t;
                }
            }
        }

        Tristate t = Tristate.fromNullableBoolean(this.sourceMap.get(GLOBAL_WILDCARD_1));
        if (t != Tristate.UNDEFINED) {
            return t;
        }

        return Tristate.fromNullableBoolean(this.sourceMap.get(GLOBAL_WILDCARD_2));
    }

}

@@ -1286,16 +1286,64 @@
 ssors;%0A%0A
+import com.google.common.collect.ImmutableMap;%0A%0A
 import m
@@ -1428,16 +1428,69 @@
 eNode;%0A%0A
+import java.util.Collections;%0Aimport java.util.Map;%0A%0A
 public c
@@ -1683,18 +1683,16 @@
 WILDCARD
-_1
  = %22*%22;%0A
@@ -1743,17 +1743,170 @@
 ARD_
-2 = %22'*'%22
+WITH_QUOTES = %22'*'%22;%0A%0A    private Map%3CString, Boolean%3E wildcardPermissions = Collections.emptyMap();%0A    private Tristate globalWildcardState = Tristate.UNDEFINED
 ;%0A%0A 
@@ -2328,44 +2328,36 @@
 his.
-sourceMap.get(node + WILDCARD_SUFFIX
+wildcardPermissions.get(node
 ));%0A
@@ -2468,32 +2468,562 @@
    %7D%0A        %7D%0A%0A
+        return this.globalWildcardState;%0A    %7D%0A%0A    @Override%0A    public void refresh() %7B%0A        ImmutableMap.Builder%3CString, Boolean%3E builder = ImmutableMap.builder();%0A        for (Map.Entry%3CString, Boolean%3E e : this.sourceMap.entrySet()) %7B%0A            String key = e.getKey();%0A            if (!key.endsWith(WILDCARD_SUFFIX) %7C%7C key.length() %3C= 2) %7B%0A                continue;%0A            %7D%0A%0A            builder.put(key.substring(0, key.length() - 2), e.getValue());%0A        %7D%0A        this.wildcardPermissions = builder.build();%0A%0A
         Tristate
@@ -3015,33 +3015,37 @@
        Tristate 
-t
+state
  = Tristate.from
@@ -3098,10 +3098,8 @@
 CARD
-_1
 ));%0A
@@ -3102,35 +3102,39 @@
 ));%0A        if (
-t !
+state =
 = Tristate.UNDEF
@@ -3157,43 +3157,15 @@
     
-return t;%0A        %7D%0A%0A        return
+state =
  Tri
@@ -3229,18 +3229,80 @@
 ARD_
-2));%0A    %7D%0A
+WITH_QUOTES));%0A        %7D%0A%0A        this.globalWildcardState = state;%0A    %7D
 %0A%7D%0A

Optimize WildcardProcessor