Help with the Bukkit Configuration

Discussion in 'Plugin Development' started by bowman3002, Jan 16, 2012.

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

    bowman3002

    Hi,
    I've been writing a plugin for some time and hit an issue with the bukkit configuration. The issue isn't with the default config.yml given to you, but with the fact that I need to create more .ymls to store all of the data I want. I've followed the "Introduction to the New Configuration" tutorial for creating extra yamls almost exactly except for the fact that I put the saving, loading, and editing in the save/load methods. This is because, in my plugin, there are a variable amount of objects that need to save their data and creating methods for each yaml would be impossible.

    In the save method I'm creating the file objects, creating the file on the disk if it doesn't exist, and creating the FileConfiguration object linked to that file. I am then using the .set() method for all of the data I need stored and saving each yaml as I finish the sets. The files are created properly, but nothing appears in them and I'm not sure why. I can put the code for the methods up if needed, but I'm not sure what I'm doing wrong.

    If anyone has any suggestions they'd be greatly appreciated; I've been trying to figure this out for a week or two.

    Here's the beginning code of the save method. It goes through the sets for the first yaml because the method is rather long.

    Code:
    String errorMessage="Failure in saving battleField " + this.name;
            battleCraft.saveConfig();
            File masterDir=new File(battleCraft.getDataFolder(), this.name);
            if(!(masterDir.exists()))
            {
                masterDir.mkdirs();
            }
            File battleFieldFile=new File(battleCraft.getDataFolder()+File.separator+this.name+File.separator+this.name+".yml");
            System.out.println(battleCraft.getDataFolder()+File.separator+this.name+File.separator+this.name+".yml");
            File sizeFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"Size.yml");
            File teamsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"Teams.yml");
            File commandPointsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"CommandPoints.yml");
            File capturePointsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"CapturePoints.yml");
            File KOTHPointsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"KOTHPoints.yml");
            File neutralFlagsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"NeutralFlags.yml");
            File medicsFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"Medics.yml");
            File blockFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"Blocks.blocks");
            File dataFile=new File(battleCraft.getDataFolder(), this.name+File.separator+"Data.data");
            
            if(!battleFieldFile.exists())
            {
                try
                {
                    battleFieldFile.createNewFile();
                    sizeFile.createNewFile();
                    teamsFile.createNewFile();
                    commandPointsFile.createNewFile();
                    capturePointsFile.createNewFile();
                    KOTHPointsFile.createNewFile();
                    neutralFlagsFile.createNewFile();
                    medicsFile.createNewFile();
                    blockFile.createNewFile();
                    dataFile.createNewFile();
                }
                catch (IOException e)
                {
                    battleCraft.getLogger().log(Level.SEVERE, errorMessage);
                }
            }
            
            FileConfiguration battleFieldConfig=YamlConfiguration.loadConfiguration(battleFieldFile);
            FileConfiguration sizeConfig=YamlConfiguration.loadConfiguration(sizeFile);
            FileConfiguration teamsConfig=YamlConfiguration.loadConfiguration(teamsFile);
            FileConfiguration commandPointsConfig=YamlConfiguration.loadConfiguration(commandPointsFile);
            FileConfiguration capturePointsConfig=YamlConfiguration.loadConfiguration(capturePointsFile);
            FileConfiguration KOTHPointsConfig=YamlConfiguration.loadConfiguration(KOTHPointsFile);
            FileConfiguration neutralFlagsConfig=YamlConfiguration.loadConfiguration(neutralFlagsFile);
            FileConfiguration medicsConfig=YamlConfiguration.loadConfiguration(medicsFile);
            
            FileOutputStream blockOutput=new FileOutputStream(blockFile);
            FileOutputStream dataOutput=new FileOutputStream(dataFile); 
            
     
            
            //sets the BattleField info        
            battleFieldConfig.set("test", blockBreak);
            battleFieldConfig.set("Options.Maker", MAKER);
            battleFieldConfig.set("Options.extraMakers", extraMakers);
            battleFieldConfig.set("Options.friendlyFire", friendlyFire);
            battleFieldConfig.set("Options.reinforcements", reinforcements);
            battleFieldConfig.set("Options.loadOut", loadout);
            battleFieldConfig.set("Options.randomSpawns", randomSpawns);
            battleFieldConfig.set("Options.maxTeamSize", maxTeamSize);
            battleFieldConfig.set("Options.commandPointPeriod", commandPointPeriod);
            battleFieldConfig.set("Options.maxTeamScore", maxTeamScore);
            battleFieldConfig.set("Options.captureTime", captureTime);
            battleFieldConfig.set("Options.KOTHTime", KOTHTime);
            battleFieldConfig.set("Options.healPeriod", healPeriod);
            battleFieldConfig.set("Options.minPlayers", minPlayers);
            battleFieldConfig.set("Options.maxPlayers", maxPlayers);
            battleFieldConfig.set("Options.spawnType", spawnType);
            battleFieldConfig.set("Options.world", world.getName());
            battleFieldConfig.set("Options.hunger", hunger);
            try 
            {
                battleFieldConfig.save(battleFieldFile);
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(BattleField.class.getName()).log(Level.SEVERE, null, ex);
            }
    Thanks,
    Bowman3002
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    If you have that many files, instead of writing one set of methods, you should try writing one set of methods for all of them. You will noticed that for all the methods that you have written so far, you should notice that some parts of the method are the same every time. You want to replace the part that is changing with parameters to the method. I'll leave it here as a programing exercise. Remember keep the invariants and replaces the variants with variables.
     
  3. Offline

    bowman3002

    Thank you so much for that. (A) It will clean up my saving and loading and make it more efficient and (2) in my transferring to the smaller methods I found my issue, in the later file configs I was saving them to the same location as the battlefield config. So when I saved all of the blank yamls they overwrote the one that should have had something in it.

    Thanks,
    Bowman3002
     
Thread Status:
Not open for further replies.

Share This Page