Advertisement
Guest User

NPC.java

a guest
Apr 6th, 2020
2,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. package me.danielthumaniel.NPCInvis;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.UUID;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.Location;
  10. import org.bukkit.craftbukkit.libs.org.apache.commons.codec.binary.Base64;
  11. import org.bukkit.craftbukkit.v1_15_R1.CraftServer;
  12. import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
  13. import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
  14. import org.bukkit.entity.Player;
  15. import org.bukkit.event.EventHandler;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.player.PlayerJoinEvent;
  18.  
  19. import com.mojang.authlib.GameProfile;
  20. import com.mojang.authlib.properties.Property;
  21. import com.mojang.authlib.properties.PropertyMap;
  22.  
  23. import net.minecraft.server.v1_15_R1.EntityPlayer;
  24. import net.minecraft.server.v1_15_R1.MinecraftServer;
  25. import net.minecraft.server.v1_15_R1.PacketPlayOutEntityDestroy;
  26. import net.minecraft.server.v1_15_R1.PacketPlayOutEntityHeadRotation;
  27. import net.minecraft.server.v1_15_R1.PacketPlayOutNamedEntitySpawn;
  28. import net.minecraft.server.v1_15_R1.PacketPlayOutPlayerInfo;
  29. import net.minecraft.server.v1_15_R1.PlayerConnection;
  30. import net.minecraft.server.v1_15_R1.PlayerInteractManager;
  31. import net.minecraft.server.v1_15_R1.WorldServer;
  32.  
  33. public class NPC implements Listener {
  34.  
  35.     public static List<EntityPlayer> NPC = new ArrayList<EntityPlayer>();
  36.     public static Map<UUID, EntityPlayer> npcPlayers = new HashMap<UUID, EntityPlayer>();
  37.     public static Map<EntityPlayer, Integer> destroyable = new HashMap<EntityPlayer, Integer>();
  38.    
  39.     public static String texture = "";
  40.    
  41.     public static void createNPC(Player player) {
  42.         MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
  43.         WorldServer world = ((CraftWorld) Bukkit.getWorld(player.getWorld().getName())).getHandle();
  44.         UUID uuid = UUID.randomUUID();
  45.         GameProfile gameProfile = new GameProfile(uuid, "me");
  46.         EntityPlayer npc = new EntityPlayer(server, world, gameProfile, new PlayerInteractManager(world));
  47.         Location loc = player.getLocation();
  48.         npc.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
  49.        
  50.         // Skins start
  51.             // get npc property map and grab it's textures
  52.         PropertyMap npcMap = gameProfile.getProperties();
  53.         Property npcProperty = npcMap.get("textures").iterator().next();
  54.             // get player's property map and grab it's textures
  55.         GameProfile playerProfile = ((CraftPlayer) player).getHandle().getProfile();
  56.         PropertyMap playerMap = playerProfile.getProperties();
  57.         Property playerProperty = playerMap.get("textures").iterator().next();
  58.             // decode the texture value
  59.         String base64 = playerProperty.getValue();
  60.         String decoded = new String(Base64.decodeBase64(base64));
  61.        
  62.         texture = decoded;
  63.        
  64.         String textureValue;
  65.         String textureSignature;
  66.        
  67.             // remove npc's textures and apply the player's textures to it
  68.         npcMap.remove("textures", npcProperty);
  69.         npcMap.put("textures", new Property("textures", textureValue, textureSignature));
  70.        
  71.         // Skins end
  72.        
  73.         addNPCPacket(npc);
  74.         NPC.add(npc);
  75.         npcPlayers.put(player.getUniqueId(), npc);
  76.         destroyable.put(npc, npc.getId());
  77.     }
  78.    
  79.     public static void addNPCPacket(EntityPlayer npc) {
  80.         for (Player player : Bukkit.getOnlinePlayers()) {
  81.             PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  82.             connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
  83.             connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
  84.             connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
  85.         }
  86.     }
  87.    
  88.     public static void addJoinPacket(Player player) {
  89.         for (EntityPlayer npc : NPC) {
  90.             PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  91.             connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
  92.             connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
  93.             connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
  94.         }
  95.     }
  96.    
  97.     public static void destroyNPCPacket(Player player) {
  98.         EntityPlayer npc = (EntityPlayer) npcPlayers.get(player.getUniqueId());
  99.         PacketPlayOutEntityDestroy removeEntity = new PacketPlayOutEntityDestroy(new int[]{destroyable.get(npc)});
  100.  
  101.         for (Player p : Bukkit.getOnlinePlayers()) {
  102.             PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
  103.             connection.sendPacket(removeEntity);
  104.         }
  105.         NPC.remove(npc);
  106.         npcPlayers.remove(player.getUniqueId());
  107.     }
  108.  
  109.     public static void removeTabList(Player player) {
  110.         EntityPlayer npc = (EntityPlayer) npcPlayers.get(player.getUniqueId());
  111.         PacketPlayOutPlayerInfo removeTabList = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc);
  112.         for (Player p : Bukkit.getOnlinePlayers()) {
  113.             PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
  114.             connection.sendPacket(removeTabList);
  115.         }
  116.     }
  117.    
  118.    
  119.     @EventHandler
  120.     public static void onJoin(PlayerJoinEvent event) {
  121.         if (NPC == null)
  122.             return;
  123.         if (NPC.isEmpty())
  124.             return;
  125.         addJoinPacket(event.getPlayer());
  126.     }
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement