[BUG] Don't fetch the player profile from Mojang each time a message is sent

This commit is contained in:
2023-10-28 18:05:48 +02:00
parent de1b394fc2
commit b4485775e3
9 changed files with 32 additions and 24 deletions

View File

@@ -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;
}
}

View File

@@ -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<String, UUID> 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);

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;

View File

@@ -156,7 +156,7 @@ public class DiscordEventHandler extends ListenerAdapter {
Optional<SDLinkAccount> 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);

View File

@@ -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) {

View File

@@ -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 {