Worked on issue #7. Issue should be fixed now.
This commit is contained in:
@@ -28,6 +28,8 @@ public abstract class NPC {
|
|||||||
protected final String name = uuid.toString().replace("-", "").substring(0, 10);
|
protected final String name = uuid.toString().replace("-", "").substring(0, 10);
|
||||||
protected final int entityId = (int) Math.ceil(Math.random() * 100000) + 100000;
|
protected final int entityId = (int) Math.ceil(Math.random() * 100000) + 100000;
|
||||||
|
|
||||||
|
protected double cosFOV = Math.cos(Math.toRadians(60));
|
||||||
|
|
||||||
private final Set<UUID> shown = new HashSet<>();
|
private final Set<UUID> shown = new HashSet<>();
|
||||||
private final Set<UUID> autoHidden = new HashSet<>();
|
private final Set<UUID> autoHidden = new HashSet<>();
|
||||||
|
|
||||||
@@ -76,6 +78,14 @@ public abstract class NPC {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void disableFOV() {
|
||||||
|
this.cosFOV = 0; // Or equals Math.cos(1/2 * Math.PI).
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFOV(double fov) {
|
||||||
|
this.cosFOV = Math.cos(Math.toRadians(60));
|
||||||
|
}
|
||||||
|
|
||||||
public Set<UUID> getShown() {
|
public Set<UUID> getShown() {
|
||||||
return shown;
|
return shown;
|
||||||
}
|
}
|
||||||
@@ -128,10 +138,14 @@ public abstract class NPC {
|
|||||||
if (auto) {
|
if (auto) {
|
||||||
sendShowPackets(player);
|
sendShowPackets(player);
|
||||||
} else {
|
} else {
|
||||||
if (shown.contains(player.getUniqueId())) {
|
if (isActuallyShown(player)) {
|
||||||
throw new RuntimeException("Cannot call show method twice.");
|
throw new RuntimeException("Cannot call show method twice.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shown.contains(player.getUniqueId())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
shown.add(player.getUniqueId());
|
shown.add(player.getUniqueId());
|
||||||
|
|
||||||
if (player.getLocation().distance(location) <= autoHideDistance) {
|
if (player.getLocation().distance(location) <= autoHideDistance) {
|
||||||
@@ -144,13 +158,10 @@ public abstract class NPC {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canSeeNPC(Player player) {
|
private boolean canSeeNPC(Player player) {
|
||||||
Vector dir = location.toVector().subtract(player.getEyeLocation().toVector()).normalize();
|
Vector dir = location.toVector().subtract(player.getEyeLocation().toVector()).normalize();
|
||||||
double dot = dir.dot(player.getLocation().getDirection());
|
double dot = dir.dot(player.getLocation().getDirection());
|
||||||
|
return dot >= cosFOV;
|
||||||
// 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.
|
// Internal method.
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ 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.Bukkit;
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -55,13 +54,6 @@ public class PlayerListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerMove(PlayerMoveEvent event) {
|
public void onPlayerMove(PlayerMoveEvent event) {
|
||||||
Location from = event.getFrom();
|
|
||||||
Location to = event.getTo();
|
|
||||||
|
|
||||||
// if (from.getX() == to.getX() && from.getY() == to.getY() && from.getZ() == to.getZ()) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
handleMove(event.getPlayer());
|
handleMove(event.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ public class NPCLibPlugin extends JavaPlugin implements Listener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
if (npc != null) {
|
||||||
|
npc.destroy(false);
|
||||||
|
}
|
||||||
|
|
||||||
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin disabled.");
|
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin disabled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package net.jitse.npclib.plugin.listeners;
|
|||||||
import net.jitse.npclib.events.NPCDestroyEvent;
|
import net.jitse.npclib.events.NPCDestroyEvent;
|
||||||
import net.jitse.npclib.events.NPCInteractEvent;
|
import net.jitse.npclib.events.NPCInteractEvent;
|
||||||
import net.jitse.npclib.events.NPCSpawnEvent;
|
import net.jitse.npclib.events.NPCSpawnEvent;
|
||||||
|
import net.jitse.npclib.events.trigger.TriggerType;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
@@ -18,11 +19,19 @@ public class NPCListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onNPCSpawn(NPCSpawnEvent event) {
|
public void onNPCSpawn(NPCSpawnEvent event) {
|
||||||
|
if (event.getTrigger() == TriggerType.AUTOMATIC) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.getPlayer().sendMessage(ChatColor.GREEN + "Spawned NPC " + event.getNPC().getEntityId());
|
event.getPlayer().sendMessage(ChatColor.GREEN + "Spawned NPC " + event.getNPC().getEntityId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onNPCDestroy(NPCDestroyEvent event) {
|
public void onNPCDestroy(NPCDestroyEvent event) {
|
||||||
|
if (event.getTrigger() == TriggerType.AUTOMATIC) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.getPlayer().sendMessage(ChatColor.RED + "Destroyed NPC " + event.getNPC().getEntityId());
|
event.getPlayer().sendMessage(ChatColor.RED + "Destroyed NPC " + event.getNPC().getEntityId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user