Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
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;
Expand Down Expand Up @@ -327,6 +329,28 @@ public static int getGenerationThreadsWithReflection(String className, String fi
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down