Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.9 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.workthreads;
import engine.Enum;
import engine.gameManager.SessionManager;
import engine.gameManager.SimulationManager;
import engine.objects.Bane;
import engine.objects.PlayerCharacter;
import engine.objects.PlayerCombatStats;
import org.pmw.tinylog.Logger;
import java.util.concurrent.*;
public class UpdateThread implements Runnable {
3 months ago
private volatile Long lastRun;
public static final Long instancedelay = 1000L;
public UpdateThread() {
Logger.info(" UpdateThread thread has started!");
}
private static final long INSTANCE_DELAY = 1500; // Adjust as needed
public void processPlayerUpdate() {
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
try {
for (PlayerCharacter player : SessionManager.getAllActivePlayerCharacters()) {
3 months ago
if (player != null) {
Future<?> future = executor.submit(() -> {
try {
player.update(true);
} catch (Exception e) {
Logger.error(e);
}
});
try {
future.get(10, TimeUnit.SECONDS); // Enforce max execution time
} catch (TimeoutException e) {
Logger.error("Player update timed out for: " + player.getName());
future.cancel(true); // Attempt to cancel the task
} catch (Exception e) {
Logger.error(e);
}
3 months ago
}
}
} catch (Exception e) {
Logger.error("UPDATE ERROR", e);
} finally {
executor.shutdown();
}
}
@Override
public void run() {
try {
processPlayerUpdate();
} catch (Exception e) {
Logger.error("Thread Execution Error", e);
}
}
public static void startUpdateThread() {
Thread updateThread;
updateThread = new Thread(new UpdateThread());
updateThread.setName("updateThread");
updateThread.start();
}
}