HELP! with AntiSwearing plugin

Discussion in 'Plugin Development' started by XBlackDragonX, Apr 30, 2015.

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

    XBlackDragonX

    Hello i am trying to make a anti swearing plugin for my server. so i got started with it and can't get further.
    I need to get 3 warnings for swearing and then a kick from the server. i can get to 1 warning and then it stops by me. i also added some commands for adding swearwords In-Game that also not works.

    Who can help me?


    Here's my code:

    Code:
    package me.xblackdragonx.antiswear;
    
    import java.io.File;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class AntiSwear extends JavaPlugin implements Listener {
    
        //CFG File Add
        FileConfiguration config;
        File cfile;
       
       
        //ASPlugin - PlayerJoinMSG
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent join) {
            Player p=join.getPlayer();
            p.sendMessage(ChatColor.GOLD + "[AntiSwear] Plugin loaded... Developed By XBlackDragonX");
        }
       
        //ASPlugin - Warning 1/3
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            for (String word: e.getMessage().split(" ")){
                if (getConfig().getStringList("Badwords").contains(word)){
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "[AntiSwear]" + getConfig().getString("Message"));
                    e.getPlayer().sendMessage(ChatColor.GOLD + "Warning 1/3");
                }
               
                
               
            }
           
    }
            public void onEnable() {
       
            //CFG File Register   
            config = getConfig();
            config.options().copyDefaults(true);
            saveConfig();
            cfile = new File(getDataFolder(), "config.yml" );
           
            //Events Register
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
            //CMD Section
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            //CMD 1 - as add
            if (cmd.getName().equalsIgnoreCase("as add")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify the bad word");
                    return true;
                }
               
               
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < args.length; i++) {
                    str.append(args[i] + " ");
                }
               
                String word = str.toString();
                getConfig().set("Badwords", word);
                saveConfig();
                sender.sendMessage(ChatColor.GREEN + "[AntiSwear] Successfully added: " + word );
                return true;
                }
           
           
            //CMD 2 - as reload   
            if (cmd.getName().equalsIgnoreCase("as reload")) {
                    config = YamlConfiguration.loadConfiguration(cfile);
                    sender.sendMessage(ChatColor.GREEN + "[AntiSwear] Reloaded config file Successfully!");
                    return true;
                }
                return true;
           
          
        }
    }
    
    my config.yml:

    Code:
    ##############################################################
    #                                                                                                                                                                #
    #                                    AntiSwear Plugin    v1.0                                                                                #
    #                                   Developed by: XBlackDragonX                                                                    #
    #                                                                                                                                                                #
    ##############################################################
    
    
    Message: Don't Swear!
    
    
    
    Badwords:
      -
    
    And my plugin.yml:

    Code:
    name: AntiSwear
    version: 1.0
    main: me.xblackdragonx.antiswear.AntiSwear
    description: Helps players stopping with swearing in the chat!
    commands:
        as add:
            usage:/<command>
            description: You can add badwords to the config in-game!
        as reload:
            usage:/<command>
            description: This reloads the config file when you added a badword
    
    I hope someone can help me!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @XBlackDragonX You can only register the "as" in the plugin.yml, it doesn't take commands there
     
  3. Offline

    mine-care

    You have to check the arguments, commands may not contain spaces.
     
  4. Offline

    XBlackDragonX

    @timtower Nice, but how do i get it working like i did? "as reload'
     
  5. Offline

    timtower Administrator Administrator Moderator

    @XBlackDragonX
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){

    The args are the words after the main command.
     
  6. Offline

    XBlackDragonX

    @timtower I really don't know what you mean. Can you explain it with a little more detail?
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. Offline

    Koobaczech

    "as add" will be parsed by java as two arguments, divided by the whitespace in between. Fix your plugin.yml to make the command be "as" only. And in your code, check if the arg length is in example, 2. arg[0] would be "add" and arg[1] would be the word, and "as" would be the command
    Code:
    if (cmd.getName().equalsIgnoreCase("as add"))
     
  9. Code:
    if(cmd.getName().equalsIgnoreCase("as") { //You have to check the command "as" and "reload" as an arg
       if(args.length > 0) { //You have to check if args.length are over 0 otherwhise it will throw a error
          if(args[0].equalsIgnoreCase("reload") { //Here goes the arg reload /as reload
              //Stuff
    
     
  10. Offline

    Tamir

    JPG2000 likes this.
  11. Offline

    JPG2000

Thread Status:
Not open for further replies.

Share This Page