Skip to content

Commit

Permalink
feat(core,plugin): initial module framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Jun 25, 2024
1 parent be79093 commit 73438a6
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.okocraft.yaminabe.core.module;

import net.kyori.adventure.key.Key;
import net.okocraft.yaminabe.core.module.context.DisableContext;
import net.okocraft.yaminabe.core.module.context.EnableContext;
import net.okocraft.yaminabe.core.module.context.InitialContext;
import org.jetbrains.annotations.NotNull;

public interface YaminabeModule {

void enable(@NotNull EnableContext context);

void disable(@NotNull DisableContext context);

interface Factory {
@NotNull
YaminabeModule init(@NotNull InitialContext context);
}

record Holder(@NotNull Key key, @NotNull YaminabeModule module) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.okocraft.yaminabe.core.module.context;

import net.okocraft.yaminabe.core.platform.listener.ListenerRegistrar;
import org.jetbrains.annotations.NotNull;

public record DisableContext(@NotNull ListenerRegistrar listenerRegistrar) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.okocraft.yaminabe.core.module.context;

import net.okocraft.yaminabe.core.platform.listener.ListenerRegistrar;
import net.okocraft.yaminabe.core.platform.scheduler.SchedulerProvider;
import org.jetbrains.annotations.NotNull;

public record EnableContext(@NotNull ListenerRegistrar listenerRegistrar, @NotNull SchedulerProvider schedulers) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.okocraft.yaminabe.core.module.context;

import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

public record InitialContext(@NotNull Path dataDirectory) {
}
19 changes: 19 additions & 0 deletions core/src/main/java/net/okocraft/yaminabe/core/modules/Modules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.okocraft.yaminabe.core.modules;

import net.kyori.adventure.key.Key;
import net.okocraft.yaminabe.core.module.YaminabeModule;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public final class Modules {

public static final Map<Key, YaminabeModule.Factory> FACTORIES;

static {
var factories = new LinkedHashMap<Key, YaminabeModule.Factory>();

FACTORIES = Collections.unmodifiableMap(factories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.okocraft.yaminabe.core.platform.listener;

import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;

public interface ListenerRegistrar {

void register(@NotNull Listener listener);

void unregister(@NotNull Listener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.okocraft.yaminabe.core.platform.scheduler;

public interface CancellableTask {

void cancel();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.okocraft.yaminabe.core.platform.scheduler;

import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.function.Consumer;

public interface Scheduler {

void runNow(@NotNull Runnable task);

void runDelayed(@NotNull Runnable task, @NotNull Duration delay);

void runAtFixedRate(@NotNull Consumer<CancellableTask> task, @NotNull Duration interval);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.okocraft.yaminabe.core.platform.scheduler;

import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;

public interface SchedulerProvider {

@NotNull
Scheduler async();

@NotNull
Scheduler entity(@NotNull Entity entity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import net.okocraft.yaminabe.core.modules.Modules;
import net.okocraft.yaminabe.core.util.YaminabeLogger;
import net.okocraft.yaminabe.plugin.YaminabeContext;
import net.okocraft.yaminabe.plugin.YaminabePlugin;
Expand All @@ -21,7 +22,8 @@ public void bootstrap(@NotNull BootstrapContext context) {
@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) {
return new YaminabePlugin(new YaminabeContext(
context.getDataDirectory()
context.getDataDirectory(),
Modules.FACTORIES // TODO: collecting modules should be done by annotation processor
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.okocraft.yaminabe.platform;

import io.papermc.paper.util.Tick;
import net.okocraft.yaminabe.core.platform.scheduler.CancellableTask;
import net.okocraft.yaminabe.core.platform.scheduler.Scheduler;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.function.Consumer;

class PapeEntityScheduler implements Scheduler {

private final Plugin plugin;
private final Entity entity;

PapeEntityScheduler(@NotNull Plugin plugin, @NotNull Entity entity) {
this.plugin = plugin;
this.entity = entity;
}

@Override
public void runNow(@NotNull Runnable task) {
this.entity.getScheduler().run(this.plugin, ignored -> task.run(), null);
}

@Override
public void runDelayed(@NotNull Runnable task, @NotNull Duration delay) {
if (delay.isNegative()) {
throw new IllegalArgumentException("delay cannot be negative");
} else if (delay.isZero()) {
this.runNow(task);
} else {
this.entity.getScheduler().runDelayed(
this.plugin,
ignored -> task.run(),
null,
Tick.tick().fromDuration(delay)
);
}
}

@Override
public void runAtFixedRate(@NotNull Consumer<CancellableTask> task, @NotNull Duration interval) {
if (interval.isNegative() || interval.isZero()) {
throw new IllegalArgumentException("interval cannot be negative or zero");
}
int ticks = Tick.tick().fromDuration(interval);
this.entity.getScheduler().runAtFixedRate(
this.plugin,
scheduledTask -> task.accept(scheduledTask::cancel),
null,
ticks,
ticks
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.okocraft.yaminabe.platform;

import net.okocraft.yaminabe.core.platform.scheduler.CancellableTask;
import net.okocraft.yaminabe.core.platform.scheduler.Scheduler;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

class PaperAsyncScheduler implements Scheduler {

private final Plugin plugin;

PaperAsyncScheduler(@NotNull Plugin plugin) {
this.plugin = plugin;
}

@Override
public void runNow(@NotNull Runnable task) {
this.plugin.getServer().getAsyncScheduler().runNow(this.plugin, ignored -> task.run());
}

@Override
public void runDelayed(@NotNull Runnable task, @NotNull Duration delay) {
if (delay.isNegative()) {
throw new IllegalArgumentException("delay cannot be negative");
} else if (delay.isZero()) {
this.runNow(task);
} else {
this.plugin.getServer().getAsyncScheduler().runDelayed(
this.plugin,
ignored -> task.run(),
delay.toMillis(),
TimeUnit.MILLISECONDS
);
}
}

@Override
public void runAtFixedRate(@NotNull Consumer<CancellableTask> task, @NotNull Duration interval) {
if (interval.isNegative() || interval.isZero()) {
throw new IllegalArgumentException("interval cannot be negative or zero");
}
this.plugin.getServer().getAsyncScheduler().runAtFixedRate(
this.plugin,
scheduledTask -> task.accept(scheduledTask::cancel),
interval.toMillis(),
interval.toMillis(),
TimeUnit.MILLISECONDS
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.okocraft.yaminabe.platform;

import net.okocraft.yaminabe.core.platform.listener.ListenerRegistrar;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public class PaperListenerRegistrar implements ListenerRegistrar {

private final Plugin plugin;

public PaperListenerRegistrar(@NotNull Plugin plugin) {
this.plugin = plugin;
}

@Override
public void register(@NotNull Listener listener) {
this.plugin.getServer().getPluginManager().registerEvents(listener, this.plugin);
}

@Override
public void unregister(@NotNull Listener listener) {
HandlerList.unregisterAll(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.okocraft.yaminabe.platform;

import net.okocraft.yaminabe.core.platform.scheduler.Scheduler;
import net.okocraft.yaminabe.core.platform.scheduler.SchedulerProvider;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public class PaperSchedulerProvider implements SchedulerProvider {

private final Plugin plugin;
private final Scheduler async;

public PaperSchedulerProvider(@NotNull Plugin plugin) {
this.plugin = plugin;
this.async = new PaperAsyncScheduler(plugin);
}

@Override
public @NotNull Scheduler async() {
return this.async;
}

@Override
public @NotNull Scheduler entity(@NotNull Entity entity) {
return new PapeEntityScheduler(this.plugin, entity);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.okocraft.yaminabe.plugin;

import net.kyori.adventure.key.Key;
import net.okocraft.yaminabe.core.module.YaminabeModule;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.util.Map;

public record YaminabeContext(@NotNull Path dataDirectory) {
public record YaminabeContext(@NotNull Path dataDirectory,
@NotNull Map<Key, YaminabeModule.Factory> moduleFactories) {
}
Loading

0 comments on commit 73438a6

Please sign in to comment.