Ban plugin Updating to UUID Help

Discussion in 'Plugin Development' started by IcyRelic, May 15, 2014.

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

    IcyRelic

    This is really something to think about.

    Lets say i did something wrong and it shows up in prism and its banable the admin would type

    /ban IcyRelic

    that will work if im online because you can get the player by name then by uuid what about offline players? how do you ban an offline player?

    Would you need to ban them by uuid? so

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

Share This Page