Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

A fast and feature rich but still easy to use Event library for Java

License

Notifications You must be signed in to change notification settings

FlorianMichael/DietrichEvents

Repository files navigation

This project has been discontinued in favour of DietrichEvents2 which is faster and has more features.

DietrichEvents

A fast and feature rich but still easy to use Event library for Java

Contact

If you encounter any issues, please report them on the issue tracker.
If you just want to talk or need help with DietrichEvents feel free to join my Discord.

How to add this to your project

Gradle/Maven

To use DietrichEvents with Gradle/Maven you can use Lenni0451's repository or Jitpack.
You can also find instructions how to implement it into your build script there.

Jar File

If you just want the latest jar file you can download it from the GitHub Actions or use the Release.

Example usage

Create instance

You can use either DietrichEvents.createThreadSafe() or DietrichEvents.createDefault() to create an instance of the EventSystem, if you want to specify the mapping function yourself, there is also a normal DietrichEvents.create(). For a thread safe DietrichEvents there is already a global instance that you can call with DietrichEvents.global().

There are a few functions in DietrichEvents to implement parts of the event system yourself:

This function allows you to override the comparator used for sorting the priorities:
setPriorityOrder(Comparator<Subscription<?>> priorityOrder);

This function allows you to override the error handling:
setErrorHandler(Consumer errorHandler);

This function allows you to replace the sorting algorithm used for sorting the priorities:
setSortCallback(BiConsumer<List<Subscription>, Comparator>> sortCallback);

Usage with FastUtil

To get an DietrichEvents that uses FastUtil, you can do this:
DietrichEvents.create(new ConcurrentHashMap<>(), Object2ObjectArrayMap::new);

Create an Event

public interface ExampleListener extends Listener {
    
    void onPreExample();
    void onExample(final EventStateType eventStateType);
    
    class ExampleEvent extends AbstractEvent<ExampleListener> {
        private final EventStateType eventStateType;
        
        public ExampleEvent(final EventStateType eventStateType) {
            this.eventStateType = eventStateType;
        }
        
        @Override
        public void call(final ExampleListener listener) {
            listener.onExample(eventStateType);
            if (eventStateType == EventStateType.PRE) {
                listener.onPreExample();
            }
        }

        @Override
        public Class<ExampleListener> getListenerType() {
            return ExampleListener.class;
        }
    }
}

Register listener

public class ExampleListenerUsage implements ExampleListener {

    // You can also use subscribeClass and subscribeClassInternal to subscribe all listeners from a specific class / object
    public void registerListeners() {
        DietrichEvents.global().subscribe(ExampleListener.class, this);
    }

    public void unregisterListeners() {
        DietrichEvents.global().unsubscribe(ExampleListener.class, this);
    }
    
    @Override
    public void onPreExample() {
    }

    @Override
    public void onExample(EventStateType eventStateType) {
    }
}

Calling an Event

// You can use either the post or the postInternal function, where postInternal has no error handling.
DietrichEvents.global().post(new ExampleListener.ExampleEvent(EventStateType.PRE));

JMH Benchmark

For a comparison you can look here