Solved Set content in config : issues (getPlayer)

Discussion in 'Plugin Development' started by To175, Nov 18, 2014.

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

    To175

    Hi,
    I don't managed to set the content in config help please :(
    Can I put numbers as a parent/child or numbers (integers) are only available for the values ?


    Because when I do
    Code:java
    1. String test = "test";
    2. getConfig().set("1",test);

    I want to have :
    Code:
    #test
    Config:
      1: 'test'
    
    But (all comments with # are delete and) I have :
    Code:
    Config:
      '1': test
    
    Please help me... :(
    if I do set("Name", "String")
    it does :

    instead of :
    :'(

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

    HeadGam3z

    Why does it matter?
     
  3. Offline

    To175

    HeadGam3z Because it is YAML... so string are between '
     
  4. Offline

    HeadGam3z

    To175
    Yaml strings can be within no quotes, single quotes, and double quotes.

    You can easily use getConfig().getString(pathToString) and get your string with no problems regardless of quotes being there or not.
     
  5. Offline

    To175

    It doesn't work :
    Code:java
    1. String blockX = "-34";
    2. String blockY = "73";
    3. String blockZ = "243";
    4. String WandTeam = "Red";
    5. String WandHeart = "1";
    6. String newLoc = blockX+":"+blockY+":"+blockZ;
    7. getConfig().set("TeamsConfig."+WandTeam+".Hearts."+WandHeart, newLoc);
    8. getConfig().set("GameDatas.Teams."+WandTeam+".Hearts."+WandHeart, "true");
    9. saveConfig();

    And when I do that the config turns FROM :
    Code:
    TeamsConfig:
      Red:
        Hearts:
          1: '23:74:-176'
    INTO :
    Code:
    TeamsConfig:
      Red:
        Hearts:
          '1': -34:73:243
    And HeadGam3z I wrote my question in SIZE 6 BECAUSE I WANTED AN ANSWER


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

    indyetoile

    To175
    If you could only make yourself understandable, that'd be great.
    Anyways, you can still get the integer, no need to add apostrophes when getting the value.
     
  7. Offline

    To175

    indyetoile
    This has no name.
    Is that possible ? =>
    Code:
    Parent:
      1: value
      2: value2
    Or I have to do that ? =>
    Code:
    Parent:
      one: value
      two: value2
     
  8. Offline

    indyetoile

    To175
    You can do both of them, just depends on what you want.
     
  9. Offline

    To175

    indyetoile
    I want 1, 2, 3 as names ! But when I do getConfig().set("1","value"), the 1 turns into '1' ! :(
     
  10. Offline

    indyetoile

    To175
    So? That doesn't matter.
     
  11. Offline

    To175

    indyetoile thanks, sorry I thought you were absent :p
    In fact when I do that, it deletes all my comments (except the three first lines of comments at the top of the file)...
    Value contains ":" so it does that finally :
    Code:
    '1': 26:74:-176
    That's why I need the apostrophes...

    Example of code :
    Code:java
    1.  
    2. String blockX = "26";
    3. String blockY = "74";
    4. String blockZ = "-176";
    5. String WandTeam = "Red";
    6. String WandHeart = "1";
    7. String newLoc = blockX+":"+blockY+":"+blockZ;
    8. getConfig().set("TeamsConfig."+WandTeam+".Hearts."+WandHeart, newLoc);
    9. getConfig().set("GameDatas.Teams."+WandTeam+".Hearts."+WandHeart, "true");
    10. saveConfig();

    OutPut :
    Delete all comments and write :
    Code:
    MainConfig:
      HeartWand: Red:1:352
    indyetoile It doesn't matter here ??

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

    mrCookieSlime

    To175
    Please dont double post. There is an Edit Button right next to the date.

    And nyes, it does not matter for you but it does matter for YAML. See, you can just ignore these, but YAML requires these to be there, so dont remove them.
     
  13. Offline

    To175

    mrCookieSlime ok sorry. Thanks,
    but what I have to do so ?
    I tried that, it didn't work (I can't getString it do nullPointer) :
    Code:
    HeartWand: Red$1$352
    • Because if I do :
    Code:
    HeartWand: 'Red$1$352'
    Then this :
    Code:java
    1. String Wand = "Red$1$352";
    2. getConfig().set("HeartWand",Wand);
    3. saveConfig();

    Will output that :
    Code:
    HeartWand: Red$1$352
     
  14. Offline

    mrCookieSlime

    To175
    Please post your entire code using Pastebin.
     
  15. Offline

    pookeythekid

    His signature says: I am French, thanks for correcting my bad English :)

    Config comments being deleted were the bane of my existence at one point. I had no choice but to learn how to use multiple configs. When you use the method saveConfig(), it does indeed remove all but the first few lines of comments. You should create a new YAML file that contains all the content that's going to be edited by the plugin, and put your comments all at the head of the file.
     
    To175 likes this.
  16. Offline

    To175

    pookeythekid Thanks, but the fact is that ... it deletes all BUT the three first lines !
     
  17. Offline

    RW_Craft

    Use
    Code:java
    1. saveDefaultConfig;
    instead of
    Code:java
    1. getConfig.options.copyDefaults(true);
    to keep the comments in the config (#)
     
  18. Offline

    To175

    RW_Craft Instead of what ? -_-
    I never wrote this line ...
     
  19. Offline

    RW_Craft

    To175 Show me your onEnable()
     
  20. Offline

    To175

    How can i make this work please ?
    mrCookieSlime I understood the problem is from here :
    Code:java
    1. String WandInfos = getConfig().getString("HeartWand");
    2. String[] ListWandInfos = WandInfos.split("$");
    3. String WandTeam = ListWandInfos[0];
    4. String WandHeart = ListWandInfos[1];
    5. String WandItem = ListWandInfos[2];

    It sais :
    Line "91" is :
    Code:java
    1. String WandHeart = ListWandInfos[1];

    In the config I have :
    Code:
    HeartWand: Blue$3$352
    
    How can I modify the code to make this config working ?
    thanks
     
  21. Offline

    mrCookieSlime

    To175
    It should be working I think. But YAML requires you to use \\ in front of some characters like dots for example.
    Maybe try to do .split("\\$") and see if that works.
     
  22. Offline

    To175

    mrCookieSlime ok thanks, and after I have to do :
    Code:java
    1. String newLoc = blockX+"$"+blockY+"$"+blockZ;
    2. getConfig().set("TeamsConfig."+WandTeam+".Hearts."+WandHeart, newLoc);

    Do I need to add "\\" ?
     
  23. Offline

    mrCookieSlime

    To175
    Nope. See my Code Example. I am talking about the part where you split the string.
     
  24. Offline

    To175

    mrCookieSlime Hum hours and hours I'm debugging...
    is this correct ?

    Code:java
    1. String player = "to175";
    2. Bukkit.getServer().getPlayer(player).sendMessage("Hello World !");

    Because I can see a NullPointerException...

    AND Is "player.getName();" a string ?
    Code:java
    1. Player player = event.getPlayer();
    2. player.getName();
     
  25. Offline

    pookeythekid

    To175 What SDK do you use? I know in Eclipse (and I think in NetBeans), in the suggestion window when you're typing a method, it shows the classes to which the method suggestions belong. If you look at player.getName(), you'll see that it belongs to String, making it a String (go figure...). And also, the wording of player.getName() is a little self-explanatory; a name is a word, and a word is displayed as a String.

    Also, I don't think YAML and Java like the $ sign very much. I'd use commas to separate the strings.

    Also again, you said the program deleted all BUT the first three lines. I know that. I was telling you to put all of the comments at the head of the file so that it won't do that.
     
  26. Offline

    To175

    pookeythekid thanks
    JDK7
    Yes I see a string... But as I said it does a nullPointer...

    And I don't understand why we have to do getPlayer() on a player....
     
  27. Offline

    Skionz

    What do you mean getPlayer() on a player? There is no getPlayer() method in the Player class. You have to use getPlayer() on an event to return the player the event is corresponding to.
     
  28. Offline

    pookeythekid

    To175 Skionz is right. Why and how are you using that method, and shouldn't it be giving you an error in the program; the red underline error, not the stack trace? Also, the only reason (that I can think of) for an NPE to occur on player.getName() is if the player doesn't exist.
     
  29. Offline

    To175

    Skionz (+ pookeythekid) because sometimes I can see that :
    Code:java
    1. Player player = (Player) sender;
    2. String playerName = player.getPlayer().getName();
    3. //WHY T F* I have "getPlayer" ?? it is already a player !

    Just answer question by reading it first... I never said I used getPlayer on the event. Why are you talking about that now ?
    Please quote me, else I can't understand...
    I have first asked :
    And then I asked an other question to test my code in an other way...
     
  30. Offline

    Skionz

    Aren't you getting a syntax error? This is incorrect.
     
Thread Status:
Not open for further replies.

Share This Page