2022-01-27 09:00:50 +00:00
|
|
|
package dev.plex.util;
|
|
|
|
|
2022-02-07 05:53:57 +00:00
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonDeserializer;
|
2022-01-27 21:23:01 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.ZoneId;
|
2022-02-07 05:06:55 +00:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2022-02-07 05:53:57 +00:00
|
|
|
import java.util.Arrays;
|
2022-01-27 09:00:50 +00:00
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
public class MojangUtils
|
|
|
|
{
|
2022-02-07 05:06:55 +00:00
|
|
|
|
|
|
|
public static AshconInfo getInfo(String name)
|
2022-01-27 09:00:50 +00:00
|
|
|
{
|
|
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
2022-02-07 05:06:55 +00:00
|
|
|
HttpGet get = new HttpGet("https://api.ashcon.app/mojang/v2/user/" + name);
|
2022-01-27 09:00:50 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
HttpResponse response = client.execute(get);
|
2022-02-07 05:06:55 +00:00
|
|
|
if (response == null || response.getEntity() == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
|
|
JSONObject object = new JSONObject(json);
|
2022-02-07 05:06:55 +00:00
|
|
|
if (!object.isNull("code") && object.getInt("code") == 404)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
client.close();
|
2022-02-07 05:06:55 +00:00
|
|
|
AshconInfo ashconInfo = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json1, typeOfT, context) ->
|
|
|
|
LocalDateTime.ofInstant(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(json1.getAsJsonPrimitive().getAsString())), ZoneId.systemDefault())).create().fromJson(json, AshconInfo.class);
|
2022-01-27 09:00:50 +00:00
|
|
|
|
2022-02-07 05:06:55 +00:00
|
|
|
Arrays.sort(ashconInfo.getUsernameHistories(), (o1, o2) -> {
|
|
|
|
if (o1.getLocalDateTime() == null || o2.getLocalDateTime() == null)
|
2022-01-27 09:00:50 +00:00
|
|
|
{
|
2022-02-07 05:06:55 +00:00
|
|
|
return 1;
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
2022-02-07 05:06:55 +00:00
|
|
|
return o1.getLocalDateTime().compareTo(o2.getLocalDateTime());
|
2022-01-27 09:00:50 +00:00
|
|
|
});
|
2022-02-07 05:06:55 +00:00
|
|
|
|
|
|
|
return ashconInfo;
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
2022-01-27 09:00:50 +00:00
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2022-02-07 05:06:55 +00:00
|
|
|
return null;
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|