Solved Plugin Help: Saving command argument to config

Discussion in 'Spigot Plugin Development' started by zackatack108, May 25, 2016.

  1. Hello spigot community, I'm having issues with getting my plugin to save a command argument that a player enters in the game.

    Here's a summary of what I'm trying to do.
    In the game I want the players to run a command "/show create <name>".
    I'm wanting the name that they put to be save to a config for the plugin. I have confirmed that the command "/Show create" works when I don't have the code to try and save to the config. As soon as I try to save to the config I have issues with it not wanting to save. I do get an error in minecraft that says "An internal error occurred while attempting to perform this command." Any idea what's going on?

    Code:

    Code (Text):
    private void createConfig(){
           
            configf = new File(getDataFolder(), "config.yml");
           
            if(!configf.exists()){
                configf.getParentFile().mkdirs();
                saveResource("config.yml", false);
            }
           
            config = new YamlConfiguration();
           
            try{
                config.load(configf);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
            Player player = (Player) sender;
            String Show = plugin.getConfig().getString("Show");
           
            if(Label.equalsIgnoreCase("show")){
                if(args[0].equalsIgnoreCase("Create")){
                    plugin.getConfig().set("Show", args[1]);
                    player.sendMessage(ChatColor.GREEN + Show + " created.");
                    plugin.saveConfig();
                }
                if(args[0].equalsIgnoreCase("Remove")){
                    player.sendMessage(ChatColor.RED + "Show Removed");
                }
            }          
           
            return false;
        }
     
  2. You need to make sure the args length is == 2
     
  3. Okay I have an if statement checking the length to be 2 and moved the code inside of the if statement. However I'm still having the same issue.
     
  4. Could you please post the stacktrace?
     
  5. The config you have defined is not the default config, so when you do plugin.getConfig() it returns the default config, not the one you have made. You dont event need to create a config because you are just accessing the default one.
     
  6. check the args length if its <= 2;


    Edit : Change your config object to this
    Code (Java):
    config = getConfig();
     
  7. So I figured out what I did wrong. I had some extra code that wasn't needed and was able to get everything working again. Thanks for the suggestions.