2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-07-04 16:57:23 +02:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-04-17 00:04:06 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Scanner;
|
2016-07-14 14:22:23 +02:00
|
|
|
import java.util.UUID;
|
2016-07-04 16:57:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Utility class and program that generate offline UUIDs for provided player names.
|
|
|
|
* <p>
|
|
|
|
* You can generate the UUID programatically using {@link #getFromNickName(String)} and
|
|
|
|
* {@link #getFromNickNames(String[])}.
|
|
|
|
*
|
|
|
|
* To use this class as a program, type
|
|
|
|
* <pre>
|
|
|
|
* java -cp<anyClassPathContainingThisClass> fr.pandacube.lib.util.OfflineUUID [playernames...]
|
|
|
|
* </pre>
|
|
|
|
* Each argument will be interpreted as a player name. If there is no argument, the program will wait for them in the
|
|
|
|
* input stream.
|
|
|
|
* For each player name, the program will print the player name, a {@code tab} character, the UUID and a line separator.
|
|
|
|
*/
|
2016-07-04 16:57:23 +02:00
|
|
|
public class OfflineUUID {
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Generate the offline {@link UUID} of the provided player name.
|
|
|
|
* @param nickname the player name to optain the offline UUID from.
|
|
|
|
* @return the offline {@link UUID} of the provided player name.
|
|
|
|
*/
|
2016-07-14 14:22:23 +02:00
|
|
|
public static UUID getFromNickName(String nickname) {
|
2022-07-10 00:55:56 +02:00
|
|
|
byte[] from_str = ("OfflinePlayer:" + nickname).getBytes(StandardCharsets.UTF_8);
|
2016-07-14 14:22:23 +02:00
|
|
|
return UUID.nameUUIDFromBytes(from_str);
|
|
|
|
}
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Generate the offline {@link UUID}s of the provided player names.
|
|
|
|
* @param nicknames an array of player name to optain the offline UUIDs from.
|
|
|
|
* @return the offline {@link UUID}s of the provided player name in an array, at the same order as the input.
|
|
|
|
*/
|
2019-10-26 23:15:49 +02:00
|
|
|
public static UUID[] getFromNickNames(String[] nicknames) {
|
2021-04-17 00:04:06 +02:00
|
|
|
Objects.requireNonNull(nicknames);
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-04 16:57:23 +02:00
|
|
|
UUID[] uuids = new UUID[nicknames.length];
|
2016-07-14 14:22:23 +02:00
|
|
|
for (int i = 0; i < nicknames.length; i++)
|
2016-07-04 16:57:23 +02:00
|
|
|
uuids[i] = getFromNickName(nicknames[i]);
|
|
|
|
return uuids;
|
|
|
|
}
|
2022-07-28 01:13:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main method for this class.
|
|
|
|
* @param args the arguments. One argument is one player name.
|
|
|
|
*/
|
2021-04-17 00:04:06 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
if (args.length == 0) {
|
|
|
|
try (Scanner s = new Scanner(System.in)) {
|
|
|
|
for(;;) {
|
2022-07-28 01:13:35 +02:00
|
|
|
System.err.print("Please input a player name: ");
|
|
|
|
if (!s.hasNextLine())
|
2021-04-17 00:04:06 +02:00
|
|
|
break;
|
|
|
|
String line = s.nextLine();
|
2022-07-28 01:13:35 +02:00
|
|
|
System.out.println(line + "\t" + getFromNickName(line));
|
2021-04-17 00:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (String arg : args)
|
2022-07-28 01:13:35 +02:00
|
|
|
System.out.println(arg + "\t" + getFromNickName(arg));
|
2021-04-17 00:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
}
|