001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.event;
019
020import com.esotericsoftware.reflectasm.MethodAccess;
021import net.tridentsdk.event.Event;
022import net.tridentsdk.event.EventNotifier;
023import net.tridentsdk.event.Importance;
024import net.tridentsdk.event.Listener;
025import net.tridentsdk.plugin.Plugin;
026
027import java.util.Comparator;
028
029/**
030 * A fast-reflection based event invoker which notifies event listeners
031 *
032 * @author The TridentSDK Team
033 * @since 0.4-alpha
034 */
035public class ReflectNotifier implements EventNotifier, Comparator<ReflectNotifier> {
036    private final MethodAccess handle;
037    private final Plugin plugin;
038    private final int index;
039    private final Listener instance;
040    private final Class<? extends Event> eventClass;
041    private final Importance importance;
042
043    ReflectNotifier(MethodAccess handle, Plugin plugin, int index, Listener instance,
044                    Class<? extends Event> eventClass, Importance importance) {
045        this.handle = handle;
046        this.plugin = plugin;
047        this.index = index;
048        this.instance = instance;
049        this.eventClass = eventClass;
050        this.importance = importance;
051    }
052
053    public MethodAccess method() {
054        return this.handle;
055    }
056
057    public int index() {
058        return this.index;
059    }
060
061    @Override
062    public Plugin plugin() {
063        return plugin;
064    }
065
066    @Override
067    public Class<? extends Event> eventType() {
068        return this.eventClass;
069    }
070
071    @Override
072
073    public Importance importance() {
074        return this.importance;
075    }
076
077    @Override
078    public void handle(Event event) {
079        this.handle.invoke(this.instance, this.index, event);
080    }
081
082    @Override
083    public Listener listener() {
084        return this.instance;
085    }
086
087    @Override
088    public int compare(ReflectNotifier registeredListener, ReflectNotifier t1) {
089        return registeredListener.importance().compareTo(t1.importance());
090    }
091}