Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
allow plugins to register custom listeners for commands
Browse files Browse the repository at this point in the history
remove registerListeners callback (use init instead)
update shadow plugin
  • Loading branch information
natanbc committed Feb 19, 2019
1 parent 75479be commit 9e24a02
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
28 changes: 19 additions & 9 deletions api/src/main/java/andesite/node/Plugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package andesite.node;

import andesite.node.event.EventDispatcher;
import andesite.node.handler.WebSocketState;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import gg.amy.singyeong.Dispatch;
Expand All @@ -16,6 +15,18 @@
* or intercept commands.
*/
public interface Plugin {
/**
* Called when plugin loading is complete. This method is the first callback to be called
* and is guaranteed to run only once.
*
* <br><br>Blocking in this method is not a problem, but will delay node initialization.
*
* @param state State of the node.
*
* @see NodeState
*/
default void init(@Nonnull NodeState state) {}

/**
* Called to allow configuration of an audio player manager. May be called more than once with
* different player managers.
Expand Down Expand Up @@ -73,21 +84,20 @@ default boolean requiresRouter() {
default void configureRouter(@Nonnull NodeState state, @Nonnull Router router) {}

/**
* Called to allow registration of listeners. The event dispatcher provided is the same as
* {@link NodeState#dispatcher()}. This method is guaranteed to run exactly once and before
* all other callbacks.
*
* <br>If you need to do any setup when the plugin loads, do it in this method.
* Starts custom listeners for this plugin. If the default listeners are disabled
* and no plugin has custom listeners, the process will exit.
*
* <br><br>Blocking in this method is not a problem, but will delay node initialization.
*
* @param state State of the node.
* @param dispatcher Event dispatcher, shortcut for {@link NodeState#dispatcher()}.
*
* @return True if a listener was started.
*
* @see NodeState
* @see EventDispatcher
*/
default void registerListeners(@Nonnull NodeState state, @Nonnull EventDispatcher dispatcher) {}
default boolean startListeners(@Nonnull NodeState state) {
return false;
}

/**
* Called when a REST request is received. Runs on the event loop, so <b>blocking should be avoided.</b>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '2.0.4'
id 'com.github.johnrengelman.shadow' version '4.0.4'
id 'com.palantir.docker' version '0.21.0'
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/andesite/node/Andesite.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private Andesite(@Nonnull Vertx vertx, @Nonnull Config config) throws IOExceptio
this.config = config;
this.audioHandler = createAudioHandler(config);
this.handler = new RequestHandler(this);
pluginManager.registerListeners(dispatcher);
pluginManager.init();
pluginManager.configurePlayerManager(playerManager);
pluginManager.configurePlayerManager(pcmPlayerManager);
this.enabledSources = SOURCE_MANAGERS.keySet().stream()
Expand Down Expand Up @@ -255,7 +255,9 @@ public static void main(String[] args) throws IOException {
var andesite = new Andesite(Vertx.vertx(), config);
Init.postInit(andesite);
//NOTE: use the bitwise or operator, as it forces evaluation of all elements
if(!(RestHandler.setup(andesite) | SingyeongHandler.setup(andesite))) {
if(!(RestHandler.setup(andesite)
| SingyeongHandler.setup(andesite)
| andesite.pluginManager().startListeners())) {
log.error("No handlers enabled, aborting");
System.exit(-1);
}
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/andesite/node/plugin/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import andesite.node.NodeState;
import andesite.node.Plugin;
import andesite.node.event.EventDispatcher;
import andesite.node.handler.WebSocketState;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import gg.amy.singyeong.Dispatch;
Expand Down Expand Up @@ -40,6 +39,18 @@ public void load(@Nonnull File path) throws IOException {
}
}

public void init() {
for(var p : plugins) {
p.init(state);
}
}

public void configurePlayerManager(@Nonnull AudioPlayerManager manager) {
for(var p : plugins) {
p.configurePlayerManager(state, manager);
}
}

public boolean requiresRouter() {
for(var p : plugins) {
if(p.requiresRouter()) {
Expand All @@ -55,16 +66,12 @@ public void configureRouter(@Nonnull Router router) {
}
}

public void configurePlayerManager(@Nonnull AudioPlayerManager manager) {
for(var p : plugins) {
p.configurePlayerManager(state, manager);
}
}

public void registerListeners(@Nonnull EventDispatcher dispatcher) {
public boolean startListeners() {
var started = false;
for(var p : plugins) {
p.registerListeners(state, dispatcher);
started |= p.startListeners(state);
}
return started;
}

public boolean customHandleHttpRequest(@Nonnull RoutingContext context) {
Expand Down

0 comments on commit 9e24a02

Please sign in to comment.