Skip to content

Instantly share code, notes, and snippets.

@jan-krueger
Last active December 28, 2020 21:53
Show Gist options
  • Save jan-krueger/10638829 to your computer and use it in GitHub Desktop.
Save jan-krueger/10638829 to your computer and use it in GitHub Desktop.
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
/**
* Created by Yonas on 20.10.2015.
*/
public class UUIDProfile {
private JSONObject result;
private String name;
private UUID identifier;
private List<String> history = new ArrayList<String>();
private boolean isPremium;
public UUIDProfile(JSONObject result) {
this.result = result;
this.work();
}
public String getName() {
return this.name;
}
public UUID getIdentifier() {
return this.identifier;
}
public boolean isPremium() {
return this.isPremium;
}
public List<String> getHistory() {
return this.history;
}
/**
* Uses the results to put them into the attributes.
*/
private void work() {
if(((JSONObject )this.result.get("result")).get("status").toString().equalsIgnoreCase("error")) {
return;
}
String username = this.result.get("username").toString();
UUID formatUUID = UUID.fromString(this.result.get("uuid").toString().replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5"));
this.isPremium = Boolean.valueOf(this.result.get("premium").toString());
Iterator<JSONObject> history = ((JSONArray) this.result.get("history")).iterator();
while(history.hasNext()) {
this.history.add(history.next().get("name").toString());
}
}
}
import org.bukkit.entity.Player;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.UUID;
/**
* Created by Yonas on 20.10.2015.
*/
public class UUIDFetcher {
private final static String API_ENDPOINT = "http://mcapi.de/api/user/%s";
public static UUIDProfile getProfile(UUID uuid) throws IOException {
return UUIDFetcher.getProfile(uuid.toString());
}
public static UUIDProfile getProfile(Player player) throws IOException {
return UUIDFetcher.getProfile(player.getName());
}
public static UUIDProfile getProfile(String playerName) throws IOException {
return new UUIDProfile(
UUIDFetcher.fetch(new URL(String.format(UUIDFetcher.API_ENDPOINT, playerName)))
);
}
private static JSONObject fetch(URL url) throws IOException {
StringBuilder jsonString = new StringBuilder();
InputStream inputStream = url.openStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
jsonString.append(line);
}
inputStream.close();
bufferedReader.close();
//--------
JSONObject jsonObject = (JSONObject)((JSONObject) JSONValue.parse(jsonString.toString()));
return jsonObject;
}
}
@blackwolf12333
Copy link

You should probably use for loops on line 44. Makes it a bit cleaner.

@jan-krueger
Copy link
Author

@blackwolf12333 You're right. Well... after this long time I updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment