respawn thread optimized
This commit is contained in:
@@ -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) {
|
||||||
|
for (Zone zone : zones) {
|
||||||
|
synchronized (zone) { // Optional: Synchronize on zone
|
||||||
|
if (!zone.respawnQue.isEmpty() &&
|
||||||
|
zone.lastRespawn + RESPAWN_INTERVAL < System.currentTimeMillis()) {
|
||||||
|
|
||||||
if (zone.respawnQue.isEmpty() == false && zone.lastRespawn + 100 < System.currentTimeMillis()) {
|
Mob respawner = zone.respawnQue.iterator().next();
|
||||||
|
if (respawner != null) {
|
||||||
Mob respawner = zone.respawnQue.iterator().next();
|
respawner.respawn();
|
||||||
|
zone.respawnQue.remove(respawner);
|
||||||
if (respawner == null)
|
zone.lastRespawn = System.currentTimeMillis();
|
||||||
continue;
|
}
|
||||||
|
}
|
||||||
respawner.respawn();
|
}
|
||||||
zone.respawnQue.remove(respawner);
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user