/*
 * Copyright 2014 Matthew Collins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.thinkofdeath.command.bukkit;

import org.bukkit.command.CommandSender;
import uk.co.thinkofdeath.command.CommandError;
import uk.co.thinkofdeath.command.types.ArgumentValidator;
import uk.co.thinkofdeath.command.types.TypeHandler;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@TypeHandler(
        value = HasPermissionHandler.class,
        clazz = CommandSender.class
)
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
/**
 * Requires the player to have at least one of the permission nodes
 */
public @interface HasPermission {
    String[] value();
}


class HasPermissionHandler implements ArgumentValidator<CommandSender> {

    private final String[] permissions;

    HasPermissionHandler(HasPermission hasPermission) {
        permissions = hasPermission.value();
    }

    @Override
    public CommandError validate(String argStr, CommandSender argument) {
        for (String permission : permissions) {
            if (argument.hasPermission(permission)) {
                return null;
            }
        }
        return new CommandError(3, "bukkit.no-permission");
    }
}
@@ -1296,16 +1296,54 @@
 alue();%0A
+    boolean wildcard() default false;%0A
 %7D%0A%0A%0Aclas
@@ -1451,16 +1451,52 @@
 issions;
+%0A    private final boolean wildcard;
 %0A%0A    Ha
@@ -1586,24 +1586,69 @@
 on.value();%0A
+        wildcard = hasPermission.wildcard();%0A
     %7D%0A%0A    @
@@ -1726,24 +1726,24 @@
 argument) %7B%0A
-
         for 
@@ -1774,24 +1774,516 @@
 missions) %7B%0A
+            if (wildcard) %7B%0A                String%5B%5D parts = permission.split(%22%5C%5C.%22);%0A                StringBuilder current = new StringBuilder();%0A                for (int i = 0; i %3C parts.length - 1; i++) %7B%0A                    String part = parts%5Bi%5D;%0A                    current.append(part);%0A                    if (argument.hasPermission(current + %22.*%22)) %7B%0A                        return null;%0A                    %7D%0A                    current.append(%22.%22);%0A                %7D%0A            %7D%0A
             

Support wildcard permissions