public static HumanNPC spawnNPC(HumanNPC npc, Location loc) { try { WorldServer ws = getWorldServer(loc.getWorld()); ws.addEntity(npc.getHandle()); npc.teleport(loc); ws.players.remove(npc.getHandle()); return npc; } catch (Exception e) { e.printStackTrace(); } return null; }
// Spawns a new NPC and registers it. public static void register(int UID, String owner, NPCCreateReason reason) { Location loc = PropertyManager.getBasic().getLocation(UID); ChatColor colour = PropertyManager.getBasic().getColour(UID); String name = PropertyManager.getBasic().getName(UID); name = ChatColor.stripColor(name); if (!Settings.getString("SpaceChar").isEmpty()) { name = name.replace(Settings.getString("SpaceChar"), " "); } String npcName = name; if (colour != null && colour != ChatColor.WHITE) { npcName = colour + name; } HumanNPC npc = NPCSpawner.spawnNPC(UID, npcName, loc); NPCCreateEvent event = new NPCCreateEvent(npc, reason, loc); Bukkit.getServer().getPluginManager().callEvent(event); npc.setNPCData( new NPCData( npcName, UID, loc, colour, PropertyManager.getBasic().getItems(UID), NPCDataManager.NPCTexts.get(UID), PropertyManager.getBasic().isTalk(UID), PropertyManager.getBasic().isLookWhenClose(UID), PropertyManager.getBasic().isTalkWhenClose(UID), owner)); PropertyManager.getBasic().saveOwner(UID, owner); PropertyManager.load(npc); registerUID(UID, npcName); list.put(UID, npc); PropertyManager.save(npc); npc.getPlayer().setSleepingIgnored(true); // Fix beds. }
// Rotates an NPC. public static void faceEntity(HumanNPC npc, Entity entity) { if (npc.getWorld() != entity.getWorld()) return; if (Settings.getBoolean("RealisticPathing") && !npc.getHandle().isInSight(((CraftEntity) entity).getHandle())) return; Location loc = npc.getLocation(), pl = entity.getLocation(); double xDiff = pl.getX() - loc.getX(); double yDiff = pl.getY() - loc.getY(); double zDiff = pl.getZ() - loc.getZ(); double DistanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); double DistanceY = Math.sqrt(DistanceXZ * DistanceXZ + yDiff * yDiff); double yaw = (Math.acos(xDiff / DistanceXZ) * 180 / Math.PI); double pitch = (Math.acos(yDiff / DistanceY) * 180 / Math.PI) - 90; if (zDiff < 0.0) { yaw = yaw + (Math.abs(180 - yaw) * 2); } npc.getHandle().yaw = (float) yaw - 90; npc.getHandle().az = npc.getHandle().yaw; npc.getHandle().pitch = (float) pitch; }
public static void removeNPCFromPlayerList(HumanNPC npc) { getWorldServer(npc.getPlayer().getWorld()).players.remove(npc.getHandle()); }
public static void despawnNPC(HumanNPC npc) { despawnNPC(npc.getHandle()); }
// Renames an npc. public static void rename(int UID, String changeTo, String owner) { HumanNPC npc = get(UID); npc.getNPCData().setName(changeTo); removeForRespawn(UID); register(UID, owner, NPCCreateReason.RESPAWN); }