-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core,plugin): initial module framework
- Loading branch information
1 parent
be79093
commit 73438a6
Showing
16 changed files
with
325 additions
and
4 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
core/src/main/java/net/okocraft/yaminabe/core/module/YaminabeModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/net/okocraft/yaminabe/core/module/context/DisableContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
8 changes: 8 additions & 0 deletions
8
core/src/main/java/net/okocraft/yaminabe/core/module/context/EnableContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
8 changes: 8 additions & 0 deletions
8
core/src/main/java/net/okocraft/yaminabe/core/module/context/InitialContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
core/src/main/java/net/okocraft/yaminabe/core/modules/Modules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/net/okocraft/yaminabe/core/platform/listener/ListenerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/net/okocraft/yaminabe/core/platform/scheduler/CancellableTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
core/src/main/java/net/okocraft/yaminabe/core/platform/scheduler/Scheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/java/net/okocraft/yaminabe/core/platform/scheduler/SchedulerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
plugin/src/main/java/net/okocraft/yaminabe/platform/PapeEntityScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
plugin/src/main/java/net/okocraft/yaminabe/platform/PaperAsyncScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
plugin/src/main/java/net/okocraft/yaminabe/platform/PaperListenerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
plugin/src/main/java/net/okocraft/yaminabe/platform/PaperSchedulerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
plugin/src/main/java/net/okocraft/yaminabe/plugin/YaminabeContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
Oops, something went wrong.