From 7c678a4a4127106576c8dbe2d2bab2d565e94e70 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 16 Dec 2023 09:50:34 -0600 Subject: [PATCH] Add DhApi.isDhThread() --- .../com/seibel/distanthorizons/api/DhApi.java | 17 ++++++++++++++++- .../seibel/distanthorizons/coreapi/ModInfo.java | 2 ++ .../distanthorizons/core/util/ThreadUtil.java | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/com/seibel/distanthorizons/api/DhApi.java b/api/src/main/java/com/seibel/distanthorizons/api/DhApi.java index 3cabd3b6b..a069e5da3 100644 --- a/api/src/main/java/com/seibel/distanthorizons/api/DhApi.java +++ b/api/src/main/java/com/seibel/distanthorizons/api/DhApi.java @@ -52,7 +52,7 @@ public class DhApi { /** * If you can see this Java Doc, this can be ignored.
- * This is just to you know that Javadocs are available and that you should use the API jar + * This is just to let you know that Javadocs are available and that you should use the API jar * instead of the full mod jar.

* * Note: Don't use this string in your code. It may change and is only for reference. @@ -118,7 +118,11 @@ public class DhApi + //==================// // always available // + //==================// + + // interfaces // /** * Used to bind/unbind Distant Horizons Api events. @@ -139,6 +143,8 @@ public class DhApi public static final IOverrideInjector overrides = OverrideInjector.INSTANCE; + // getters // + /** * This version should only be updated when breaking changes are introduced to the Distant Horizons API. * @since API 1.0.0 @@ -174,4 +180,13 @@ public class DhApi */ public static int getNetworkProtocolVersion() { return ModInfo.PROTOCOL_VERSION; } + + // methods // + + /** + * Returns true if the thread this method was called from is owned by Distant Horizons. + * @since API 1.1.0 + */ + public static boolean isDhThread() { return Thread.currentThread().getName().startsWith(ModInfo.THREAD_NAME_PREFIX); } + } diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/ModInfo.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/ModInfo.java index 9283d700a..8e35bebcd 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/ModInfo.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/ModInfo.java @@ -48,5 +48,7 @@ public final class ModInfo public static final String NETWORKING_RESOURCE_NAMESPACE = "distant_horizons"; public static final String MULTIVERSE_PLUGIN_NAMESPACE = "world_control"; + /** All DH owned threads should start with this string to allow for easier debugging and profiling. */ + public static String THREAD_NAME_PREFIX = "DH-"; } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java b/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java index 63a102c64..eacd25811 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java @@ -24,6 +24,7 @@ import com.seibel.distanthorizons.core.config.types.ConfigEntry; import com.seibel.distanthorizons.core.util.threading.DhThreadFactory; import com.seibel.distanthorizons.core.util.threading.RateLimitedThreadPoolExecutor; import com.seibel.distanthorizons.core.util.threading.ThreadPools; +import com.seibel.distanthorizons.coreapi.ModInfo; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -38,8 +39,7 @@ public class ThreadUtil { private static final Logger LOGGER = LogManager.getLogger(); - /** The prefix isn't strictly required, but makes debugging and profiling much easier. */ - public static String THREAD_NAME_PREFIX = "DH-"; + public static String THREAD_NAME_PREFIX = ModInfo.THREAD_NAME_PREFIX; public static int MINIMUM_RELATIVE_PRIORITY = -4; public static int DEFAULT_RELATIVE_PRIORITY = 0;