From 22d4558d84036bc9314dcb887eda67895069a7a3 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 30 Jun 2024 08:10:49 +0100 Subject: [PATCH] use an enum for feature states --- .../server/feature/DisableSubCommand.java | 17 +++------ .../server/feature/EnableSubCommand.java | 4 +-- .../server/feature/ListSubCommand.java | 12 +++---- .../features/base/profile/FeatureProfile.java | 35 ++++++++++++++++--- .../bat/service/CommandService.java | 2 +- .../bat/service/FeatureService.java | 10 ++++++ 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java index c3d75c9..ed13b5e 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java @@ -38,7 +38,7 @@ public class DisableSubCommand extends BatSubCommand { OptionMapping featureOption = interaction.getOption("feature"); if (featureOption == null) { interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("You must provide a feature to disable") + .setDescription("You must provide a feature to enabled") .build()).queue(); return; } @@ -50,24 +50,17 @@ public class DisableSubCommand extends BatSubCommand { return; } Feature feature = FeatureService.INSTANCE.getFeature(featureName); - if (!feature.isCanBeDisabled()) { + if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.DISABLED) { interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("The feature `%s` cannot be disabled".formatted(feature.getName())) + .setDescription("The feature `%s` is already enabled".formatted(feature.getName())) .build()).queue(); return; } - if (featureProfile.isFeatureDisabled(feature)) { - interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("The feature `%s` is already disabled".formatted(feature.getName())) - .build()).queue(); - return; - } - - featureProfile.setFeatureState(feature, true); + featureProfile.disableFeature(feature); guildService.saveGuild(guild); interaction.replyEmbeds(EmbedUtils.successEmbed() - .setDescription("Successfully disabled the `%s` feature".formatted(feature.getName())) + .setDescription("Successfully enabled the `%s` feature".formatted(feature.getName())) .build()).queue(); } } diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java index 64b83f4..50b4d44 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java @@ -50,14 +50,14 @@ public class EnableSubCommand extends BatSubCommand { return; } Feature feature = FeatureService.INSTANCE.getFeature(featureName); - if (!featureProfile.isFeatureDisabled(feature)) { + if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The feature `%s` is already enabled".formatted(feature.getName())) .build()).queue(); return; } - featureProfile.setFeatureState(feature, false); + featureProfile.enableFeature(feature); guildService.saveGuild(guild); interaction.replyEmbeds(EmbedUtils.successEmbed() .setDescription("Successfully enabled the `%s` feature".formatted(feature.getName())) diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/ListSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/ListSubCommand.java index d5dccc6..d5190cb 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/ListSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/ListSubCommand.java @@ -23,15 +23,13 @@ public class ListSubCommand extends BatSubCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { StringBuilder featureStates = new StringBuilder(); - for (Feature feature : FeatureService.INSTANCE.getFeatures().values()) { + for (Feature feature : FeatureService.INSTANCE.getFeaturesSorted()) { FeatureProfile featureProfile = guild.getFeatureProfile(); - if (featureProfile.isFeatureDisabled(feature)) { - featureStates.append("❌ ").append(feature.getName()).append("\n"); - } else { - featureStates.append("✅ ").append(feature.getName()).append("\n"); - } + featureStates.append("%s `%s`\n".formatted( + featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED ? "✅" : "❌", + feature.getName() + )); } - interaction.replyEmbeds(EmbedUtils.genericEmbed() .setTitle("Feature List") .setDescription(featureStates.toString()) diff --git a/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java b/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java index 3ae69a9..00bfb51 100644 --- a/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java +++ b/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java @@ -10,10 +10,12 @@ import java.util.Map; * @author Fascinated (fascinated7) */ public class FeatureProfile extends Profile { + private static final FeatureState DEFAULT_STATE = FeatureState.ENABLED; + /** * The feature states */ - private Map featureStates; + private Map featureStates; public FeatureProfile() { super("feature"); @@ -24,27 +26,45 @@ public class FeatureProfile extends Profile { * * @return the feature states */ - public boolean isFeatureDisabled(Feature feature) { + public FeatureState getFeatureState(Feature feature) { if (this.featureStates == null) { this.featureStates = new HashMap<>(); } if (feature == null) { - return false; + return DEFAULT_STATE; } String featureName = feature.getName().toUpperCase(); if (!this.featureStates.containsKey(featureName)) { - this.featureStates.put(featureName, false); + this.featureStates.put(featureName, DEFAULT_STATE); } return this.featureStates.get(featureName); } + /** + * Enables the feature + * + * @param feature the feature to enable + */ + public void enableFeature(Feature feature) { + this.setFeatureState(feature, FeatureState.ENABLED); + } + + /** + * Disables the feature + * + * @param feature the feature to disable + */ + public void disableFeature(Feature feature) { + this.setFeatureState(feature, FeatureState.DISABLED); + } + /** * Sets the feature state * * @param feature the feature to set the state for * @param state the state to set */ - public void setFeatureState(Feature feature, boolean state) { + public void setFeatureState(Feature feature, FeatureState state) { if (this.featureStates == null) { this.featureStates = new HashMap<>(); } @@ -55,4 +75,9 @@ public class FeatureProfile extends Profile { public void reset() { this.featureStates = null; } + + public enum FeatureState { + ENABLED, + DISABLED + } } diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 1371da8..ec054ae 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -222,7 +222,7 @@ public class CommandService extends ListenerAdapter { if (guild != null) { FeatureProfile featureProfile = guild.getFeatureProfile(); - if (featureProfile.isFeatureDisabled(command.getFeature())) { + if (featureProfile.getFeatureState(command.getFeature()) == FeatureProfile.FeatureState.DISABLED) { event.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The feature `%s` is disabled in this guild".formatted(command.getFeature().getName())) .build()).setEphemeral(true).queue(); diff --git a/src/main/java/cc/fascinated/bat/service/FeatureService.java b/src/main/java/cc/fascinated/bat/service/FeatureService.java index b3e5c6a..bef3baa 100644 --- a/src/main/java/cc/fascinated/bat/service/FeatureService.java +++ b/src/main/java/cc/fascinated/bat/service/FeatureService.java @@ -13,6 +13,7 @@ import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -78,4 +79,13 @@ public class FeatureService { public boolean isFeature(@NonNull String name) { return features.containsKey(name.toLowerCase()); } + + /** + * Gets the features sorted by name and status + * + * @return The features sorted + */ + public List getFeaturesSorted() { + return features.values().stream().sorted((feature1, feature2) -> feature1.getName().compareToIgnoreCase(feature2.getName())).toList(); + } }