Browse Source

respawn thread optimized

lakebane
FatBoy-DOTC 13 hours ago
parent
commit
86435a3563
  1. 50
      src/engine/mobileAI/Threads/MobRespawnThread.java

50
src/engine/mobileAI/Threads/MobRespawnThread.java

@ -13,6 +13,9 @@ import engine.objects.Mob;
import engine.objects.Zone; import engine.objects.Zone;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.Collection;
/** /**
* Thread blocks until MagicBane dispatch messages are * Thread blocks until MagicBane dispatch messages are
* enqueued then processes them in FIFO order. The collection * enqueued then processes them in FIFO order. The collection
@ -25,41 +28,48 @@ import org.pmw.tinylog.Logger;
public class MobRespawnThread implements Runnable { public class MobRespawnThread implements Runnable {
private volatile boolean running = true;
private static final long RESPAWN_INTERVAL = 100; // Configurable interval
public MobRespawnThread() { public MobRespawnThread() {
Logger.info(" MobRespawnThread thread has started!"); Logger.info("MobRespawnThread initialized.");
} }
@Override @Override
public void run() { public void run() {
while (running) {
while (true) {
try { try {
for (Zone zone : ZoneManager.getAllZones()) { Collection<Zone> zones = ZoneManager.getAllZones();
if (zones != null) {
if (zone.respawnQue.isEmpty() == false && zone.lastRespawn + 100 < System.currentTimeMillis()) { for (Zone zone : zones) {
synchronized (zone) { // Optional: Synchronize on zone
Mob respawner = zone.respawnQue.iterator().next(); if (!zone.respawnQue.isEmpty() &&
zone.lastRespawn + RESPAWN_INTERVAL < System.currentTimeMillis()) {
if (respawner == null) Mob respawner = zone.respawnQue.iterator().next();
continue; if (respawner != null) {
respawner.respawn();
respawner.respawn(); zone.respawnQue.remove(respawner);
zone.respawnQue.remove(respawner); zone.lastRespawn = System.currentTimeMillis();
zone.lastRespawn = System.currentTimeMillis(); }
}
}
} }
} }
Thread.sleep(100); // Prevent busy-waiting
} catch (Exception e) { } catch (Exception e) {
Logger.error(e); Logger.error("Error in MobRespawnThread", e);
} }
} }
Logger.info("MobRespawnThread stopped.");
} }
public void stop() {
running = false;
}
public static void startRespawnThread() { public static void startRespawnThread() {
Thread respawnThread; Thread respawnThread = new Thread(new MobRespawnThread());
respawnThread = new Thread(new MobRespawnThread());
respawnThread.setName("respawnThread"); respawnThread.setName("respawnThread");
respawnThread.start(); respawnThread.start();
} }

Loading…
Cancel
Save