Solved NMS 1.16.5 - Send fake armor packet

Discussion in 'Plugin Development' started by byteswing, Dec 27, 2023.

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

    byteswing

    hi everyone, in this code I am trying to send a packet that will display a leather helmet on the player's head.
    but after logging on to the server, only the diamond helmet is visible on the head (I thought that before displaying the helmet on the head, you first need the player to already have a helmet)


    how can I make the leather armor display?

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    handHead(e.getPlayer());
    }

    public void handHead(Player p) {
    CraftPlayer player = ((CraftPlayer) p);
    player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    List<Pair<EnumItemSlot, net.minecraft.server.v1_16_R3.ItemStack>> list = new ArrayList();
    list.add(new Pair<>(EnumItemSlot.HEAD, CraftItemStack.asNMSCopy(new ItemStack(Material.LEATHER_HELMET))));
    PacketPlayOutEntityEquipment equipment = new PacketPlayOutEntityEquipment(player.getEntityId(),list);
    for (Player l : Bukkit.getOnlinePlayers()) {
    CraftPlayer cp = (CraftPlayer) l;
    cp.getHandle().playerConnection.sendPacket(equipment);
    }
    }

    Code:
    importcom.comphenix.protocol.PacketType;
    importcom.comphenix.protocol.ProtocolLibrary;
    importcom.comphenix.protocol.ProtocolManager;
    importcom.comphenix.protocol.events.ListenerPriority;
    importcom.comphenix.protocol.events.PacketAdapter;
    importcom.comphenix.protocol.events.PacketContainer;
    importcom.comphenix.protocol.events.PacketEvent;
    importcom.comphenix.protocol.wrappers.EnumWrappers;
    importcom.comphenix.protocol.wrappers.EnumWrappers.ItemSlot;
    importcom.comphenix.protocol.wrappers.Pair;
    importorg.bukkit.Bukkit;
    importorg.bukkit.Material;
    importorg.bukkit.entity.Player;
    importorg.bukkit.event.Listener;
    importorg.bukkit.inventory.ItemStack;
    importorg.bukkit.plugin.java.JavaPlugin;
    
    importjava.util.ArrayList;
    importjava.util.List;
    
    publicfinalclass TickPacket extends JavaPlugin implements Listener {
    
       private ProtocolManager protocolManager;// Declaration of ProtocolManager variable
    
        @Override
       publicvoid onEnable(){
            protocolManager = ProtocolLibrary.getProtocolManager();// Initialize ProtocolManager
            registerPacketListener();// Call the method to register packet listener
       }
    
        @Override
       publicvoid onDisable(){
            protocolManager.removePacketListeners(this);// Remove packet listeners when the plugin is disabled
       }
    
       // Method to register a packet listener
       privatevoid registerPacketListener(){
            protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.ENTITY_EQUIPMENT){
                @Override
               publicvoid onPacketSending(PacketEvent event){// Method called when a packet is being sent
                    PacketContainer packet = event.getPacket();// Get the packet being sent
                    Player player = event.getPlayer();// Get the player associated with the event
    
                   if(packet.getType()== PacketType.Play.Server.ENTITY_EQUIPMENT){// Check if the packet type is ENTITY_EQUIPMENT
                        PacketContainer newItemPacket = packet.deepClone();// Create a deep copy of the original packet
                        List<Pair<EnumWrappers.ItemSlot, ItemStack>> list =new ArrayList<>();// Create a list to hold item slot and item stack pairs
    
                       // Add leather armor to the list for different equipment slots
                        list.add(new Pair<>(ItemSlot.HEAD, new ItemStack(Material.LEATHER_HELMET)));
                        list.add(new Pair<>(ItemSlot.CHEST, new ItemStack(Material.LEATHER_CHESTPLATE)));
                        list.add(new Pair<>(ItemSlot.LEGS, new ItemStack(Material.LEATHER_LEGGINGS)));
                        list.add(new Pair<>(ItemSlot.FEET, new ItemStack(Material.LEATHER_BOOTS)));
    
                       for(Player p : Bukkit.getOnlinePlayers()){// Iterate through all online players
                           if(player.getUniqueId()== p.getUniqueId())continue;// Skip the current player
                           // Add the items from the main hand and offhand of other players to the list
                            list.add(new Pair<>(ItemSlot.MAINHAND, p.getInventory().getItemInMainHand()));
                            list.add(new Pair<>(ItemSlot.OFFHAND, p.getInventory().getItemInOffHand()));
                            newItemPacket.getIntegers().write(0, p.getEntityId());// Set the entity ID for the new packet
                            newItemPacket.getSlotStackPairLists().write(0, list);// Set the slot stack pairs list for the new packet
                            event.setPacket(newItemPacket);// Update the packet being sent to the new modified packet
                       }
                   }
               }
           });
       }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 27, 2023
Thread Status:
Not open for further replies.

Share This Page