From 679143c3312b969c05928cbbda4839a6a665dba2 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 25 Jun 2024 12:36:40 +0100 Subject: [PATCH] cleanup profiles --- .../bat/command/impl/PingCommand.java | 2 +- .../fascinated/bat/common/ProfileHolder.java | 42 +++++++++++++++++++ .../fascinated/bat/event/EventListener.java | 6 +-- .../NumberOneScoreFeedListener.java | 7 ++-- .../scoresaber/UserScoreFeedListener.java | 6 +-- .../command/scoresaber/LinkSubCommand.java | 2 +- .../command/scoresaber/ScoreSaberCommand.java | 2 +- .../cc/fascinated/bat/model/BatGuild.java | 32 +------------- .../java/cc/fascinated/bat/model/BatUser.java | 32 +------------- .../scoresaber/ScoreSaberAccountToken.java | 2 +- .../ScoreSaberLeaderboardToken.java | 2 +- .../ScoreSaberPageMetadataToken.java | 2 +- .../ScoreSaberPlayerScoreToken.java | 2 +- .../scoresaber/ScoreSaberScoreToken.java | 2 +- .../scoresaber/ScoreSaberScoresPageToken.java | 2 +- .../bat/service/ScoreSaberService.java | 1 + 16 files changed, 65 insertions(+), 79 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/ProfileHolder.java rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberAccountToken.java (97%) rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java (97%) rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java (84%) rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java (84%) rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberScoreToken.java (97%) rename src/main/java/cc/fascinated/bat/model/{ => token}/beatsaber/scoresaber/ScoreSaberScoresPageToken.java (85%) diff --git a/src/main/java/cc/fascinated/bat/command/impl/PingCommand.java b/src/main/java/cc/fascinated/bat/command/impl/PingCommand.java index 047657a..37539c2 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/PingCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/PingCommand.java @@ -27,7 +27,7 @@ public class PingCommand extends BatCommand { public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction) { long time = System.currentTimeMillis(); interaction.reply("Pinging...").queue(response -> { - response.editOriginal(("Gateway response time: `%sms`\nAPI response time `%sms`").formatted( + response.editOriginal("Gateway response time: `%sms`\nAPI response time `%sms`".formatted( DiscordService.JDA.getGatewayPing(), System.currentTimeMillis() - time )).queue(); diff --git a/src/main/java/cc/fascinated/bat/common/ProfileHolder.java b/src/main/java/cc/fascinated/bat/common/ProfileHolder.java new file mode 100644 index 0000000..b931457 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/ProfileHolder.java @@ -0,0 +1,42 @@ +package cc.fascinated.bat.common; + +import lombok.Getter; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Fascinated (fascinated7) + */ +@Getter +public class ProfileHolder { + + /** + * The profiles for the holder + */ + private Map profiles; + + /** + * Gets a profile for the holder + * + * @param clazz The class of the profile + * @param The type of the profile + * @return The profile + */ + public T getProfile(Class clazz) { + if (profiles == null) { + profiles = new HashMap<>(); + } + + Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null); + if (profile == null) { + try { + profile = (Profile) clazz.newInstance(); + profiles.put(profile.getProfileKey(), profile); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + } + return (T) profile; + } +} diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index 9373905..92ecc1f 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -1,8 +1,8 @@ package cc.fascinated.bat.event; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberLeaderboardToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; /** * @author Fascinated (fascinated7) diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java b/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java index f0971b2..de3f686 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java @@ -6,11 +6,10 @@ import cc.fascinated.bat.common.NumberUtils; import cc.fascinated.bat.common.ScoreSaberUtils; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.scoresaber.profiles.GuildNumberOneScoreFeedProfile; -import cc.fascinated.bat.features.scoresaber.profiles.GuildUserScoreFeedProfile; import cc.fascinated.bat.model.BatGuild; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberLeaderboardToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.GuildService; import lombok.extern.log4j.Log4j2; diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java b/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java index 7631243..dd7f26a 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java @@ -6,9 +6,9 @@ import cc.fascinated.bat.common.NumberUtils; import cc.fascinated.bat.common.ScoreSaberUtils; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.scoresaber.profiles.GuildUserScoreFeedProfile; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberLeaderboardToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.GuildService; diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/LinkSubCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/LinkSubCommand.java index c69e52c..28080a6 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/LinkSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/LinkSubCommand.java @@ -2,7 +2,7 @@ package cc.fascinated.bat.features.scoresaber.command.scoresaber; import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberAccountToken; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.features.scoresaber.profiles.UserScoreSaberProfile; diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java index 7d9de96..03ef9af 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java @@ -5,7 +5,7 @@ import cc.fascinated.bat.common.Colors; import cc.fascinated.bat.common.DateUtils; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.NumberUtils; -import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberAccountToken; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.features.scoresaber.profiles.UserScoreSaberProfile; diff --git a/src/main/java/cc/fascinated/bat/model/BatGuild.java b/src/main/java/cc/fascinated/bat/model/BatGuild.java index 37eadce..c284663 100644 --- a/src/main/java/cc/fascinated/bat/model/BatGuild.java +++ b/src/main/java/cc/fascinated/bat/model/BatGuild.java @@ -1,6 +1,7 @@ package cc.fascinated.bat.model; import cc.fascinated.bat.common.Profile; +import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.service.DiscordService; import lombok.Getter; import lombok.NonNull; @@ -19,42 +20,13 @@ import java.util.Map; @RequiredArgsConstructor @Getter @Setter @Document(collection = "guilds") -public class BatGuild { +public class BatGuild extends ProfileHolder { /** * The ID of the guild */ @NonNull @Id private final String id; - /** - * The profiles for this guild - */ - private Map profiles; - - /** - * Gets the profile for the guild - * - * @param clazz The class of the profile - * @param The type of the profile - * @return The profile - */ - public T getProfile(Class clazz) { - if (profiles == null) { - profiles = new HashMap<>(); - } - - Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null); - if (profile == null) { - try { - profile = (Profile) clazz.newInstance(); - profiles.put(profile.getProfileKey(), profile); - } catch (InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - } - } - return (T) profile; - } - /** * Gets the guild as the JDA Guild * diff --git a/src/main/java/cc/fascinated/bat/model/BatUser.java b/src/main/java/cc/fascinated/bat/model/BatUser.java index f9d1029..b142f95 100644 --- a/src/main/java/cc/fascinated/bat/model/BatUser.java +++ b/src/main/java/cc/fascinated/bat/model/BatUser.java @@ -1,6 +1,7 @@ package cc.fascinated.bat.model; import cc.fascinated.bat.common.Profile; +import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.service.DiscordService; import lombok.Getter; import lombok.NonNull; @@ -19,42 +20,13 @@ import java.util.Map; @RequiredArgsConstructor @Getter @Setter @Document(collection = "users") -public class BatUser { +public class BatUser extends ProfileHolder { /** * The ID of the user */ @NonNull @Id private final String id; - /** - * The profiles for this user - */ - private Map profiles; - - /** - * Gets the profile for the user - * - * @param clazz The class of the profile - * @param The type of the profile - * @return The profile - */ - public T getProfile(Class clazz) { - if (profiles == null) { - profiles = new HashMap<>(); - } - - Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null); - if (profile == null) { - try { - profile = (Profile) clazz.newInstance(); - profiles.put(profile.getProfileKey(), profile); - } catch (InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - } - } - return (T) profile; - } - /** * Gets the guild as the JDA Guild * diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberAccountToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberAccountToken.java similarity index 97% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberAccountToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberAccountToken.java index 6e5dcea..f470417 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberAccountToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberAccountToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java similarity index 97% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java index c3005ce..4d5dd49 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberLeaderboardToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.Getter; import lombok.ToString; diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java similarity index 84% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java index d3790e5..7698a19 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPageMetadataToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.Getter; import lombok.ToString; diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java similarity index 84% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java index d30a991..83d7873 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberPlayerScoreToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.Getter; import lombok.ToString; diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoreToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoreToken.java similarity index 97% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoreToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoreToken.java index d70a116..b1afa3b 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoreToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoreToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.Getter; import lombok.ToString; diff --git a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoresPageToken.java b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoresPageToken.java similarity index 85% rename from src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoresPageToken.java rename to src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoresPageToken.java index 4ebc4d3..f552fff 100644 --- a/src/main/java/cc/fascinated/bat/model/beatsaber/scoresaber/ScoreSaberScoresPageToken.java +++ b/src/main/java/cc/fascinated/bat/model/token/beatsaber/scoresaber/ScoreSaberScoresPageToken.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.model.beatsaber.scoresaber; +package cc.fascinated.bat.model.token.beatsaber.scoresaber; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java b/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java index c5410c1..5783708 100644 --- a/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java +++ b/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java @@ -8,6 +8,7 @@ import cc.fascinated.bat.exception.BadRequestException; import cc.fascinated.bat.exception.ResourceNotFoundException; import cc.fascinated.bat.model.beatsaber.scoresaber.*; import cc.fascinated.bat.features.scoresaber.profiles.UserScoreSaberProfile; +import cc.fascinated.bat.model.token.beatsaber.scoresaber.*; import com.google.gson.JsonObject; import lombok.NonNull; import lombok.SneakyThrows;