Solved AsyncPlayerChatEvent questions

Discussion in 'Spigot Plugin Development' started by Pierre26, Jun 25, 2021.

  1. Hello, I have several questions regarding the AsyncPlayerChatEvent:

    1) I used e.getMessage (). StartsWith ("!"), But how do I remove the "!" so that players receive the message without this "!" ?

    2) How can I make some people see the message but not others?

    Thanks,

    Code (Text):
    package fr.falkanox.bingo.events;

    import dev.jcsoftware.jscoreboards.JPerPlayerMethodBasedScoreboard;
    import fr.falkanox.bingo.Bingo;
    import fr.falkanox.bingo.states.GState;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class AsyncChat implements Listener {
       
        private JPerPlayerMethodBasedScoreboard scoreboard;
        private Bingo main;
       
        public AsyncChat(JPerPlayerMethodBasedScoreboard scoreboard, Bingo main){
            this.main = main;
            this.scoreboard = scoreboard;
        }

        @EventHandler
        public void onChat(AsyncPlayerChatEvent e){

            Player p = e.getPlayer();

            if(main.isState(GState.WAITING) || main.isState(GState.STARTING)){

                if(scoreboard.findTeam("Bleue").get().isOnTeam(p.getUniqueId())){

                    e.setFormat("§bBleue " + "%1$s" + " §f: §7" + "%2$s");


                } else if(scoreboard.findTeam("Rouge").get().isOnTeam(p.getUniqueId())){

                    e.setFormat("§cRouge " + "%1$s" + " §f: §7" + "%2$s");

                } else if(scoreboard.findTeam("Neutre").get().isOnTeam(p.getUniqueId())){

                    e.setFormat("§7" + "%1$s" + " §f: §7" + "%2$s");

                }

            } else if(main.isState(GState.PLAYING)){

                if(scoreboard.findTeam("Bleue").get().isOnTeam(p.getUniqueId())){

                    if(e.getMessage().startsWith("!")){

                        //remove "!" and global message (all players)

                    } else {

                        //blue team message

                    }

                } else if(scoreboard.findTeam("Rouge").get().isOnTeam(p.getUniqueId())){

                    if(e.getMessage().startsWith("!")){

                        //remove "!" and global message (all players)

                       
                    } else {

                        //red team message
                       
                    }

                }

            } else {

                if(scoreboard.findTeam("Bleue").get().isOnTeam(p.getUniqueId())){

                    e.setFormat("§bBleue " + "%1$s" + " §f: §7" + "%2$s");


                } else if(scoreboard.findTeam("Rouge").get().isOnTeam(p.getUniqueId())){

                    e.setFormat("§cRouge " + "%1$s" + " §f: §7" + "%2$s");

                }

            }

        }

    }
     
     
  2. 1) player.sendMessage(e.getMessage().substring(1));

    2) e.setCancelled(true);
     
  3. you can remove the first char of that string,

    steps:


    -get the message chars list
    -remove the item that has the index of 0
    - then make a sentence out of the rest chars
    - send the result to the player.
     
  4. SteelPhoenix

    Moderator

    Cancelling will completely stop the message from sending. You can remove specific players from the recipients list so they don't get the message.