Solved ProtocolLib Manger is null

Discussion in 'Spigot Plugin Development' started by G4mingJon4s, May 14, 2022.

  1. I have a method, wich sends a packet to the player, but when executing, i get the error, that ProtocolLibrary.getProtocolManager() is null. Code (var time and wallBlock are given):
    Code (Java):
        public void addActivator(JARPlayer jarPlayer) {
            activators.put(jarPlayer, time);

            int x = wallBlock.getX();
            int y = wallBlock.getY();
            int z = wallBlock.getZ();
            PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.WORLD_PARTICLES);

            packet.getBlockPositionModifier().write(0, new BlockPosition(x, y, z));
            packet.getBlockData().write(0, WrappedBlockData.createData(Material.LIME_WOOL));

            try {
                manager.sendServerPacket(jarPlayer.getPlayer(), packet);
            } catch (Exception exception){
                activators.remove(jarPlayer);
            }
        }
     
  2. You have to check if ProtocolLibrary is loaded first
     
  3. Why not just using Player#spawnParticle or World#spawnParticle? No need for Protocollib.
    spawnParticle

    Code (Java):

    public void blub(Player player, Location wallBlock) {

        int x = wallBlock.getBlockX();
        int y = wallBlock.getBlockY();
        int z = wallBlock.getBlockZ();

        player.spawnParticle(Particle.BLOCK_DUST, x, y, z, 1, Material.LIME_WOOL.createBlockData());
        player.spawnParticle(Particle.BLOCK_MARKER, x, y, z, 1, Material.LIME_WOOL.createBlockData());
        player.spawnParticle(Particle.BLOCK_CRACK, x, y, z, 1, Material.LIME_WOOL.createBlockData());
    }
     
     
  4. Usually in cases like this you have accidentally shaded ProtocolLib into your jar. If you're using Maven, make sure it has provided scope, not compile.
     
  5. You need to add ProtocolLib as dependency in plugin.yml
     
  6. This fixed it! Thanks!
    When getting the maven dependency lines in the protocol github, <scope> is not included...