Skip to content

Instantly share code, notes, and snippets.

@HardstylesDev
Last active November 26, 2021 18:56
Show Gist options
  • Save HardstylesDev/10ed39027feba3d1328837a1a64618ac to your computer and use it in GitHub Desktop.
Save HardstylesDev/10ed39027feba3d1328837a1a64618ac to your computer and use it in GitHub Desktop.
@Getter
@Setter
public class NPC {
private Location location;
private String name;
private EntityPlayer entity;
public NPC(Location location, OfflinePlayer fake) {
this.location = location;
MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
WorldServer nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
entity = new EntityPlayer(nmsServer, nmsWorld, new GameProfile(fake.getUniqueId(), ""), new PlayerInteractManager(nmsWorld));
entity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw() * 256.0F / 360.0F, location.getPitch() * 256.0F / 360.0F);
}
public void show(Player... target) {
remove(target);
PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, entity);
PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn(entity);
PacketPlayOutEntityHeadRotation rotate = new PacketPlayOutEntityHeadRotation(entity, (byte) (location.getYaw() * 256.0F / 360.0F));
for (Player player : target) {
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
connection.sendPacket(add);
connection.sendPacket(spawn);
connection.sendPacket(rotate);
}
}
public void hideAndShow(Player... target) {
remove(target);
// if you remove the delay the NPC will dissapear completely.
new BukkitRunnable() {
@Override
public void run() {
show(target);
}
}.runTaskLater(Core.i(), 2);
}
public void setHand(ItemStack item) {
entity.inventory.setItem(entity.inventory.itemInHandIndex, CraftItemStack.asNMSCopy(item));
}
public void remove(Player... player) {
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(entity.getId());
for (Player viewer : player) {
((CraftPlayer) viewer).getHandle().playerConnection.sendPacket(packet);
}
}
public void onInteract(Player p) {
p.sendMessage(ChatColor.GREEN + "Thanks for interacting with me, " + p.getName() + "!");
//todo open inventory or sth
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment