Search in sources :

Example 1 with WorldUnloadEvent

use of org.bukkit.event.world.WorldUnloadEvent in project BKCommonLib by bergerhealer.

the class OfflineWorldLoadedChangeListener method enable.

@Override
public void enable() throws Throwable {
    OfflineWorld.setLoadedWorldSupplier(this);
    // Listen for when worlds load and unload
    Bukkit.getPluginManager().registerEvents(new Listener() {

        @EventHandler(priority = EventPriority.LOWEST)
        public void onWorldInit(WorldInitEvent event) {
            OfflineWorld.BukkitWorldSupplier supplier = OfflineWorld.of(event.getWorld()).loadedWorldSupplier;
            if (supplier instanceof WorldSupplier) {
                ((WorldSupplier) supplier).update(event.getWorld());
            }
        }

        @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
        public void onWorldUnload(WorldUnloadEvent event) {
            OfflineWorld.BukkitWorldSupplier supplier = OfflineWorld.of(event.getWorld()).loadedWorldSupplier;
            if (supplier instanceof WorldSupplier) {
                ((WorldSupplier) supplier).update(null);
            }
            // Just in case!
            OfflineWorld.clearByBukkitWorldCache();
        }
    }, plugin);
    // Periodically clear the by-Bukkit world mapping to avoid memory leaks - just in case!
    // No need to run this on the main thread as it's all synchronized anyway
    // 10 minutes
    final int clearInterval = 1200;
    this.asyncClearTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, OfflineWorld::clearByBukkitWorldCache, clearInterval, clearInterval);
}
Also used : BukkitWorldSupplier(com.bergerkiller.bukkit.common.offline.OfflineWorld.BukkitWorldSupplier) WorldUnloadEvent(org.bukkit.event.world.WorldUnloadEvent) Listener(org.bukkit.event.Listener) WorldInitEvent(org.bukkit.event.world.WorldInitEvent) EventHandler(org.bukkit.event.EventHandler) BukkitWorldSupplier(com.bergerkiller.bukkit.common.offline.OfflineWorld.BukkitWorldSupplier)

Aggregations

BukkitWorldSupplier (com.bergerkiller.bukkit.common.offline.OfflineWorld.BukkitWorldSupplier)1 EventHandler (org.bukkit.event.EventHandler)1 Listener (org.bukkit.event.Listener)1 WorldInitEvent (org.bukkit.event.world.WorldInitEvent)1 WorldUnloadEvent (org.bukkit.event.world.WorldUnloadEvent)1