From b4485775e3e0c66718687bfebd6f056b51cf0968 Mon Sep 17 00:00:00 2001 From: HypherionMC Date: Sat, 28 Oct 2023 18:05:48 +0200 Subject: [PATCH] [BUG] Don't fetch the player profile from Mojang each time a message is sent --- .../sdlink/core/accounts/DiscordAuthor.java | 15 +++++++++-- .../core/accounts/MinecraftAccount.java | 25 ++++++++++--------- .../verification/StaffUnverifyCommand.java | 2 +- .../StaffVerifyAccountCommand.java | 2 +- .../UnverifyAccountSlashCommand.java | 2 +- .../verification/VerifyAccountCommand.java | 2 +- .../discord/events/DiscordEventHandler.java | 2 +- .../discord/DiscordMessageBuilder.java | 2 +- .../sdlink/core/util/SystemUtils.java | 4 --- 9 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/hypherionmc/sdlink/core/accounts/DiscordAuthor.java b/src/main/java/com/hypherionmc/sdlink/core/accounts/DiscordAuthor.java index 94df68a..971893b 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/accounts/DiscordAuthor.java +++ b/src/main/java/com/hypherionmc/sdlink/core/accounts/DiscordAuthor.java @@ -6,6 +6,7 @@ package com.hypherionmc.sdlink.core.accounts; import com.hypherionmc.sdlink.core.config.SDLinkConfig; import com.hypherionmc.sdlink.core.services.SDLinkPlatform; +import com.mojang.authlib.GameProfile; import lombok.Getter; /** @@ -27,10 +28,13 @@ public class DiscordAuthor { private final boolean isServer; @Getter - private final String username; + private String username; @Getter - private final String uuid; + private String uuid; + + @Getter + private GameProfile profile = null; /** * Internal. Use {@link #of(String, String, String)} @@ -71,4 +75,11 @@ public class DiscordAuthor { username ); } + + public DiscordAuthor setGameProfile(GameProfile profile) { + this.profile = profile; + this.username = profile.getName(); + this.uuid = profile.getId().toString(); + return this; + } } diff --git a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java index 448f20e..17f2bab 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java +++ b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java @@ -49,7 +49,7 @@ public class MinecraftAccount { private final boolean isValid; /** - * Internal. Use {@link #standard(String)} or {@link #offline(String)} + * Internal. Use {@link #of(String)} (String)} or {@link #of(GameProfile)} * @param username The Username of the Player * @param uuid The UUID of the player * @param isOffline Is this an OFFLINE/Unauthenticated Account @@ -63,10 +63,11 @@ public class MinecraftAccount { } /** - * Tries to convert a Username to an online user account. If it can not, it will return an offline user - * @param username The username to search for + * Try to fetch a player from the Mojang API. + * Will return an offline player if the request fails, or if they don't have a valid account + * @param username The username of the player */ - public static MinecraftAccount standard(String username) { + public static MinecraftAccount of(String username) { Pair player = fetchPlayer(username); if (player.getRight() == null) { @@ -81,6 +82,14 @@ public class MinecraftAccount { ); } + /** + * Convert a GameProfile into a MinecraftAccount for usage inside the mod + * @param profile The player GameProfile + */ + public static MinecraftAccount of(GameProfile profile) { + return new MinecraftAccount(profile.getName(), profile.getId(), profile.getId().version() == 3, true); + } + /** * Convert a username to an offline account * @param username The Username to search for @@ -95,14 +104,6 @@ public class MinecraftAccount { ); } - /** - * Convert GameProfile to Minecraft account - * @param profile The profile of the player - */ - public static MinecraftAccount fromGameProfile(GameProfile profile) { - return standard(profile.getName()); - } - public static SDLinkAccount getStoredFromUUID(String uuid) { sdlinkDatabase.reloadCollection("verifiedaccounts"); return sdlinkDatabase.findById(uuid, SDLinkAccount.class); diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java index 5a91b71..8deb315 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java @@ -52,7 +52,7 @@ public class StaffUnverifyCommand extends SDLinkSlashCommand { return; } - MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + MinecraftAccount minecraftAccount = MinecraftAccount.of(mcname); Result result = minecraftAccount.unverifyAccount(member, event.getGuild()); event.reply(result.getMessage()).setEphemeral(true).queue(); } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java index 444193f..44c3627 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java @@ -40,7 +40,7 @@ public class StaffVerifyAccountCommand extends SDLinkSlashCommand { return; } - MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + MinecraftAccount minecraftAccount = MinecraftAccount.of(mcname); Result result = minecraftAccount.verifyAccount(member, event.getGuild()); event.reply(result.getMessage()).setEphemeral(true).queue(); diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java index 50e2d87..a885719 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java @@ -34,7 +34,7 @@ public class UnverifyAccountSlashCommand extends SDLinkSlashCommand { for (SDLinkAccount account : accounts) { if (account.getDiscordID() != null && account.getDiscordID().equalsIgnoreCase(event.getMember().getId())) { - MinecraftAccount minecraftAccount = MinecraftAccount.standard(account.getUsername()); + MinecraftAccount minecraftAccount = MinecraftAccount.of(account.getUsername()); Result result = minecraftAccount.unverifyAccount(event.getMember(), event.getGuild()); event.reply(result.getMessage()).setEphemeral(true).queue(); break; diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java index cb5622d..0e908e7 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java @@ -49,7 +49,7 @@ public class VerifyAccountCommand extends SDLinkSlashCommand { continue; if (account.getVerifyCode().equalsIgnoreCase(String.valueOf(mcCode))) { - MinecraftAccount minecraftAccount = MinecraftAccount.standard(account.getUsername()); + MinecraftAccount minecraftAccount = MinecraftAccount.of(account.getUsername()); Result result = minecraftAccount.verifyAccount(event.getMember(), event.getGuild()); event.reply(result.getMessage()).setEphemeral(true).queue(); return; diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java b/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java index e51c526..cd1bba5 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java @@ -156,7 +156,7 @@ public class DiscordEventHandler extends ListenerAdapter { Optional account = accounts.stream().filter(a -> a.getDiscordID().equalsIgnoreCase(event.getUser().getId())).findFirst(); account.ifPresent(a -> { - MinecraftAccount acc = MinecraftAccount.standard(a.getUsername()); + MinecraftAccount acc = MinecraftAccount.of(a.getUsername()); if (acc != null) { sdlinkDatabase.remove(a, SDLinkAccount.class); diff --git a/src/main/java/com/hypherionmc/sdlink/core/messaging/discord/DiscordMessageBuilder.java b/src/main/java/com/hypherionmc/sdlink/core/messaging/discord/DiscordMessageBuilder.java index ebf3189..ed2e770 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/messaging/discord/DiscordMessageBuilder.java +++ b/src/main/java/com/hypherionmc/sdlink/core/messaging/discord/DiscordMessageBuilder.java @@ -41,7 +41,7 @@ public final class DiscordMessageBuilder { } if (SDLinkConfig.INSTANCE.chatConfig.useLinkedNames && this.author != DiscordAuthor.SERVER) { - MinecraftAccount account = MinecraftAccount.standard(author.getUsername()); + MinecraftAccount account = author.getProfile() != null ? MinecraftAccount.of(author.getProfile()) : MinecraftAccount.of(author.getUsername()); DiscordUser discordUser = account.getDiscordUser(); if (account != null && discordUser != null) { diff --git a/src/main/java/com/hypherionmc/sdlink/core/util/SystemUtils.java b/src/main/java/com/hypherionmc/sdlink/core/util/SystemUtils.java index 222c92a..9d6549e 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/util/SystemUtils.java +++ b/src/main/java/com/hypherionmc/sdlink/core/util/SystemUtils.java @@ -6,10 +6,6 @@ package com.hypherionmc.sdlink.core.util; import java.text.CharacterIterator; import java.text.StringCharacterIterator; -import java.util.Arrays; -import java.util.List; -import java.util.Random; -import java.util.concurrent.TimeUnit; public class SystemUtils {