Expose event listener registry in API

This commit is contained in:
Brady 2018-10-06 20:16:38 -05:00
parent 6c9f317f31
commit d5130aa6ba
No known key found for this signature in database
GPG Key ID: 73A788379A197567
4 changed files with 20 additions and 16 deletions

View File

@ -19,6 +19,7 @@ package baritone.api;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.event.listener.IGameEventListener;
import java.util.Iterator;
import java.util.ServiceLoader;
@ -71,4 +72,8 @@ public final class BaritoneAPI {
public static IWorldProvider getWorldProvider() {
return baritone.getWorldProvider();
}
public static void registerEventListener(IGameEventListener listener) {
baritone.registerEventListener(listener);
}
}

View File

@ -19,6 +19,7 @@ package baritone.api;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.event.listener.IGameEventListener;
/**
* @author Brady
@ -67,4 +68,11 @@ public interface IBaritoneProvider {
* @return The {@link IWorldProvider} instance
*/
IWorldProvider getWorldProvider();
/**
* Registers a {@link IGameEventListener} with Baritone's "event bus".
*
* @param listener The listener
*/
void registerEventListener(IGameEventListener listener);
}

View File

@ -21,7 +21,6 @@ import baritone.api.BaritoneAPI;
import baritone.api.Settings;
import baritone.api.event.listener.IGameEventListener;
import baritone.behavior.*;
import baritone.cache.WorldProvider;
import baritone.event.GameEventHandler;
import baritone.utils.BaritoneAutoTest;
import baritone.utils.InputOverrideHandler;
@ -36,7 +35,6 @@ import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* @author Brady
@ -61,19 +59,13 @@ public enum Baritone {
private File dir;
private ThreadPoolExecutor threadPool;
/**
* List of consumers to be called after Baritone has initialized
*/
private List<Consumer<Baritone>> onInitConsumers;
/**
* Whether or not Baritone is active
*/
private boolean active;
Baritone() {
this.onInitConsumers = new ArrayList<>();
this.gameEventHandler = new GameEventHandler();
}
public synchronized void init() {
@ -81,7 +73,6 @@ public enum Baritone {
return;
}
this.threadPool = new ThreadPoolExecutor(4, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());
this.gameEventHandler = new GameEventHandler();
this.inputOverrideHandler = new InputOverrideHandler();
// Acquire the "singleton" instance of the settings directly from the API
@ -109,8 +100,6 @@ public enum Baritone {
this.active = true;
this.initialized = true;
this.onInitConsumers.forEach(consumer -> consumer.accept(this));
}
public boolean isInitialized() {
@ -157,8 +146,4 @@ public enum Baritone {
public File getDir() {
return this.dir;
}
public void registerInitListener(Consumer<Baritone> runnable) {
this.onInitConsumers.add(runnable);
}
}

View File

@ -20,6 +20,7 @@ package baritone;
import baritone.api.IBaritoneProvider;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.event.listener.IGameEventListener;
import baritone.behavior.*;
import baritone.cache.WorldProvider;
@ -58,4 +59,9 @@ public final class BaritoneProvider implements IBaritoneProvider {
public IWorldProvider getWorldProvider() {
return WorldProvider.INSTANCE;
}
@Override
public void registerEventListener(IGameEventListener listener) {
Baritone.INSTANCE.registerEventListener(listener);
}
}