-
Notifications
You must be signed in to change notification settings - Fork 4
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
Theo
committed
Jul 10, 2022
1 parent
70c5070
commit 3ba9f8f
Showing
5 changed files
with
64 additions
and
3 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
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
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
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,56 @@ | ||
package io.github.punishmentsx.utils; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import lombok.Data; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.util.UUID; | ||
|
||
public @Data class WebPlayer { | ||
|
||
private String name; | ||
private UUID uuid; | ||
private boolean valid; | ||
|
||
public WebPlayer(String name) { | ||
fromUrl("https://api.mojang.com/users/profiles/minecraft/" + name); | ||
} | ||
|
||
private void fromUrl(String s) { | ||
try { | ||
|
||
URL url = new URL(s); | ||
HttpsURLConnection c = (HttpsURLConnection) url.openConnection(); | ||
c.setRequestMethod("GET"); | ||
c.setHostnameVerifier((hostname, session) -> true); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream())); | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
int cp; | ||
while ((cp = reader.read()) != -1) { | ||
stringBuilder.append((char) cp); | ||
} | ||
|
||
String jsonString = stringBuilder.toString(); | ||
JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject(); | ||
if (json != null) { | ||
this.uuid = UUID.fromString(json.get("id").getAsString().replaceFirst( | ||
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", | ||
"$1-$2-$3-$4-$5" | ||
)); | ||
this.name = json.get("name").getAsString(); | ||
this.valid = true; | ||
} else { | ||
this.name = null; | ||
this.uuid = null; | ||
this.valid = false; | ||
} | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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