PacketPlayOutCamera weird things

Discussion in 'Spigot Plugin Development' started by theEvilReaper, May 15, 2016.

  1. Hey Guys,

    every time I send a packet via reflection there is a strange result. The perspective is kind of different towards vanilla spectatormode.

    To show you what I really mean, please take a look at the video.



    Has anyone an idea to solve the problem?

    I´m using the latest spigot 1.8.8 build
     
  2. Please provide your code
     
  3. Hey,

    the code I use for that:
    Code (Text):
    private static Class<?> camera = getNMSClass("PacketPlayOutCamera");
     
        public void setCamera(Player p, int entityID) {
            try {
                Object packet = camera.newInstance();
                Field field = packet.getClass().getDeclaredField("a");
                field.setAccessible(true);
                field.set(packet, entityID);
                sendPacket(p, packet);
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
        public void setCamera(Player p, Entity ent) {
            setCamera(p, ent.getEntityId());
        }
     
        public void removeCamera(Player p) {
            setCamera(p, p.getEntityId());
        }

       static void sendPacket(Player p, Object packet) {
            try {
                Object nmsPlayer = getNMSPlayer(p);
                Object connection = nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer);
                connection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(connection, packet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        static Class<?> getNMSClass(String className) {
            String fullName = "net.minecraft.server." + getVersion() + className;
            Class<?> clazz = null;
            try {
                clazz = Class.forName(fullName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return clazz;
        }
     
        public static Object getNMSPlayer(Player p) {
            try {
                return p.getClass().getMethod("getHandle").invoke(p);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    Code (Text):
    public void setView(Player p, Player rightClicked) {
            Spectator spec = spectatorManager.getProfiles().get(p);
            spec.setSpectating(rightClicked.getEntityId());
        }
       
        @EventHandler
        public void on(PlayerInteractAtEntityEvent e) {
            if (e.getRightClicked() instanceof Player && spectatorManager.getPlayersAsKeySet().contains(e.getPlayer()) && spectatorManager.isInGamePhase()) {
                Player player = e.getPlayer();
                Player rightClicked = (Player) e.getRightClicked();
                if (!spectatorManager.getPlayersAsKeySet().contains(rightClicked)) {
                    this.setView(player, rightClicked);
                }
            }
        }

    I store the player and the EntityID in a HashMap that works already fine.
     
  4. Have you tried setting the players gamemode to spectator? The client might require to be in spectator mode to be able to use that feature