use of org.bukkit.event.world.WorldInitEvent 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);
}
Aggregations