1.13.2 Plugin Development: ProtocolLib usage crashing player

Discussion in 'Spigot Plugin Development' started by Jdsgames, Jan 20, 2020.

  1. Code (Java):

    import java.awt.Color;
    import java.lang.reflect.InvocationTargetException;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;

    import com.comphenix.protocol.PacketType;
    import com.comphenix.protocol.ProtocolLibrary;
    import com.comphenix.protocol.ProtocolManager;
    import com.comphenix.protocol.events.PacketContainer;
    import com.comphenix.protocol.wrappers.EnumWrappers;

    import dev.jdsgames.trajectorypads.TrajectoryPads;
    import io.netty.util.internal.ThreadLocalRandom;

    public class SpawnParticlesRunnable implements Runnable
    {
        // Current Plugin
        private TrajectoryPads trajectoryPads;
        // Protocol Manager
        private ProtocolManager pm;
        private PacketContainer packet;
     
        // Current Player Effected
        private Player player;
        // Current Iteration
        private int currentIteration;
        // Max Iterations
        private int numberOfIterations;
        // Task ID
        private int taskID;
     
        public SpawnParticlesRunnable(TrajectoryPads trajectoryPads, Player player, int numberOfIterations)
        {
            this.trajectoryPads = trajectoryPads;
            this.player = player;
            this.currentIteration = 0;
            this.numberOfIterations = numberOfIterations;
         
            taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(trajectoryPads, this , 5L, 5L);
        }
     
        @Override
        public void run()
        {
            Bukkit.getServer().broadcastMessage("Current Particle: " + (currentIteration+1));
         
            // Define a R G B setup
            int red = ThreadLocalRandom.current().nextInt(200,255);
            int green = ThreadLocalRandom.current().nextInt(200,255);
            int blue = ThreadLocalRandom.current().nextInt(240,255);
         
            // Define Location to Spawn Particles
            Location loc = player.getLocation();
         
            // Default XYZ
            double x = loc.getX();
            double y = loc.getY();
            double z = loc.getZ();
         
            // Offset XYZ
            double xOff = ThreadLocalRandom.current().nextDouble(0.0,1.5);
            double yOff = ThreadLocalRandom.current().nextDouble(0.0,1.5);
            double zOff = ThreadLocalRandom.current().nextDouble(0.0,1.5);
         
            // Swap Positive and Negative of Offsets
            if(ThreadLocalRandom.current().nextInt(2)==0)
                xOff *= -1;
            if(ThreadLocalRandom.current().nextInt(2)==0)
                yOff *= -1;
            if(ThreadLocalRandom.current().nextInt(2)==0)
                zOff *= -1;
         
            // Define a Color
            Color color = new Color(red,green,blue);
         
            pm = ProtocolLibrary.getProtocolManager();
            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    packet = pm.createPacket(PacketType.Play.Server.WORLD_PARTICLES);
                 
                    packet.getModifier().writeDefaults();
                 
                    packet.getParticles().writeSafely(0, EnumWrappers.Particle.REDSTONE);
                 
                    packet.getFloat().writeSafely(0, (float) x);
                    packet.getFloat().writeSafely(1, (float) y);
                    packet.getFloat().writeSafely(2, (float) z);
                    packet.getFloat().writeSafely(3, (float) (red/255));
                    packet.getFloat().writeSafely(4, (float) (green/255));
                    packet.getFloat().writeSafely(5, (float) (blue/255));
                    packet.getFloat().writeSafely(6, (float) 1F);
                 
                 
                    // Crashing Player:
                    // 19.01 17:35:24 [Server] ERROR java.lang.NullPointerException
                    // 19.01 17:35:24 [Disconnect] User bail97 has disconnected, reason: Internal Exception: io.netty.handler.codec.EncoderException: java.lang.NullPointerException
                    if(packet != null)
                    {
                        try
                        {
                            pm.sendServerPacket(player, packet, true);
                        }
                        catch (InvocationTargetException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
             
            }.runTaskTimer(trajectoryPads, 20, 2);
         
         
            // Up Iteration Count
            currentIteration++;
         
            // Too Many Iterations Cancel
            if(currentIteration >= numberOfIterations)
            {
                Bukkit.getServer().broadcastMessage("TOO BAD YOU'RE CANCELLED");
                // Cancel this task
                Bukkit.getServer().getScheduler().cancelTask(taskID);
            }
        }
    }
     
    Ok for starters I have been doing java for awhile myself and just got back into using the Spigot API so I am trying to get back into things. At this point I do understand I am using a Runnable rather than a BukkitRunnable which the scheduler only wants Runnables anymore as BukkitRunnables are deprecated.

    The event itself seems to schedule fine and doesn't crash if I do not attempt to send the particle to the player. So it will print out in game Current Particle: 1 -> 50 then "TOO BAD YOU'RE CANCELLED"
    This setup was testing the usage of Runnable to make sure it worked in game.

    I have tested null cases on the Player, PM, and Packet and they all seem fine. However, it completely boots the character out randomly between the packets and nothing displays to the user.

    20.01 06:20:00 [Server] ERROR java.lang.NullPointerException
    20.01 06:20:00 [Server] WARN io.netty.handler.codec.EncoderException: java.lang.NullPointerException
    20.01 06:20:00 [Server] WARN at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:125)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelInjector$1.write(ChannelInjector.java:249)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:801)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831)
    20.01 06:20:00 [Server] WARN at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1071)
    20.01 06:20:00 [Server] WARN at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:300)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelProxy.writeAndFlush(ChannelProxy.java:327)
    20.01 06:20:00 [Server] WARN at net.minecraft.server.v1_13_R2.NetworkManager.lambda$4(NetworkManager.java:198)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelInjector$3.lambda$onMessageScheduled$1(ChannelInjector.java:321)
    20.01 06:20:00 [Server] WARN at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    20.01 06:20:00 [Server] WARN at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
    20.01 06:20:00 [Server] WARN at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:313)
    20.01 06:20:00 [Server] WARN at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    20.01 06:20:00 [Server] WARN at java.lang.Thread.run(Unknown Source)
    20.01 06:20:00 [Server] WARN Caused by: java.lang.NullPointerException
    20.01 06:20:00 [Server] WARN at net.minecraft.server.v1_13_R2.PacketPlayOutWorldParticles.b(SourceFile:66)
    20.01 06:20:00 [Server] WARN at net.minecraft.server.v1_13_R2.PacketEncoder.encode(SourceFile:46)
    20.01 06:20:00 [Server] WARN at net.minecraft.server.v1_13_R2.PacketEncoder.encode(SourceFile:15)
    20.01 06:20:00 [Server] WARN at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
    20.01 06:20:00 [Server] WARN at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    20.01 06:20:00 [Server] WARN at java.lang.reflect.Method.invoke(Unknown Source)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelInjector.encode(ChannelInjector.java:473)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelInjector.access$100(ChannelInjector.java:64)
    20.01 06:20:00 [Server] WARN at com.comphenix.protocol.injector.netty.ChannelInjector$1.encode(ChannelInjector.java:243)
    20.01 06:20:00 [Server] WARN at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:107)
    20.01 06:20:00 [Server] WARN ... 16 more
    20.01 06:20:01 [Disconnect] User JdsgamesYT has disconnected, reason: Internal Exception: io.netty.handler.codec.EncoderException: java.lang.NullPointerException

    This is where I am stuck as any reference to sending packets similar to what I am doing shows similar code.
     
    #1 Jdsgames, Jan 20, 2020
    Last edited: Jan 20, 2020
  2. Updated code now stuck with this exception on the null pointer.
     
  3. why are you using packets for particles, use one of the api methods

    upload_2020-1-20_9-13-52.png
     
  4. Honestly, was going off of various references and they all utilized what I showed above.
    Which now seems like a huge waste of time >.>

    Thank you @_Donut_
     
    #4 Jdsgames, Jan 20, 2020
    Last edited: Jan 20, 2020