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

dev.alangomes.springspigot.reactive.EventEmitter Maven / Gradle / Ivy

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

import dev.alangomes.springspigot.context.Context;
import dev.alangomes.springspigot.event.EventUtil;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.FieldDefaults;
import lombok.val;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

@AllArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
class EventEmitter implements ObservableOnSubscribe {

    @Getter
    Listener listener = new Listener() {
    };

    Class eventClazz;

    ObserveEvent observeEvent;

    Plugin plugin;

    Context context;

    @Override
    public void subscribe(ObservableEmitter observableEmitter) {
        val pluginManager = plugin.getServer().getPluginManager();
        pluginManager.registerEvent(eventClazz, listener, observeEvent.priority(), (l, event) -> {
            if (eventClazz.isAssignableFrom(event.getClass())) {
                T emittedEvent = (T) event;
                context.runWithSender(EventUtil.getSender(emittedEvent), () -> observableEmitter.onNext(emittedEvent));
            }
        }, plugin, observeEvent.ignoreCancelled());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy