Files
BattleBane/src/engine/mobileAI/Threads/MobRespawnThread.java
T

77 lines
3.0 KiB
Java
Raw Normal View History

2023-08-01 19:19:39 -05:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
2023-08-01 20:13:23 -05:00
package engine.mobileAI.Threads;
2023-08-01 19:19:39 -05:00
import engine.gameManager.ZoneManager;
import engine.objects.Mob;
import engine.objects.Zone;
import org.pmw.tinylog.Logger;
2025-01-18 03:35:35 -06:00
import java.util.ArrayList;
import java.util.Collection;
2023-08-01 19:19:39 -05:00
/**
* Thread blocks until MagicBane dispatch messages are
* enqueued then processes them in FIFO order. The collection
* is thread safe.
* <p>
* Any large messages not time sensitive such as load object
* sent to more than a single individual should be spawned
* individually on a DispatchMessageThread.
*/
public class MobRespawnThread implements Runnable {
2025-01-18 03:35:35 -06:00
private volatile boolean running = true;
private static final long RESPAWN_INTERVAL = 100; // Configurable interval
2023-08-01 19:19:39 -05:00
public MobRespawnThread() {
2025-01-18 03:35:35 -06:00
Logger.info("MobRespawnThread initialized.");
2023-08-01 19:19:39 -05:00
}
@Override
public void run() {
2025-01-18 03:35:35 -06:00
while (running) {
2023-08-02 08:07:41 -04:00
try {
2025-01-18 03:35:35 -06:00
Collection<Zone> zones = ZoneManager.getAllZones();
if (zones != null) {
for (Zone zone : zones) {
synchronized (zone) { // Optional: Synchronize on zone
2025-02-15 07:52:39 -06:00
if (!Zone.respawnQue.isEmpty() && Zone.lastRespawn + RESPAWN_INTERVAL < System.currentTimeMillis()) {
2023-08-02 08:07:41 -04:00
2025-02-15 07:52:39 -06:00
Mob respawner = Zone.respawnQue.iterator().next();
2025-01-18 03:35:35 -06:00
if (respawner != null) {
respawner.respawn();
2025-02-15 07:52:39 -06:00
Zone.respawnQue.remove(respawner);
Zone.lastRespawn = System.currentTimeMillis();
2025-01-26 08:49:20 -06:00
Thread.sleep(100);
2025-01-18 03:35:35 -06:00
}
}
}
2023-08-01 19:50:52 -05:00
}
2023-08-01 19:19:39 -05:00
}
2025-01-18 03:35:35 -06:00
Thread.sleep(100); // Prevent busy-waiting
2023-08-02 08:07:41 -04:00
} catch (Exception e) {
2025-01-18 03:35:35 -06:00
Logger.error("Error in MobRespawnThread", e);
2023-08-01 19:19:39 -05:00
}
}
2025-01-18 03:35:35 -06:00
Logger.info("MobRespawnThread stopped.");
2023-08-01 19:19:39 -05:00
}
2025-01-18 03:35:35 -06:00
public void stop() {
running = false;
}
2023-08-01 19:19:39 -05:00
public static void startRespawnThread() {
2025-01-18 03:35:35 -06:00
Thread respawnThread = new Thread(new MobRespawnThread());
2023-08-01 19:19:39 -05:00
respawnThread.setName("respawnThread");
respawnThread.start();
}
}