1.16.5 Restoring player's game state without killing/relogging

Discussion in 'Spigot Plugin Development' started by i998979, Jul 6, 2021.

  1. Hello. So recently I'm working on something related to the packet GameStateChange via ProtocolLib.
    What I am doing right now is:
    - Change player's game state (technically gamemode)
    - send the packet above
    - Create a packet that makes the player spectate to a newly spawned entity(villager)
    - send the packet above

    What I'm now struggling with is how to restore player's game state (gamemode) without killing them / asking them to relogin?
    I know killing/reloggins do the trick but that's not what I wanted.

    So anyone got any idea? Thank you so much.

    If you are wondering the code is here. (It looks messy i know...)
    Code (Java):
                // Change gamemode
                int reason = 3;

                // Gamemode value input check
                float value;
                try {
                    value = Float.parseFloat(args[1]);
                } catch (Exception e) {
                    player.sendMessage("§cYou have to enter which gamemode you want to change.");
                    return false;
                }

                //////////////////////////////
                // Game State Change packet //
                //////////////////////////////

                // Initialize packet container
                PacketContainer packetServerGameStateChange = new PacketContainer(PacketType.Play.Server.GAME_STATE_CHANGE);

                // Change game state (unsigned bytes)
                packetServerGameStateChange.getSpecificModifier(PacketPlayOutGameStateChange.a.class)
                        .write(0, new PacketPlayOutGameStateChange.a(reason));

                // Change values
                packetServerGameStateChange.getFloat().write(0, value);

                // Send packet
                try {
                    ProtocolLibrary.getProtocolManager().sendServerPacket(player, packetServerGameStateChange);
                    player.sendMessage("§aPacket packetServerGameStateChange sent.");
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }


                ////////////////////
                // Camera Packet //
                ///////////////////

                // Spawn spectate target
                // Location spawn = new Location(player.getWorld(), 105, 4, 208);
                Location spawn = player.getLocation();
                LivingEntity dummy0 = dummy.get(uuid);
                if (dummy0 == null) {
                    dummy0 = (LivingEntity) player.getWorld().spawnEntity(spawn, EntityType.VILLAGER);
                    dummy0.addPotionEffect(new PotionEffect(
                            PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false, false));
                    dummy.put(uuid, dummy0);
                }

                // Initialize packet container
                PacketContainer packetServerCamera = new PacketContainer(PacketType.Play.Server.CAMERA);

                // Change camera target
                packetServerCamera.getIntegers().write(0, dummy0.getEntityId());

                // Send packet
                try {
                    ProtocolLibrary.getProtocolManager().sendServerPacket(player, packetServerCamera);
                    player.sendMessage("§aPacket packetServerCamera sent.");
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
     
    • Funny Funny x 1