Advertisement
Guest User

Untitled

a guest
Jun 18th, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. package net.wardensmp.plugins.dev.npc;
  2.  
  3. import com.google.gson.JsonObject;
  4. import com.google.gson.JsonParser;
  5. import com.mojang.authlib.GameProfile;
  6. import com.mojang.authlib.properties.Property;
  7. import net.minecraft.server.v1_16_R3.*;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
  11. import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
  12. import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
  13. import org.bukkit.entity.Player;
  14.  
  15. import java.io.InputStreamReader;
  16. import java.net.URL;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Objects;
  20. import java.util.UUID;
  21.  
  22. public class NPC {
  23.  
  24. private static List<EntityPlayer> NPCs = new ArrayList<>();
  25.  
  26. public static void createNPC(Player player, String skin, String name) {
  27. MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
  28. WorldServer world = ((CraftWorld) Objects.requireNonNull(Bukkit.getWorld(player.getWorld().getName()))).getHandle();
  29. GameProfile profile = new GameProfile(UUID.randomUUID(), ChatColor.translateAlternateColorCodes('&', name));
  30. EntityPlayer npc = new EntityPlayer(server, world, profile, new PlayerInteractManager(world));
  31. npc.setLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(),
  32. player.getLocation().getYaw(), player.getLocation().getPitch());
  33.  
  34. String[] names = getSkin(player, skin);
  35. profile.getProperties().put("textures", new Property("textures", names[0], names[1]));
  36.  
  37. addNPCPacket(npc);
  38. NPCs.add(npc);
  39. }
  40.  
  41. public static String[] getSkin(Player player, String name) {
  42. try {
  43. URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
  44. InputStreamReader reader = new InputStreamReader(url.openStream());
  45. String uuid = new JsonParser().parse(reader).getAsJsonObject().get("id").getAsString();
  46. URL url1 = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
  47. InputStreamReader reader1 = new InputStreamReader(url1.openStream());
  48. JsonObject property =
  49. new JsonParser().parse(reader1).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
  50. String texture = property.get("value").getAsString();
  51. String signature = property.get("signature").getAsString();
  52. return new String[] {texture, signature};
  53. } catch (Exception e) {
  54. EntityPlayer p = ((CraftPlayer) player).getHandle();
  55. GameProfile profile = p.getProfile();
  56. Property property = profile.getProperties().get("textures").iterator().next();
  57. String texture = property.getValue();
  58. String signature = property.getSignature();
  59. return new String[] {texture, signature};
  60. }
  61. }
  62.  
  63. public static void addNPCPacket(EntityPlayer npc) {
  64. for (Player player : Bukkit.getOnlinePlayers()) {
  65. PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  66. connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
  67. connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
  68. connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
  69. }
  70. }
  71.  
  72. public static void addJoinPacket(Player player) {
  73. for (EntityPlayer npc : NPCs) {
  74. PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  75. connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
  76. connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
  77. connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
  78. }
  79. }
  80.  
  81. public static List<EntityPlayer> getNPCs() {
  82. return NPCs;
  83. }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement