From bf6612edd044ba8297e279e91248379e8618990a Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Sat, 7 Jun 2025 00:17:51 +1400 Subject: [PATCH] Identify Moonrise worker threads correctly --- .../com/dfsek/terra/AbstractPlatform.java | 24 +++++++++++++++++++ .../com/dfsek/terra/bukkit/PlatformImpl.java | 2 +- .../terra/lifecycle/LifecyclePlatform.java | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java b/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java index 6e4e8ad73..e9afb3bd3 100644 --- a/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java +++ b/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java @@ -31,6 +31,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UncheckedIOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -327,6 +329,28 @@ public abstract class AbstractPlatform implements Platform { return 0; } + + private static final String moonrise = "Moonrise"; + public static int getMoonriseGenerationThreadsWithReflection() { + try { + Class prioritisedThreadPoolClazz = Class.forName("ca.spottedleaf.concurrentutil.executor.thread.PrioritisedThreadPool"); + Method getCoreThreadsMethod = prioritisedThreadPoolClazz.getDeclaredMethod("getCoreThreads"); + getCoreThreadsMethod.setAccessible(true); + Class moonriseCommonClazz = Class.forName("ca.spottedleaf.moonrise.common.util.MoonriseCommon"); + Object pool = moonriseCommonClazz.getDeclaredField("WORKER_POOL").get(null); + int threads = ((Thread[]) getCoreThreadsMethod.invoke(pool)).length; + logger.info("{} found, setting {} generation threads.", moonrise, threads); + return threads; + } catch (ClassNotFoundException e) { + logger.info("{} not found.", moonrise); + } catch (NoSuchMethodException | NoSuchFieldException e) { + logger.warn("{} found, but field/method not found this probably means {0} has changed its code and " + + "Terra has not updated to reflect that.", moonrise); + } catch (IllegalAccessException | InvocationTargetException e) { + logger.error("Failed to access thread values in {}, assuming 1 generation thread.", moonrise, e); + } + return 0; + } @Override public void register(TypeRegistry registry) { diff --git a/platforms/bukkit/common/src/main/java/com/dfsek/terra/bukkit/PlatformImpl.java b/platforms/bukkit/common/src/main/java/com/dfsek/terra/bukkit/PlatformImpl.java index 68ba921ad..32a059721 100644 --- a/platforms/bukkit/common/src/main/java/com/dfsek/terra/bukkit/PlatformImpl.java +++ b/platforms/bukkit/common/src/main/java/com/dfsek/terra/bukkit/PlatformImpl.java @@ -57,7 +57,7 @@ public class PlatformImpl extends AbstractPlatform { private int generationThreads; public PlatformImpl(TerraBukkitPlugin plugin) { - generationThreads = getGenerationThreadsWithReflection("ca.spottedleaf.moonrise.common.util.MoonriseCommon", "WORKER_THREADS", "Moonrise"); + generationThreads = getMoonriseGenerationThreadsWithReflection(); if (generationThreads == 0) { generationThreads = 1; } diff --git a/platforms/mixin-lifecycle/src/main/java/com/dfsek/terra/lifecycle/LifecyclePlatform.java b/platforms/mixin-lifecycle/src/main/java/com/dfsek/terra/lifecycle/LifecyclePlatform.java index a7828ef11..0a658700f 100644 --- a/platforms/mixin-lifecycle/src/main/java/com/dfsek/terra/lifecycle/LifecyclePlatform.java +++ b/platforms/mixin-lifecycle/src/main/java/com/dfsek/terra/lifecycle/LifecyclePlatform.java @@ -45,7 +45,7 @@ public abstract class LifecyclePlatform extends ModPlatform { public LifecyclePlatform() { generationThreads = getGenerationThreadsWithReflection("com.ishland.c2me.base.common.GlobalExecutors", "GLOBAL_EXECUTOR_PARALLELISM", "C2ME"); if (generationThreads == 0) { - generationThreads = getGenerationThreadsWithReflection("ca.spottedleaf.moonrise.common.util.MoonriseCommon", "WORKER_THREADS", "Moonrise"); + generationThreads = getMoonriseGenerationThreadsWithReflection(); } if (generationThreads == 0) { generationThreads = 1; }