From 271328ef6fd4031b3b951f27fe8eee207feddfbc Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Fri, 5 Apr 2024 16:18:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20CompletableTask=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +- build.gradle.kts | 2 +- .../common/util/async/CompletableTask.java | 164 ++++++++++++++++++ .../core/common/util/async/RunTask.java | 5 + .../core/common/util/async/SupplyTask.java | 5 + 5 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/CompletableTask.java create mode 100644 core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/RunTask.java create mode 100644 core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/SupplyTask.java diff --git a/README.md b/README.md index f1cb78a..716d0a1 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ repositories { dependencies { // 对于 Bukkit 插件 - compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.3") + compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.4-SNAPSHOT") // 对于 BungeeCord 插件 - compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.3") + compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.4-SNAPSHOT") } ``` @@ -54,13 +54,13 @@ dependencies { cn.hamster3.mc.plugin core-bukkit - 1.3.3 + 1.3.4-SNAPSHOT cn.hamster3.mc.plugin core-bungee - 1.3.3 + 1.3.4-SNAPSHOT diff --git a/build.gradle.kts b/build.gradle.kts index e4a617a..cd64bc6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "cn.hamster3.mc.plugin" -version = "1.3.3" +version = "1.3.4-SNAPSHOT" description = "叁只仓鼠的 Minecraft 插件开发通用工具包" subprojects { diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/CompletableTask.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/CompletableTask.java new file mode 100644 index 0000000..0d61f46 --- /dev/null +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/CompletableTask.java @@ -0,0 +1,164 @@ +package cn.hamster3.mc.plugin.core.common.util.async; + +import cn.hamster3.mc.plugin.core.common.api.CoreAPI; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +@SuppressWarnings({"unused", "CallToPrintStackTrace"}) +public class CompletableTask { + @NotNull + private final List> onSuccess; + @NotNull + private final List> onFailed; + @NotNull + private State state; + private T value; + private Throwable throwable; + + public CompletableTask() { + state = State.WAITING; + onSuccess = new ArrayList<>(); + onFailed = new ArrayList<>(); + } + + @NotNull + public static CompletableTask runAsync(@NotNull RunTask runTask) { + CompletableTask task = new CompletableTask<>(); + CoreAPI.getInstance().getExecutorService().submit(() -> { + try { + runTask.run(); + task.success(null); + } catch (Exception e) { + task.failed(e); + } + }); + return task; + } + + @NotNull + public static CompletableTask supplyAsync(@NotNull SupplyTask supplyTask) { + CompletableTask task = new CompletableTask<>(); + CoreAPI.getInstance().getExecutorService().submit(() -> { + try { + T call = supplyTask.call(); + task.success(call); + } catch (Exception e) { + task.failed(e); + } + }); + return task; + } + + public void success(@Nullable T value) { + if (state != State.WAITING) { + throw new IllegalStateException(); + } + this.value = value; + state = State.SUCCESS; + synchronized (this) { + notifyAll(); + } + for (Consumer success : onSuccess) { + try { + success.accept(value); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void failed(@Nullable Throwable throwable) { + if (state != State.WAITING) { + throw new IllegalStateException(); + } + this.value = null; + state = State.FAILED; + this.throwable = throwable; + synchronized (this) { + notifyAll(); + } + for (Consumer consumer : onFailed) { + try { + consumer.accept(throwable); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + @NotNull + public CompletableTask onSuccess(@NotNull Consumer consumer) { + if (state == State.SUCCESS) { + try { + consumer.accept(value); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + onSuccess.add(consumer); + } + return this; + } + + @NotNull + public CompletableTask onFailed(@NotNull Consumer consumer) { + if (state == State.FAILED) { + try { + consumer.accept(throwable); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + onFailed.add(consumer); + } + return this; + } + + public T get() throws Throwable { + if (state == State.WAITING) { + synchronized (this) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + switch (state) { + case SUCCESS: + return value; + case FAILED: + throw throwable; + } + return null; + } + + public T getOrDefault(T defaultValue) { + if (state == State.WAITING) { + synchronized (this) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + switch (state) { + case SUCCESS: + return value; + case FAILED: + return defaultValue; + } + return defaultValue; + } + + public enum State { + WAITING, + SUCCESS, + FAILED + } +} diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/RunTask.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/RunTask.java new file mode 100644 index 0000000..59fd43c --- /dev/null +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/RunTask.java @@ -0,0 +1,5 @@ +package cn.hamster3.mc.plugin.core.common.util.async; + +public interface RunTask { + void run() throws Exception; +} diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/SupplyTask.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/SupplyTask.java new file mode 100644 index 0000000..e8cba53 --- /dev/null +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/async/SupplyTask.java @@ -0,0 +1,5 @@ +package cn.hamster3.mc.plugin.core.common.util.async; + +public interface SupplyTask { + T call() throws Exception; +}