Solved Get config string and add to it

Discussion in 'Plugin Development' started by Condolent, May 7, 2014.

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

    Condolent

    Hey again.
    So I'm trying to make to if a new player does a command, it's going to write the players name into the string "classed_players" in my config-file.

    I've gotten to this:
    Code:java
    1. if(cmd.getName().eqalsIgnoreCase("warrior") {
    2. getConfig().getString("classed_players");
    3. }


    And I can't remember what the code is to add something into a string?
     
  2. Code:java
    1. getConfig().set("classes_players", player.getName());


    Just a note but, if it's a config file, why store just one player name? If it's multiple players, you may need a stringList.
     
  3. Offline

    Garris0n

    Store the UUID in string form, not the name. Names can change.
     
  4. Offline

    xTigerRebornx

    Condolent Do you plan on using this to save Player data across the server stopping and starting? If so, then you'll want to use the UUID of the Player instead of their name to prepare for when Players will be able to change names.
    Either way, you want to set the value for the Player's UUID/Name
    Small pseudo-code example
    Code:
    Player player = (Player) sender;
     
    getConfig().set("path.for." + player.getUniqueId() + ".data", "Some data here");
    // Untested, is only Pseudo-code
    // Would do something along the lines of setting the Object passed in to the path that is dynamic to the Player's UUID
    // I.E. if the Player's UUID was 12345 (just an example), then it'd be set to path.for.12345.data in your config
    // You could then try grabbing it with something like
    Object o = getConfig().get("path.for." + player.getUniqueId() + "data");
    // Preform checks on 'o' as needed, you can use the methods added for easy access in the getConfig() method aswell (such as getString() and setString())
    Note: Is pseudo-code, the Player.getUniqueId() method is the method for getting the Player's UUID, check javadocs if it is wrong.
     
  5. Offline

    MoseMister

    Condolent

    What your asking is basic java.

    String addedStuff = (StringFromConfig + "new string");
     
  6. Offline

    Condolent

    DJSkepter xTigerRebornx Did not work with the set() :/
    Nothing happens!

    I made it work if I added saveConfig() when you did the command, but that messes up the whole config since I have it on saveDefaultConfig() normally. Also I think I have to make it as a list, cause I don't want it to replace the names everytime, only add to the string.
    EXAMPLE:
    Condolent does /command -> It writes my name in the config-string "classed_players". And later Notch does /command and it adds his name to the config string "classed_players" so it'll look like this: "classed_players: Condolent, Notch,
    So yeah, my question now is how do I make it a StringList and how do I do to make it save without messing the whole config up?

    My config-file looks like this:
    Code:
    # Default configuration for McRPG
    # Read through and change what's needed before publicing on your server for other players!
    # To disable something, change the 'enable' to 'disable' vice versa.
     
    # Make the /y <message> command only availible to players with admin permission? (mcrpg.admin)
    admin_yell: 'disable'
     
    # Send the player a welcome-message when joining?
    # If enable it will send the player a message
    welcome_msg: 'enable'
     
     
    #############################################
    #                                            #
    #                CLASS-LOGGER                #
    #                                            #
    #############################################
     
    # Following players have the classes set. This is editable!
     
    classed_players: 
    So without the saveDefaultConfig() it deletes all spaces and stuff..

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Condolent In order to do this, you would use a stringList (as there is multiple people):

    Code:java
    1. /** Setting a player */
    2. List<String> s = getConfig().getStringList("classed_players");
    3. s.add("UUID here");
    4. getConfig().set("classed_players", s);
    5.  
    6. /** Getting a player */
    7. List<String> s = getConfig().getStringList("classed_players");
    8. if (s.contains("UUID here")) {
    9. String playerName = UUIDUtils.getNameFromUUID(s.get(indexFromString(s, "UUID here")));
    10. /* Do offline check here */
    11. Player player = Bukkit.getPlayerExact(playerName);
    12. }


    Requires this method:
    Code:java
    1. privateint indexFromString(List<String> s, String string) {
    2. for (int i = 0; i < s.size(); i++) {
    3. String str = s.get(i);
    4. if(str.equals(string)) {
    5. return i;
    6. }
    7. }
    8. return 0;
    9. }


    And thanks to BigTeddy98

    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.DataOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStreamReader;
    5. import java.net.HttpURLConnection;
    6. import java.net.Proxy;
    7. import java.net.URL;
    8. import java.net.URLConnection;
    9. import java.util.Scanner;
    10.  
    11. import net.minecraft.util.com.google.gson.Gson;
    12.  
    13. import org.json.simple.JSONObject;
    14. import org.json.simple.parser.JSONParser;
    15.  
    16. public class UUIDUtils {
    17.  
    18. /*
    19. * Class made by BigTeddy98.
    20. *
    21. * UUIDLibrary is class to convert UUID <-> Playername
    22. *
    23. * 1. No warranty is given or implied. 2. All damage is your own
    24. * responsibility. 3. If you want to use this in your plugins, a credit
    25. * would we appreciated.
    26. */
    27.  
    28. private static Gson gson = new Gson();
    29.  
    30. public static String getNameFromUUID(String uuid) {
    31. String name = null;
    32. try {
    33. URL url = new URL("[url]https://sessionserver.mojang.com/session/minecraft/profile/[/url]" + uuid);
    34. URLConnection connection = url.openConnection();
    35. Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
    36. String json = jsonScanner.next();
    37. JSONParser parser = new JSONParser();
    38. Object obj = parser.parse(json);
    39. name = (String) ((JSONObject) obj).get("name");
    40. jsonScanner.close();
    41. } catch (Exception ex) {
    42. ex.printStackTrace();
    43. }
    44. return name;
    45. }
    46.  
    47. public static String getUUIDFromName(String name) {
    48. try {
    49. ProfileData profC = new ProfileData(name);
    50. String UUID = null;
    51. for (int i = 1; i <= 100; i++) {
    52. PlayerProfile[] result = post(new URL("[url]https://api.mojang.com/profiles/page/[/url]" + i), Proxy.NO_PROXY, gson.toJson(profC).getBytes());
    53. if (result.length == 0) {
    54. break;
    55. }
    56. UUID = result[0].getId();
    57. }
    58. return UUID;
    59. } catch (Exception e) {
    60. e.printStackTrace();
    61. }
    62. return null;
    63. }
    64.  
    65. private static PlayerProfile[] post(URL url, Proxy proxy, byte[] bytes) throws IOException {
    66. HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
    67. connection.setRequestMethod("POST");
    68. connection.setRequestProperty("Content-Type", "application/json");
    69. connection.setDoInput(true);
    70. connection.setDoOutput(true);
    71.  
    72. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    73. out.write(bytes);
    74. out.flush();
    75. out.close();
    76.  
    77. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    78. StringBuffer response = new StringBuffer();
    79. String line;
    80. while ((line = reader.readLine()) != null) {
    81. response.append(line);
    82. response.append('\r');
    83. }
    84. reader.close();
    85. return gson.fromJson(response.toString(), SearchResult.class).getProfiles();
    86. }
    87.  
    88. private static class PlayerProfile {
    89. private String id;
    90.  
    91. public String getId() {
    92. return id;
    93. }
    94. }
    95.  
    96. private static class SearchResult {
    97. private PlayerProfile[] profiles;
    98.  
    99. public PlayerProfile[] getProfiles() {
    100. return profiles;
    101. }
    102. }
    103.  
    104. private static class ProfileData {
    105.  
    106. @SuppressWarnings("unused")
    107. private String name;
    108. @SuppressWarnings("unused")
    109. private String agent = "minecraft";
    110.  
    111. public ProfileData(String name) {
    112. this.name = name;
    113. }
    114. }
    115. }
     
Thread Status:
Not open for further replies.

Share This Page