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);
}
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);
}
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));
}
}
}
Aggregations