Solved Setting players names in config.

Discussion in 'Spigot Plugin Development' started by TechieCrow, Nov 18, 2015.

  1. Hey guys,

    I was wondering how to set a player name in the config when doing a command, this is for my plugin Pokes, basically when someone does /poke <player> the targeted players name will get added to the config along with an integer that will increment by 1 each time that player gets poked.

    Really I just need help adding the players name to the config as a list then I think I can figure out how to increment the poked number so it shows something like this:

    Code (Text):

        - PlayerA: 7
        - PlayerB: 3
        - PlayerC: 10
    (feel free to look at the source code but be warned.... It's a bit messy and you might go blind! - (Not updated to the latest version but nothing changed to effect this.)
  2. You can't set values for elements in a list. You might be looking for a Map, or you could try just getting and setting an int using the player's name as part of the path (players.playername, etc.)

    Either way, I strongly recommend you use UUIDs so players don't lose all their pokes when they change names. Unfortunately, yaml doesn't handle raw UUIDs well, you'll want to convert them to and from strings as needed with UUID#toString and UUID#fromString
  3. Instead of saving it as a list, save each as its own path and then just loop through the configuration section to grab each of the vakues, a quick google search will teach you configuration sections and looping through config values
  4. Thanks for the reply guys, I'm almost there I think.

    I honestly forgot about UUIDs so I will be using them instead of the players name although I will say the reason why I wanted to use players names was simply because I eventually want to make the config account for the poer and poked players like PlayerA poked PlayerB: 45 with UUIDs it's still possible but holy cow the config will get confusing haha, is it possible to get the players name from UUID? That way it will show their UUIDs and their last known username to make it a little easier to read and find out who is who.

    Anyway back to it, @Jikoo you stated I needed to use "UUID#toString and UUID#fromString" I think you mean using the toString for setting it in the config and fromString for getting it from the config? If that's the case I'm probably butchering the code here btu this is what I have to check the config to see if the UUID is set, if not add it, if it is set then I want to increment the value by 1:

    Code (Text):

    //set the users UUID and poke count of 1 in config
    getConfig().set("Players." + target.getUniqueId().toString() + ".Pokes", 1);

    //get users UUID and increment poke count by 1
    getConfig().getInt("Players." + target.getUniqueId().fromString() + ".Pokes", ++i);
    Like I said, I'm probably butchering the code here, basically it sets the UUID and value in the config perfectly fine but the "fromString()" gives me an erring asking for String but I have no idea what is suppose to go there.
  5. Use UUID to fetch OfflinePlayer, call OfflinePlayer#getName
    Not exactly. If you have the UUID, anywhere where you'd need to use it in a path or value, you'd convert it to a string. However, if you wanted to convert that path or value back to a UUID to get a Player or something, you'd need to use UUID#fromString.
  6. Thank you so much for your help! I finally got it working, now that I can see the working code it's pretty obvious how it's done. xD
    • Friendly Friendly x 1