Hello,
I think an important tool for administrators/moderators (now that we can change names) would be a command to get a player’s previous names. Thanks to mojang’s api that doesn’t seem to be very complicated to do.
The idea would be:
- getting targeted player’s UUID
- sending request to mojang (
https://api.mojang.com/user/profiles/{uuid here}/names
) - sending result to CommandSource instance
I’m posting this here so the people interested can eventually help. I’m not looking for people to do the work for me, I only thought it could be of use to the community. The thing is I’m absolutely not familliar with http request in java . Does anyone have any good tutorial about that ?
Also I presume the command process should be async, Is it possible to do it using the scheduler service ?
EDIT
Thanks to all your answers, I came up with this (special thanks to dualspiral’s Hammer). I tested it, it works fine but tell me if you see any mistake in the code (here we assume MyPluginClassHere
is the plugin’s main class and its methods are correctly implemented).
public class PrevNamesExecutor implements CommandExecutor
{
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
Player player = args.<Player>getOne("player").get();
UUID uuid = player.getUniqueId();
src.sendMessage(Texts.of(TextColors.AQUA, "Trying to get player's previous names"));
MyPluginClassHere.getTaskBuilder().execute(new PreviousNamesFetcher(uuid, src)).async().submit(MyPluginClassHere.getInstance());
return CommandResult.success();
}
}
and somewhere else:
public class PrevNamesFetcher implements Runnable
{
private CommandSource src;
private UUID uuid;
public PrevNamesFetcher(UUID uuid, CommandSource src)
{
this.uuid = uuid;
this.src = src;
}
public void run()
{
String str = getPrevNames();
if (str == null)
{
src.sendMessage(Texts.of(TextColors.RED, "Error while getting player's previous names"));
return;
}
TextBuilder builder = Texts.builder("");
Iterator<JsonElement> iter = (new JsonParser()).parse(str).getAsJsonArray().iterator();
builder.append(
Texts.of(TextColors.DARK_AQUA, "Previous names: "),
Texts.of(TextColors.AQUA, iter.next().getAsJsonObject().get("name").getAsString().concat("\n")));
while (iter.hasNext())
{
JsonObject obj = iter.next().getAsJsonObject();
Date date = new Date(obj.get("changedToAt").getAsLong());
String day = (new SimpleDateFormat("dd-MM-yyyy")).format(date);
String time = (new SimpleDateFormat("HH:mm:ss")).format(date);
builder.append(
Texts.of(TextColors.DARK_AQUA, "changed to "),
Texts.of(TextColors.AQUA, obj.get("name").getAsString()),
Texts.of(TextColors.DARK_AQUA, " on "),
Texts.of(TextColors.AQUA, day),
Texts.of(TextColors.DARK_AQUA, " at "),
Texts.of(TextColors.AQUA, time));
}
src.sendMessage(builder.build());
}
public String getPrevNames()
{
String target = String.format("https://api.mojang.com/user/profiles/%s/names", uuid.toString().replace("-", ""));
String r = null;
HttpURLConnection connection = null;
try
{
URL url = new URL(target);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.connect();
if (connection.getResponseCode() != 200)
{
return null;
}
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = rd.readLine()) != null)
{
response.append(line);
response.append('\r').append('\n');
}
rd.close();
r = response.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
return r;
}
}