Added chunk unload event listener.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package net.jitse.npclib;
|
||||
|
||||
import net.jitse.npclib.api.NPC;
|
||||
import net.jitse.npclib.listeners.ChunkListener;
|
||||
import net.jitse.npclib.listeners.PacketListener;
|
||||
import net.jitse.npclib.listeners.PlayerLeaveListener;
|
||||
import net.jitse.npclib.listeners.PlayerMoveListener;
|
||||
@@ -47,6 +48,7 @@ public class NPCLib {
|
||||
|
||||
pluginManager.registerEvents(new PlayerMoveListener(), plugin);
|
||||
pluginManager.registerEvents(new PlayerLeaveListener(), plugin);
|
||||
pluginManager.registerEvents(new ChunkListener(), plugin);
|
||||
|
||||
new PacketListener().start(plugin);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) Jitse Boonstra 2018 All rights reserved.
|
||||
*/
|
||||
|
||||
package net.jitse.npclib.listeners;
|
||||
|
||||
import net.jitse.npclib.NPCManager;
|
||||
import net.jitse.npclib.api.NPC;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstras
|
||||
*/
|
||||
public class ChunkListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onChunkUnload(ChunkUnloadEvent event) {
|
||||
Chunk chunk = event.getChunk();
|
||||
|
||||
for (NPC npc : NPCManager.getAllNPCs()) {
|
||||
Chunk npcChunk = npc.getLocation().getChunk();
|
||||
|
||||
if (chunk.getX() == npcChunk.getX() && chunk.getZ() == npcChunk.getZ()) {
|
||||
// Unloaded chunk with NPC in it. Hiding it from all players currently shown to.
|
||||
|
||||
for (UUID uuid : npc.getShown()) {
|
||||
// Safety check so it doesn't send packets if the NPC has already
|
||||
// been automatically despawned by the system.
|
||||
if (npc.getAutoHidden().contains(uuid)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
npc.hide(Bukkit.getPlayer(uuid), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ package net.jitse.npclib.listeners;
|
||||
|
||||
import net.jitse.npclib.NPCManager;
|
||||
import net.jitse.npclib.api.NPC;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -41,8 +42,11 @@ public class PlayerMoveListener implements Listener {
|
||||
continue; // NPC was never supposed to be shown to the player.
|
||||
}
|
||||
|
||||
// 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();
|
||||
boolean inRange = player.getLocation().distanceSquared(npc.getLocation()) <= (hideDistance * hideDistance);
|
||||
double distanceSquared = player.getLocation().distanceSquared(npc.getLocation());
|
||||
boolean inRange = distanceSquared <= (hideDistance * hideDistance) || distanceSquared <= (Bukkit.getViewDistance() << 4);
|
||||
if (npc.getAutoHidden().contains(player.getUniqueId())) {
|
||||
// Check if the player and NPC are within the range to sendShowPackets it again.
|
||||
if (inRange) {
|
||||
|
||||
Reference in New Issue
Block a user