mirror of
https://github.com/plexusorg/Plex.git
synced 2025-07-04 16:56:40 +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:
@ -1,31 +1,34 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandArgumentException;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.MojangUtils;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Comparator;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@CommandParameters(description = "Get the name history of a player", usage = "/<command> <player>", aliases = "nh")
|
||||
@CommandParameters(name = "namehistory", description = "Get the name history of a player", usage = "/<command> <player>", aliases = "nh")
|
||||
@CommandPermissions(level = Rank.OP)
|
||||
public class NameHistoryCMD extends PlexCommand
|
||||
{
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy 'at' HH:mm:ss");
|
||||
|
||||
public NameHistoryCMD()
|
||||
{
|
||||
super("namehistory");
|
||||
}
|
||||
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public Component execute(CommandSender sender, String[] args)
|
||||
@ -35,44 +38,52 @@ public class NameHistoryCMD extends PlexCommand
|
||||
throw new CommandArgumentException();
|
||||
}
|
||||
String username = args[0];
|
||||
JSONArray array;
|
||||
try
|
||||
|
||||
|
||||
UUID uuid;
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
|
||||
if (offlinePlayer != null)
|
||||
{
|
||||
JSONObject profile = (JSONObject)PlexUtils.simpleGET("https://api.mojang.com/users/profiles/minecraft/" + username);
|
||||
String uuid = (String)profile.get("id");
|
||||
array = (JSONArray)PlexUtils.simpleGET("https://api.mojang.com/user/profiles/" + uuid + "/names");
|
||||
}
|
||||
catch (ParseException | IOException ex)
|
||||
uuid = offlinePlayer.getUniqueId();
|
||||
} else
|
||||
{
|
||||
send(tl("nameHistoryFail", username));
|
||||
return;
|
||||
uuid = MojangUtils.getUUID(username);
|
||||
}
|
||||
|
||||
array.sort(Comparator.reverseOrder());
|
||||
|
||||
StringBuilder result = new StringBuilder()
|
||||
.append(tl("nameHistoryTitle", username))
|
||||
.append("\n");
|
||||
for (Object o : array)
|
||||
if (uuid == null)
|
||||
{
|
||||
JSONObject object = (JSONObject)o;
|
||||
Object changedToAt = object.get("changedToAt");
|
||||
if (changedToAt == null)
|
||||
{
|
||||
changedToAt = "O";
|
||||
}
|
||||
else
|
||||
{
|
||||
changedToAt = DATE_FORMAT.format(changedToAt);
|
||||
}
|
||||
result.append(tl("nameHistoryBody", object.get("name"), changedToAt))
|
||||
.append("\n");
|
||||
return Component.text("Couldn't find this user! Please check if your spelling was correct and this player exists").color(NamedTextColor.RED);
|
||||
}
|
||||
send(result.toString());
|
||||
PlexLog.debug("NameHistory UUID: " + uuid);
|
||||
|
||||
List<Map.Entry<String, LocalDateTime>> history = MojangUtils.getNameHistory(uuid);
|
||||
PlexLog.debug("NameHistory Size: " + history.size());
|
||||
List<Component> historyList = Lists.newArrayList();
|
||||
history.forEach(entry ->
|
||||
{
|
||||
if (entry.getValue() != null)
|
||||
{
|
||||
historyList.add(
|
||||
Component.text(entry.getKey()).color(NamedTextColor.GOLD)
|
||||
.append(Component.space())
|
||||
.append(Component.text("-").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.space())
|
||||
.append(Component.text(DATE_FORMAT.format(entry.getValue())).color(NamedTextColor.GOLD)));
|
||||
} else
|
||||
{
|
||||
historyList.add(
|
||||
Component.text(entry.getKey()).color(NamedTextColor.GOLD)
|
||||
.append(Component.space()));
|
||||
}
|
||||
});
|
||||
send(sender, Component.text("Name History (" + username + ")").color(NamedTextColor.GOLD));
|
||||
send(sender, Component.text("-----------------------------").color(NamedTextColor.GOLD).decoration(TextDecoration.STRIKETHROUGH, true));
|
||||
historyList.forEach(component -> send(sender, component));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(CommandSender sender, String[] args)
|
||||
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
|
||||
{
|
||||
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
|
||||
}
|
||||
|
Reference in New Issue
Block a user