From 07a31463e441546754a36763b33d74e9b14d1ba0 Mon Sep 17 00:00:00 2001 From: ByteException_ Date: Sat, 11 Sep 2021 17:31:33 +0200 Subject: [PATCH] Add nukkit support Signed-off-by: ByteException_ --- nukkit/pom.xml | 68 +++++++++++ .../taskchain/NukkitTaskChainFactory.java | 112 ++++++++++++++++++ pom.xml | 1 + sponge/pom.xml | 2 +- 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 nukkit/pom.xml create mode 100644 nukkit/src/main/java/co/aikar/taskchain/NukkitTaskChainFactory.java diff --git a/nukkit/pom.xml b/nukkit/pom.xml new file mode 100644 index 0000000..cd01448 --- /dev/null +++ b/nukkit/pom.xml @@ -0,0 +1,68 @@ + + + + + 4.0.0 + + co.aikar + taskchain-parent + 3.7.3-SNAPSHOT + ../pom.xml + + + taskchain-nukkit + 3.7.3-SNAPSHOT + jar + TaskChain (Nukkit) + + + + opencollab-repo-snapshot + https://repo.opencollab.dev/maven-snapshots/ + + false + + + true + + + + + + + co.aikar + taskchain-core + 3.7.3-SNAPSHOT + + + cn.nukkit + nukkit + 1.0-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/nukkit/src/main/java/co/aikar/taskchain/NukkitTaskChainFactory.java b/nukkit/src/main/java/co/aikar/taskchain/NukkitTaskChainFactory.java new file mode 100644 index 0000000..ec9c1f3 --- /dev/null +++ b/nukkit/src/main/java/co/aikar/taskchain/NukkitTaskChainFactory.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016-2021 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.taskchain; + +import cn.nukkit.Player; +import cn.nukkit.event.EventHandler; +import cn.nukkit.event.Listener; +import cn.nukkit.event.plugin.PluginDisableEvent; +import cn.nukkit.plugin.Plugin; + +import java.util.concurrent.TimeUnit; + +public class NukkitTaskChainFactory extends TaskChainFactory { + + private NukkitTaskChainFactory(Plugin plugin, AsyncQueue asyncQueue) { + super(new NukkitGameInterface(plugin, asyncQueue)); + } + + public static TaskChainFactory create(Plugin plugin) { + return new NukkitTaskChainFactory(plugin, new TaskChainAsyncQueue()); + } + + private static class NukkitGameInterface implements GameInterface { + + private final Plugin plugin; + + private final AsyncQueue asyncQueue; + + private NukkitGameInterface(Plugin plugin, AsyncQueue asyncQueue) { + this.plugin = plugin; + this.asyncQueue = asyncQueue; + } + + @Override + public boolean isMainThread() { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public AsyncQueue getAsyncQueue() { + return this.asyncQueue; + } + + @Override + public void postToMain(Runnable run) { + if (this.plugin.isEnabled()) { + this.plugin.getServer().getScheduler().scheduleTask(this.plugin, run); + return; + } + + run.run(); + } + + @Override + public void scheduleTask(int gameUnits, Runnable run) { + if (this.plugin.isEnabled()) { + this.plugin.getServer().getScheduler().scheduleDelayedTask(this.plugin, run, gameUnits); + return; + } + + run.run(); + } + + @Override + public void registerShutdownHandler(TaskChainFactory factory) { + this.plugin.getServer().getPluginManager().registerEvents(new Listener() { + @EventHandler + public void onPluginDisable(PluginDisableEvent event) { + if (event.getPlugin().equals(NukkitGameInterface.this.plugin)) + factory.shutdown(60, TimeUnit.SECONDS); + } + }, this.plugin); + } + + public static final TaskChainAbortAction MESSAGE = new TaskChainAbortAction() { + @Override + public void onAbort(TaskChain chain, Player player, String message) { + player.sendMessage(message); + } + }; + + public static final TaskChainAbortAction COLOR_MESSAGE = new TaskChainAbortAction() { + @Override + public void onAbort(TaskChain chain, Player player, String message) { + player.sendMessage(message.replaceAll("&", "ยง")); + } + }; + + } + +} diff --git a/pom.xml b/pom.xml index 875fa83..5ef009b 100644 --- a/pom.xml +++ b/pom.xml @@ -91,5 +91,6 @@ core bukkit sponge + nukkit diff --git a/sponge/pom.xml b/sponge/pom.xml index 8f5539f..6df034d 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -40,7 +40,7 @@ sponge-maven-repo Sponge maven repo - http://repo.spongepowered.org/maven + https://repo.spongepowered.org/maven true