Fix spawners being reset when world is unloaded.

This commit is contained in:
Noah van der Aa 2021-05-09 20:17:25 +02:00 committed by Brianna
parent 5f6c21c2f4
commit a6bad531f2
2 changed files with 48 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import com.songoda.epicspawners.listeners.EntityListeners;
import com.songoda.epicspawners.listeners.InteractListeners;
import com.songoda.epicspawners.listeners.InventoryListeners;
import com.songoda.epicspawners.listeners.SpawnerListeners;
import com.songoda.epicspawners.listeners.WorldListeners;
import com.songoda.epicspawners.lootables.LootablesManager;
import com.songoda.epicspawners.player.PlayerData;
import com.songoda.epicspawners.player.PlayerDataManager;
@ -153,6 +154,7 @@ public class EpicSpawners extends SongodaPlugin {
pluginManager.registerEvents(new InteractListeners(this), this);
pluginManager.registerEvents(new InventoryListeners(), this);
pluginManager.registerEvents(new SpawnerListeners(this), this);
pluginManager.registerEvents(new WorldListeners(this), this);
int timeout = Settings.AUTOSAVE.getInt() * 60 * 20;
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, timeout, timeout);

View File

@ -0,0 +1,46 @@
package com.songoda.epicspawners.listeners;
import com.songoda.epicspawners.EpicSpawners;
import com.songoda.epicspawners.spawners.spawner.PlacedSpawner;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import java.util.Map;
import java.util.function.Consumer;
/**
* Created by noahvdaa on 5/09/2021.
*/
public class WorldListeners implements Listener {
private final EpicSpawners plugin;
public WorldListeners(EpicSpawners plugin) {
this.plugin = plugin;
}
@EventHandler
public void onWorldLoad(WorldLoadEvent e) {
// Unload previous spawners belonging to this world.
for (PlacedSpawner ps : plugin.getSpawnerManager().getSpawners()) {
if (e.getWorld().getName().equals(ps.getWorld().getName())) {
plugin.getSpawnerManager().removeSpawnerFromWorld(ps);
}
}
// Load spawners back in.
plugin.getDataManager().getSpawners(new Consumer<Map<Location, PlacedSpawner>>() {
@Override
public void accept(Map<Location, PlacedSpawner> locationPlacedSpawnerMap) {
for(PlacedSpawner ps : locationPlacedSpawnerMap.values()){
if (e.getWorld().getName().equals(ps.getWorld().getName())) {
plugin.getSpawnerManager().addSpawnerToWorld(ps.getLocation(), ps);
}
}
}
});
}
}