Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] New event driver + api #396

Merged
merged 9 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions api/src/main/java/com/craftmend/openaudiomc/api/ApiHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,46 @@ public class ApiHolder {
static WorldApi worldApiInstance;
static VoiceApi voiceApiInstance;
static MediaApi mediaApiInstance;
static EventApi eventApiInstance;

public static void initiate(
ClientApi clientApi,
WorldApi worldApi,
VoiceApi voiceApi,
MediaApi mediaApi
ClientApi clientApi
) {
if (clientApiInstance != null) throw new IllegalStateException("Api already initiated");

clientApiInstance = clientApi;
}

public static void initiate(
WorldApi worldApi
) {
if (worldApiInstance != null) throw new IllegalStateException("Api already initiated");

worldApiInstance = worldApi;
}

public static void initiate(
VoiceApi voiceApi
) {
if (voiceApiInstance != null) throw new IllegalStateException("Api already initiated");

voiceApiInstance = voiceApi;
}

public static void initiate(
MediaApi mediaApi
) {
if (mediaApiInstance != null) throw new IllegalStateException("Api already initiated");

mediaApiInstance = mediaApi;
}

public static void initiate(
EventApi eventApi
) {
if (eventApiInstance != null) throw new IllegalStateException("Api already initiated");

eventApiInstance = eventApi;
}

}
42 changes: 42 additions & 0 deletions api/src/main/java/com/craftmend/openaudiomc/api/EventApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.craftmend.openaudiomc.api;

import com.craftmend.openaudiomc.api.events.BaseEvent;
import com.craftmend.openaudiomc.api.events.SingleHandler;

public interface EventApi {

/**
* Get an instance of the event api. May be null if the plugin is not loaded yet
* @return instance
*/
static EventApi getInstance() {
return ApiHolder.eventApiInstance;
}

/**
* Register a listener for events annotated with @Handler
* @param listener the listener to register
*/
void registerHandlers(Object listener);

/**
* Unregister a listener for events annotated with @Handler
* @param listener the listener to unregister
*/
void unregisterHandlers(Object listener);

/**
* Call an event
* @param event the event to call
*/
BaseEvent callEvent(BaseEvent event);

/**
* Register a handler for a specific event
* @param event the event to listen for
* @param handler the handler to call
* @param <T> the event type
*/
<T extends BaseEvent> void registerHandler(Class<T> event, SingleHandler<T> handler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ public interface Client {
* @param media the media to play
*/
void playMedia(@NotNull Media media);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.craftmend.openaudiomc.api.events;

public abstract class BaseEvent {

/**
* Base event class, all events should extend this class
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.craftmend.openaudiomc.api.events;

public interface Cancellable {

/**
* Check if the event is cancelled
* @return true if the event is cancelled
*/
boolean isCancelled();

/**
* Set the event to cancelled
* @param cancelled true if the event should be cancelled
*/
void setCancelled(boolean cancelled);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.craftmend.openaudiomc.api.events;

import com.craftmend.openaudiomc.api.clients.Client;

public class CancellableClientEvent extends ClientEvent implements Cancellable {

private boolean cancelled = false;

/**
* Create a new client event
*
* @param client the client that this event is about
*/
public CancellableClientEvent(Client client) {
super(client);
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.craftmend.openaudiomc.api.events;

public class CancellableEvent extends BaseEvent implements Cancellable {

private boolean cancelled = false;

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.craftmend.openaudiomc.api.events;

import com.craftmend.openaudiomc.api.clients.Client;

public class ClientEvent extends BaseEvent {

private final Client client;

/**
* Create a new client event
* @param client the client that this event is about
*/
public ClientEvent(Client client) {
this.client = client;
}

/**
* Get the client that this event is about
* @return the client
*/
public Client getClient() {
return client;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.craftmend.openaudiomc.api.events;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Handler {

/**
* This is a marker annotation for event handlers.
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.craftmend.openaudiomc.api.events;

public interface SingleHandler<T> {

@Handler
void handle(T event);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.basic.Actor;
import com.craftmend.openaudiomc.api.events.CancellableEvent;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ClientAuthenticationEvent extends CancellableEvent {

private Actor actor;
private String token;

/**
* This event is called when a client is attempting to authenticate
*
* @param actor The actor that is trying to authenticate
* @param token The token that is being used
*/
public ClientAuthenticationEvent(Actor actor, String token) {
this.actor = actor;
this.token = token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.ClientEvent;

public class ClientConnectEvent extends ClientEvent {
/**
* Create a new client connect event, representing a client that has connected to the web client
*
* @param client the client that this event is about
*/
public ClientConnectEvent(Client client) {
super(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.ClientEvent;

public class ClientDisconnectEvent extends ClientEvent {
/**
* Create a new client event that represents a client that has disconnected from the web client
*
* @param client the client that this event is about
*/
public ClientDisconnectEvent(Client client) {
super(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.CancellableClientEvent;

public class ClientEnableVoiceEvent extends CancellableClientEvent {

public ClientEnableVoiceEvent(Client client) {
super(client);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.ClientEvent;
import com.craftmend.openaudiomc.api.voice.VoicePeerOptions;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ClientPeerAddedEvent extends ClientEvent {

private Client peer;
private VoicePeerOptions options;

/**
* Create a new client event
*
* @param client the client that this event is about
* @param peer the peer that was added
* @param options the options that were used to add the peer
*/
public ClientPeerAddedEvent(Client client, Client peer, VoicePeerOptions options) {
super(client);
this.peer = peer;
this.options = options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.ClientEvent;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ClientPeerRemovedEvent extends ClientEvent {

private Client peer;

/**
* Create a new client event
*
* @param client the client that this event is about
* @param peer the peer that was removed
*/
public ClientPeerRemovedEvent(Client client, Client peer) {
super(client);
this.peer = peer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.craftmend.openaudiomc.api.events.client;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.events.ClientEvent;
import com.craftmend.openaudiomc.api.media.MediaError;

public class MediaErrorEvent extends ClientEvent {

private String mediaSource;
private MediaError mediaError;

/**
* This event resembles an internal error in the web client (like bad or failed HTTP media requests).
*
* @param client the client that this event is about
*/
public MediaErrorEvent(Client client, String mediaSource, MediaError mediaError) {
super(client);
this.mediaSource = mediaSource;
this.mediaError = mediaError;
}

/**
* Get the media source that failed
*
* @return the media source
*/
public String getMediaSource() {
return mediaSource;
}

/**
* Get the media error
*
* @return the media error
*/
public MediaError getMediaError() {
return mediaError;
}
}
Loading
Loading