Player message to not send in chat

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Nov 10, 2018.

  1. How do i make it so when a player is in an arraylist, when they type something in chat it sends it but no one can read it. And i need to check if the message matches a word
     
  2. Hello
    Do you want to check the player is existing on the ArrayList when the player existing the ArrayList, the player message will be sent to op player only? and check the messages matches the word
     
  3. Listen to the async chat event,
    Use event.getRecipents().clear();
    Then just add the sending player back to the recipients list

    It will still show up in the console and to the sending player



    Sent from my iPhone using Tapatalk
     
  4. Good idea!
     
  5. I agree with @dNiym. Would do it like that, too :)
     
  6. Or if you don't want it to be visible to anyone and just be read by the plugin you can cancel the event and fire whatever code based on e.getMessage()
     
  7. So I made exactly the thing you want really quick.

    First, you have to prepare your plugin. In your main class (the class which extends JavaPlugin), you should have a method which returns you the instance of your main class.

    Code (Text):
    public class YourMainClass extends JavaPlugin {
     
        private static YourMainClass instance; //The variable which will hold the instance of your plugin

            @Override
            public void onEnable() {
                instance = this; //Here we'll initialize the variable

                //other things you have in your onEnable()...
            }

            @Override
            public void onDisable() {
                //other things you have in your onDisable()...
            }
     
            //And we can use this method to get the instance from everywhere in your plugin!
            public static Core getInstance() {
                return instance;
            }
    }

    First step done! Now create a class called ChatCancel and copy the code below:

    Code (Text):
    public class ChatCancel implements Listener {
     
        public interface ChatCancelListener {
            public void onCancel(AsyncPlayerChatEvent event);
        }
     
        private ArrayList<Player> cancellingPlayers;
        private ArrayList<ChatCancelListener> chatCancelListeners;
     
        public ChatCancel() {
            cancellingPlayers = new ArrayList<>();
            chatCancelListeners = new ArrayList<>();
            Bukkit.getServer().getPluginManager().registerEvents(this, YourMainClass.getInstance());
        }
     
        public void disable() {
            cancellingPlayers.clear();
            chatCancelListeners.clear();
            HandlerList.unregisterAll(this);
        }
     
        @EventHandler
        public void onAsyncChat(AsyncPlayerChatEvent event) {
            if(cancellingPlayers.contains(event.getPlayer())) {
                event.getRecipients().clear();
                for (ChatCancelListener chatCancelListener : chatCancelListeners) {
                    chatCancelListener.onCancel(event);
                }
            }
        }
     
        public boolean isCancellingPlayer(Player player) {
            return cancellingPlayers.contains(player);
        }
     
        public void addPlayer(Player player) {
            if(!cancellingPlayers.contains(player)) cancellingPlayers.add(player);
        }
     
        public void removePlayer(Player player) {
            cancellingPlayers.remove(player);
        }
     
        public void setPlayer(Player player, boolean state) {
            if(state) addPlayer(player);
            else removePlayer(player);
        }
     
        public void addListener(ChatCancelListener chatCancelListener) {
            if(!chatCancelListeners.contains(chatCancelListener)) chatCancelListeners.add(chatCancelListener);
        }
     
        public void removeListener(ChatCancelListener chatCancelListener) {
            chatCancelListeners.remove(chatCancelListener);
        }
     
        public ArrayList<Player> getCancellingPlayers() {
            return cancellingPlayers;
        }
     
        public ArrayList<ChatCancelListener> getListeners() {
            return chatCancelListeners;
        }

    }
    Now head to your main class again.
    Create a new variable of type ChatCancel (the class we just created) below your instance variable, which should also be static. This variable will hold an instance of the ChatCancel system.
    Code (Text):
    private static YourMainClass instance;
    private static ChatCancel chatCancel;
    Go into your onEnable(), below instance = this; and add chatCancel = new ChatCancel();
    Code (Text):
    @Override
        public void onEnable() {
            instance = this; //Here we'll initialize the variable
            chatCancel = new ChatCancel();

            //other things you have in your onEnable()...
        }
    Go into your onDisable(), and add chatCancel.disable(); to it.
    Code (Text):
    @Override
        public void onDisable() {
            chatCancel.disable();

            //other things you have in your onDisable()...
        }
    Create a method to access the ChatCancel system

    Code (Text):
    public static ChatCancel getChatCancel() {
            return chatCancel;
    }

    Now from everywhere in your plugin, you can access the ChatCancel system by YourMainClass.chatCancel.

    So now that we are set up, how do we use the system?
    Actually, it's pretty easy.

    With
    Code (Text):
    addPlayer(player)
    removePlayer(player)
    setPlayer(player, state)
    you can manage, which players are affected by the system. So that's the ArrayList you are talking about.

    So now that you have your players registered by the system, you also have to tell the system what should happen when it blocks a chat message.
    You can do this by using addListener(chatCacelListener);
    Code (Text):
    YourMainClass.chatCancel.addListener(new ChatCancelListener() {
        @Override
        onCancel(AsyncPlayerChatEvent event) {
              //What should happen from now? The message has already been blocked

              //Optional: You can let the listener disable itself again, so it's like a one time use
              YourMainClass.chatCancel.removeListener(this);
         }
    });

    So I think you should get the idea... Have the system accessible from everywhere, registering players, registering the " behaviors" (listeners)
    If you don't get how this works, then I am sorry for making it too complex for you :) You should not just copy-paste the code, but understand how it works. So if it is too complex, you should stick with the simple version of just creating an AsyncChat listener ^^

    This code is untested!
     
    #7 Kitt3120, Nov 10, 2018
    Last edited: Nov 10, 2018
  8. I figured this out but thanks a lot for helping! I will be reading it for advice
     
    • Like Like x 1
  9. Just a quick reminder :) If you solved your problem, please mark this thread as solved. On the opposite, if you need more help, just leave it open ;)
     
  10. Just simply use this =>

    Code (Text):

    @EventHandler
    public void onChat(PlayerAsyncChatEvent event) {
        event.setCancelled(true);
    }
     
    This would be most efficient since you just cancel the message but your code can obiously work with it :) (Store the message (event.getMessage()) in a String ;) )
     
  11. Yea, as I mentioned. If that's too complex, you can just use the listener :D And if you are talking about the

    Code (Text):
    @EventHandler
        public void onAsyncChat(AsyncPlayerChatEvent event) {
            if(cancellingPlayers.contains(event.getPlayer())) {
                event.getRecipients().clear();
                for (ChatCancelListener chatCancelListener : chatCancelListeners) {
                    chatCancelListener.onCancel(event);
                }
            }
        }
    part in my system, that is on purpose, as I thought that the message should not get canceled but only displayed to the player itself.
     

Share This Page