Correct errors on world change

This commit is contained in:
MrMicky 2018-04-25 14:17:52 +02:00
parent 5139b29f7c
commit 1fba0d9ff0
2 changed files with 28 additions and 1 deletions

View File

@ -51,7 +51,7 @@ public class ChunkListener implements Listener {
for (NPC npc : NPCManager.getAllNPCs()) {
Chunk npcChunk = npc.getLocation().getChunk();
if (chunk.getX() == npcChunk.getX() && chunk.getZ() == npcChunk.getZ()) {
if (chunk == npcChunk) {
// Loaded chunk with NPC in it. Showing it to the players again.
for (UUID uuid : npc.getShown()) {
@ -62,6 +62,10 @@ public class ChunkListener implements Listener {
Player player = Bukkit.getPlayer(uuid);
if (npcChunk.getWorld() != player.getWorld()) {
continue; // Player and NPC are not in the same world
}
double hideDistance = npc.getAutoHideDistance();
double distanceSquared = player.getLocation().distanceSquared(npc.getLocation());
boolean inRange = distanceSquared <= (hideDistance * hideDistance) || distanceSquared <= (Bukkit.getViewDistance() << 4);

View File

@ -8,9 +8,11 @@ import net.jitse.npclib.NPCManager;
import net.jitse.npclib.api.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
@ -34,6 +36,22 @@ public class PlayerListener implements Listener {
}
}
@EventHandler
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
Player player = event.getPlayer();
World from = event.getFrom();
// The PlayerTeleportEvent is call, and will handle visiiblity in the new world
for (NPC npc : NPCManager.getAllNPCs()) {
if (npc.getLocation().getWorld() == from) {
if (!npc.getAutoHidden().contains(player.getUniqueId())) {
npc.getAutoHidden().add(player.getUniqueId());
npc.hide(player, true);
}
}
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Location from = event.getFrom();
@ -52,11 +70,16 @@ public class PlayerListener implements Listener {
}
private void handleMove(Player player) {
World world = player.getWorld();
for (NPC npc : NPCManager.getAllNPCs()) {
if (!npc.getShown().contains(player.getUniqueId())) {
continue; // NPC was never supposed to be shown to the player.
}
if (npc.getLocation().getWorld() != world) {
continue; // NPC is not in the same world
}
// If Bukkit doesn't track the NPC entity anymore, bypass the hiding distance variable.
// This will cause issues otherwise (e.g. custom skin disappearing).
double hideDistance = npc.getAutoHideDistance();