Identify Moonrise worker threads correctly

This commit is contained in:
HaHaWTH 2025-06-07 00:17:51 +14:00
parent d90a4200fe
commit bf6612edd0
3 changed files with 26 additions and 2 deletions

View File

@ -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;
@ -328,6 +330,28 @@ public abstract class AbstractPlatform implements Platform {
}
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) {
loaders.register(registry);

View File

@ -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;
}

View File

@ -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;
}