cn.nukkit.plugin.MethodEventExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.plugin;
import cn.nukkit.event.Event;
import cn.nukkit.event.Listener;
import cn.nukkit.utils.EventException;
import lombok.extern.log4j.Log4j2;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author MagicDroidX (Nukkit Project)
*/
@Log4j2
public class MethodEventExecutor implements EventExecutor {
private final Method method;
public MethodEventExecutor(Method method) {
this.method = method;
}
@SuppressWarnings("unchecked")
@Override
public void execute(Listener listener, Event event) throws EventException {
try {
Class[] params = (Class[]) method.getParameterTypes();
for (Class param : params) {
if (param.isAssignableFrom(event.getClass())) {
method.invoke(listener, event);
break;
}
}
} catch (InvocationTargetException ex) {
throw new EventException(ex.getCause() != null ? ex.getCause() : ex);
} catch (ClassCastException ex) {
log.debug("Ignoring a ClassCastException", ex);
// We are going to ignore ClassCastException because EntityDamageEvent can't be cast to EntityDamageByEntityEvent
} catch (Throwable t) {
throw new EventException(t);
}
}
public Method getMethod() {
return method;
}
}