From 6b5f080374b579d971b1cd52d120af6fffa24e75 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sun, 22 Jul 2018 00:45:49 -0400 Subject: [PATCH] Add mc util methods --- .../Basic-PlayerProfile-API.patch | 2 +- Spigot-Server-Patches/MC-Utils.patch | 73 ++++++++++++++++++- .../String-based-Action-Bar-API.patch | 10 +-- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/Spigot-Server-Patches/Basic-PlayerProfile-API.patch b/Spigot-Server-Patches/Basic-PlayerProfile-API.patch index efeff2f566..688de6b70d 100644 --- a/Spigot-Server-Patches/Basic-PlayerProfile-API.patch +++ b/Spigot-Server-Patches/Basic-PlayerProfile-API.patch @@ -404,7 +404,7 @@ index 000000000..3aceb0ea8 + } +} diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java -index 02940d697..4539b5601 100644 +index 70db1cc14..9ab3844fc 100644 --- a/src/main/java/net/minecraft/server/MCUtil.java +++ b/src/main/java/net/minecraft/server/MCUtil.java @@ -0,0 +0,0 @@ diff --git a/Spigot-Server-Patches/MC-Utils.patch b/Spigot-Server-Patches/MC-Utils.patch index dc0bb83ecf..8e7e57c1d1 100644 --- a/Spigot-Server-Patches/MC-Utils.patch +++ b/Spigot-Server-Patches/MC-Utils.patch @@ -18,7 +18,7 @@ index c3e990bdf..e2a7b4be2 100644 } } diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java -index 002da2a19..70a7edf57 100644 +index 121a137f3..35ec4981c 100644 --- a/src/main/java/net/minecraft/server/BlockPosition.java +++ b/src/main/java/net/minecraft/server/BlockPosition.java @@ -0,0 +0,0 @@ import org.apache.logging.log4j.Logger; @@ -181,7 +181,7 @@ index a540167d6..add618866 100644 } diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java new file mode 100644 -index 000000000..a4b0901cf +index 000000000..edaa7713d --- /dev/null +++ b/src/main/java/net/minecraft/server/MCUtil.java @@ -0,0 +0,0 @@ @@ -193,9 +193,13 @@ index 000000000..a4b0901cf +import org.spigotmc.AsyncCatcher; + +import javax.annotation.Nullable; ++import java.util.Queue; ++import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; ++import java.util.concurrent.TimeUnit; ++import java.util.concurrent.TimeoutException; +import java.util.function.Supplier; +import java.util.regex.Pattern; + @@ -205,6 +209,65 @@ index 000000000..a4b0901cf + private MCUtil() {} + + ++ public static boolean isMainThread() { ++ return MinecraftServer.getServer().isMainThread(); ++ } ++ ++ public static void processQueue() { ++ Runnable runnable; ++ Queue processQueue = getProcessQueue(); ++ while ((runnable = processQueue.poll()) != null) { ++ try { ++ runnable.run(); ++ } catch (Exception e) { ++ MinecraftServer.LOGGER.error("Error executing task", e); ++ } ++ } ++ } ++ public static T processQueueWhileWaiting(CompletableFuture future) { ++ try { ++ if (isMainThread()) { ++ while (!future.isDone()) { ++ try { ++ return future.get(1, TimeUnit.MILLISECONDS); ++ } catch (TimeoutException ignored) { ++ processQueue(); ++ } ++ } ++ } ++ return future.get(); ++ } catch (Exception e) { ++ throw new RuntimeException(e); ++ } ++ } ++ ++ public static void ensureMain(Runnable run) { ++ ensureMain(null, run); ++ } ++ /** ++ * Ensures the target code is running on the main thread ++ * @param reason ++ * @param run ++ * @return ++ */ ++ public static void ensureMain(String reason, Runnable run) { ++ if (AsyncCatcher.enabled && Thread.currentThread() != MinecraftServer.getServer().primaryThread) { ++ if (reason != null) { ++ new IllegalStateException("Asynchronous " + reason + "!").printStackTrace(); ++ } ++ getProcessQueue().add(run); ++ return; ++ } ++ run.run(); ++ } ++ ++ private static Queue getProcessQueue() { ++ return MinecraftServer.getServer().processQueue; ++ } ++ ++ public static T ensureMain(Supplier run) { ++ return ensureMain(null, run); ++ } + /** + * Ensures the target code is running on the main thread + * @param reason @@ -214,14 +277,16 @@ index 000000000..a4b0901cf + */ + public static T ensureMain(String reason, Supplier run) { + if (AsyncCatcher.enabled && Thread.currentThread() != MinecraftServer.getServer().primaryThread) { -+ new IllegalStateException( "Asynchronous " + reason + "! Blocking thread until it returns ").printStackTrace(); ++ if (reason != null) { ++ new IllegalStateException("Asynchronous " + reason + "! Blocking thread until it returns ").printStackTrace(); ++ } + Waitable wait = new Waitable() { + @Override + protected T evaluate() { + return run.get(); + } + }; -+ MinecraftServer.getServer().processQueue.add(wait); ++ getProcessQueue().add(wait); + try { + return wait.get(); + } catch (InterruptedException | ExecutionException e) { diff --git a/Spigot-Server-Patches/String-based-Action-Bar-API.patch b/Spigot-Server-Patches/String-based-Action-Bar-API.patch index 2de31c81a7..0fef359074 100644 --- a/Spigot-Server-Patches/String-based-Action-Bar-API.patch +++ b/Spigot-Server-Patches/String-based-Action-Bar-API.patch @@ -5,7 +5,7 @@ Subject: [PATCH] String based Action Bar API diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java -index a4b0901cf..02940d697 100644 +index edaa7713d..70db1cc14 100644 --- a/src/main/java/net/minecraft/server/MCUtil.java +++ b/src/main/java/net/minecraft/server/MCUtil.java @@ -0,0 +0,0 @@ @@ -20,8 +20,8 @@ index a4b0901cf..02940d697 100644 +import javax.annotation.Nonnull; import javax.annotation.Nullable; - import java.util.concurrent.ExecutionException; - import java.util.concurrent.Executor; + import java.util.Queue; + import java.util.concurrent.CompletableFuture; @@ -0,0 +0,0 @@ public final class MCUtil { private MCUtil() {} @@ -45,8 +45,8 @@ index a4b0901cf..02940d697 100644 + return ExceptionUtils.getFullStackTrace(new Throwable(str)); + } - /** - * Ensures the target code is running on the main thread + public static boolean isMainThread() { + return MinecraftServer.getServer().isMainThread(); @@ -0,0 +0,0 @@ public final class MCUtil { } return null;