Solved ProtocolLib Cancel Message

Discussion in 'Plugin Development' started by hanahouhanah, Sep 28, 2013.

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

    hanahouhanah

    I want to check if a player sent a certain message. This can't be achieved by checking if the message contains their name (the message sent is entirely the actual message. In the logger, it appears as just the message, there's no name attached).
    How would I achieve this?
    What I have so far that doesn't work:
    Code:java
    1. protocolManager.addPacketListener(new PacketAdapter(this,ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL, Packets.Client.CHAT) {
    2. @Override
    3. public void onPacketReceiving(PacketEvent e) {
    4. if (e.getPacketID() == Packets.Client.CHAT) {
    5. PacketContainer packet = e.getPacket();
    6. String message = packet.getStrings().read(0);
    7. if(message.contains(getConfig().getString("SurvivalGamesStartMessage"))){
    8. if(e.getSource() instanceof Player){
    9. e.setCancelled(true);
    10. }else{ // Do fun stuff

    This is one of the many things I've been stuck on in my project. Now, I think (and hope to my pizza god) this is the last thing I need to achieve my goal.
    Any help/pointers is appreciated.
     
  2. Offline

    Janmm14

    hanahouhanah
    All the packets are coming from players. To get the player who sent the chat message, do e.getPlayer();
    If you want to get the chat message, do:
    e.getPacket().getStrings().get(0); (hopefully right)

    Edit: its .read(0);
     
  3. Offline

    hanahouhanah

    Janmm14
    I see... Is it possible to check if the message was broadcasted and not sent by a player?
    I'm trying to check if
    Code:
     if(message.contains(getConfig().getString("SurvivalGamesStartMessage"))){
    was sent by a player and if so, cancel. But if it's sent by anything else (whether it was through a broadcast message or message from plugin), it'll continue. But if this is only called when a player chats, how else would I do it?
     
  4. Offline

    Janmm14

    hanahouhanah
    What you are doing with your Listener is checking messages sent by players to the server (either chat or commands).
    When you want to listen to message ssent by the server, ou have to do ConnectionSide.SEVER_SIDE and you have to use onPacketSending().

    You can't determine how message was sent.
     
  5. Offline

    hanahouhanah

    Janmm14

    I tried that, but it still continues to go through as a player sending the message.

    Code:java
    1. protocolManager.addPacketListener(new PacketAdapter(this,ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Client.CHAT) {
    2. @Override
    3. public void onPacketSending(PacketEvent e) {
    4. if (e.getPacketID() == Packets.Client.CHAT) {
    5. PacketContainer packet = e.getPacket();
    6. String message = packet.getStrings().read(0);
    7. if(message.contains(getConfig().getString("SurvivalGamesStartMessage"))){


    Tried changing
    Code:
    if (e.getPacketID() == Packets.Client.CHAT) {
    to
    Code:
    if (e.getPacketID() == Packets.Server.CHAT) {
    and still the same result.
     
  6. Offline

    Janmm14

  7. Offline

    hanahouhanah

    Janmm14
    Same result.

    Janmm14
    Decided to check if the player was null and if so, then continue. So I tested it ingame and the message did not come up. However, when I typed it in broadcast (through console), it didn't give me my second debug (saying player is null).
    Code:
    protocolManager.addPacketListener(new PacketAdapter(this,ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.CHAT) {
                @Override
                public void onPacketSending(PacketEvent e) {
                    if (e.getPacketID() == Packets.Server.CHAT) {
                        PacketContainer packet = e.getPacket();
                        String message = packet.getStrings().read(0);
                        if(message.contains(getConfig().getString("SurvivalGamesStartMessage"))){
                            for(Player p : Bukkit.getOnlinePlayers()){
                                if(e.getPlayer() != null){
                                    System.out.println("Player did send message");
                                }else{
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Offline

    Janmm14

  9. Offline

    hanahouhanah

    Janmm14
    Just after that, I want to get all the players so I can do something in the else statement. For a minor performance increase, probably should move the if statement above the loop, but I'm just trying to get this working before anything else.
    I also encountered another problem. I can't use "&" in ymls even if I'm trying to translate it to a color code, so I tried translating "§"since it seemed to be working, but I'd get an unusual character.
    [​IMG] which is the "§" I was trying to replace.
    I tried this:
    Code:
    message.replaceAll("§", "");
    But it didn't seem to work.

    Thanks for sticking by and helping so far :)
     
  10. Offline

    nisovin

    What exactly are you trying to accomplish here? I'm failing to see why you need a packet listener for this at all. If you're trying to detect when someone sends a certain message, you can just use AsyncPlayerChatEvent. If you need some kind of console command, just use the regular command system. I don't understand what you're trying to do.
     
  11. Offline

    hanahouhanah

    nisovin
    I'm trying to get when a player receives a certain message, but they can't send it. I just managed to finish that (I think).
    Now I can't replace the § so the "A" with a symbol doesn't appear. When I tried to replaceAll, the "A" was the same, and the colors weren't replaced.
    This is what I have for that:
    Code:
    String message = ChatColor.translateAlternateColorCodes('§', getConfig().getString("SurvivalGamesStartMessage"));
                    String aft = message.replaceAll("§", "");
                    getServer().broadcastMessage(aft);
     
  12. Offline

    nisovin

    Use ChatColor.stripColors
     
Thread Status:
Not open for further replies.

Share This Page