SHARE
TWEET

Untitled

a guest Dec 15th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.caved_in.commons.event;
  2.  
  3. import org.bukkit.event.Event;
  4. import org.bukkit.event.EventPriority;
  5. import org.bukkit.event.Listener;
  6. import org.bukkit.plugin.EventExecutor;
  7. import org.bukkit.plugin.Plugin;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.function.Consumer;
  12.  
  13. public class EventListener<T extends Event> implements Listener {
  14.     private final List<Consumer<T>> listeners = new ArrayList<>();
  15.  
  16.     public EventListener(Plugin parent, Class<T> eventClass) {
  17.         this(parent,eventClass,EventPriority.NORMAL);
  18.     }
  19.  
  20.     public EventListener(Plugin plugin, Class<T> eventClass, EventPriority priority) {
  21.         EventExecutor eventExecutor = (listener, e) -> {
  22.           eventListener((T)e);
  23.         };
  24.  
  25.         plugin.getServer().getPluginManager().registerEvent(eventClass, this, priority, eventExecutor, plugin);
  26.     }
  27.  
  28.     public boolean onEvent(Consumer<T> listener) { return this.listeners.add(listener); }
  29.  
  30.     protected void eventListener(T event) {
  31.         for (Consumer<T> listener : listeners) {
  32.             listener.accept(event);
  33.         }
  34.     }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top