Convert to Ashcon API for NameHistory

This commit is contained in:
Taah 2022-02-06 21:06:55 -08:00
parent 282da7fdbb
commit fddf57d7f5
7 changed files with 131 additions and 85 deletions

View File

@ -43,7 +43,7 @@ public class BanManager
statement.setString(3, ban.getBanner() == null ? "" : ban.getBanner().toString()); statement.setString(3, ban.getBanner() == null ? "" : ban.getBanner().toString());
statement.setString(4, ban.getIp()); statement.setString(4, ban.getIp());
statement.setString(5, ban.getReason()); statement.setString(5, ban.getReason());
statement.setLong(6, ban.getEndDate().toInstant(ZoneOffset.of(ZoneId.systemDefault().getId())).toEpochMilli()); statement.setLong(6, ban.getEndDate().toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli());
statement.setBoolean(7, ban.isActive()); statement.setBoolean(7, ban.isActive());
statement.execute(); statement.execute();

View File

@ -6,11 +6,13 @@ import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.AshconInfo;
import dev.plex.util.MojangUtils; import dev.plex.util.MojangUtils;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -39,41 +41,29 @@ public class NameHistoryCMD extends PlexCommand
} }
String username = args[0]; String username = args[0];
UUID uuid; AshconInfo info = MojangUtils.getInfo(username);
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username); if (info == null)
if (offlinePlayer != null)
{
uuid = offlinePlayer.getUniqueId();
}
else
{
uuid = MojangUtils.getUUID(username);
}
if (uuid == null)
{ {
return Component.text("Couldn't find this user! Please check if your spelling was correct and this player exists").color(NamedTextColor.RED); return Component.text("Couldn't find this user! Please check if your spelling was correct and this player exists").color(NamedTextColor.RED);
} }
PlexLog.debug("NameHistory UUID: " + uuid); PlexLog.debug("NameHistory UUID: " + info.getUuid());
PlexLog.debug("NameHistory Size: " + info.getUsernameHistories().length);
List<Map.Entry<String, LocalDateTime>> history = MojangUtils.getNameHistory(uuid);
PlexLog.debug("NameHistory Size: " + history.size());
List<Component> historyList = Lists.newArrayList(); List<Component> historyList = Lists.newArrayList();
history.forEach(entry -> Arrays.stream(info.getUsernameHistories()).forEach(history ->
{ {
if (entry.getValue() != null) if (history.getLocalDateTime() != null)
{ {
historyList.add( historyList.add(
Component.text(entry.getKey()).color(NamedTextColor.GOLD) Component.text(history.getUsername()).color(NamedTextColor.GOLD)
.append(Component.space()) .append(Component.space())
.append(Component.text("-").color(NamedTextColor.DARK_GRAY)) .append(Component.text("-").color(NamedTextColor.DARK_GRAY))
.append(Component.space()) .append(Component.space())
.append(Component.text(DATE_FORMAT.format(entry.getValue())).color(NamedTextColor.GOLD))); .append(Component.text(DATE_FORMAT.format(history.getLocalDateTime())).color(NamedTextColor.GOLD)));
} }
else else
{ {
historyList.add( historyList.add(
Component.text(entry.getKey()).color(NamedTextColor.GOLD) Component.text(history.getUsername()).color(NamedTextColor.GOLD)
.append(Component.space())); .append(Component.space()));
} }
}); });

View File

@ -1,18 +1,16 @@
package dev.plex.punishment; package dev.plex.punishment;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.Gson;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import dev.plex.util.adapter.LocalDateTimeAdapter; import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@Getter @Getter
@Setter @Setter
public class Punishment public class Punishment
@ -44,11 +42,11 @@ public class Punishment
public String toJSON() public String toJSON()
{ {
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create().toJson(this); return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create().toJson(this);
} }
public static Punishment fromJson(String json) public static Punishment fromJson(String json)
{ {
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create().fromJson(json, Punishment.class); return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).create().fromJson(json, Punishment.class);
} }
} }

View File

