Search in sources :

Example 1 with EventIdentifier

use of com.laytonsmith.annotations.EventIdentifier in project CommandHelper by EngineHub.

the class BukkitEntityListener method onTargetLiving.

@EventIdentifier(event = Driver.TARGET_ENTITY, className = "org.bukkit.event.entity.EntityTargetEvent")
public void onTargetLiving(Event event) {
    BukkitMCTargetEvent ete = new BukkitMCTargetEvent(event);
    MCEntity target = ete.getTarget();
    if (target == null || !(target instanceof MCPlayer)) {
        return;
    }
    EventUtils.TriggerListener(Driver.TARGET_ENTITY, "target_player", ete);
}
Also used : MCEntity(com.laytonsmith.abstraction.MCEntity) MCPlayer(com.laytonsmith.abstraction.MCPlayer) EventIdentifier(com.laytonsmith.annotations.EventIdentifier)

Example 2 with EventIdentifier

use of com.laytonsmith.annotations.EventIdentifier in project CommandHelper by EngineHub.

the class BukkitInventoryListener method onItemSwap.

@EventIdentifier(event = Driver.ITEM_SWAP, className = "org.bukkit.event.player.PlayerSwapHandItemsEvent")
public void onItemSwap(Event event) {
    BukkitMCItemSwapEvent is = new BukkitInventoryEvents.BukkitMCItemSwapEvent(event);
    EventUtils.TriggerListener(Driver.ITEM_SWAP, "item_swap", is);
}
Also used : BukkitMCItemSwapEvent(com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCItemSwapEvent) EventIdentifier(com.laytonsmith.annotations.EventIdentifier)

Example 3 with EventIdentifier

use of com.laytonsmith.annotations.EventIdentifier in project CommandHelper by EngineHub.

the class CommandHelperPlugin method registerEventsDynamic.

/*
	 * This method is based on Bukkit's JavaPluginLoader:createRegisteredListeners
	 * Part of this code would be run normally using the other register method
	 */
public void registerEventsDynamic(Listener listener) {
    for (final java.lang.reflect.Method method : listener.getClass().getMethods()) {
        EventIdentifier identifier = method.getAnnotation(EventIdentifier.class);
        EventHandler defaultHandler = method.getAnnotation(EventHandler.class);
        EventPriority priority = EventPriority.LOWEST;
        Class<? extends Event> eventClass;
        if (defaultHandler != null) {
            priority = defaultHandler.priority();
        }
        if (identifier == null) {
            if (defaultHandler != null && method.getParameterTypes().length == 1) {
                try {
                    eventClass = (Class<? extends Event>) method.getParameterTypes()[0];
                } catch (ClassCastException e) {
                    continue;
                }
            } else {
                continue;
            }
        } else {
            if (!identifier.event().existsInCurrent()) {
                continue;
            }
            try {
                eventClass = (Class<? extends Event>) Class.forName(identifier.className());
            } catch (ClassNotFoundException | ClassCastException e) {
                CHLog.GetLogger().e(CHLog.Tags.RUNTIME, "Could not listen for " + identifier.event().name() + " because the class " + identifier.className() + " could not be found." + " This problem is not expected to occur, so please report it on the bug" + " tracker if it does.", Target.UNKNOWN);
                continue;
            }
        }
        HandlerList handler;
        try {
            handler = (HandlerList) ReflectionUtils.invokeMethod(eventClass, null, "getHandlerList");
        } catch (ReflectionUtils.ReflectionException ref) {
            Class eventSuperClass = eventClass.getSuperclass();
            if (eventSuperClass != null) {
                try {
                    handler = (HandlerList) ReflectionUtils.invokeMethod(eventSuperClass, null, "getHandlerList");
                } catch (ReflectionUtils.ReflectionException refInner) {
                    CHLog.GetLogger().e(CHLog.Tags.RUNTIME, "Could not listen for " + identifier.event().name() + " because the handler for class " + identifier.className() + " could not be found. An attempt has already been made to find the" + " correct handler, but" + eventSuperClass.getName() + " did not have it either. Please report this on the bug tracker.", Target.UNKNOWN);
                    continue;
                }
            } else {
                CHLog.GetLogger().e(CHLog.Tags.RUNTIME, "Could not listen for " + identifier.event().name() + " because the handler for class " + identifier.className() + " could not be found. An attempt has already been made to find the" + " correct handler, but no superclass could be found." + " Please report this on the bug tracker.", Target.UNKNOWN);
                continue;
            }
        }
        final Class<? extends Event> finalEventClass = eventClass;
        EventExecutor executor = new EventExecutor() {

            @Override
            public void execute(Listener listener, Event event) throws EventException {
                try {
                    if (!finalEventClass.isAssignableFrom(event.getClass())) {
                        return;
                    }
                    method.invoke(listener, event);
                } catch (InvocationTargetException ex) {
                    throw new EventException(ex.getCause());
                } catch (Throwable t) {
                    throw new EventException(t);
                }
            }
        };
        if (this.getServer().getPluginManager().useTimings()) {
            handler.register(new TimedRegisteredListener(listener, executor, priority, this, false));
        } else {
            handler.register(new RegisteredListener(listener, executor, priority, this, false));
        }
    }
}
Also used : HandlerList(org.bukkit.event.HandlerList) EventIdentifier(com.laytonsmith.annotations.EventIdentifier) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) Listener(org.bukkit.event.Listener) RegisteredListener(org.bukkit.plugin.RegisteredListener) EventException(org.bukkit.event.EventException) EventHandler(org.bukkit.event.EventHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) EventExecutor(org.bukkit.plugin.EventExecutor) Event(org.bukkit.event.Event) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent) PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) ReflectionUtils(com.laytonsmith.PureUtilities.Common.ReflectionUtils) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) RegisteredListener(org.bukkit.plugin.RegisteredListener) EventPriority(org.bukkit.event.EventPriority)

Aggregations

EventIdentifier (com.laytonsmith.annotations.EventIdentifier)3 ReflectionUtils (com.laytonsmith.PureUtilities.Common.ReflectionUtils)1 MCEntity (com.laytonsmith.abstraction.MCEntity)1 MCPlayer (com.laytonsmith.abstraction.MCPlayer)1 BukkitMCItemSwapEvent (com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCItemSwapEvent)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Event (org.bukkit.event.Event)1 EventException (org.bukkit.event.EventException)1 EventHandler (org.bukkit.event.EventHandler)1 EventPriority (org.bukkit.event.EventPriority)1 HandlerList (org.bukkit.event.HandlerList)1 Listener (org.bukkit.event.Listener)1 PlayerCommandPreprocessEvent (org.bukkit.event.player.PlayerCommandPreprocessEvent)1 ServerCommandEvent (org.bukkit.event.server.ServerCommandEvent)1 EventExecutor (org.bukkit.plugin.EventExecutor)1 RegisteredListener (org.bukkit.plugin.RegisteredListener)1 TimedRegisteredListener (org.bukkit.plugin.TimedRegisteredListener)1