2018-04-26 13:52:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jitse Boonstra
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.jitse.npclib.listeners;
|
|
|
|
|
2019-08-03 13:47:12 +02:00
|
|
|
import net.jitse.npclib.NPCLib;
|
|
|
|
import net.jitse.npclib.internal.NPCManager;
|
|
|
|
import net.jitse.npclib.internal.SimpleNPC;
|
2018-04-26 13:52:49 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Jitse Boonstra
|
|
|
|
*/
|
|
|
|
public class PlayerListener implements Listener {
|
|
|
|
|
2019-08-03 13:47:12 +02:00
|
|
|
private final NPCLib instance;
|
|
|
|
|
|
|
|
public PlayerListener(NPCLib instance) {
|
|
|
|
this.instance = instance;
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:52:49 +02:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
|
|
Player player = event.getPlayer();
|
2019-08-03 13:47:12 +02:00
|
|
|
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
|
2019-02-21 16:52:13 +01:00
|
|
|
npc.getAutoHidden().remove(player.getUniqueId());
|
2018-04-26 13:52:49 +02:00
|
|
|
|
|
|
|
// Don't need to use NPC#hide since the entity is not registered in the NMS server.
|
2019-02-21 16:52:13 +01:00
|
|
|
npc.getShown().remove(player.getUniqueId());
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
World from = event.getFrom();
|
|
|
|
|
|
|
|
// The PlayerTeleportEvent is call, and will handle visibility in the new world.
|
2019-08-03 13:47:12 +02:00
|
|
|
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
|
2018-04-26 13:52:49 +02:00
|
|
|
if (npc.getLocation().getWorld().equals(from)) {
|
|
|
|
if (!npc.getAutoHidden().contains(player.getUniqueId())) {
|
|
|
|
npc.getAutoHidden().add(player.getUniqueId());
|
2018-05-06 23:34:55 +02:00
|
|
|
npc.hide(player, true, false);
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerMove(PlayerMoveEvent event) {
|
|
|
|
handleMove(event.getPlayer());
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
|
|
|
handleMove(event.getPlayer());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleMove(Player player) {
|
|
|
|
World world = player.getWorld();
|
2019-08-03 13:47:12 +02:00
|
|
|
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
|
2018-04-26 13:52:49 +02:00
|
|
|
if (!npc.getShown().contains(player.getUniqueId())) {
|
|
|
|
continue; // NPC was never supposed to be shown to the player.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!npc.getLocation().getWorld().equals(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).
|
2019-08-03 13:47:12 +02:00
|
|
|
double hideDistance = instance.getAutoHideDistance();
|
2018-04-26 13:52:49 +02:00
|
|
|
double distanceSquared = player.getLocation().distanceSquared(npc.getLocation());
|
2019-05-02 17:49:14 +02:00
|
|
|
boolean inRange = distanceSquared <= (Math.pow(hideDistance, 2))
|
|
|
|
&& distanceSquared <= (Math.pow(Bukkit.getViewDistance() << 4, 2));
|
2018-04-26 13:52:49 +02:00
|
|
|
if (npc.getAutoHidden().contains(player.getUniqueId())) {
|
|
|
|
// Check if the player and NPC are within the range to sendShowPackets it again.
|
|
|
|
if (inRange) {
|
|
|
|
npc.getAutoHidden().remove(player.getUniqueId());
|
2018-05-07 10:14:55 +02:00
|
|
|
npc.show(player, true);
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check if the player and NPC are out of range to sendHidePackets it.
|
|
|
|
if (!inRange) {
|
|
|
|
npc.getAutoHidden().add(player.getUniqueId());
|
2018-05-07 10:14:55 +02:00
|
|
|
npc.hide(player, true, true);
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|