Adding words to a config

Discussion in 'Plugin Development' started by Random_Tomato, Nov 4, 2017.

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

    Random_Tomato

    Hey I need a little help I am unsure of how to add words to a config through commands
    I am unsure on how to register the config

    Code:
     
    package me.chatControl;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.project.main;
    
    public class addWord implements CommandExecutor {
        private main plugin;
       
        public addWord(main pl) {
            plugin = pl;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player to execute this command");
                    return false;
                }
               
                Player player = (Player) sender;
                if(args[0].equalsIgnoreCase("addword")){
                    if (player.hasPermission("bigbrother.addword")){
                        this.getConfig().getStringList("Banned Words").add(args[1]);
                        this.reloadConfig();
                    }
                }
                else {
                    String prefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefix"));
                    String permission = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("NoPermission"));
                    player.sendMessage(prefix + "" + permission);
                }
        return true;
        }
     }
    
    
     
  2. Offline

    Zombie_Striker

    @Random_Tomato
    Accessing the config does not directly change the values in the config. You will need to save the string list as a List, then add the word to that list, and then resave the list to the config, and then save the config.
     
  3. Offline

    Random_Tomato

  4. Offline

    Zombie_Striker

    @Random_Tomato
    List<String> list = getConfig().getStringlist....
    list.add(....)
    getConfig.set(.... , list);
    saveConfig
     
  5. Offline

    Random_Tomato

    @Zombie_Striker
    StringBuilder str = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
    str.append(args);
    String word = str.toString();
    plugin.getConfig().set("Banned Words", word);
    plugin.saveConfig();
    This is what I have so far, how would I go about making it a list rather than an individual word
     
  6. @Random_Tomato He just showed you how... Are you unfamiliar with java.util.List and ArrayList? StringBuilder is used for being able to build strings together more efficiently when iterating or adding groups of strings that aren't in one command line (example):
    Code:
    for (int i = 0; i < 1000; i++) {
    builder.append(strings[i]);
    }
    Also, if you want to make a flawless word or swear banner/filter, you will need to consider ways of getting around the filter, and ways the filter might trigger accidentally when the person isn't swearing (such as saying "assassin"). You should also almost certainly look into Big-O-Notation... add a hundred or even ten banned words and this could consume significant CPU looking through the entire message and seeing if it contains one of the banned words.
     
    Last edited: Nov 5, 2017
Thread Status:
Not open for further replies.

Share This Page