Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

UUID Support #28

wants to merge 2 commits into from

Conversation

RemainingToast
Copy link

#26

@RemainingToast
Copy link
Author

@AlexProgrammerDE can you review this please

Copy link
Owner

@AlexProgrammerDE AlexProgrammerDE left a 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No static getters!

Comment on lines +164 to +169
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>

Copy link
Owner

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.

Comment on lines +24 to +26
@Nullable UUID uuid = null;
@Nullable UUID senderUUID = null;
@Nullable String name = null;
Copy link
Owner

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}")) {
Copy link
Owner

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.

Comment on lines +17 to +62
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"));
}
}
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants