Help with save and reading from file

Discussion in 'Spigot Plugin Development' started by zackatack108, May 1, 2017.

  1. Okay so I'm having some trouble with this Capture the flags plugin I'm coding. The problem I'm having is that when I try to type in a command like /ctf create [arena] I keep getting internal exception error and looking at the console I can't determine what I did wrong. I checked my code multiple times and looked online and everything seems fine so that's why I'm coming here.

    Also when I check the config to see if the arena was added nothing has changed in the config.

    Here's my code
    Code (Text):
    //public bool onCommand: commands for plugin
        //Input: user enters commands
        //Output: None
        //Return: true when command has run successfully
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args){
           
            //create player variable
            Player player = (Player) sender;
           
            //create fileconfiguration variable
            FileConfiguration config = plugin.getConfig();
           
            //Check to see if a player is sending the command
            if(sender instanceof Player){
               
                if(cmd.getName().equalsIgnoreCase("ctf")){
                   
                   
                    switch(args[0].toLowerCase()){
                   
                    //call command /ctf join [arena]
                    case "join":
                       
                        if (args[1].equalsIgnoreCase("test")){
                           
                            //send player message
                            player.sendMessage(ChatColor.YELLOW + "Joining CTF arena");
                           
                        }
                       
                        break;
                       
                    //call command /ctf leave
                    case "leave":
                       
                        player.sendMessage(ChatColor.YELLOW + "Leaving CTF arena");
                       
                        break;
                       
                    //call command /ctf create [arena]
                    case "create":
                       
                        if(args.length == 2){
                           
                            player.sendMessage("Test");
                           
                            if(config.contains("arenas." + args[1])){
                               
                                player.sendMessage("test 2");
                               
                                //display error message to user saying arena already exist
                                player.sendMessage(ChatColor.RED + "Error: Arena already exist with that name");
                               
                            } else {
                               
                                player.sendMessage("test 3");
                               
                                //create arena and save it to config file
                                config.set("arenas." + args[1], args[1]);
                               
                                //Display message to user saying arena was created
                                player.sendMessage(ChatColor.YELLOW + "Creating CTF arena " + args[1]);
                               
                                //Save config
                                plugin.saveConfig();
                               
                            }
                       
                        //Check to see if player has permission
                        //if(player.hasPermission("ctf.create")){
                                                       
                        //    }                      
                           
                        } else {
                           
                            //display error message to user
                            player.sendMessage(ChatColor.RED + "Sorry you don't have the permission");
                            player.sendMessage(ChatColor.RED + "Please contact your server administrator if you belive this is an error");
                           
                        }
                       
                        break;
                       
                    //call command /ctf edit [arena]
                    case "edit":
                       
                        //Check to see if player has permission
                        if(player.hasPermission("ctf.edit")){
                           
                            //Check to see if arena exist
                            if(config.contains("arenas." + args[1])){
                               
                                //display message to user saying they are now in edit mode
                                player.sendMessage(ChatColor.YELLOW + "Editing CTF arena " + args[1]);
                               
                                //Call editArena
                               
                            } else {
                               
                                //Display message to user saying arena doesn't exist
                                player.sendMessage(ChatColor.RED + "Error: Arena doesn't exist");
                               
                            }                      
                           
                        } else {
                           
                            //display error message to user
                            player.sendMessage(ChatColor.RED + "Sorry you don't have the permission");
                            player.sendMessage(ChatColor.RED + "Please contact your server administrator if you belive this is an error");
                           
                        }
                       
                        break;
                       
                    //call command /ctf delete [arena]
                    case "remove":
                       
                        //Check to see if player has permission
                        if(player.hasPermission("ctf.remove")){
                           
                            //Check to see if arena exist in file
                            if(config.contains("arenas." + args[1])){
                               
                                //remove the arena from the config
                                config.set("arenas" + args[1], null);
                               
                                //display message to user saying arena was removed
                                player.sendMessage(ChatColor.YELLOW + "Removing CTF arena " + args[1]);
                               
                            } else {
                               
                                //display error message saying arena doesn't exist in file
                                player.sendMessage(ChatColor.RED + "Error: Arena name doesn't exist in config");
                               
                            }
                           
                        } else {
                           
                            //display error message to user
                            player.sendMessage(ChatColor.RED + "Sorry you don't have the permission");
                            player.sendMessage(ChatColor.RED + "Please contact your server administrator if you belive this is an error");
                           
                        }
                                           
                        break;
                       
                    //default if commands don't work
                    default:
                       
                        //Display error messsage
                        player.sendMessage(ChatColor.RED + "Error: input not valid.");
                       
                        break;
                    }
                   
                }
               
            }
           
            return true;
        }

    [18:59:11 INFO]: zackatack108 issued server command: /ctf join Test
    [18:59:11 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ctf' in plugin Capture_the_Flag v2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1354) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1189) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
    at me.zackatack108.CaptureTheFlag.Main_CTF.onCommand(Main_CTF.java:94) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-a3f6ea5-6194f6c]
    ... 15 more
     
  2. Okay whoa you need to make another class or something to manage your config file. Make methods in the config file to set and get data. Doing a config file inside of a command is not a good idea!

    Also I think the error might be because of an exception with creating the file. You would need to use try/catch to catch the exception, or add it to the method header. Not completely sure though.
     
  3. What is line 94 in your Main_CTF class?
     
  4. I do have a separate function that creates the config file
    Code (Text):
    //private void createConfig(): create the config
        //Input: Check to see if config exist
        //Ouput: creates config if it doesn't exist
        //Return: none
        private void createConfig() {
           
            try {
               
                //Check to see theirs a folder
                if(!getDataFolder().exists()){
                    getDataFolder().mkdirs();
                }
               
                //Create file
                File configFile = new File(getDataFolder(), "config.yml");
               
                //Check if file doesn't exist
                if(!configFile.exists()){
                   
                    //Display error message to console
                    getLogger().info("Config.yml not found, creating file");
                   
                    //create file
                    saveDefaultConfig();
                   
                } else {
                   
                    //Display message saying file was found
                    getLogger().info("Config.yml found, loading file");
                   
                }
               
            } catch (Exception except) {
               
                //display error message to console
                except.printStackTrace();
            }
           
        }

    Line 94 is
    Code (Text):
    FileConfiguration config = plugin.getConfig();
     
  5. Why just have getConfig().options().copyDefaults(true);
    and then saveConfig(); in your onEnable?

    Also make sure your plugin instance isn't null.
     
  6. You should make methods to save/store data in your config class instead of doing it all in the command.
    You can then access the methods by instantiating the config class.