Plex/src/main/java/dev/plex/command/impl/NameHistoryCMD.java

79 lines
2.6 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.command.impl;
2020-11-05 21:17:14 +00:00
import com.google.common.collect.ImmutableList;
2021-01-03 07:21:15 +00:00
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandArgumentException;
import dev.plex.command.source.CommandSource;
2020-11-06 01:29:38 +00:00
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.List;
2021-01-03 07:21:15 +00:00
import dev.plex.command.PlexCommand;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
@CommandParameters(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");
}
@Override
public void execute(CommandSource sender, String[] args)
{
if (args.length != 1)
2020-11-06 01:29:38 +00:00
{
throw new CommandArgumentException();
2020-11-06 01:29:38 +00:00
}
String username = args[0];
JSONArray array;
try
{
2020-11-06 01:29:38 +00:00
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)
{
send(tl("nameHistoryFail", username));
return;
}
array.sort(Comparator.reverseOrder());
StringBuilder result = new StringBuilder()
.append(tl("nameHistoryTitle", username))
.append("\n");
for (Object o : array)
{
2020-11-06 01:29:38 +00:00
JSONObject object = (JSONObject)o;
Object changedToAt = object.get("changedToAt");
if (changedToAt == null)
2020-11-06 01:29:38 +00:00
{
changedToAt = "O";
2020-11-06 01:29:38 +00:00
}
else
2020-11-06 01:29:38 +00:00
{
changedToAt = DATE_FORMAT.format(changedToAt);
2020-11-06 01:29:38 +00:00
}
result.append(tl("nameHistoryBody", object.get("name"), changedToAt))
2020-11-06 01:29:38 +00:00
.append("\n");
}
send(result.toString());
}
@Override
public List<String> onTabComplete(CommandSource sender, String[] args)
{
2020-11-05 21:17:14 +00:00
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}