Using a protocollib event?

Discussion in 'Spigot Plugin Development' started by Kyllian, May 26, 2017.

  1. Hey,

    I'm trying to use a protocollib event, I registered it. But when I use @EventHandler tag
    I get this error:

    [14:24:42 ERROR]: [Timeout] Timeout v0.01 attempted to register an invalid EventHandler method signature "public void me.kyllian.timeout.listeners.OnPacketEvent.onPacketEvent(com.comphenix.protocol.events.PacketEvent)" in class me.kyllian.timeout.listeners.OnPacketEvent

    So, What's the way to register this event?
     
  2. Alright thanks, I now have

    Code (Text):
    package me.kyllian.timeout;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.comphenix.protocol.ProtocolLibrary;
    import com.comphenix.protocol.ProtocolManager;
    import com.comphenix.protocol.events.PacketAdapter;
    import com.comphenix.protocol.events.PacketEvent;

    import me.kyllian.timeout.commands.CMD_Timeout;
    import me.kyllian.timeout.listeners.OnPacketEvent;

    public class Timeout extends JavaPlugin {

        public static Timeout main;
        private ProtocolManager protocolManager;

        public static Timeout getInstance() {
            return main;
        }

        public ArrayList<Player> inTimeout = new ArrayList<Player>();

        public void onLoad() {
            protocolManager = ProtocolLibrary.getProtocolManager();
        }

        public void onEnable() {
            if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
                System.out.println("Timeout > You need ProtocolLib in order to use this plugin");
                Bukkit.getPluginManager().disablePlugin((Plugin) this);
                return;
            }
            main = this;

            getCommand("timeout").setExecutor(new CMD_Timeout());
            Bukkit.getPluginManager().registerEvents(new OnPacketEvent(), this);

            protocolManager.addPacketListener(new PacketAdapter(null) {
                @Override
                public void onPacketSending(PacketEvent e) {
                    new Thread(() -> {
                        for (Player player : inTimeout) {
                            if (e.getPlayer().equals(player))
                                e.setCancelled(true);
                        }
                    }).start();
                }
            });
        }
    }
     
    That should be able to listen to all packets?
     
  3. I now have this:

    Code (Text):
    package me.kyllian.timeout;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.comphenix.protocol.PacketType;
    import com.comphenix.protocol.ProtocolLibrary;
    import com.comphenix.protocol.ProtocolManager;
    import com.comphenix.protocol.events.PacketAdapter;
    import com.comphenix.protocol.events.PacketEvent;

    import me.kyllian.timeout.commands.CMD_Timeout;

    public class Timeout extends JavaPlugin {

        public static Timeout main;
        private ProtocolManager protocolManager;

        public static Timeout getInstance() {
            return main;
        }

        public ArrayList<Player> inTimeout = new ArrayList<Player>();

        @Override
        public void onLoad() {
            protocolManager = ProtocolLibrary.getProtocolManager();
        }
       
        @Override
        public void onEnable() {
            if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
                System.out.println("Timeout > You need ProtocolLib in order to use this plugin");
                Bukkit.getPluginManager().disablePlugin((Plugin) this);
                return;
            }
            main = this;

            getCommand("timeout").setExecutor(new CMD_Timeout());

            protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Client.KEEP_ALIVE) {
                @Override
                public void onPacketSending(PacketEvent e) {
                    new Thread(() -> {
                        for (Player player : inTimeout) {
                            if (e.getPlayer().equals(player))
                                e.setCancelled(true);
                        }
                    }).start();
                }
            });
           
            protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Client.KEEP_ALIVE) {
                @SuppressWarnings("unused")
                public void onPacketRecieving(PacketEvent e) {
                    new Thread(() -> {
                        for (Player player : inTimeout) {
                            if (e.getPlayer().equals(player))
                                e.setCancelled(true);
                        }
                    }).start();
                }
            });
        }
    }
     
    But gives me the error:

    Code (Text):
    [15:24:42 ERROR]: [Timeout] Unhandled exception number 32 occured in onPacketReceiving(PacketEvent) for Timeout
    java.lang.IllegalStateException: Override onPacketReceiving to get notifcations of received packets!
            at com.comphenix.protocol.events.PacketAdapter.onPacketReceiving(PacketAdapter.java:310) ~[ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.SortedPacketListenerList.invokeReceivingListener(SortedPacketListenerList.java:114) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketRecieving(SortedPacketListenerList.java:67) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.PacketFilterManager.handlePacket(PacketFilterManager.java:610) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.PacketFilterManager.invokePacketRecieving(PacketFilterManager.java:577) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ProtocolInjector.packetReceived(ProtocolInjector.java:323) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ProtocolInjector.onPacketReceiving(ProtocolInjector.java:288) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ChannelInjector.decode(ChannelInjector.java:541) [ProtocolLib.jar:4.2.1]
            at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:249) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:149) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at com.comphenix.protocol.injector.netty.ChannelInjector$4.channelRead(ChannelInjector.java:273) [ProtocolLib.jar:4.2.1]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:150) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [spigot.jar:git-Spigot-625bc00-d09304e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    [15:25:15 ERROR]: [Timeout] Unhandled exception number 64 occured in onPacketReceiving(PacketEvent) for Timeout
    java.lang.IllegalStateException: Override onPacketReceiving to get notifcations of received packets!
            at com.comphenix.protocol.events.PacketAdapter.onPacketReceiving(PacketAdapter.java:310) ~[ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.SortedPacketListenerList.invokeReceivingListener(SortedPacketListenerList.java:114) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketRecieving(SortedPacketListenerList.java:67) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.PacketFilterManager.handlePacket(PacketFilterManager.java:610) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.PacketFilterManager.invokePacketRecieving(PacketFilterManager.java:577) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ProtocolInjector.packetReceived(ProtocolInjector.java:323) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ProtocolInjector.onPacketReceiving(ProtocolInjector.java:288) [ProtocolLib.jar:4.2.1]
            at com.comphenix.protocol.injector.netty.ChannelInjector.decode(ChannelInjector.java:541) [ProtocolLib.jar:4.2.1]
            at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:249) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:149) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at com.comphenix.protocol.injector.netty.ChannelInjector$4.channelRead(ChannelInjector.java:273) [ProtocolLib.jar:4.2.1]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:150) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [spigot.jar:git-Spigot-625bc00-d09304e]
            at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [spigot.jar:git-Spigot-625bc00-d09304e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    >
     
  4. You need to specify a packet.. example to block tab;

    Code (Java):

    protManager = ProtocolLibrary.getProtocolManager();
    protManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, Server.TAB_COMPLETE) {
        public void onPacketSending(PacketEvent e) {
            if ((e.getPacketType() == PacketType.Play.Server.TAB_COMPLETE) &&
                    (!e.getPlayer().hasPermission("AntiTab.Bypass"))) {
                e.setCancelled(true);
            }
        }
    });