-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
GH-41 Add command cooldowns #40
Conversation
...economy-core/src/main/java/com/eternalcode/economy/config/implementation/CommandsConfig.java
Show resolved
Hide resolved
...onomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownConfig.java
Show resolved
Hide resolved
@coderabbitai full review |
✅ Actions performedFull review triggered. |
WalkthroughThe changes introduce command cooldown functionality to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (7)
eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/CommandsConfig.java (2)
9-10
: LGTM: Class structure is appropriate. Consider adding class-level documentation.The
CommandsConfig
class extendsOkaeriConfig
, which is appropriate for a configuration class. The structure is simple and focused. To improve maintainability, consider adding a class-level Javadoc comment explaining the purpose of this configuration class and its role in the system.Here's a suggested Javadoc comment:
/** * Configuration class for managing command cooldowns in the EternalEconomy plugin. * This class extends OkaeriConfig to leverage its configuration management capabilities. */ public class CommandsConfig extends OkaeriConfig { // ... existing code ... }
11-18
: LGTM: Well-implemented cooldowns configuration. Consider enhancing flexibility and documentation.The
cooldowns
field is well-implemented, providing a flexible way to configure command cooldowns. The default entry for the "pay" command serves as a good example. The @comment annotation offers clear guidance on the field's purpose.To further improve this implementation, consider the following suggestions:
Use an immutable map for better thread-safety:
public final Map<String, CommandCooldownConfig> cooldowns = Map.ofEntries( Map.entry("pay", new CommandCooldownConfig()) );Add more example entries to showcase different cooldown configurations:
public final Map<String, CommandCooldownConfig> cooldowns = Map.ofEntries( Map.entry("pay", new CommandCooldownConfig()), Map.entry("balance", new CommandCooldownConfig().withDuration(5)) );Enhance the comment to include information about the
CommandCooldownConfig
class:@Comment({ "Cooldowns for commands", "You can set a cooldown for each command, e.g. 'pay' command:", "The CommandCooldownConfig allows you to specify duration and other cooldown properties." })These changes would make the configuration more robust and informative for users.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownConfig.java (2)
10-11
: LGTM: 'duration' field is well-defined with a helpful comment.The 'duration' field is appropriately typed and initialized. The comment provides clear guidance on setting the duration.
Consider adding a range of recommended values in the comment to guide users in setting appropriate cooldown durations.
14-18
: LGTM: 'message' field uses builder pattern effectively for complex message configuration.The 'message' field is well-structured using the Notice builder pattern. The use of gradient colors and placeholders enhances the user experience.
Consider extracting the common prefix
"<b><gradient:#00FFA2:#34AE00>ECONOMY</gradient></b> <dark_gray>➤</dark_gray> "
into a constant or configuration value for reuse across different messages in the application.eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownEditor.java (2)
20-39
: LGTM: Theedit
method effectively implements cooldown logic.The method correctly iterates through the configured cooldowns and applies the appropriate
CooldownContext
to matching commands. It efficiently breaks the loop once a match is found, which is good for performance.Consider optimizing for scenarios with a large number of cooldowns:
@Override public CommandBuilder<CommandSender> edit(CommandBuilder<CommandSender> commandBuilder) { Meta meta = commandBuilder.meta(); + String commandName = commandBuilder.getName(); + CommandCooldownConfig cooldown = commandsConfig.cooldowns.get(commandName); - for (Map.Entry<String, CommandCooldownConfig> entry : commandsConfig.cooldowns.entrySet()) { - String commandName = entry.getKey(); - boolean isCurrent = commandBuilder.isNameOrAlias(commandName); - - if (!isCurrent) { - continue; - } - - CommandCooldownConfig cooldown = entry.getValue(); - + if (cooldown != null) { meta.put(Meta.COOLDOWN, new CooldownContext(commandName, cooldown.duration, cooldown.bypassPermission)); - break; + } else { + for (Map.Entry<String, CommandCooldownConfig> entry : commandsConfig.cooldowns.entrySet()) { + if (commandBuilder.isAlias(entry.getKey())) { + cooldown = entry.getValue(); + meta.put(Meta.COOLDOWN, new CooldownContext(entry.getKey(), cooldown.duration, cooldown.bypassPermission)); + break; + } + } } return commandBuilder; }This optimization first checks for an exact match using the command's name, which is typically faster than iterating through all entries. It falls back to checking aliases only if no exact match is found.
1-41
: Overall, theCommandCooldownEditor
class is well-implemented.The class effectively integrates cooldown functionality into the command system. It's well-structured, follows good practices like dependency injection, and correctly implements the
Editor<CommandSender>
interface. The suggested optimization for theedit
method could potentially improve performance for configurations with many cooldowns.Consider adding unit tests for this class to ensure its behavior remains correct as the project evolves. Tests should cover scenarios such as:
- Editing a command with a matching cooldown configuration.
- Editing a command without a matching cooldown configuration.
- Handling of commands with aliases.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java (1)
35-35
: Simplify lambda expression in.notice()
methodThe lambda parameter
notice
in.notice(notice -> cooldown.message)
is not used within the lambda body. You can simplify the lambda expression for clarity.Apply this diff to simplify the lambda expression:
- .notice(notice -> cooldown.message) + .notice(() -> cooldown.message)Ensure that the
.notice()
method accepts aSupplier<String>
or equivalent functional interface without parameters.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (5)
- eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java (5 hunks)
- eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownConfig.java (1 hunks)
- eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownEditor.java (1 hunks)
- eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java (1 hunks)
- eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/CommandsConfig.java (1 hunks)
🧰 Additional context used
🔇 Additional comments (13)
eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/CommandsConfig.java (1)
1-8
: LGTM: Package declaration and imports are appropriate.The package declaration is correct, and the imports are relevant to the class implementation. Good job on keeping the imports clean and focused.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownConfig.java (4)
1-7
: LGTM: Import statements are appropriate and concise.The import statements are well-organized and relevant to the class functionality. No unused imports are present.
9-9
: LGTM: Class declaration and structure are well-defined.The class name
CommandCooldownConfig
clearly describes its purpose, and extendingOkaeriConfig
is appropriate for a configuration class. The public access modifier allows necessary access from other components.
12-13
: LGTM: 'bypassPermission' field is well-defined with a clear purpose.The 'bypassPermission' field is appropriately typed and initialized. The permission string follows a common naming convention, and the comment clearly explains its purpose.
1-19
: Overall, the CommandCooldownConfig class is well-designed and implemented.The class effectively encapsulates cooldown configuration settings, providing flexibility for customization while offering sensible defaults. It follows good practices for configuration management using OkaeriConfig. The use of comments and annotations enhances usability and maintainability.
Great job on creating a clean and well-structured configuration class!
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownEditor.java (2)
1-12
: LGTM: Class structure and imports are well-organized.The
CommandCooldownEditor
class is properly structured and imports the necessary dependencies. It correctly implements theEditor<CommandSender>
interface, which aligns with its purpose of editing command builders.
14-18
: LGTM: Constructor follows dependency injection principle.The constructor is well-designed, taking a
CommandsConfig
parameter and initializing thecommandsConfig
field. This approach allows for easy testing and flexibility in providing different configurations.eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java (1)
34-38
: Verify compatibility of the return typeThe method
get
is expected to return anObject
, but the returned value is built using theNoticeService
. Verify that the object returned bynoticeService.create().notice()...
is compatible with the expected return type and works correctly with the rest of the codebase.Run the following script to search for the return type of
noticeService.create()
and ensure compatibility:eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java (5)
21-22
: Necessary Imports for Command Cooldown Functionality AddedThe imports
CommandCooldownEditor
andCommandCooldownMessage
are correctly added to support the new command cooldown features.
31-31
: CommandsConfig Import Added CorrectlyThe import
CommandsConfig
is required to load command configurations fromcommands.yml
.
47-47
: LiteMessages Import AddedThe import of
LiteMessages
enables the use of predefined message keys, such asCOMMAND_COOLDOWN
.
88-88
: Ensure Proper Exception Handling When Loading CommandsConfigWhile
CommandsConfig
is initialized similarly to other configurations, please verify that exception handling is properly implemented to handle cases wherecommands.yml
may be missing or malformed. This will prevent potential runtime errors during plugin initialization.
120-121
: Command Cooldown Functionality Integrated into liteCommands BuilderThe methods
.message(LiteMessages.COMMAND_COOLDOWN, new CommandCooldownMessage(noticeService, commandsConfig))
and.editorGlobal(new CommandCooldownEditor(commandsConfig))
correctly integrate the command cooldown features into theliteCommands
setup.
Releted: #41
Summary by CodeRabbit
New Features
Improvements
Bug Fixes