Trying to hide the item in hand to other players with packet in 1.16.1 but having a problem

Discussion in 'Plugin Development' started by Koraizon, Oct 10, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Koraizon

    Hey, I'm trying to do a plugin for a game and I wanted to be able to hide the item in hand to other players.
    So I searched on internet what i could use to do that and i learned about packet.
    I understood packet like just sprite that you show but that aren't actually physical i think.
    So i did a bit of research on how i could change the items in hand to show just air with packet.
    After a while I thought i had done it but i have a bit of a problem.
    That's the code I'm using:

    Code:
    @EventHandler
        public void PlayerItem(PlayerItemHeldEvent e) {
            Player p = e.getPlayer();
         
            final List<com.mojang.datafixers.util.Pair<EnumItemSlot, ItemStack>> equipmentList = new ArrayList<>();
            equipmentList.add(new com.mojang.datafixers.util.Pair<>(EnumItemSlot.MAINHAND, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.AIR))));
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment(p.getEntityId(), equipmentList);
         
            for(Player player : Bukkit.getOnlinePlayers()) {
                if(!(p.equals(player))) {
                    PlayerConnection pc = ((CraftPlayer)player).getHandle().playerConnection;
                    pc.sendPacket(packet);
                }
            }
        }

    So basically, when the player change the item he hold, his main hand should change to air to everyone except from him but when i use it the item became air but come back right after so it don't work and I don't understand why...
    I already tried to use a RunTaskLater or make a Priority for the event because i thought that maybe it put the item after the PlayerItemHeldEvent but it didn't work either
     
  2. Offline

    Mathias Eklund

    Player Info packets are sent very often so you would have to resend the packet to the applicable players a lot to make it stay as the item you want them to see, otherwise they'll get the actual real info.
     
  3. Offline

    Koraizon

    But when i use it in the opposite way, i mean when i hide it to the player and show it to the others it works and this is what i don't understand about it.
    When i change the
    Code:
    if(!(p.equals(player)))
    to
    Code:
    if((p.equals(player)))
    it disappear to the player and show it to the others like i would like to do but in the other way.
    Or maybe it's because the Player Info Packet to self is only used when the packet change and not as many time as the others Player Info Packet?
     
Thread Status:
Not open for further replies.

Share This Page