Add mc util methods

This commit is contained in:
Aikar 2018-07-22 00:45:49 -04:00
parent bf2c56e8ae
commit 6219932b89
No known key found for this signature in database
GPG Key ID: 401ADFC9891FAAFE
3 changed files with 82 additions and 17 deletions

View File

@ -1,4 +1,4 @@
From 68857c2aac1bd54c01818909cbcd0ca010e2affa Mon Sep 17 00:00:00 2001
From 34e03f427dce5395f45f2fbfa8400f4c40b73194 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 28 Mar 2016 20:55:47 -0400
Subject: [PATCH] MC Utils
@ -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
@@ -10,7 +10,7 @@ import org.apache.logging.log4j.Logger;
@ -181,10 +181,10 @@ 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 +1,201 @@
@@ -0,0 +1,266 @@
+package net.minecraft.server;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -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<Runnable> processQueue = getProcessQueue();
+ while ((runnable = processQueue.poll()) != null) {
+ try {
+ runnable.run();
+ } catch (Exception e) {
+ MinecraftServer.LOGGER.error("Error executing task", e);
+ }
+ }
+ }
+ public static <T> T processQueueWhileWaiting(CompletableFuture <T> 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<Runnable> getProcessQueue() {
+ return MinecraftServer.getServer().processQueue;
+ }
+
+ public static <T> T ensureMain(Supplier<T> 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> T ensureMain(String reason, Supplier<T> 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<T> wait = new Waitable<T>() {
+ @Override
+ protected T evaluate() {
+ return run.get();
+ }
+ };
+ MinecraftServer.getServer().processQueue.add(wait);
+ getProcessQueue().add(wait);
+ try {
+ return wait.get();
+ } catch (InterruptedException | ExecutionException e) {

View File

@ -1,11 +1,11 @@
From 470c28710bb1546fec26de9724cad439462497a7 Mon Sep 17 00:00:00 2001
From fb8e8823dca02ed4ef2a87cd6c993de3d69a2b91 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 27 Dec 2016 15:02:42 -0500
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
@@ -1,10 +1,13 @@
@ -20,9 +20,9 @@ index a4b0901cf..02940d697 100644
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
@@ -17,6 +20,24 @@ public final class MCUtil {
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
@@ -21,6 +24,24 @@ public final class MCUtil {
private MCUtil() {}
@ -45,9 +45,9 @@ index a4b0901cf..02940d697 100644
+ return ExceptionUtils.getFullStackTrace(new Throwable(str));
+ }
/**
* Ensures the target code is running on the main thread
@@ -198,4 +219,13 @@ public final class MCUtil {
public static boolean isMainThread() {
return MinecraftServer.getServer().isMainThread();
@@ -263,4 +284,13 @@ public final class MCUtil {
}
return null;
}

View File

@ -1,4 +1,4 @@
From eb7e8603e48e1cfac564dfd91c528198ac7e7a8d Mon Sep 17 00:00:00 2001
From 6a116ada95d75bf31b18a32818efe792c9b39d49 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 15 Jan 2018 22:11:48 -0500
Subject: [PATCH] Basic PlayerProfile API
@ -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
@@ -1,6 +1,9 @@
@ -417,7 +417,7 @@ index 02940d697..4539b5601 100644
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftWorld;
@@ -66,6 +69,10 @@ public final class MCUtil {
@@ -131,6 +134,10 @@ public final class MCUtil {
return run.get();
}