mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-01 17:01:56 +01:00
Add mc util methods
This commit is contained in:
parent
1db0b7ed58
commit
6b5f080374
3 changed files with 75 additions and 10 deletions
|
@ -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
|
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
|
--- a/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
@@ -0,0 +0,0 @@
|
@@ -0,0 +0,0 @@
|
||||||
|
|
|
@ -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
|
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
|
--- a/src/main/java/net/minecraft/server/BlockPosition.java
|
||||||
+++ b/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;
|
@@ -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
|
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 000000000..a4b0901cf
|
index 000000000..edaa7713d
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
@@ -0,0 +0,0 @@
|
@@ -0,0 +0,0 @@
|
||||||
|
@ -193,9 +193,13 @@ index 000000000..a4b0901cf
|
||||||
+import org.spigotmc.AsyncCatcher;
|
+import org.spigotmc.AsyncCatcher;
|
||||||
+
|
+
|
||||||
+import javax.annotation.Nullable;
|
+import javax.annotation.Nullable;
|
||||||
|
+import java.util.Queue;
|
||||||
|
+import java.util.concurrent.CompletableFuture;
|
||||||
+import java.util.concurrent.ExecutionException;
|
+import java.util.concurrent.ExecutionException;
|
||||||
+import java.util.concurrent.Executor;
|
+import java.util.concurrent.Executor;
|
||||||
+import java.util.concurrent.Executors;
|
+import java.util.concurrent.Executors;
|
||||||
|
+import java.util.concurrent.TimeUnit;
|
||||||
|
+import java.util.concurrent.TimeoutException;
|
||||||
+import java.util.function.Supplier;
|
+import java.util.function.Supplier;
|
||||||
+import java.util.regex.Pattern;
|
+import java.util.regex.Pattern;
|
||||||
+
|
+
|
||||||
|
@ -205,6 +209,65 @@ index 000000000..a4b0901cf
|
||||||
+ private MCUtil() {}
|
+ 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
|
+ * Ensures the target code is running on the main thread
|
||||||
+ * @param reason
|
+ * @param reason
|
||||||
|
@ -214,14 +277,16 @@ index 000000000..a4b0901cf
|
||||||
+ */
|
+ */
|
||||||
+ public static <T> T ensureMain(String reason, Supplier<T> run) {
|
+ public static <T> T ensureMain(String reason, Supplier<T> run) {
|
||||||
+ if (AsyncCatcher.enabled && Thread.currentThread() != MinecraftServer.getServer().primaryThread) {
|
+ 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>() {
|
+ Waitable<T> wait = new Waitable<T>() {
|
||||||
+ @Override
|
+ @Override
|
||||||
+ protected T evaluate() {
|
+ protected T evaluate() {
|
||||||
+ return run.get();
|
+ return run.get();
|
||||||
+ }
|
+ }
|
||||||
+ };
|
+ };
|
||||||
+ MinecraftServer.getServer().processQueue.add(wait);
|
+ getProcessQueue().add(wait);
|
||||||
+ try {
|
+ try {
|
||||||
+ return wait.get();
|
+ return wait.get();
|
||||||
+ } catch (InterruptedException | ExecutionException e) {
|
+ } catch (InterruptedException | ExecutionException e) {
|
||||||
|
|
|
@ -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
|
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
|
--- a/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
||||||
@@ -0,0 +0,0 @@
|
@@ -0,0 +0,0 @@
|
||||||
|
@ -20,8 +20,8 @@ index a4b0901cf..02940d697 100644
|
||||||
|
|
||||||
+import javax.annotation.Nonnull;
|
+import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -0,0 +0,0 @@ public final class MCUtil {
|
@@ -0,0 +0,0 @@ public final class MCUtil {
|
||||||
|
|
||||||
private MCUtil() {}
|
private MCUtil() {}
|
||||||
|
@ -45,8 +45,8 @@ index a4b0901cf..02940d697 100644
|
||||||
+ return ExceptionUtils.getFullStackTrace(new Throwable(str));
|
+ return ExceptionUtils.getFullStackTrace(new Throwable(str));
|
||||||
+ }
|
+ }
|
||||||
|
|
||||||
/**
|
public static boolean isMainThread() {
|
||||||
* Ensures the target code is running on the main thread
|
return MinecraftServer.getServer().isMainThread();
|
||||||
@@ -0,0 +0,0 @@ public final class MCUtil {
|
@@ -0,0 +0,0 @@ public final class MCUtil {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue