Adding an Increment Number Key in YML File

Discussion in 'Plugin Development' started by GeekyCompz, May 31, 2013.

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

    GeekyCompz

    Hello there,
    I am trying to store information numbered. However I cannot create another number it only overwrites the current key (obvious from code):
    ymlfile.set(player + "signs" + "1", "sign text here");

    However I want the 1 to ascend by 1 each time it is set. For example if 1 exists then create 2, if 2 exists then create 3......

    I was thinking some kind of i++ code. Although I am not that great at that kind of stuff...

    SO I was hoping one of you geniuses could figure this out. :D THANKS!
     
  2. Offline

    Seadragon91

    Here a example:
    Code:
    YamlConfiguration yaml = new YamlConfiguration();
    yaml.load(file);
     
    int counter = 1;
    while (yaml.contains("" + counter))
        counter++;
    
     
  3. Offline

    GeekyCompz

    Seadragon91
    Ok so it errored. It seems that this is wrong.
    Code:
    while (plugin.signslogger.contains("" + counter))
    its a null pointer exception? Any other way... here is what I am using:
    Code:
                SignsPlus plugin;
       
        public SignLog(SignsPlus plugin1){
            this.plugin = plugin1;
        }
     
                int counter = 1;
            while (plugin.signslogger.contains("" + counter))
                counter++;
           
            plugin.signslogger.set(player.getName() + counter + ".Location.X", locationX);
    "plugin.signslogger" is a custom yml file I created...

    Are there any other ways to solve this question? ? ? ? ? ? ?? ? ? ? THANKS

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    chasechocolate

    GeekyCompz is the "signslogger" variable null? Also, the last three lines of code don't seem to be in any method... Was that a copy-and-paste error, or are they just inside the class but not inside a method?
     
  5. Offline

    GeekyCompz

    chasechocolate that code was just a snippet from the real class/es. :D

    chasechocolate ok so the signslogger is null. DAMN! I just managed to create the YML file and actually have it create it in the plugins folder. Now this is how I actually am using "signslogger":

    PHP:
    public YamlConfiguration signslogger;
    public 
    File signsconfig;
     
    public 
    YamlConfiguration loadSignsLog() {
            
    File signsconfig = new File(getDataFolder(), "Logged-Signs.yml");
            if(!
    signsconfig.exists()) {
                try {
                    
    getDataFolder().mkdir();
                    
    signsconfig.createNewFile();
                    
    YamlConfiguration signslogger YamlConfiguration.loadConfiguration(signsconfig);
                    
    signslogger.save(signsconfig);
            }catch(
    IOException e) {
                    
    e.printStackTrace(); // So they notice
                    
    logger.severe("[SignsPlus] ยง4Couldn't create the signs logging file.");
                }
            }
            return 
    null;
        }
    HOPE it HELPS! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page