@ -0,0 +1,52 @@
package dev.plex.util;
import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.List;
@Getter
@Setter
public class AshconInfo
{
private String uuid;
private String username;
@SerializedName("username_history")
private UsernameHistory[] usernameHistories;
private Textures textures;
@Getter
@Setter
@NoArgsConstructor
public static class UsernameHistory
{
private String username;
@SerializedName("changed_at")
private LocalDateTime localDateTime;
}
@Getter
@Setter
@NoArgsConstructor
public static class Textures
{
private boolean custom;
private boolean slim;
private SkinData raw;
}
@Getter
@Setter
@NoArgsConstructor
public static class SkinData
{
private String value;
private String signature;
}
}

View File

@ -1,15 +1,23 @@
package dev.plex.util; package dev.plex.util;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.List; import java.time.ZonedDateTime;
import java.util.Map; import java.time.format.DateTimeFormatter;
import java.util.UUID; import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.google.common.collect.Multimap;
import com.google.gson.*;
import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -20,21 +28,37 @@ import org.json.JSONObject;
public class MojangUtils public class MojangUtils
{ {
public static UUID getUUID(String name)
public static AshconInfo getInfo(String name)
{ {
CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://api.mojang.com/users/profiles/minecraft/" + name); HttpGet get = new HttpGet("https://api.ashcon.app/mojang/v2/user/" + name);
try try
{ {
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
if (response == null || response.getEntity() == null)
{
return null;
}
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JSONObject object = new JSONObject(json); JSONObject object = new JSONObject(json);
if (!object.isNull("code") && object.getInt("code") == 404)
{
return null;
}
client.close(); client.close();
return UUID.fromString(new StringBuilder(object.getString("id")) AshconInfo ashconInfo = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json1, typeOfT, context) ->
.insert(8, "-") LocalDateTime.ofInstant(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(json1.getAsJsonPrimitive().getAsString())), ZoneId.systemDefault())).create().fromJson(json, AshconInfo.class);
.insert(13, "-")
.insert(18, "-") Arrays.sort(ashconInfo.getUsernameHistories(), (o1, o2) -> {
.insert(23, "-").toString()); if (o1.getLocalDateTime() == null || o2.getLocalDateTime() == null)
{
return 1;
}
return o1.getLocalDateTime().compareTo(o2.getLocalDateTime());
});
return ashconInfo;
} }
catch (IOException e) catch (IOException e)
{ {
@ -42,45 +66,4 @@ public class MojangUtils
return null; return null;
} }
} }
public static List<Map.Entry<String, LocalDateTime>> getNameHistory(UUID uuid)
{
Map<String, LocalDateTime> names = Maps.newHashMap();
String uuidString = uuid.toString().replace("-", "");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet("https://api.mojang.com/user/profiles/" + uuidString + "/names");
try
{
HttpResponse response = httpClient.execute(get);
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JSONArray array = new JSONArray(json);
array.forEach(object ->
{
JSONObject obj = new JSONObject(object.toString());
String name = obj.getString("name");
if (!obj.isNull("changedToAt"))
{
long dateTime = obj.getLong("changedToAt");
Instant instant = Instant.ofEpochMilli(dateTime);
LocalDateTime time = LocalDateTime.ofInstant(instant, ZoneId.of("America/Los_Angeles"));
names.put(name, time);
}
else
{
names.put(name, null);
}
});
}
catch (IOException e)
{
e.printStackTrace();
}
return names.entrySet().stream().sorted(Map.Entry.comparingByValue((o1, o2) -> {
if (o1 == null || o2 == null)
{
return 1;
}
return o1.compareTo(o2);
})).collect(Collectors.toList());
}
} }

View File

@ -0,0 +1,20 @@
package dev.plex.util.adapter;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime>
{
@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
Instant instant = Instant.ofEpochMilli(json.getAsJsonPrimitive().getAsLong());
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
}

View File

@ -6,14 +6,17 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
public class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime> public class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime>
{ {
@Override @Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context)
{ {
return new JsonPrimitive(src.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); return new JsonPrimitive(src.toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli());
} }
} }