Help with configs

Discussion in 'Plugin Development' started by gamerguy14, May 22, 2012.

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

    gamerguy14

    I am working on a feature in a plugin that requires the admin to create new sections in the config based on what values they put in another place but whenever the sections are changed, bukkit removes the changes by copying the default config back. What I want to know is how to keep those sections either by changing the default config or stopping bukkit from copying the sections out of the default config after it has been copied for the first time.
     
  2. Offline

    BobbyD441

    Can you post some code please?
     
  3. Offline

    gamerguy14

    This is just the basics of what I want, the actual case has some unnecessary parts in it.

    Code:
    FileConfiguration config = getConfig();
    List<String> listOfStrings = config.getStringList("List");
    for (String x : listOfStrings) {
        String someString = config.getString(x + ".String Spot");
        int num = config.getInt(x + ".num");
    }
    That is basically it. Path with x is not in the default config and gets erased when the config is loaded.
     
  4. Offline

    BobbyD441

    How do you create the initial config? Do you check if the file is already there before you write a first one?

    Code:
    file = new File(getDataFolder(), "config.yml");
            fileConfig = new YamlConfiguration();
            if(!file.exists())
            {                                    // checks if the yaml does not exists
                file.getParentFile().mkdirs();                    // creates the /plugins/<pluginName>/ directory if not found
                try
                    {
                        fileConfig = getConfig();
                        getConfig().set("some.config", 1);
                        saveConfig();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
            }
            else
            {
               
            }
     
  5. Offline

    gamerguy14

    I just use the getConfig() method and it gives the config object with the config set to the default config.yml.
     
  6. Offline

    BobbyD441

    Yes... But do you write the default config everytime the plugin starts, or do you check if the file is already there?
    And please post some code, i cant see errors without it =/
     
  7. Offline

    gamerguy14

    I put a default config.yml in the .jar and bukkit copies that file.
     
  8. Offline

    BobbyD441

    every time the plugin starts?
     
  9. Offline

    gamerguy14

    It copies it when the getConfig() method is called which happens every time that the plugin starts so that it can be read.
     
  10. Offline

    BobbyD441

    You keep describing your code... Yet, I see no code =/
     
  11. Offline

    Sagacious_Zed Bukkit Docs

    getConfig() reads config.yml from your jar if they exist, and reads config,yml from your plugin's data folder. The config.yml in the data folder will take precedence.

    getConfig() WILL NOT write a file to the disk.
     
  12. Offline

    gamerguy14

    The other methods that are not shown don't matter to what I am trying to get help for. They are use methods that read values from the config.

    Code:
        private PluginDescriptionFile info;
        private FileConfiguration config;
        private Map<World, ArrayList<Location>> origins;
        private String message, joinMessage, portalMessage, respawnMessage, tpMessage;
        private boolean sendMessage, sendJoinMessage, sendPortalMessage, sendRespawnMessage, sendTpMessage;
        private StatSolver zones, damage, health, xp;
        private ArrayList<EntityType> affectedMobs;
        private boolean useAffectedMobs;
        private HashMap<UUID, Integer> levels;
        private PluginManager manager;
        private ArrayList<Drop> drops;   
     
        @Override
        public void onEnable() {
            info = getDescription();
            origins = new HashMap<World, ArrayList<Location>>();
            manager = getServer().getPluginManager();
       
            getConfig().options().copyDefaults(true);
            config = getConfig();
       
            getMessages();
            setupOrigins();
            zones = getEquation("Equations.Zone");
            damage = getEquation("Equations.Damage");
            health = getEquation("Equations.Health");
            xp = getEquation("Equations.XP");
            setupDrops();
       
            manager.registerEvents(new Entities(this), this);
            manager.registerEvents(new Players(this), this);
       
            getCommand("zone").setExecutor(new Commands(this));
       
            levels = new HashMap<UUID, Integer>();
            List<World> worlds = getServer().getWorlds();
            for (World world : worlds) {
                List<LivingEntity> entities = world.getLivingEntities();
                for (LivingEntity entity : entities) {
                    levels.put(entity.getUniqueId(), level(closestOriginDistance(entity.getLocation())));
                    entity.setHealth(health(levels.get(entity.getUniqueId())));
                }
            }
        }
     
        private void setupDrops() {
            drops = new ArrayList<Drop>();
            if (!config.contains("Drops")) return;
            List<String> dropper = config.getStringList("Drops");
            for (String x : dropper) {
                if (!config.contains(x)) {
                    System.out.print("[" + info.getName() + "] Config does not conatain " + x);
                    continue;
                }
                List<String> mobs;
                if (!config.contains(x + ".Mobs")) mobs = new ArrayList<String>();
                else mobs = config.getStringList(x + ".Mobs");
                ArrayList<EntityType> usedMobs = new ArrayList<EntityType>();
                for (String y : mobs) usedMobs.add(EntityType.fromName(y));
                int startZone;
                if (!config.contains(x + ".Start Zone")) startZone = 0;
                else startZone = config.getInt(x + ".Start Zone");
                int endZone;
                if (!config.contains(x + ".End Zone")) endZone = -1;
                else endZone = config.getInt(x + ".End Zone");
                int numerator, denominator;
                if (!config.contains(x + ".Odds")) {
                    numerator = 1;
                    denominator = 1;
                }
                else {
                    String odds = config.getString(x + ".Odds");
                    String[] odder = odds.split("/");
                    if (odder.length == 1) {
                        numerator = 1;
                        denominator = 1;
                    }
                    else {
                        numerator = Integer.parseInt(odder[0]);
                        denominator = Integer.parseInt(odder[1]);
                    }
                }
                if (!config.contains(x + ".Items")) {
                    System.out.println("[" + info.getName() + "] " + x + " needs items");
                    continue;
                }
                List<String> items = config.getStringList(x + ".Items");
                ArrayList<ItemStack> allItems = new ArrayList<ItemStack>();
                for (String item : items) {
                    String[] parts = item.split(",");
                    int id = Integer.parseInt(parts[0]);
                    int amt;
                    if (parts.length == 1) amt = 1;
                    else amt = Integer.parseInt(parts[1]);
                    allItems.add(new ItemStack(id, amt));
                }
                drops.add(new Drop(allItems, startZone, endZone, numerator, denominator, usedMobs, this));
            }
        }
     
  13. Offline

    thehutch

    Just create a config.yml like you would a plugin.yml then use saveDefaultConfig(); which will extract it from the .jar and place it nicely in your plugins folde.
     
  14. Offline

    gamerguy14

    I want it to not get put in my plugins folder.
     
  15. Offline

    thehutch

    I don't understand your answer ;(

    Your original question was to stop Bukkit from overwriting the config,yml once it has been made.

    Do as I say:

    1. Create a config.yml inside your .jar like you would create your plugin.yml
    2. inside the onEnable() add this.
      • Code:
        File config = new File(this.getDataFolder(), "config.yml");
         
        if (!config.exists()) {
            this.saveDefaultConfig();
        }
    3. You're done that's all you have to do now is fill in the config.yml which is inside your plugins jar with all the fields and stuff you want to be configured
    To get data from the config do the following:

    1. FileConfiguration config = <plugin>.getConfig();
    2. config.getString("path.to.string");
    3. Done!
     
  16. Offline

    gamerguy14


    What the problem is, is that every time I run the plugin, it changes the fields in config.yml that I created and do not exist in the default config.yml that is in the .jar. I want to create the config with default values on the first run then I don't want the config.yml to be changed at all by the plugin or bukkit and only be able to be changed by going into the file and typing in new values.
     
  17. Offline

    BobbyD441

    That answer has been given twice in this thread...
    Code:
    File config = new File(this.getDataFolder(), "config.yml");
     
    if (!config.exists()) {
        this.saveDefaultConfig();
    }
     
  18. Offline

    gamerguy14

    This still doesn't work.
     
  19. why does it not work? explain.
     
  20. Offline

    Sagacious_Zed Bukkit Docs

    Do you have an onDisable method? Also just show all of your code, you may be causing some unintentional side effect somewhere. Seriously, the location where you think the problem is rarely is where the problem originally started.
     
  21. Offline

    gamerguy14

    Okay I found the problem. I wasn't copying in the right .jar so no matter what I tried it didn't work. Fixed now.
     
Thread Status:
Not open for further replies.

Share This Page