Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package cNameBot;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. import com.fasterxml.jackson.databind.JsonNode;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14.  
  15. public class MojangUtil {
  16. private static ObjectMapper mapper = new ObjectMapper();
  17.  
  18. public static HashMap<String, String> getStats() {
  19. String data = getRequest("https://status.mojang.com/check");
  20. JsonNode root;
  21. HashMap<String, String> map = new HashMap<>();
  22. try {
  23. root = mapper.readTree(data);
  24. for (JsonNode node : root) {
  25. node.fields().forEachRemaining(entry -> {
  26. map.put(entry.getKey(), entry.getValue().textValue());
  27. });
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. return map;
  33. }
  34.  
  35. public static String getUUIDFrom(String name) {
  36. String data = postRequest("https://api.mojang.com/profiles/minecraft", "[\"" + name + "\"]");
  37. try {
  38. JsonNode root = mapper.readTree(data);
  39. JsonNode node = root.path(0);
  40. return node.get("id").asText();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. return null;
  45. }
  46.  
  47. public static List<String> getPastNames(String name) {
  48. return getPastNamesFromUUID(getUUIDFrom(name));
  49. }
  50.  
  51. public static List<String> getPastNamesFromUUID(String uuid) {
  52. String data = getRequest("https://api.mojang.com/user/profiles/" + uuid + "/names");
  53. List<String> list = new LinkedList<String>();
  54. try {
  55. JsonNode root = mapper.readTree(data);
  56. for (JsonNode node : root) {
  57. String name = node.get("name").asText();
  58. if (!list.contains(name))
  59. list.add(name);
  60. }
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. return list;
  65. }
  66.  
  67. @SuppressWarnings("deprecation")
  68. public static String postRequest(String url, String data) {
  69. HttpURLConnection connection;
  70. try {
  71. URL url2 = new URL(url);
  72. connection = (HttpURLConnection) url2.openConnection();
  73. connection.setRequestMethod("POST");
  74. connection.setRequestProperty("Content-Type", "application/json");
  75. connection.setDoOutput(true);
  76. connection.setDoInput(true);
  77. DataOutputStream stream = new DataOutputStream(connection.getOutputStream());
  78. stream.write(data.getBytes("UTF-8"));
  79. stream.flush();
  80. stream.close();
  81. DataInputStream inStream = new DataInputStream(connection.getInputStream());
  82. String returnable = inStream.readLine();
  83. inStream.close();
  84. return returnable;
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. return "ERROR";
  88. }
  89. }
  90.  
  91. @SuppressWarnings("deprecation")
  92. public static String getRequest(String url) {
  93. HttpURLConnection connection;
  94. try {
  95. URL url2 = new URL(url);
  96. connection = (HttpURLConnection) url2.openConnection();
  97. connection.setRequestMethod("GET");
  98. connection.setDoOutput(true);
  99. DataInputStream inStream = new DataInputStream(connection.getInputStream());
  100. String returnable = inStream.readLine();
  101. inStream.close();
  102. return returnable;
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. return "ERROR";
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement