diff --git a/api/src/main/java/net/jitse/npclib/api/NPC.java b/api/src/main/java/net/jitse/npclib/api/NPC.java index 1173487..2bb856e 100644 --- a/api/src/main/java/net/jitse/npclib/api/NPC.java +++ b/api/src/main/java/net/jitse/npclib/api/NPC.java @@ -20,15 +20,13 @@ import java.util.UUID; public interface NPC { /** - * * @param player * @return unique hologram for that user */ Hologram getPlayerHologram(Player player); /** - * - * @param uniqueLines The text that the targetPlayer will see + * @param uniqueLines The text that the targetPlayer will see * @param targetPlayer The target player * @return object instance * @author Gatt @@ -36,17 +34,15 @@ public interface NPC { NPC setPlayerLines(List uniqueLines, Player targetPlayer); /** - * - * @param uniqueLines The text that the targetPlayer will see + * @param uniqueLines The text that the targetPlayer will see * @param targetPlayer The target player - * @param update whether or not to send the update packets + * @param update whether or not to send the update packets * @return object instance * @author Gatt */ NPC setPlayerLines(List uniqueLines, Player targetPlayer, boolean update); /** - * * @param targetPlayer The target player * @return the lines that the targetPlayer will see, if null; default lines. */ @@ -91,6 +87,13 @@ public interface NPC { */ NPC create(); + /** + * Check whether the NPCs packets have already been generated. + * + * @return Whether NPC#create has been called yet. + */ + boolean isCreated(); + /** * Get the ID of the NPC. * diff --git a/api/src/main/java/net/jitse/npclib/internal/NPCBase.java b/api/src/main/java/net/jitse/npclib/internal/NPCBase.java index 90c7cbb..b1b1040 100644 --- a/api/src/main/java/net/jitse/npclib/internal/NPCBase.java +++ b/api/src/main/java/net/jitse/npclib/internal/NPCBase.java @@ -41,6 +41,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { protected UUID uuid = new UUID(new Random().nextLong(), 0); protected String name = uuid.toString().replace("-", "").substring(0, 10); protected GameProfile gameProfile = new GameProfile(uuid, name); + protected boolean created = false; protected NPCLib instance; protected List text; @@ -177,7 +178,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { @Override public boolean isShown(Player player) { - if (player == null) return false; + Objects.requireNonNull(player, "Player object cannot be null"); return shown.contains(player.getUniqueId()) && !autoHidden.contains(player.getUniqueId()); } @@ -190,9 +191,15 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { @Override public NPC create() { createPackets(); + this.created = true; return this; } + @Override + public boolean isCreated() { + return created; + } + public void onLogout(Player player) { getAutoHidden().remove(player.getUniqueId()); getShown().remove(player.getUniqueId()); // Don't need to use NPC#hide since the entity is not registered in the NMS server. diff --git a/api/src/main/java/net/jitse/npclib/listeners/PacketListener.java b/api/src/main/java/net/jitse/npclib/listeners/PacketListener.java index 109fe60..5c10cbe 100755 --- a/api/src/main/java/net/jitse/npclib/listeners/PacketListener.java +++ b/api/src/main/java/net/jitse/npclib/listeners/PacketListener.java @@ -49,7 +49,7 @@ public class PacketListener { } private boolean handleInteractPacket(Player player, Object packet) { - if (!packetPlayInUseEntityClazz.isInstance(packet)) + if (!packetPlayInUseEntityClazz.isInstance(packet) || player == null) return true; // We aren't handling the packet. NPCBase npc = null; @@ -62,7 +62,7 @@ public class PacketListener { // ~ Kneesnap, 9 / 20 / 2019. for (NPCBase testNPC : NPCManager.getAllNPCs()) { - if (testNPC.isShown(player) && testNPC.getEntityId() == packetEntityId) { + if (testNPC.isCreated() && testNPC.isShown(player) && testNPC.getEntityId() == packetEntityId) { npc = testNPC; break; } diff --git a/nms/v1_16_R1/src/main/java/net/jitse/npclib/nms/v1_16_R1/NPC_v1_16_R1.java b/nms/v1_16_R1/src/main/java/net/jitse/npclib/nms/v1_16_R1/NPC_v1_16_R1.java index 40266a0..4895d61 100644 --- a/nms/v1_16_R1/src/main/java/net/jitse/npclib/nms/v1_16_R1/NPC_v1_16_R1.java +++ b/nms/v1_16_R1/src/main/java/net/jitse/npclib/nms/v1_16_R1/NPC_v1_16_R1.java @@ -36,17 +36,17 @@ public class NPC_v1_16_R1 extends NPCBase { public NPC_v1_16_R1(NPCLib instance, List lines) { super(instance, lines); } + @Override public Hologram getPlayerHologram(Player player) { Hologram holo = super.getPlayerHologram(player); - if (holo == null){ + if (holo == null) { holo = new Hologram(MinecraftVersion.V1_16_R1, location.clone().add(0, 0.5, 0), getPlayerLines(player)); } super.textDisplayHolograms.put(player.getUniqueId(), holo); return holo; } - @Override public void createPackets() { Bukkit.getOnlinePlayers().forEach(this::createPackets);