Help needed for Custom Chat Plugin

Discussion in 'Plugin Development' started by TheReadiez, Jul 31, 2018.

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

    TheReadiez

    I am creating a custom chat plugin for a Minecraft Server. I have recently started coding again and have lost a bit of my knowledge. So i need some help with my code. I am looking to add in two features to the code i already have and some info on whether the current code will work perfectly fine.

    I am wondering if the current code i have will block the advertisement of other websites or servers on the server. If not then how would i implement that. I am also looking to add in a features that will automatically notify any staff members online when somebody swears. Due to the swear word being banned from chat anyway and the message not being shown to any players. The system would have to notify staff, of the player and what they said. Letting them warn or mute them.
    The code is shown below:
    Code:
    package me.ghg.chatreader;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatReader extends JavaPlugin implements Listener{
      
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            if(!getConfig().contains("words")){
                List<String> words = new ArrayList<String>();
                words.add("fuck");
                words.add("cunt");
                words.add("nigger");
                words.add("faggot");
                words.add("spastic");
                words.add("twat");
                words.add("bitch");
                words.add("bellend");
                words.add("ass");
                words.add("pussy");
                words.add("Fucking");
                words.add("Shitting");
                words.add(".net");
                words.add(".com");
                words.add(".co.uk");
                words.add(".org");
                words.add(":25565");
                getConfig().set("words", words);
            }
            saveConfig();
        }
      
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            String msg = e.getMessage();
            List<String> words = getConfig().getStringList("words");
            for(int i = 0; i < words.size(); i++){
                if(msg.contains(words.get(i))){
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(" &c&lExotic Chat Bot - Swearing/Advertising Is Not Permitted On The ###### Network!");
    
                  
            
              
                  
                }
            }
        }
    [/code[
    
    
    Thank you for your help
     
    Last edited by a moderator: Aug 2, 2018
  2. Offline

    The_Spaceman

    Code:
    public static boolean isValidURL(String urlString) {
        try {
            new URL(urlString).toURI();
            return true;}
        catch (Exception e) {
            return false;
        }
    }
    
    This will check if it is an valid URL, but you can try something with .startWith("www.")

    and in the piece where the chat message gets blocked you have to add something like:

    Code:
    for (Player pl : adminPlayers) {
        pl.sendMessage(player.getName() + " just said " + words.get(i) + ", pls punish him");
    }
    
     
Thread Status:
Not open for further replies.

Share This Page