Skip to content
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

Merged
merged 2 commits into from
Oct 20, 2024
Merged

GH-41 Add command cooldowns #40

merged 2 commits into from
Oct 20, 2024

Conversation

Rollczi
Copy link
Member

@Rollczi Rollczi commented Sep 22, 2024

Releted: #41

Summary by CodeRabbit

  • New Features

    • Introduced command cooldown functionality to enhance user experience and command management.
    • Added configuration options for command cooldowns via a new commands.yml file.
  • Improvements

    • Enhanced user feedback through formatted messages related to command cooldowns.
  • Bug Fixes

    • Resolved issues with command execution timing, ensuring smoother interactions.

@Rollczi Rollczi changed the title Add command cooldowns GH-35 Add command cooldowns Sep 22, 2024
@Rollczi Rollczi added the 🆕 feature New feature or request label Sep 22, 2024
@Rollczi Rollczi requested a review from igoyek September 22, 2024 21:22
@vLuckyyy vLuckyyy linked an issue Sep 22, 2024 that may be closed by this pull request
@vLuckyyy vLuckyyy changed the title GH-35 Add command cooldowns GH-41 Add command cooldowns Sep 22, 2024
@vLuckyyy
Copy link
Member

vLuckyyy commented Oct 9, 2024

@coderabbitai full review

Copy link

coderabbitai bot commented Oct 9, 2024

✅ Actions performed

Full review triggered.

Copy link

coderabbitai bot commented Oct 9, 2024

Walkthrough

The changes introduce command cooldown functionality to the EconomyBukkitPlugin class and related components. A new CommandCooldownConfig class manages cooldown settings, while CommandCooldownEditor and CommandCooldownMessage facilitate command modification and user messaging, respectively. The CommandsConfig class is updated to include mappings for command cooldowns, enhancing the overall command management system. These enhancements are designed to improve user feedback and control over command execution timing.

Changes

File Path Change Summary
eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java Enhanced to support command cooldowns; added imports, updated onEnable method for CommandsConfig.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownConfig.java New class added to manage command cooldown settings with fields for duration, bypass permission, and message.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownEditor.java New class added to modify command builders for cooldown configurations; includes constructor and edit method.
eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java New class added to generate messages related to command cooldowns; includes fields for notice service and commands config.
eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/CommandsConfig.java New class added to manage command cooldown configurations with a public field mapping command names to cooldown settings.

Poem

In the land of code where bunnies play,
Cooldowns now keep the spammers at bay.
With messages bright and editors keen,
Commands are managed, a coder's dream!
Hops of delight in the plugin's new tune,
A joyful update, we celebrate soon! 🐇✨


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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 extends OkaeriConfig, 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:

  1. Use an immutable map for better thread-safety:

    public final Map<String, CommandCooldownConfig> cooldowns = Map.ofEntries(
        Map.entry("pay", new CommandCooldownConfig())
    );
  2. 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))
    );
  3. 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: The edit 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, the CommandCooldownEditor 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 the edit 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:

  1. Editing a command with a matching cooldown configuration.
  2. Editing a command without a matching cooldown configuration.
  3. 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() method

The 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 a Supplier<String> or equivalent functional interface without parameters.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3892ab5 and b47032f.

📒 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 extending OkaeriConfig 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 the Editor<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 the commandsConfig 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 type

The method get is expected to return an Object, but the returned value is built using the NoticeService. Verify that the object returned by noticeService.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 Added

The imports CommandCooldownEditor and CommandCooldownMessage are correctly added to support the new command cooldown features.


31-31: CommandsConfig Import Added Correctly

The import CommandsConfig is required to load command configurations from commands.yml.


47-47: LiteMessages Import Added

The import of LiteMessages enables the use of predefined message keys, such as COMMAND_COOLDOWN.


88-88: Ensure Proper Exception Handling When Loading CommandsConfig

While CommandsConfig is initialized similarly to other configurations, please verify that exception handling is properly implemented to handle cases where commands.yml may be missing or malformed. This will prevent potential runtime errors during plugin initialization.


120-121: Command Cooldown Functionality Integrated into liteCommands Builder

The methods .message(LiteMessages.COMMAND_COOLDOWN, new CommandCooldownMessage(noticeService, commandsConfig)) and .editorGlobal(new CommandCooldownEditor(commandsConfig)) correctly integrate the command cooldown features into the liteCommands setup.

@vLuckyyy vLuckyyy merged commit e209415 into master Oct 20, 2024
@vLuckyyy vLuckyyy deleted the command-cooldowns branch October 20, 2024 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cooldown for /pay command
4 participants