/*
 * This file is part of gplugins, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2017, Jamie Mansfield <https://www.jamierocks.uk/>
 *
 * 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 uk.jamierocks.canary.gplugins.hook;

import com.google.common.collect.Lists;
import net.canarymod.Canary;
import net.canarymod.hook.Dispatcher;
import net.canarymod.hook.Hook;
import net.canarymod.hook.HookExecutionException;
import net.canarymod.hook.HookHandler;
import net.canarymod.plugin.Plugin;
import net.canarymod.plugin.PluginListener;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

/**
 * Allows for the searching of hooks in gplugins, and the registration thereof.
 */
public final class HookProcessor {

    public static List<HookReference> scan(Plugin plugin, Object pluginObject) {
        final List<HookReference> references = Lists.newArrayList();

        Arrays.stream(pluginObject.getClass().getDeclaredMethods()).filter(method -> method.isAnnotationPresent(HookHandler.class)).forEach(m -> {
            final HookHandler handler = m.getDeclaredAnnotation(HookHandler.class);

            // We first need to check the parameter count
            if (m.getParameterCount() != 1) {
                final String hookReference = String.format("%s#%s", m.getDeclaringClass().getSimpleName(), m.getName());
                Canary.log.error(String.format("Failed to process HookHandler: %s. Expected 1 parameters, found %d!", hookReference, m.getParameterCount()));
                return;
            }

            final Class<?> hookClass = m.getParameterTypes()[0];

            // Check the hookClass is assignable from Hook
            if (!Hook.class.isAssignableFrom(hookClass)) {
                final String hookReference = String.format("%s#%s", m.getDeclaringClass().getSimpleName(), m.getName());
                Canary.log.error(String.format("Failed to process HookHandler: %s. Parameter is not assignable from Hook!", hookReference));
                return;
            }

            // Create Dispatcher
            final Dispatcher dispatcher = new GDispatcher(pluginObject) {
                @Override
                public void execute(Object pluginObject, Hook hook) {
                    try {
                        m.invoke(pluginObject, hook);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        throw new HookExecutionException(e.getMessage(), e);
                    }
                }
            };
            dispatcher.ignoreCanceled = handler.ignoreCanceled();
        });

        return references;
    }

    public static void registerListener(Plugin plugin, Object pluginObject) {
        final PluginListener listener = new FakeListener(plugin);
        scan(plugin, pluginObject).forEach(h -> h.register(listener));
    }

    private HookProcessor() {
    }

}

@@ -1841,31 +1841,16 @@
 e%3E scan(
-Plugin plugin, 
 Object p
@@ -2274,129 +2274,8 @@
 ) %7B%0A
-                final String hookReference = String.format(%22%25s#%25s%22, m.getDeclaringClass().getSimpleName(), m.getName());%0A
     
@@ -2299,38 +2299,24 @@
 y.log.error(
-String.format(
 %22Failed to p
@@ -2335,18 +2335,21 @@
 andler: 
-%25s
+%7B%7D#%7B%7D
 . Expect
@@ -2375,27 +2375,88 @@
 und 
-%25d!%22, hookReference
+%7B%7D!%22,%0A                        m.getDeclaringClass().getSimpleName(), m.getName()
 , m.
@@ -2475,17 +2475,16 @@
 Count())
-)
 ;%0A      
@@ -2704,129 +2704,8 @@
 ) %7B%0A
-                final String hookReference = String.format(%22%25s#%25s%22, m.getDeclaringClass().getSimpleName(), m.getName());%0A
     
@@ -2737,22 +2737,8 @@
 ror(
-String.format(
 %22Fai
@@ -2769,10 +2769,13 @@
 er: 
-%25s
+%7B%7D#%7B%7D
 . Pa
@@ -2816,22 +2816,82 @@
 k!%22,
- hookReference
+%0A                        m.getDeclaringClass().getSimpleName(), m.getName(
 ));%0A
@@ -3698,21 +3698,72 @@
     
-scan(plugin, 
+Canary.hooks().registerListener(listener, plugin);%0A        scan(
 plug

Remove usage of String#format() and register FakeListeners