Skip to content

Instantly share code, notes, and snippets.

@MiniDigger
Created May 29, 2017 17:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MiniDigger/f369210813bd65d43a62468b8dafcaeb to your computer and use it in GitHub Desktop.
Save MiniDigger/f369210813bd65d43a62468b8dafcaeb to your computer and use it in GitHub Desktop.
GlobalEventListener for Spigot/Bukkit, licenced under GPLv3
package me.minidigger.test.test;
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;
import org.reflections.Reflections;
public class GlobalEventListener implements Listener {
private Plugin plugin;
public void doStuff(Plugin plugin) {
this.plugin = plugin;
// search event classes
Reflections reflections = new Reflections("org.bukkit");// change to also find custom events
Set<Class<? extends Event>> eventClasses = reflections.getSubTypesOf(Event.class).stream().
filter(clazz -> Arrays.stream(clazz.getDeclaredFields())
.anyMatch(field -> field.getType().getName().endsWith("HandlerList")))
.collect(Collectors.toSet());
plugin.getLogger().info("Found " + eventClasses.size() + " available events!");
plugin.getLogger()
.info(eventClasses.stream().map(Class::getName).collect(Collectors.joining(", ")));
// register events
EventExecutor eventExecutor = (listener, event) -> iGetCalledForEveryEvent(event);
eventClasses.forEach(clazz -> plugin.getServer().getPluginManager()
.registerEvent(clazz, this, EventPriority.MONITOR, eventExecutor, plugin));
}
private final String[] ignored = {"VehicleBlockCollisionEvent", "EntityAirChangeEvent",
"VehicleUpdateEvent", "ChunkUnloadEvent", "ChunkLoadEvent"};
public void iGetCalledForEveryEvent(Event event) {
if (Arrays.stream(ignored).anyMatch(ignored -> event.getEventName().equals(ignored))) {
return;
}
plugin.getLogger().info(event.getEventName() + " was called!");
if (event instanceof PlayerJoinEvent) {
PlayerJoinEvent playerJoinEvent = (PlayerJoinEvent) event;
plugin.getLogger().info(playerJoinEvent.getPlayer().getName() + " joined the game!!!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment