Skip to content

Commit

Permalink
docs: add javadoc for event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Sep 22, 2024
1 parent 4ece0e4 commit 39dfa20
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/src/main/java/org/allaymc/api/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@
public interface EventBus {
ApiInstanceHolder<Factory> FACTORY = ApiInstanceHolder.create();

/**
* Create a new event bus.
*
* @return a new event bus.
*/
static EventBus create() {
return FACTORY.get().create();
}

/**
* Register a listener.
*
* @param listener the listener to register.
*/
void registerListener(Object listener);

/**
* Unregister a listener.
*
* @param listener the listener to unregister.
*/
void unregisterListener(Object listener);

/**
* Call an event.
*
* @param event the event to call.
* @return the event.
* @param <E> the type of the event.
*/
<E extends Event> E callEvent(E event);

interface Factory {
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/org/allaymc/api/eventbus/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
import java.lang.annotation.Target;

/**
* An annotation to mark a method as an event handler.
*
* @author daoge_cmd
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface EventHandler {
/**
* Whether the event handler should be called asynchronously.
*
* @return {@code true} if the event handler should be called asynchronously, otherwise {@code false}
*/
boolean async() default false;

/**
* The priority of the event handler.
*
* @return the priority of the event handler
*/
int priority() default 0;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package org.allaymc.api.eventbus.event;

/**
* An interface to represent a cancellable event.
* <p>
* Event that implements this interface is cancelled.
*
* @author daoge_cmd
*/
public interface CancellableEvent {
/**
* Check whether the event is cancelled.
*
* @return {@code true} if the event is cancelled, otherwise {@code false}.
*/
boolean isCancelled();

/**
* Set the event to be cancelled or not.
*
* @param value {@code true} if the event is cancelled, otherwise {@code false}.
*/
void setCancelled(boolean value);

/**
* Cancel the event.
*/
default void cancel() {
setCancelled(true);
}
Expand Down
16 changes: 16 additions & 0 deletions api/src/main/java/org/allaymc/api/eventbus/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
import org.allaymc.api.server.Server;

/**
* An abstract class to represent an event.
*
* @author daoge_cmd
*/
public abstract class Event {
@Getter
private boolean cancelled = false;

/**
* Call the event with the default event bus.
*/
public void call() {
call(Server.getInstance().getEventBus());
}

/**
* Call the event with the specified event bus.
*
* @param eventBus the event bus to call the event.
*/
public void call(EventBus eventBus) {
eventBus.callEvent(this);
}

/**
* Cancel the event.
*
* @param value {@code true} if the event is cancelled, otherwise {@code false}.
* @throws EventException if the event is not cancellable.
*/
public void setCancelled(boolean value) {
if (!(this instanceof CancellableEvent)) {
throw new EventException("Event is not Cancellable");
Expand Down

0 comments on commit 39dfa20

Please sign in to comment.