FakePlayer spawn error

Discussion in 'Spigot Plugin Development' started by Simagdo, Aug 22, 2017.

  1. Hey there,

    i want to spawn a fake player but i always get this error:

    upload_2017-8-22_20-27-2.png

    here is my FakePlayer class:
    Code (Text):

    public class FakePlayer extends Reflections {

        int entityID;
        Location location;
        GameProfile gameProfile;
        String name;
       
        public FakePlayer(Location location, String name) {
            entityID = (int) Math.ceil(Math.random() * 1000) + 2000;
            this.location = location.clone();
            gameProfile = new GameProfile(UUID.randomUUID(), name);
        }

        public void changeSkin() {
            String value = "eyJ0aW1lc3RhbXAiOjE0NTIwODMxODcyOTUsInByb2ZpbGVJZCI6IjFiZGMzZGE1YmFiMDQyZGRiYWRjMDQ0N2E4NTE0NjA3IiwicHJvZmlsZU5hbWUiOiJLeW94ZSIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2YxODkzODFhZDU3ODRlNDAyNDkwN2Q4NmUxNDYyOTQzNzY1N2FhMjFkNmJlMTM0ODVjZTc5YWQ3YjdiYWMxIn19fQ==";
            String signature = "oI8zfgDs1SKgxzWUUaw4cuBcn95K40m9nB5qUYt18laHa2t03WUm6e2Xo0PZC0kI3/XNUEp7aZTGonhuhthZB/Jg1/s8E8e7IABJ/wz2vuSdzIs1AniD91NrapXuWT4Gipj648+fZ8kU9JQ+SOyv99kh3TEF0h/HO1y7TGldMfIEJt3JhixxvL8BYQS4GjQ0/bC8Locy1ClAgKFGWphrbKbkNt3w4g2xIK2tFkS9CwzaVQ9x0lXLG5eJgTBodLK/KLnd5ItNpQF1KigplQx3zqZ91ejwtaegGeDQb1oA8AXkdySUvaHiZogmJ3WxaKlCzHDYpnTZ4Qt3pIfeQNN/D6+ut5f18+uWf+f/Uql3aWsl6YgwmCn09AMqJBjhD56O74h9y3ZJhwvA9Vh1qCFYwwL3U/wub/HEbfaMfQUR7n875CdM6qCVDlPKxTKvFC10S2ZMvnG74+AxrDADuw8zWuWquKO7sSVNIqBeG4483VZbxzM5UGj9+9oW6lagFkOGgW314wUylM5VAiYY/QoetglqIUbrYyf6h5pxEG0d5j/erFmf4v3G1Fq0aNTo71OBEkNGmsbtKJ6xEeuycIuBPmDuYFhgA+uUcVZmG6Bdyv2mFe3qMph/ujovBUaqCiVG2sQ05spIPS20o3/vzTWFxVzmCs97kZ+DiQjEBNukUhg=";
            gameProfile.getProperties().put("textures", new Property("textures", value, signature));
        }
       
        public void spawnFakePlayer(Player player, Location location) {
            MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer world = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
            EntityPlayer npc = new EntityPlayer(server, world, new GameProfile(player.getUniqueId(), this.name), new PlayerInteractManager(world));
            npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());

            for (Player p : Bukkit.getOnlinePlayers()) {
                PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
                connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
                connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
            }
        }

        public void teleport(Location location) {
            PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
            setValue(packet, "a", entityID);
            setValue(packet, "b", getFixLocation(location.getX()));
            setValue(packet, "c", getFixLocation(location.getY()));
            setValue(packet, "d", getFixLocation(location.getZ()));
            setValue(packet, "e", getFixRotation(location.getYaw()));
            setValue(packet, "f", getFixRotation(location.getPitch()));

            sendPacket(packet);
            headRotation(location.getYaw(), location.getPitch());
            this.location = location.clone();
        }

        public void headRotation(float yaw, float pitch) {
            PacketPlayOutEntity.PacketPlayOutEntityLook packet = new PacketPlayOutEntity.PacketPlayOutEntityLook(entityID, getFixRotation(yaw), getFixRotation(pitch), true);
            PacketPlayOutEntityHeadRotation packetHead = new PacketPlayOutEntityHeadRotation();
            setValue(packetHead, "a", entityID);
            setValue(packetHead, "b", getFixRotation(yaw));

            sendPacket(packet);
            sendPacket(packetHead);
        }

        public int getFixLocation(double pos) {
            return (int) MathHelper.floor(pos * 32.0D);
        }

        public byte getFixRotation(float yawpitch) {
            return (byte) ((int) (yawpitch * 256.0F / 360.0F));
        }

    }
     
    Reflections class:
    Code (Text):

    public class Reflections {

        public void setValue(Object obj, String name, Object value) {
            try {
                Field field = obj.getClass().getDeclaredField(name);
                field.setAccessible(true);
                field.set(obj, value);
            } catch (Exception e) {
            }
        }

        public Object getValue(Object obj, String name) {
            try {
                Field field = obj.getClass().getDeclaredField(name);
                field.setAccessible(true);
                return field.get(obj);
            } catch (Exception e) {
            }
            return null;
        }

        public void sendPacket(Packet<?> packet, Player player) {
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
        }

        public void sendPacket(Packet<?> packet) {
            for (Player player : Bukkit.getOnlinePlayers()) sendPacket(packet, player);
        }
    }
     
    I am using Intellij idea and i have set the compiler to eclipse.
     
  2. Mas

    Mas

    Not sure exatly what your issue is, but as a sidenote you should be caching all your reflection fields/methods in your Reflection class, it's quite intensive to fetch them every time, especially if done regularly.
     
  3. How can i do it?
     
  4. Have a map in your reflections class, everytime the getvalue method is called, check if they object is a key in the map, if so, return the value, otherwise do the reflection and then add it to the map before returning it.
     
  5. Ok, but how can I fix the error?
     
  6. Can nobody help me?
     
  7. Here:
    setValue(packet, "b", getFixLocation(location.getX()));
    setValue(packet, "c", getFixLocation(location.getY()));
    setValue(packet, "d", getFixLocation(location.getZ()));
    setValue(packet, "e", getFixRotation(location.getYaw()));
    setValue(packet, "f", getFixRotation(location.getPitch()));
    The "original" field is an integer, not a byte what return your method
     
  8. You've created a custom GameProfile in your FakePlayer, why don't you use it ?
     
  9. I am only using this for the teleport method, which I am currently not using.

    For what can i use the GameProfile for, other than applying the skin?
     
  10. I mean, why are you using
    Code (Java):
    EntityPlayer npc = new EntityPlayer(server, world, new GameProfile(player.getUniqueId(), this.name), new PlayerInteractManager(world));
    instead of
    Code (Java):
    EntityPlayer npc = new EntityPlayer(server, world, this.gameProfile, new PlayerInteractManager(world));