From cc0c64a610efed72b6b48fa562900eaaf84c178e Mon Sep 17 00:00:00 2001 From: JitseB Date: Mon, 7 May 2018 10:14:55 +0200 Subject: [PATCH] Work on issue #7 (skin rendering issue). --- .../main/java/net/jitse/npclib/api/NPC.java | 21 +++++++++++++ .../npclib/listeners/PlayerListener.java | 10 +++--- .../net/jitse/npclib/plugin/NPCLibPlugin.java | 31 ++++++++++++------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/commons/src/main/java/net/jitse/npclib/api/NPC.java b/commons/src/main/java/net/jitse/npclib/api/NPC.java index 6c20b7e..2b3469d 100644 --- a/commons/src/main/java/net/jitse/npclib/api/NPC.java +++ b/commons/src/main/java/net/jitse/npclib/api/NPC.java @@ -15,6 +15,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.Vector; import java.util.*; @@ -113,6 +114,17 @@ public abstract class NPC { return; } + if (!canSeeNPC(player)) { + if (!auto) { + shown.add(player.getUniqueId()); + } + + if (!autoHidden.contains(player.getUniqueId())) { + autoHidden.add(player.getUniqueId()); + } + return; + } + if (auto) { sendShowPackets(player); } else { @@ -132,6 +144,15 @@ public abstract class NPC { } } + public boolean canSeeNPC(Player player) { + Vector dir = location.toVector().subtract(player.getEyeLocation().toVector()).normalize(); + double dot = dir.dot(player.getLocation().getDirection()); + Bukkit.broadcastMessage("dot: " + dot); + // 0.5 equals a FOV of 60 deg (but should be 0.55) + // We want to spawn the NPC *just* before the player can see it. + return dot >= 0.5; + } + // Internal method. protected abstract void sendShowPackets(Player player); diff --git a/commons/src/main/java/net/jitse/npclib/listeners/PlayerListener.java b/commons/src/main/java/net/jitse/npclib/listeners/PlayerListener.java index 8b6f37e..3252315 100644 --- a/commons/src/main/java/net/jitse/npclib/listeners/PlayerListener.java +++ b/commons/src/main/java/net/jitse/npclib/listeners/PlayerListener.java @@ -58,9 +58,9 @@ public class PlayerListener implements Listener { Location from = event.getFrom(); Location to = event.getTo(); - if (from.getX() == to.getX() && from.getY() == to.getY() && from.getZ() == to.getZ()) { - return; - } +// if (from.getX() == to.getX() && from.getY() == to.getY() && from.getZ() == to.getZ()) { +// return; +// } handleMove(event.getPlayer()); } @@ -89,14 +89,14 @@ public class PlayerListener implements Listener { if (npc.getAutoHidden().contains(player.getUniqueId())) { // Check if the player and NPC are within the range to sendShowPackets it again. if (inRange) { - npc.show(player, true); npc.getAutoHidden().remove(player.getUniqueId()); + npc.show(player, true); } } else { // Check if the player and NPC are out of range to sendHidePackets it. if (!inRange) { - npc.hide(player, true, true); npc.getAutoHidden().add(player.getUniqueId()); + npc.hide(player, true, true); } } } diff --git a/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java b/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java index 2cd427d..b8a4e3d 100644 --- a/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java +++ b/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java @@ -23,6 +23,7 @@ import java.util.Arrays; public class NPCLibPlugin extends JavaPlugin implements Listener { private NPCLib npcLib; + private NPC npc; @Override public void onEnable() { @@ -48,17 +49,25 @@ public class NPCLibPlugin extends JavaPlugin implements Listener { return; } - MineSkinFetcher.fetchSkinFromIdAsync(168841, skin -> { - NPC npc = npcLib.createNPC(skin, Arrays.asList( - ChatColor.BOLD + "NPC Library", "", - "Create your own", "non-player characters", - "with the simplistic", "API of NPCLib!" - )); - npc.create(event.getPlayer().getLocation()); - - for (Player player : getServer().getOnlinePlayers()) { - npc.show(player); + if (npc != null) { + if (npc.isActuallyShown(event.getPlayer())) { + npc.hide(event.getPlayer()); + } else { + npc.show(event.getPlayer()); } - }); + } else { + MineSkinFetcher.fetchSkinFromIdAsync(168841, skin -> { + npc = npcLib.createNPC(skin, Arrays.asList( + ChatColor.BOLD + "NPC Library", "", + "Create your own", "non-player characters", + "with the simplistic", "API of NPCLib!" + )); + npc.create(event.getPlayer().getLocation()); + + for (Player player : getServer().getOnlinePlayers()) { + npc.show(player); + } + }); + } } }