Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mrkirby153.polaris.npc;
- import com.mojang.authlib.GameProfile;
- import com.mojang.authlib.properties.Property;
- import me.mrkirby153.polaris.core.Core;
- import net.minecraft.server.v1_8_R3.*;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
- import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.Plugin;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Stream;
- public class HumanNPC extends EntityPlayer {
- private List<Packet> spawnPackets;
- private Packet despawnPacket;
- private Set<UUID> tracked = new HashSet<>();
- private GameProfile profile;
- private HumanNPC(WorldServer server, GameProfile profile, PlayerInteractManager manager) {
- super(MinecraftServer.getServer(), server, profile, manager);
- }
- @Override
- public IChatBaseComponent getPlayerListName() {
- return null;
- }
- private static final Function<HumanNPC, List<Packet>> getSpawnPackets = npc -> {
- if (npc.spawnPackets == null) {
- Packet[] packets = new Packet[]{
- new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc),
- new PacketPlayOutNamedEntitySpawn(npc),
- new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc)
- };
- npc.spawnPackets = Arrays.asList(packets);
- }
- return npc.spawnPackets;
- };
- //TODO: Figure out why only valid uuids and stuff can be used when in online-mode
- public static HumanNPC createNpc(Plugin plugin, UUID uuid, String name, Location location) {
- if (uuid.version() == 4) { // clear version
- long msb = uuid.getMostSignificantBits();
- msb &= ~0x0000000000004000L;
- msb |= 0x0000000000002000L;
- uuid = new UUID(msb, uuid.getLeastSignificantBits());
- }
- GameProfile profile = new GameProfile(uuid, name);
- WorldServer wS = ((CraftWorld) location.getWorld()).getHandle();
- HumanNPC npc = new HumanNPC(wS, profile, new PlayerInteractManager(wS));
- npc.profile = profile;
- npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getYaw());
- // Skin lookup
- ProfileFetcher.fetchProfile(name, (data, ex) -> npc.setSkin(data));
- return npc;
- }
- public static HumanNPC createNpc(Plugin plugin, String name, Location location) {
- return createNpc(plugin, UUID.randomUUID(), name, location);
- }
- public void spawn(Player player) {
- Bukkit.getScheduler().runTaskLater(Core.plugin(), () -> {
- PlayerConnection pc = ((CraftPlayer) player).getHandle().playerConnection;
- getSpawnPackets.apply(this).stream().forEach(pc::sendPacket);
- this.tracked.add(player.getUniqueId());
- }, 1L);
- }
- public void setSkin(Property skinProperty) {
- if (skinProperty == null) {
- System.out.println("Null!");
- return;
- }
- this.profile.getProperties().removeAll("textures");
- this.profile.getProperties().put("textures", skinProperty);
- getTrackedPlayers().forEach(this::respawn);
- }
- public void respawn(Player player) {
- System.out.println("Respawning for " + player.getName());
- despawn(player, false);
- spawn(player);
- }
- protected Stream<Player> getTrackedPlayers() {
- return tracked.stream().map(Bukkit::getPlayer).filter(player -> player != null);
- }
- public void despawn(Player player) {
- despawn(player, false);
- }
- public void despawn(Player player, boolean logout) {
- if (!logout) {
- cleanup(player);
- }
- }
- protected void cleanup(Player player) {
- PlayerConnection pc = ((CraftPlayer) player).getHandle().playerConnection;
- if (despawnPacket == null)
- despawnPacket = new PacketPlayOutEntityDestroy(getId());
- pc.sendPacket(despawnPacket);
- }
- protected double getDistanceSquared(Player player) {
- Location loc = player.getLocation();
- double dx = loc.getX() - this.locX;
- double dy = loc.getY() - this.locY;
- double dz = loc.getZ() - this.locZ;
- return dx * dx + dy * dy + dz * dz;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement