Skip to content

Instantly share code, notes, and snippets.

@hugo4715
Created February 16, 2016 19:02
Show Gist options
  • Save hugo4715/393a41bcf4cf98c31907 to your computer and use it in GitHub Desktop.
Save hugo4715/393a41bcf4cf98c31907 to your computer and use it in GitHub Desktop.
/*********************IMPORTANT*********************************************
* Need to figure out how to remove the fakePlayer's name in the tab list.**
***************************************************************************/
public class NPCUtils {
private static List<Integer> npcs = new ArrayList<Integer>();
public static void despawnAll(){
for(ForcePlayer cur : PlayerManager.getPlayers()){
for(Integer i : npcs){
if(!cur.getPlayer().isOnline())continue;
final PlayerConnection connection = ((CraftPlayer) cur.getPlayer()).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutEntityDestroy(i));
}
}
}
public static int spawnNPC(Location l, String npcName, UUID skin, int timeDespawn, Player... players) {
return spawnNPC(l, npcName, skin, timeDespawn, true, players);
}
/**
*
* @param l The location where the npc will apear
* @param npcName The name of the spawned npc
* @param skin The uuid of his skin
* @param timeDespawn How many tick will it live
* @param showName true if he is sneaking
* @param players Who will see it
* @return
*/
public static int spawnNPC(Location l, String npcName, UUID skin, int timeDespawn,boolean showName, Player... players) {
MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
final EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld,new GameProfile(skin, ""),new PlayerInteractManager(nmsWorld));
npc.displayName = null;
if(!showName){
npc.setSneaking(true);
npc.setCustomNameVisible(false);
}
npc.setLocation(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
npcs.add(npc.getId());
new Thread(new Runnable(){
@Override
public void run() {
if(players.length == 0){
for(Player cur : Bukkit.getOnlinePlayers()) {
final PlayerConnection connection = ((CraftPlayer) cur).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, npc));
connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
}
}else{
for(Player p : players){
final PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, npc));
connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
}
}
new BukkitRunnable()
{
public void run()
{
for(Player cur : players) {
if(!cur.isOnline())continue;
final PlayerConnection connection = ((CraftPlayer) cur).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, npc));
connection.sendPacket(new PacketPlayOutEntityDestroy(npc.getId()));
npcs.remove((Object)npc.getId());
}
}
}.runTaskLater(Main.getInstance(), timeDespawn);
}
}).run();
return npc.getId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment