Skip to content

Commit

Permalink
clean(*): expose event4j and remove wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Jun 19, 2024
1 parent 8b75d74 commit a8bef6b
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 291 deletions.
4 changes: 4 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id ("chronus.common-conventions")
}

dependencies {
api(libs.event4j)
}
11 changes: 7 additions & 4 deletions api/src/main/java/net/okocraft/chronus/api/Chronus.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.okocraft.chronus.api;

import net.okocraft.chronus.api.eventbus.EventBus;
import com.github.siroshun09.event4j.caller.AsyncEventCaller;
import net.okocraft.chronus.api.event.ChronusEvent;
import org.jetbrains.annotations.NotNull;

/**
Expand Down Expand Up @@ -28,9 +29,11 @@ static boolean isInitialized() {
}

/**
* Gets the {@link EventBus}.
* Gets an {@link AsyncEventCaller} to call {@link ChronusEvent}s.
*
* @return the {@link EventBus}
* @return an {@link AsyncEventCaller}
*/
@NotNull EventBus getEventBus();
@NotNull AsyncEventCaller<ChronusEvent> eventCaller();

// TODO: add an interface for subscribing event listeners
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.okocraft.chronus.api.event.listener;

import com.github.siroshun09.event4j.listener.SubscribedListener;
import com.github.siroshun09.event4j.priority.Priority;
import net.okocraft.chronus.api.event.ChronusEvent;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public interface ListenerManager {

<E extends ChronusEvent> @NotNull SubscribedListener<?, E, Priority> subscribe(@NotNull Consumer<? super E> consumer);

<E extends ChronusEvent> @NotNull SubscribedListener<?, E, Priority> subscribe(@NotNull Consumer<? super E> consumer, @NotNull Priority priority);

void unsubscribe(@NotNull SubscribedListener<?, ? extends ChronusEvent, Priority> listener);

void unsubscribeAll();

}
39 changes: 0 additions & 39 deletions api/src/main/java/net/okocraft/chronus/api/eventbus/EventBus.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ plugins {

dependencies {
implementation(projects.chronusApi)
implementation(libs.event4j)
}
23 changes: 17 additions & 6 deletions core/src/main/java/net/okocraft/chronus/core/ChronusCore.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.okocraft.chronus.core;

import com.github.siroshun09.event4j.priority.Priority;
import com.github.siroshun09.event4j.simple.EventServiceProvider;
import net.okocraft.chronus.api.APISetter;
import net.okocraft.chronus.api.event.ChronusEvent;
import net.okocraft.chronus.core.api.ChronusAPI;
import net.okocraft.chronus.core.api.eventbus.EventBusWrapper;
import net.okocraft.chronus.core.logger.ChronusLogger;
import net.okocraft.chronus.core.platform.Platform;
import net.okocraft.chronus.core.platform.scheduler.Scheduler;
import org.jetbrains.annotations.NotNull;
import org.slf4j.helpers.SubstituteLogger;

Expand All @@ -19,10 +22,11 @@ public static ChronusCore initialize(@NotNull Platform platform) {
}

private final Platform platform;
private ChronusAPI api;
private final EventServiceProvider<Void, ChronusEvent, Priority> eventServiceProvider;

private ChronusCore(@NotNull Platform platform) {
this.platform = platform;
this.eventServiceProvider = createEventServiceProvider(platform.scheduler());
}

/**
Expand All @@ -39,17 +43,24 @@ public void startup() {
platform.getLogger().info("Chronus - " + getClass().getPackage().getImplementationVersion());
platform.getLogger().info("Running on " + platform.getName() + " " + platform.getVersion());

api = new ChronusAPI(EventBusWrapper.create());

APISetter.set(api);
APISetter.set(new ChronusAPI(
this.eventServiceProvider.asyncCaller()
));
}

/**
* Performs shutdown.
*/
public void shutdown() {
APISetter.unset();
}

api.getEventBus().close();
private static @NotNull EventServiceProvider<Void, ChronusEvent, Priority> createEventServiceProvider(@NotNull Scheduler scheduler) {
return EventServiceProvider.factory()
.keyClass(Void.class) // Change later
.eventClass(ChronusEvent.class)
.orderComparator(Priority.COMPARATOR, Priority.NORMAL)
.executor(scheduler::runAsync)
.create();
}
}
18 changes: 5 additions & 13 deletions core/src/main/java/net/okocraft/chronus/core/api/ChronusAPI.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package net.okocraft.chronus.core.api;

import com.github.siroshun09.event4j.caller.AsyncEventCaller;
import net.okocraft.chronus.api.Chronus;
import net.okocraft.chronus.core.api.eventbus.EventBusWrapper;
import net.okocraft.chronus.api.event.ChronusEvent;
import org.jetbrains.annotations.NotNull;

/**
* A class for implementing {@link Chronus}.
*/
public class ChronusAPI implements Chronus {

private final EventBusWrapper eventBusWrapper;

public ChronusAPI(@NotNull EventBusWrapper eventBusWrapper) {
this.eventBusWrapper = eventBusWrapper;
}

@Override
public @NotNull EventBusWrapper getEventBus() {
return eventBusWrapper;
}
public record ChronusAPI(
@NotNull AsyncEventCaller<ChronusEvent> eventCaller
) implements Chronus {
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit a8bef6b

Please sign in to comment.