Solved getConfig().set(getConfig().getString("blabla"), ++)

Discussion in 'Plugin Development' started by PlazmaStorm, Jul 28, 2014.

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

    PlazmaStorm

    how would i increase the number set in the config.yml file
    i know ++ adds a number but i get an error when ussing it:

    Syntax error on token "++", Expression expected after this token
    any ideas?
    thank you
     
  2. Offline

    ZodiacTheories

    PlazmaStorm

    getConfig().set(getConfig().getInt("example"), getConfig().getInt("example") + 1);

    I think it should work, untested
     
  3. Offline

    PlazmaStorm

  4. Offline

    ZodiacTheories

    PlazmaStorm

    What happens when you try it? Code please? Stack-trace?
     
  5. Offline

    PlazmaStorm

    ZodiacTheories
    getConfig().set(getConfig().getInt("example"), getConfig().getInt("example") + 1);
    .set is underlined and says:
    change to 'get'
    and when i change it to get (i known its useless but anyways) it says:
    change to 'set'
     
  6. Offline

    theguynextdoor

    PlazmaStorm I don't know what you are trying to achieve, however.

    Referring to your initial code:
    1. You are storing a path to a config variable in the config?
    2. ++ is not a number. It is a unary operator. It increments a value by 1. On it's own however it means nothing because there is no value for it to increment.

    Referring to your most recent posted code
    3. The reason the code ZodiacTheories provided won't work is because you are using getInt for the path variable. The path is a string, not an integer.
     
  7. Offline

    ZodiacTheories

    theguynextdoor

    But the OP said a number, thus I presumed he was using the wrong method
     
  8. Offline

    manguvana

    Code:
    this.getConfig().set("your.config.path", getConfig().getInt("your.config.path") +1);
    
     
  9. Offline

    PlazmaStorm

    manguvana
    nope still dosent work thx for trying to help though
     
  10. Offline

    theguynextdoor

    PlazmaStorm What manguvana said should work for what you want to achieve assuming we have understood the problem correctly.

    What he said will increase the value of the int in the config by 1. Make sure that once you have set the value that you are using the saveConfig method.
     
  11. Offline

    PlazmaStorm

    theguynextdoor
    what i am trying to do is as soon as a player uses a command they get a reward and a note in the config says a player has used it. so the config does somthing like that:
    PeopleWhoUsedCommand: 16
    is this clearer?
    thanks for helping
    ps: if you want some code i can post it just ask

    theguynextdoor
    ho that must be the problem i forgot to save the config
    ill try it out thx for pointing this out.
    totally forgot
    sorry

    theguynextdoor
    still dosent work...
    this is my code:
    Code:java
    1. if(getConfig().getString("string1") != null) {
    2. this.getConfig().set(getConfig().getString(getConfig().getString("string1")), getConfig().getInt(getConfig().getString(getConfig().getString("string1")) +1));
    3. saveConfig();
    4. }else{
    5. getConfig().createSection(getConfig().getString("string1"));
    6. getConfig().set(getConfig().getString("string1"), number);
    7. }

    I have more just done wanna show most of it.
    and the reason why it goes getConfig().getString twise is because i want the server owner to select the string name himself for example:
    stringName: lol
    lol: 14

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

    theguynextdoor

    Try to simplify your code, because atm it is far too easy to get lost in a tonne of brackets and methods.

    Trying this.

    Create a variable for your path. Your path being the path to the value of "stringName" .. or "string1" (I don't know which you are actually using).

    Create an integer value which is equal to the int you get from the config when using that path variable.
    Now set the value of the integer, using the set method, where your path is the path to the integer (i.e your path variable) and then the second parameter is equal to your value++ or value + 1.

    Then save your config.
     
  13. Offline

    PlazmaStorm

    theguynextdoor
    so somthing like this:
    Code:java
    1. int clicks = 1;
    2. if(getConfig().getString(getConfig().getString("string1") != null) {
    3. this.getConfig().set(getConfig().getString(getConfig().getString("string1"), getConfig().getInt(getConfig().getString(getConfig().getString("string1") clicks++)
    4. saveConfig();
    5. }else{
    6. getConfig().createSection(getConfig().getString(getConfig().getString("string1")));
    7. getConfig().set(getConfig().getString(getConfig().getString("string1")), clicks)
    8. }
    9.  

    Sorry i really aint to good at this.
    if you know a good way to learn java please tell me
     
  14. Offline

    theguynextdoor

    PlazmaStorm Allow me to simplify, and explain a bit.

    First things first, lets get your config organisation sorted. You effectively are just changing the value of an integer in the config, but you are letting the user choose the name of the path to the integer.

    Effectively, all you are doing is taking a path name and changing the value of an integer at that path.
    So could we not just skip the middle man and go straight to the int value?

    So you would just have a clicks variable, which you are wishing to set in the config, and also a path variable which simply leads to that int.

    So to get the int it is a matter of

    Code:java
    1. getConfig().getInt(pathName);


    then to set the value it is a matter of

    Code:java
    1. getConfig().set(pathName, clicks);


    and then you save the config after you edit it.
     
  15. Offline

    PlazmaStorm

    theguynextdoor
    i did what you said but now i get this error:
     
  16. Offline

    theguynextdoor

    Read the stack-trace then show me the line of code which the source of the error is coming from. (Hint: it's in your onCommand method).
     
  17. Offline

    PlazmaStorm

    i know it is on line 113 i saw it but i can figure out the problem

    config.set(config.getString("string1"), clicks++);
     
  18. Offline

    theguynextdoor

    Your 'config' variable is null. Show me the code where you initialise it,
     
  19. Offline

    PlazmaStorm

    FileConfiguration config;

    if(getConfig().getString("clicks") != null) {
    config.set(config.getString("clicks"), clicks++);
    saveConfig();
    }else{
    config.createSection("clicks");
    config.set("clicks", clicks);
    saveConfig();

    }
     
  20. Offline

    theguynextdoor

    You never insitialise your config variable to anything. Set it equal to getConfig()
     
  21. Offline

    PlazmaStorm

    theguynextdoor
    ok now the problem is even with this code
    Code:java
    1.  
    2. int clicks = 1
    3. if(getConfig().getString("clicks") != null) {
    4. getConfig().set("clicks", clicks++);
    5. saveConfig();
    6. }else{
    7. getConfig().createSection("clicks");
    8. getConfig().set("clicks", 1);
    9. saveConfig();
    10. }

    i do not get any errors
    but the config number always stays as 1
    im really sorry for all this trouble hope you dont mind
     
  22. Offline

    ZodiacTheories

  23. Offline

    caderape

    onEnable() getconfig().options.copydefaut(true);
    onDisable() savedefautconfig();
    (im not sure the clicks++ will work like that, i never tested, but may be)
    try to separate.
    int i = getconfig().getint("clicks") + 1;
    getconfig().set("clicks", i);

    it's an integer i guess, it's jsut checking if it's not null

    ps: you can define a File too.
    public File configf;
    configf = new File(GetdataFolder(), "config.yml");
    then you load the file after a change. getconfig().load(configf);
     
  24. Offline

    theguynextdoor

    Keep your code as it is, but replace

    clicks++
    with
    ++clicks
     
  25. Offline

    PlazmaStorm

    theguynextdoor
    it dosent work...
    now it shows a 2 all the time
    because it is 1+clicks = 2
    the clicks isnt save after it.
    how would i do that
     
  26. Offline

    DigitalSniperz

    Easy,

    Int number = getConfig().getInt("Your.Path")+1;
    getConfig().set("Your.Path", number);

    Do not even say, this dosn't work. I have multiple plugins using this and does not fail. If it does not work for you then your doing something wrong.
     
  27. Offline

    theguynextdoor

    If you want the clicks value to persist, then just edit it outside the method. So do, clicks++ or click += 1; and then you set the value in the config to that of just clicks.
     
  28. Offline

    stonar96

    Is it really so difficult to increase a variable in config?? I think your problem is the knowledge of java.

    Maybe it doesn't increas because you always initialise your variable with 1 before runnung your config code. You have to use a global variable.
     
    SpongyBacon likes this.
  29. Offline

    DigitalSniperz

    How about you try saving the config after editing it. Only a wizard knows these things.

    saveConfig();
     
  30. Offline

    caderape

Thread Status:
Not open for further replies.

Share This Page