-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UUID Support #28
base: main
Are you sure you want to change the base?
UUID Support #28
Conversation
@AlexProgrammerDE can you review this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address the following. Otherwise, it looks mostly fine. Will give it another look after these are fixed.
@@ -15,9 +16,13 @@ | |||
import java.util.logging.Logger; | |||
|
|||
public final class PistonMute extends JavaPlugin { | |||
|
|||
@Getter | |||
public static Logger log; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No static getters!
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>2.11.0</version> | ||
</dependency> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all the newline after this text is not needed, and secondly it has to be relocated.
@Nullable UUID uuid = null; | ||
@Nullable UUID senderUUID = null; | ||
@Nullable String name = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to mark them as nullable. They are already initialized to null.
senderUUID = ((Player) sender).getUniqueId(); | ||
} | ||
|
||
if (args[0].matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this with a try catch of UUID#fromString() in a utility method. I don't like hardcoding this pattern.
public class MojangUtil { | ||
|
||
private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s"; | ||
private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names"; | ||
|
||
@SneakyThrows | ||
public static UUID getUUID(String name) { | ||
String UUIDJson = IOUtils.toString(new URL(String.format(UUID_URL, name))); | ||
|
||
if(UUIDJson.isEmpty()) return null; | ||
|
||
JSONObject UUIDObject = (JSONObject) JSONValue.parseWithException(UUIDJson); | ||
|
||
return UUIDTypeAdapter.fromString(UUIDObject.get("id").toString()); | ||
} | ||
|
||
@SneakyThrows | ||
public static String getName(UUID uuid) { | ||
String nameJson = IOUtils.toString(new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid)))); | ||
|
||
JSONArray nameValue = (JSONArray) JSONValue.parseWithException(nameJson); | ||
|
||
String playerSlot = nameValue.get(nameValue.size()-1).toString(); | ||
|
||
JSONObject nameObject = (JSONObject) JSONValue.parseWithException(playerSlot); | ||
|
||
return nameObject.get("name").toString(); | ||
} | ||
|
||
private static class UUIDTypeAdapter extends TypeAdapter<UUID> { | ||
public void write(final JsonWriter out, final UUID value) throws IOException { | ||
out.value(fromUUID(value)); | ||
} | ||
|
||
public UUID read(final JsonReader in) throws IOException { | ||
return fromString(in.nextString()); | ||
} | ||
|
||
public static String fromUUID(final UUID value) { | ||
return value.toString().replace("-", ""); | ||
} | ||
|
||
public static UUID fromString(final String input) { | ||
return UUID.fromString(input.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excuse me, WHAT? Bukkit#getOfflinePlayer already works with uuids. It even fetches a player if none is stored locally. Check the javadoc of that method.
#26