adding a string to a StringList

Discussion in 'Spigot Plugin Development' started by JordieNiels, Jun 20, 2016.

  1. Sorry if you get ofended by me. Im new to the systems of coding and just need a step in the right direction. (so no spoonfeeding)

    I am trying to make a list in my config file that will block certant people from moving. And removing them if they are already in the list.

    My method of doing this is:
    Code (Text):
    getConfig.getStringList("stopmoving").add(target.getName());
    saveConfig();
    But whenever i call the command nothing happens. No errors ot anything.
    Is there a way i can fix this. I have set up everything in the onEnable() to.
     
  2. Well for starters, it's called programming, not coding :)


    As for you problem, wouldn't it be easier to use an ArrayList rather than saving and loading from the config constantly? Would be less resource intensive too.


    Then when you shutdown the server, you can just overwrite whatever list was there before.
     
  3. When you get an object from the config, it gives you what it makes from going into what basically amounts to a text file and figuring out what type it is and making an object from it. So you can't just get the list and add to it. You have to get the list, add to it, then put it back.

    In case that explanation wasn't clear, here's an analogy I thought of:

    Getting from the config is like asking someone to read your paper to you. You get the thing that's on the paper and everything's fine. But if you want to change something in the paper, you can't just change it in your memory, you have to tell the person to change it.
     
    #3 Trophonix, Jun 20, 2016
    Last edited: Jun 20, 2016
  4. the problem is i want to keep the list even when i reload or restart
     
  5. Just put saving/loading methods in your onDisable()/onEnable(). Will be much cleaner and less prone to bugs that way.
     
  6. Make a list object. Set it to the list in the config. Save it to the config onDisable
     
  7. Well, for starters, you don't have to make unnecessary comments.

    But, when storing a list string, the list you are accessing is immutable, meaning, you cannot change a List<String>.

    So, where I create a list of strings to store in the config, this is how I do it:


    Code (Text):
    List<String> stringList = new ArrayList<String>();

    stringList.add("Your string here.");

    config.set(yourPathToStringList, (List<String>)stringList);
    saveConfig(); //or you can just do config.reload() if you're using the default
    loadConfig(); //config
     
    EDIT: If you want the string list that your making to contain the contents of the current list in the config, just set the list you've created (shown above) equal to the list in the config and apply the same steps.
     
  8. I was just trying to make a joke, hence the smiley face :(
     
  9. Oh, okay. I thought you were being serious. It's hard to judge sarcasm over the internet. :p