Fix NPC's for unloaded worlds

This commit is contained in:
Shaun Bennett 2016-07-21 14:22:38 -05:00
parent 0262452b6a
commit 9ed0bbe43a
1 changed files with 38 additions and 13 deletions

View File

@ -2,14 +2,7 @@ package mineplex.core.npc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import org.bukkit.*;
import org.bukkit.configuration.InvalidConfigurationException;
@ -87,6 +80,7 @@ public class NpcManager extends MiniPlugin
private final Creature _creature;
private final List<Npc> _npcs = new ArrayList<>();
private final Queue<NpcsRecord> _queuedNpcs = new LinkedList<>();
final Map<UUID, Npc> _npcMap = new HashMap<>();
private final Set<UUID> _npcDeletingPlayers = new HashSet<>();
@ -504,14 +498,22 @@ public class NpcManager extends MiniPlugin
{
record.detach();
if (Bukkit.getWorld(record.getWorld()) == null)
{
// World isnt loaded yet, add to queue
_queuedNpcs.add(record);
}
else
{
Npc npc = new Npc(this, record);
_npcs.add(npc);
if (npc.getChunk().isLoaded())
if (npc.getChunk() != null && npc.getChunk().isLoaded())
spawnNpc(npc);
}
}
}
}
public void clearNpcs(boolean deleteFromDatabase) throws SQLException
{
@ -561,11 +563,34 @@ public class NpcManager extends MiniPlugin
for (Npc npc : _npcs)
{
if (npc.getEntity() != null && !npc.getEntity().isValid() && npc.getChunk().isLoaded())
if (npc.getEntity() != null && !npc.getEntity().isValid() && npc.getChunk() != null && npc.getChunk().isLoaded())
spawnNpc(npc);
}
}
@EventHandler
public void processNpcQueue(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC_05)
return;
Iterator<NpcsRecord> iterator = _queuedNpcs.iterator();
while (iterator.hasNext())
{
NpcsRecord record = iterator.next();
if (Bukkit.getWorld(record.getWorld()) != null)
{
Npc npc = new Npc(this, record);
_npcs.add(npc);
iterator.remove();
if (npc.getChunk() != null && npc.getChunk().isLoaded())
spawnNpc(npc);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent event)
{