feat: IBus interface

This commit is contained in:
zaaarf 2023-08-18 18:12:58 +02:00
parent 0dbbe17261
commit d48726c80f
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
4 changed files with 31 additions and 6 deletions

View file

@ -1,4 +1,7 @@
package ftbsc.geb.api; package ftbsc.geb;
import ftbsc.geb.api.IBus;
import ftbsc.geb.api.IEvent;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.Map; import java.util.Map;
@ -6,10 +9,10 @@ import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
* The main event bus class. * The official GEB implementation of {@link IBus}.
* @since 0.1.0 * @since 0.1.0
*/ */
public class GEB { public class GEB implements IBus {
/** /**
* The identifier of this bus. Methods * The identifier of this bus. Methods
@ -40,6 +43,7 @@ public class GEB {
/** /**
* @return the identifier of this bus * @return the identifier of this bus
*/ */
@Override
public String getIdentifier() { public String getIdentifier() {
return identifier; return identifier;
} }
@ -50,6 +54,7 @@ public class GEB {
* @param event the event to fire * @param event the event to fire
* @return true if the event was canceled, false otherwise * @return true if the event was canceled, false otherwise
*/ */
@Override
public boolean handleEvent(IEvent event) { public boolean handleEvent(IEvent event) {
try { try {
return eventMapper.get(event.getClass()).newInstance(event).callListeners(this.getIdentifier()); return eventMapper.get(event.getClass()).newInstance(event).callListeners(this.getIdentifier());

View file

@ -0,0 +1,20 @@
package ftbsc.geb.api;
/**
* A generic interface for a bus that can work with this
* event system.
* @since 0.1.0
*/
public interface IBus {
/**
* @return the identifier of this bus
*/
String getIdentifier();
/**
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
* @param event the event to fire
* @return true if the event was canceled, false otherwise
*/
boolean handleEvent(IEvent event);
}

View file

@ -10,7 +10,7 @@ public interface IEvent {
* calls to listeners, but its return values will be ignored. You probably * calls to listeners, but its return values will be ignored. You probably
* don't want to touch this. * don't want to touch this.
* @param identifier the identifier of the bus that's calling this * @param identifier the identifier of the bus that's calling this
* @return the value {@link GEB#handleEvent(IEvent)} will return for this * @return the value {@link IBus#handleEvent(IEvent)} will return for this
*/ */
default boolean callListeners(String identifier) { default boolean callListeners(String identifier) {
return false; return false;

View file

@ -1,6 +1,6 @@
package ftbsc.geb.api.annotations; package ftbsc.geb.api.annotations;
import ftbsc.geb.api.GEB; import ftbsc.geb.api.IBus;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -8,7 +8,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* This annotation should mark either a static instance of {@link GEB} * This annotation should mark either a static instance of {@link IBus}
* or a static method returning one. * or a static method returning one.
* @since 0.1.0 * @since 0.1.0
*/ */