Switch to utilizing the new events bus
This commit is contained in:
parent
4ebf878bc8
commit
92d152d04f
10 changed files with 57 additions and 234 deletions
|
@ -43,6 +43,11 @@ repositories {
|
||||||
// mavenCentral()
|
// mavenCentral()
|
||||||
maven {
|
maven {
|
||||||
url = "https://maven.zontreck.dev/repository/internal"
|
url = "https://maven.zontreck.dev/repository/internal"
|
||||||
|
name = "Aria's Creations Caches"
|
||||||
|
}
|
||||||
|
|
||||||
|
maven {
|
||||||
|
url = "https://maven.zontreck.dev/repository/zontreck"
|
||||||
name = "Aria's Creations"
|
name = "Aria's Creations"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,6 +59,9 @@ dependencies {
|
||||||
|
|
||||||
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
|
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
|
||||||
implementation 'com.google.guava:guava:31.1-jre'
|
implementation 'com.google.guava:guava:31.1-jre'
|
||||||
|
|
||||||
|
implementation "dev.zontreck:EventsBus:${Bus_API}.${Bus_Patch}"
|
||||||
|
api "dev.zontreck:EventsBus:${Bus_API}.${Bus_Patch}:sources"
|
||||||
}
|
}
|
||||||
|
|
||||||
def MAVEN_PASSWORD_PROPERTY = "AriasCreationsMavenPassword"
|
def MAVEN_PASSWORD_PROPERTY = "AriasCreationsMavenPassword"
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
apiVer = 1.3
|
apiVer=1.3
|
||||||
|
Bus_API=1.0
|
||||||
|
Bus_Patch=16
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
|
@ -0,0 +1 @@
|
||||||
|
rootProject.name = "LibAC"
|
|
@ -1,31 +1,26 @@
|
||||||
package dev.zontreck.ariaslib.events;
|
package dev.zontreck.ariaslib.events;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import dev.zontreck.eventsbus.Cancellable;
|
||||||
|
import dev.zontreck.eventsbus.Event;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
@Cancellable
|
||||||
|
public class CommandEvent extends Event {
|
||||||
public class CommandEvent extends Event
|
|
||||||
{
|
|
||||||
public String command;
|
public String command;
|
||||||
public List<String> arguments = Lists.newArrayList();
|
public List<String> arguments = Lists.newArrayList();
|
||||||
public CommandEvent(String commandString)
|
|
||||||
{
|
public CommandEvent(String commandString) {
|
||||||
String[] cmds = commandString.split(" ");
|
String[] cmds = commandString.split(" ");
|
||||||
if(cmds.length > 0)
|
if (cmds.length > 0) {
|
||||||
{
|
|
||||||
command = cmds[0];
|
command = cmds[0];
|
||||||
if(cmds.length==1)return;
|
if (cmds.length == 1) return;
|
||||||
for(int i=1;i<cmds.length;i++)
|
for (int i = 1; i < cmds.length; i++) {
|
||||||
{
|
|
||||||
arguments.add(cmds[i]);
|
arguments.add(cmds[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}else throw new IllegalArgumentException("The command cannot be empty!");
|
} else throw new IllegalArgumentException("The command cannot be empty!");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isCancellable() {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
package dev.zontreck.ariaslib.events;
|
|
||||||
|
|
||||||
|
|
||||||
public abstract class Event {
|
|
||||||
private boolean isCancelled=false;
|
|
||||||
/**
|
|
||||||
* True is the event ended early due to a unhandled exception.
|
|
||||||
*
|
|
||||||
* The event bus will continue processing.
|
|
||||||
*/
|
|
||||||
public boolean exceptionThrown = false;
|
|
||||||
|
|
||||||
|
|
||||||
public Event()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract boolean isCancellable();
|
|
||||||
|
|
||||||
public boolean isCancelled()
|
|
||||||
{
|
|
||||||
return isCancelled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCancelled(boolean cancel)
|
|
||||||
{
|
|
||||||
if(!isCancellable())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isCancelled=cancel;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,96 +0,0 @@
|
||||||
package dev.zontreck.ariaslib.events;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import dev.zontreck.ariaslib.events.annotations.Subscribe;
|
|
||||||
import dev.zontreck.ariaslib.exceptions.EventRegistrationException;
|
|
||||||
|
|
||||||
public class EventBus
|
|
||||||
{
|
|
||||||
public static final EventBus BUS = new EventBus();
|
|
||||||
public EventsListenerList listeners = new EventsListenerList();
|
|
||||||
/**
|
|
||||||
* METHODS MUST BE STATIC!!!
|
|
||||||
* @param clazz
|
|
||||||
*/
|
|
||||||
public void register(Class<?> clazz)
|
|
||||||
{
|
|
||||||
Arrays.stream(clazz.getMethods())
|
|
||||||
.filter(m->Modifier.isStatic(m.getModifiers()))
|
|
||||||
.filter(m->m.isAnnotationPresent(Subscribe.class))
|
|
||||||
.forEach(m-> {
|
|
||||||
try {
|
|
||||||
registerListeners(clazz, m, m);
|
|
||||||
} catch (EventRegistrationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerListeners(final Object target, final Method method, final Method real) throws EventRegistrationException {
|
|
||||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
|
||||||
if(parameterTypes.length != 1)
|
|
||||||
{
|
|
||||||
throw new EventRegistrationException("Method "+method+" has an @Subscribe annotation but must have only 1 argument, no more and no less");
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<?> eventType = parameterTypes[0];
|
|
||||||
|
|
||||||
if(!Event.class.isAssignableFrom(eventType))
|
|
||||||
{
|
|
||||||
throw new EventRegistrationException("Event ["+eventType+"] is not a subtype of ["+Event.class.getName()+"]");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
register(eventType, target, real);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void register(Class<?> eventType, Object target, Method real) {
|
|
||||||
listeners.addEventMethod(eventType, real, real.getDeclaringClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Posts an event to the event bus
|
|
||||||
* @param event
|
|
||||||
* @return True if the event was cancelled.
|
|
||||||
*/
|
|
||||||
public boolean post(Event event)
|
|
||||||
{
|
|
||||||
boolean cancelled = false;
|
|
||||||
for (Map.Entry<String, List<EventContainer>> entry : listeners.events.entrySet()) {
|
|
||||||
Class<?> eventClazz;
|
|
||||||
try {
|
|
||||||
eventClazz = Class.forName(entry.getKey());
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
List<EventContainer> containers = entry.getValue();
|
|
||||||
|
|
||||||
if(eventClazz.equals(event.getClass()))
|
|
||||||
{
|
|
||||||
for (EventContainer eventContainer : containers) {
|
|
||||||
try {
|
|
||||||
//Event t = (Event)eventClazz.getConstructor().newInstance();
|
|
||||||
eventContainer.function.invoke(eventContainer.containingClass, event);
|
|
||||||
} catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException | SecurityException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (Exception e)
|
|
||||||
{
|
|
||||||
event.exceptionThrown=true;
|
|
||||||
}
|
|
||||||
|
|
||||||
cancelled=event.isCancelled();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cancelled;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
package dev.zontreck.ariaslib.events;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public class EventContainer {
|
|
||||||
public Method function;
|
|
||||||
public Class<?> containingClass;
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package dev.zontreck.ariaslib.events;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
|
|
||||||
public class EventsListenerList {
|
|
||||||
|
|
||||||
public Map<String, List<EventContainer>> events = Maps.newHashMap();
|
|
||||||
|
|
||||||
public void addEventMethod(Class<?> e, Method action, Class<?> clazz)
|
|
||||||
{
|
|
||||||
// Add a new container!
|
|
||||||
EventContainer contains = new EventContainer();
|
|
||||||
contains.function=action;
|
|
||||||
contains.containingClass = clazz;
|
|
||||||
|
|
||||||
if((events.entrySet().stream()
|
|
||||||
.filter(entry -> entry.getKey() == e.getName()).count()) == 0){
|
|
||||||
List<EventContainer> tmp = Lists.newArrayList();
|
|
||||||
tmp.add(contains);
|
|
||||||
events.put(e.getName(), tmp);
|
|
||||||
}else {
|
|
||||||
events.get(e.getName()).add(contains);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
package dev.zontreck.ariaslib.events.annotations;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Retention(value = RetentionPolicy.RUNTIME)
|
|
||||||
@Target(value = ElementType.METHOD)
|
|
||||||
public @interface Subscribe
|
|
||||||
{
|
|
||||||
}
|
|
|
@ -1,32 +1,36 @@
|
||||||
package dev.zontreck.ariaslib.terminal;
|
package dev.zontreck.ariaslib.terminal;
|
||||||
|
|
||||||
import java.io.Console;
|
|
||||||
|
|
||||||
import dev.zontreck.ariaslib.events.CommandEvent;
|
import dev.zontreck.ariaslib.events.CommandEvent;
|
||||||
import dev.zontreck.ariaslib.events.EventBus;
|
|
||||||
import dev.zontreck.ariaslib.util.DelayedExecutorService;
|
import dev.zontreck.ariaslib.util.DelayedExecutorService;
|
||||||
|
import dev.zontreck.eventsbus.Bus;
|
||||||
|
|
||||||
public class ConsolePrompt extends Task
|
import java.io.Console;
|
||||||
{
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
public class ConsolePrompt extends Task {
|
||||||
public static final Console console = System.console();
|
public static final Console console = System.console();
|
||||||
|
|
||||||
public ConsolePrompt()
|
public ConsolePrompt() {
|
||||||
{
|
super("ConsolePrompt", true);
|
||||||
super("ConsolePrompt",true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run() {
|
||||||
{
|
|
||||||
// Print a prompt
|
// Print a prompt
|
||||||
console.printf("\n"+Terminal.PREFIX+ " > ");
|
console.printf("\n" + Terminal.PREFIX + " > ");
|
||||||
String commandInput = console.readLine();
|
String commandInput = console.readLine();
|
||||||
|
|
||||||
CommandEvent event = new CommandEvent(commandInput);
|
CommandEvent event = new CommandEvent(commandInput);
|
||||||
if(!EventBus.BUS.post(event)){
|
try {
|
||||||
|
if (!Bus.Post(event)) {
|
||||||
DelayedExecutorService.getInstance().schedule(new ConsolePrompt(), 2);
|
DelayedExecutorService.getInstance().schedule(new ConsolePrompt(), 2);
|
||||||
}
|
}
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue