Solved How To Remove a String Out of the Config with a Command

Discussion in 'Plugin Development' started by AtomFeGaming, May 18, 2018.

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

    AtomFeGaming

    I have tried many different ways to figure out why this isn't working. I am fairly new to coding and can't figure out why the command /delcurse isn't removing a word for example "hello" in the config.

    Code for /delcurse:
    Code:
    if (cmd.getName().equalsIgnoreCase("delcurse")) {
                if (!sender.hasPermission("curse.delete")) {
                    sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return true;
                    }
               
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a bad word to remove!");
                    return true;
                    }
               
                if (badwords.contains(args[0])) {
                    getConfig().set(args[0], null);
                    saveConfig();
                    sender.sendMessage(ChatColor.GREEN + "You have removed " + args[0] + " from the curse list!");
                    return true;
                }
                return true;

    Here is my config:
    Code:
    badwords:
      - hello
      - FeelsGoodMan
     
  2. Offline

    timtower Administrator Administrator Moderator

    @AtomFeGaming Get the entire list.
    Remove the string from the list.
    Set the list.
     
  3. Offline

    AtomFeGaming

    What do you mean?
     
  4. Offline

    timtower Administrator Administrator Moderator

    badwords is a string list.
    Retrieve that list.
    Remove the word from it.
    Set the list in the config again.
     
  5. Offline

    AtomFeGaming

    How tho? I'm new at coding so what would that look like?
     
  6. Offline

    timtower Administrator Administrator Moderator

    I won't spoonfeed.
     
  7. Offline

    AtomFeGaming

    Here's all of my code. I think I did this right.
    Code:
    package me.atomfe.curseproof;
    
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CurseProof extends JavaPlugin implements Listener {
       
        @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 + "Don't curse! You might get muted!");
            }
        }
       
        public void onEnable() {
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            Player p = (Player) sender;
            List<String> badwords = getConfig().getStringList("badwords");
           
            if (cmd.getName().equalsIgnoreCase("addcurse")) {
                for (int i = 0; i < args.length; i++) {
                    }
                if (!sender.hasPermission("curse.add")) {
                    sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return true;
                    }
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a bad word to add!");
                    return true;
                    }
                badwords.add(args[0]);
                getConfig().set("badwords", badwords);
                saveConfig();
                sender.sendMessage(ChatColor.GREEN + "You have added " + args[0] + " to the curse list!");
                }   
           
            if (cmd.getName().equalsIgnoreCase("delcurse")) {
               
                if (!sender.hasPermission("curse.delete")) {
                    sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return true;
                    }
               
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a bad word to remove!");
                    return true;
                    }
               
                    badwords.remove(badwords);
                    getConfig().set("badwords", badwords);
                    saveConfig();
                    sender.sendMessage(ChatColor.GREEN + "You have removed " + args[0] + " from the curse list!");
                return true;
                }
            return true;
            }
    
    }
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    AtomFeGaming

    Its not working though. "hello" is still in the config
     
  10. Offline

    timtower Administrator Administrator Moderator

    @AtomFeGaming Might want to remove args[0] instead of the entire list :p
     
  11. Offline

    AtomFeGaming

    What? I did do that.

    EDIT: I found my error. Can you change this to solved.
     
    Last edited: May 19, 2018
  12. Offline

    MightyOne

    @AtomFeGaming you have to :D you can edit the title and change the prefix to solved
     
Thread Status:
Not open for further replies.

Share This Page