ProtocolLib

3M Downloads

How to filter actionbar messages? Whats wrong?

MrClean1337 opened this issue ยท 1 comments

commented

Make sure you're doing the following

  • You're using the latest build for your server version
  • This isn't an issue caused by another plugin
  • You've checked for duplicate issues
  • You didn't use /reload

Describe the question
Hello, my main goal is to filter some actionbar messages packets out for players.

API method(s) used
Just PacketEvents ...

Expected behavior
Some actionbar messages should be filtered out / canceled.

Code

        protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.CHAT) {
            @Override
            public void onPacketSending(PacketEvent event) {
                System.out.println(event.getPacketType().toString());
                System.out.println(event.getPacket().getChatComponents().getValues().size());
                PacketContainer packet = event.getPacket();

                EnumWrappers.ChatType type = packet.getChatTypes().read(0);

                if(type == null) return;

                System.out.println(type);

                if (type == EnumWrappers.ChatType.GAME_INFO) {
                    BaseComponent[] baseComponents = (BaseComponent[]) packet.getModifier().read(1);
                    String text = baseComponents[0].toLegacyText();
                    if (text != null) {
                        getLogger().info(text);
                    }
                }
            }
        });

Additional context
The code above logs nothing if a player gets a actionbar message.
After that, I tried something like this:

        protocolManager.addPacketListener(new PacketAdapter(
                this,
                ListenerPriority.NORMAL,
                PacketType.Play.Server.SET_ACTION_BAR_TEXT) {
            @Override
            public void onPacketSending(PacketEvent event) {
                System.out.println(event.getPacketType().toString());
            }
        });

annnd... for some reason, some actionbar messages will log.
But only these from plotsquared, not from all 20~ other plugins.
[STDOUT] SET_ACTION_BAR_TEXT[class=ClientboundSetActionBarTextPacket, id=70]

Yes, I did a bit of googling and ChatGPT xd
https://www.spigotmc.org/threads/1-18-protocollib-and-action-bar.551072/#post-4385253
https://www.spigotmc.org/threads/1-13-protocollib-catching-and-changing-an-action-bar-message.331309/
https://www.spigotmc.org/threads/protocollib-actionbar-packet-problem.586038/#post-4530670

ChatGPT gave me this:

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

public class ActionbarMonitorPlugin extends JavaPlugin implements Listener {

    private ProtocolManager protocolManager;

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);
        protocolManager = ProtocolLibrary.getProtocolManager();
        registerActionBarInterceptor();
    }

    private void registerActionBarInterceptor() {
        protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Server.CHAT) {
            @Override
            public void onPacketSending(PacketEvent event) {
                if (event.getPacketType() == PacketType.Play.Server.CHAT) {
                    Player player = event.getPlayer();
                    String message = event.getPacket().getStrings().read(0);

                    // Check if it is an action bar message
                    if (event.getPacket().getChatTypes().read(0) == ChatType.GAME_INFO) {
                        getLogger().info("Received action bar message from player " + player.getName() + ": " + message);
                    }
                }
            }
        });
    }
}

Didn't work either.
What I'm doing wrong?

commented

I guess trying to solve this problem with ChatGPG is the worst option here...

If you are talking about 1.19.4 use PacketType.Play.Server.SYSTEM_CHAT. CHAT is only used for (signed) player chat messages.