All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.alangomes.springspigot.event.SpringEventExecutor Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
package dev.alangomes.springspigot.event;

import dev.alangomes.springspigot.context.Context;
import lombok.SneakyThrows;
import lombok.val;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class SpringEventExecutor {

    @Autowired
    private Context context;

    public EventExecutor create(Method method) {
        val eventType = method.getParameters()[0].getType();
        return (listener, event) -> {
            if (!eventType.isInstance(event)) return;
            context.runWithSender(EventUtil.getSender(event), () -> {
                triggerEvent(method, listener, event);
            });
        };
    }

    @SneakyThrows
    private void triggerEvent(Method method, Listener listener, Event event) {
        AopUtils.invokeJoinpointUsingReflection(listener, method, new Object[] {event});
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy