Lightweight Java event library created and maintained by 7orivorian
- Include JitPack in your maven build file
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- Add Wraith as a dependency
<dependency>
<groupId>com.github.7orivorian</groupId>
<artifactId>Wraith</artifactId>
<version>3.3.0</version>
</dependency>
- Add JitPack to your root
build.gradle
at the end of repositories
repositories {
maven {
url 'https://jitpack.io'
}
}
- Add the dependency
dependencies {
implementation 'com.github.7orivorian:Wraith:3.3.0'
}
Download a .jar
file from releases
- Clone this repository
- Run
mvn package
Packaged file can be found in the target/
directory.
While the code itself is thoroughly documented, here's a simple guide to help you get started with the latest features.
Details...
To define a subscriber, you have multiple options:
Extending the Subscriber class:
public class ExampleSubscriber extends Subscriber {
// ...
}
Implementing the ISubscriber interface:
public class ExampleSubscriber implements ISubscriber {
// ...
}
Once you've defined your subscriber, you can subscribe it to an event bus directly within the subscriber's constructor:
public class Consts {
private static final IEventBus EVENT_BUS = new EventBus();
}
public class ExampleSubscriber extends Subscriber {
public ExampleSubscriber() {
Consts.EVENT_BUS.subscribe(this);
}
}
Alternatively, you can subscribe a subscriber externally:
public class Example {
private static final IEventBus EVENT_BUS = new EventBus();
public static void main(String[] args) {
EVENT_BUS.subscribe(new ExampleSubscriber());
}
}
Details...
Any class can be used as an event. For instance:
public class ExampleEvent {
private String message;
public ExampleEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Details...
For class event listeners, you can define your listeners as follows:
public class ExampleListener extends EventListener<ExampleEvent> {
public ExampleListener() {
super(ExampleEvent.class);
}
@Override
public void invoke(ExampleEvent event) {
event.setMessage("Hello world!");
}
}
public class ExampleSubscriber extends Subscriber {
public ExampleSubscriber() {
// Register the listener
registerListener(new ExampleListener());
}
}
Lambda event listeners provide a more concise way to achieve the same functionality:
public class ExampleSubscriber extends Subscriber {
public ExampleSubscriber() {
// Register the listener
registerListener(
new LambdaEventListener<>(ExampleEvent.class, event -> event.setMessage("Hello world!"))
);
}
}
Details...
To dispatch an event, call one of the dispatch
methods defined in EventBus
, passing your event as a parameter:
import me.tori.wraith.event.staged.EventStage;
public class Example {
private static final IEventBus EVENT_BUS = new EventBus();
public static void main(String[] args) {
ExampleEvent event = new ExampleEvent("world greetings");
EVENT_BUS.dispatch(event);
System.out.println(event.getMessage());
}
}
Please explore the example folder for even more Wraith implementations!
Contributions are welcome! Feel free to open a pull request.
- Utilize similar formatting and practises to the rest of the codebase
- Do not include workspace files (such as an
.idea/
ortarget/
directory) in your pull request - Include unit tests for any features you add
To make a contribution, follow these steps:
- Fork and clone this repository
- Make the changes to your fork
- Submit a pull request
The MIT License is a permissive open-source license that allows you to use, modify, and distribute the software for both personal and commercial purposes. You are not required to share your changes, but you must include the original copyright notice and disclaimer in your distribution. The software is provided "as is," without any warranties or conditions.