Skip to content
/ Rokit Public

A modern and efficient event system for java

Notifications You must be signed in to change notification settings

starobot/Rokit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ROKIT

CodeFactor

A fast, efficient and flexible event system for java 21.

Here my goal was to rework StaroEventSystem entirely, make it more flexible, but without the loss of performance at the end.

Dependency: gradle:

mavenCentral()
maven { url 'https://jitpack.io' }
implementation 'com.github.starobot:Rokit:1.2'

maven:

<repositories>
	<repository>
		<id>jitpack.io</id>
		 <url>https://jitpack.io</url>
	</repository>
</repositories>
	<dependency>
	    <groupId>com.github.starobot</groupId>
	    <artifactId>Rokit</artifactId>
	    <version>1.2</version>
	</dependency>

How to use it:

First, you're gonna need to make an instance of the eventBus.

EventBus eventBus = EventBus.builder()
                .build();

For the custom listener annotation and listener factory you can use this builder tool.

BiFunction<Object, Method, EventListener> customFactory = (instance, method) ->
                new CustomListener(instance, method, method.getAnnotation(CustomAnnotation.class).priority().getVal());

EventBus eventBus = EventBus.builder()
                .registerListenerFactory(CustomAnnotation.class, customFactory)
                .build();

Now, to receive an event, we need to subscribe the lister class, so the methods annotated with @Listener start receiving events.

eventBus.subscribe(new EventReceivingClass());

To remove the listener, there's this method. The class containing listener methods will no longer get executed upon dispatching an event.

eventBus.unsubscribe(new EventReceivingClass());

To dispatch an event, you can use method "post"

eventBus.post(new Event());

The eventBus also supports dispatching the event data type directly. (Shoutout to cattyn) For posting such an event, you must make a wrapper for the event class and specify the field you are passing. For example, if we have an event with a String:

public class StringEvent {
    private final String name;

    public StringEvent(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

}

Now, to successfully dispatch it we would only need to specify a wrapper for the event:

EventBus eventBus = EventBus.builder()
                .wrapSingle(StringEvent.class, StringEvent::getName)
                .build();

To get the data directly, you can make make a listener method with two arguments now:

@Listener
public void onEvent(StringEvent<?> event, String name) {
     //if (name ....
}

TODO:

  1. fix custom multieventconsumer creation
  2. make an empty eventbus instance option

About

A modern and efficient event system for java

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages