Skip to content

Instantly share code, notes, and snippets.

@TehBrian
Created October 21, 2023 20:14
Show Gist options
  • Save TehBrian/6ce4286a8ff43dcc1f4bbc355a270990 to your computer and use it in GitHub Desktop.
Save TehBrian/6ce4286a8ff43dcc1f4bbc355a270990 to your computer and use it in GitHub Desktop.
SuperDuperEventListener
public final class SuperDuperEventListener implements Listener {
private final JavaPlugin plugin;
@Inject
public SuperDuperEventListener(
final JavaPlugin plugin
) {
this.plugin = plugin;
}
public void doStuff() {
// search event classes
Reflections reflections = new Reflections();
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());
this.plugin.getLogger().info("Found " + eventClasses.size() + " available events!");
this.plugin.getLogger().info(eventClasses.stream().map(Class::getName).collect(Collectors.joining(", ")));
// register events
EventExecutor eventExecutor = (listener, event) -> this.iGetCalledForEveryEvent(event);
eventClasses.forEach(clazz -> this.plugin.getServer().getPluginManager()
.registerEvent(clazz, this, EventPriority.MONITOR, eventExecutor, this.plugin));
}
private static final String[] IGNORED = {"VehicleBlockCollisionEvent", "EntityAirChangeEvent",
"VehicleUpdateEvent", "ChunkUnloadEvent", "ChunkLoadEvent", "PlayerMoveEvent", "EntityDamageByBlockEvent",
"EntitiesLoadEvent", "EntityPoseChangeEvent", "EntitiesUnloadEvent", "EntityMoveEvent",
"ServerTickEndEvent", "ServerTickStartEvent", "PlayerNaturallySpawnCreaturesEvent",
"PlayerChunkLoadEvent", "PlayerChunkUnloadEvent", "GenericGameEvent"};
public void iGetCalledForEveryEvent(final Event event) {
if (Arrays.stream(IGNORED).anyMatch(ignored -> event.getEventName().equals(ignored))) {
return;
}
this.plugin.getServer().sendMessage(Component.text(event.getEventName()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment