Added chunk unload event listener.

This commit is contained in:
JitseB
2018-04-19 15:26:42 +02:00
parent 0be943d1ae
commit 7cfde040da
4 changed files with 52 additions and 2 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ This is an API made specifically for spigot servers (Minecraft). Current support
### Notice ### Notice
If any developer out there knows how to convert this project to Maven, please consider contacting me or creating a pull request. I have yet to come up with a good solution for the (older) NMS code repositories. I would love to add a CI system to this repository for easier collaboration. If any developer out there knows how to convert this project to Maven, please consider contacting me or creating a pull request. I have yet to come up with a good solution for the (older) NMS code repositories. I would love to add a CI system to this repository for easier collaboration.
## Donate ## Donate
[![PayPal](https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg)](https://paypal.me/JitseB) [![PayPal](https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg)](https://paypal.me/JitseB)
+2
View File
@@ -5,6 +5,7 @@
package net.jitse.npclib; package net.jitse.npclib;
import net.jitse.npclib.api.NPC; import net.jitse.npclib.api.NPC;
import net.jitse.npclib.listeners.ChunkListener;
import net.jitse.npclib.listeners.PacketListener; import net.jitse.npclib.listeners.PacketListener;
import net.jitse.npclib.listeners.PlayerLeaveListener; import net.jitse.npclib.listeners.PlayerLeaveListener;
import net.jitse.npclib.listeners.PlayerMoveListener; import net.jitse.npclib.listeners.PlayerMoveListener;
@@ -47,6 +48,7 @@ public class NPCLib {
pluginManager.registerEvents(new PlayerMoveListener(), plugin); pluginManager.registerEvents(new PlayerMoveListener(), plugin);
pluginManager.registerEvents(new PlayerLeaveListener(), plugin); pluginManager.registerEvents(new PlayerLeaveListener(), plugin);
pluginManager.registerEvents(new ChunkListener(), plugin);
new PacketListener().start(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.NPCManager;
import net.jitse.npclib.api.NPC; import net.jitse.npclib.api.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; 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. 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(); 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())) { if (npc.getAutoHidden().contains(player.getUniqueId())) {
// Check if the player and NPC are within the range to sendShowPackets it again. // Check if the player and NPC are within the range to sendShowPackets it again.
if (inRange) { if (inRange) {