Advertisement
mrkirby153

Untitled

Mar 13th, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package me.mrkirby153.polaris.npc;
  2.  
  3. import com.mojang.authlib.GameProfile;
  4. import com.mojang.authlib.properties.Property;
  5. import me.mrkirby153.polaris.core.Core;
  6. import net.minecraft.server.v1_8_R3.*;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  10. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.plugin.Plugin;
  13.  
  14. import java.util.*;
  15. import java.util.function.Function;
  16. import java.util.stream.Stream;
  17.  
  18. public class HumanNPC extends EntityPlayer {
  19.  
  20.     private List<Packet> spawnPackets;
  21.     private Packet despawnPacket;
  22.     private Set<UUID> tracked = new HashSet<>();
  23.     private GameProfile profile;
  24.  
  25.  
  26.     private HumanNPC(WorldServer server, GameProfile profile, PlayerInteractManager manager) {
  27.         super(MinecraftServer.getServer(), server, profile, manager);
  28.     }
  29.  
  30.     @Override
  31.     public IChatBaseComponent getPlayerListName() {
  32.         return null;
  33.     }
  34.  
  35.     private static final Function<HumanNPC, List<Packet>> getSpawnPackets = npc -> {
  36.         if (npc.spawnPackets == null) {
  37.             Packet[] packets = new Packet[]{
  38.                     new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc),
  39.                     new PacketPlayOutNamedEntitySpawn(npc),
  40.                     new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc)
  41.             };
  42.             npc.spawnPackets = Arrays.asList(packets);
  43.         }
  44.         return npc.spawnPackets;
  45.     };
  46.  
  47.     //TODO: Figure out why only valid uuids and stuff can be used when in online-mode
  48.     public static HumanNPC createNpc(Plugin plugin, UUID uuid, String name, Location location) {
  49.         if (uuid.version() == 4) { // clear version
  50.             long msb = uuid.getMostSignificantBits();
  51.             msb &= ~0x0000000000004000L;
  52.             msb |= 0x0000000000002000L;
  53.             uuid = new UUID(msb, uuid.getLeastSignificantBits());
  54.         }
  55.         GameProfile profile = new GameProfile(uuid, name);
  56.         WorldServer wS = ((CraftWorld) location.getWorld()).getHandle();
  57.         HumanNPC npc = new HumanNPC(wS, profile, new PlayerInteractManager(wS));
  58.         npc.profile = profile;
  59.         npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getYaw());
  60.         // Skin lookup
  61.         ProfileFetcher.fetchProfile(name, (data, ex) -> npc.setSkin(data));
  62.         return npc;
  63.     }
  64.  
  65.     public static HumanNPC createNpc(Plugin plugin, String name, Location location) {
  66.         return createNpc(plugin, UUID.randomUUID(), name, location);
  67.     }
  68.  
  69.     public void spawn(Player player) {
  70.         Bukkit.getScheduler().runTaskLater(Core.plugin(), () -> {
  71.             PlayerConnection pc = ((CraftPlayer) player).getHandle().playerConnection;
  72.             getSpawnPackets.apply(this).stream().forEach(pc::sendPacket);
  73.             this.tracked.add(player.getUniqueId());
  74.         }, 1L);
  75.     }
  76.  
  77.     public void setSkin(Property skinProperty) {
  78.         if (skinProperty == null) {
  79.             System.out.println("Null!");
  80.             return;
  81.         }
  82.         this.profile.getProperties().removeAll("textures");
  83.         this.profile.getProperties().put("textures", skinProperty);
  84.         getTrackedPlayers().forEach(this::respawn);
  85.     }
  86.  
  87.     public void respawn(Player player) {
  88.         System.out.println("Respawning for " + player.getName());
  89.         despawn(player, false);
  90.         spawn(player);
  91.     }
  92.  
  93.     protected Stream<Player> getTrackedPlayers() {
  94.         return tracked.stream().map(Bukkit::getPlayer).filter(player -> player != null);
  95.     }
  96.  
  97.     public void despawn(Player player) {
  98.         despawn(player, false);
  99.     }
  100.  
  101.     public void despawn(Player player, boolean logout) {
  102.         if (!logout) {
  103.             cleanup(player);
  104.         }
  105.     }
  106.  
  107.     protected void cleanup(Player player) {
  108.         PlayerConnection pc = ((CraftPlayer) player).getHandle().playerConnection;
  109.         if (despawnPacket == null)
  110.             despawnPacket = new PacketPlayOutEntityDestroy(getId());
  111.         pc.sendPacket(despawnPacket);
  112.     }
  113.  
  114.     protected double getDistanceSquared(Player player) {
  115.         Location loc = player.getLocation();
  116.         double dx = loc.getX() - this.locX;
  117.         double dy = loc.getY() - this.locY;
  118.         double dz = loc.getZ() - this.locZ;
  119.         return dx * dx + dy * dy + dz * dz;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement