mirror of
https://github.com/plexusorg/Plex.git
synced 2025-06-29 14:56:43 +00:00
Migrate (what I could find) legacy component system uses to kyori component system
Create List command Remove fionn command Remove test command Add Mojang Utils Auto add Plex Players back to cache on start if any are online
This commit is contained in:
76
src/main/java/dev/plex/util/MojangUtils.java
Normal file
76
src/main/java/dev/plex/util/MojangUtils.java
Normal file
@ -0,0 +1,76 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
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.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MojangUtils
|
||||
{
|
||||
public static UUID getUUID(String name)
|
||||
{
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
HttpGet get = new HttpGet("https://api.mojang.com/users/profiles/minecraft/" + name);
|
||||
try
|
||||
{
|
||||
HttpResponse response = client.execute(get);
|
||||
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
JSONObject object = new JSONObject(json);
|
||||
client.close();
|
||||
return UUID.fromString(new StringBuilder(object.getString("id"))
|
||||
.insert(8, "-")
|
||||
.insert(13, "-")
|
||||
.insert(18, "-")
|
||||
.insert(23, "-").toString());
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
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()).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@ package dev.plex.util;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.config.Config;
|
||||
import dev.plex.storage.StorageType;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.PluginCommandYamlParser;
|
||||
@ -177,7 +179,12 @@ public class PlexUtils
|
||||
|
||||
public static void broadcast(String s)
|
||||
{
|
||||
Bukkit.broadcastMessage(s);
|
||||
Bukkit.broadcast(LegacyComponentSerializer.legacyAmpersand().deserialize(s));
|
||||
}
|
||||
|
||||
public static void broadcast(Component component)
|
||||
{
|
||||
Bukkit.broadcast(component);
|
||||
}
|
||||
|
||||
public static Object simpleGET(String url) throws IOException, ParseException
|
||||
|
Reference in New Issue
Block a user