-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a6a0d8
commit 9414a68
Showing
2 changed files
with
104 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/anticope/rejects/utils/accounts/GetPlayerUUID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package anticope.rejects.utils.accounts; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
|
||
public class GetPlayerUUID { | ||
|
||
public static UUID getUUID(String playerName) { | ||
// Thanks Bento | ||
try { | ||
Gson gsonReader = new Gson(); | ||
JsonObject jsonObject = gsonReader.fromJson( | ||
getURLContent("https://api.mojang.com/users/profiles/minecraft/" + playerName), | ||
JsonObject.class); | ||
|
||
String userIdString = jsonObject.get("id").toString().replace("\"", "") | ||
.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"); | ||
|
||
return UUID.fromString(userIdString); | ||
} catch (Exception ignored) { | ||
return UUID.randomUUID(); | ||
} | ||
} | ||
|
||
private static String getURLContent(String requestedUrl) { | ||
String returnValue; | ||
|
||
try { | ||
URL url = new URL(requestedUrl); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
returnValue = br.lines().collect(Collectors.joining()); | ||
br.close(); | ||
} catch (Exception e) { | ||
returnValue = ""; | ||
} | ||
|
||
return returnValue; | ||
} | ||
|
||
} |