mirror of
https://github.com/BX-Team/DivineMC.git
synced 2025-12-25 17:59:36 +00:00
async mob spawning
This commit is contained in:
@@ -299,6 +299,7 @@ public class DivineConfig {
|
||||
public static boolean forceMinecraftCommand = false;
|
||||
public static boolean disableLeafDecay = false;
|
||||
public static boolean commandBlockParseResultsCaching = true;
|
||||
public static boolean enableAsyncSpawning = true;
|
||||
private static void miscSettings() {
|
||||
skipUselessSecondaryPoiSensor = getBoolean("settings.misc.skip-useless-secondary-poi-sensor", skipUselessSecondaryPoiSensor);
|
||||
clumpOrbs = getBoolean("settings.misc.clump-orbs", clumpOrbs,
|
||||
@@ -318,6 +319,8 @@ public class DivineConfig {
|
||||
"Disables leaf block decay.");
|
||||
commandBlockParseResultsCaching = getBoolean("settings.misc.command-block-parse-results-caching", commandBlockParseResultsCaching,
|
||||
"Caches the parse results of command blocks, can significantly reduce performance impact.");
|
||||
enableAsyncSpawning = getBoolean("settings.misc.enable-async-spawning", enableAsyncSpawning,
|
||||
"Enables optimization that will offload much of the computational effort involved with spawning new mobs to a different thread.");
|
||||
}
|
||||
|
||||
public static boolean disableDisconnectSpam = false;
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.bxteam.divinemc.util;
|
||||
|
||||
import ca.spottedleaf.moonrise.common.util.TickThread;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class AsyncProcessor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(AsyncProcessor.class);
|
||||
|
||||
private final BlockingQueue<Runnable> taskQueue;
|
||||
private final Thread workerThread;
|
||||
private volatile boolean isRunning;
|
||||
|
||||
public AsyncProcessor(String threadName) {
|
||||
this.taskQueue = new LinkedBlockingQueue<>();
|
||||
this.isRunning = true;
|
||||
|
||||
this.workerThread = new TickThread(() -> {
|
||||
while (isRunning || !taskQueue.isEmpty()) {
|
||||
try {
|
||||
Runnable task = taskQueue.take();
|
||||
task.run();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("An unexpected error occurred when running async processor: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}, threadName);
|
||||
|
||||
this.workerThread.start();
|
||||
}
|
||||
|
||||
public void submit(Runnable task) {
|
||||
if (!isRunning) {
|
||||
throw new IllegalStateException("AsyncExecutor is not running.");
|
||||
}
|
||||
|
||||
taskQueue.offer(task);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
isRunning = false;
|
||||
workerThread.interrupt();
|
||||
}
|
||||
|
||||
public void shutdownNow() {
|
||||
isRunning = false;
|
||||
workerThread.interrupt();
|
||||
taskQueue.clear();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user