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-875 Add server capacity command. #899

Merged
merged 3 commits into from
Feb 6, 2025
Merged

GH-875 Add server capacity command. #899

merged 3 commits into from
Feb 6, 2025

Conversation

vLuckyyy
Copy link
Member

Fixes #875

Copy link
Contributor

coderabbitai bot commented Jan 30, 2025

Walkthrough

This change introduces a new command for adjusting server capacity. The pull request adds a new method to the translation interface and corresponding fields in different language implementations to support messages for slot changes. It also updates the reflection utility with functions for dynamic method invocation. In the setslot feature package, new classes are added: a command handler to process slot commands, a service to update the server capacity using reflection, and a saver to persist the new capacity value into the server.properties file. Additionally, message classes for different locales are provided for notifications related to slot setting.

Assessment against linked issues

Objective Addressed Explanation
Add server capacity change command (#875)

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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
Contributor

@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

🧹 Nitpick comments (5)
eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/PLServerCapacityMessages.java (2)

8-11: Add class documentation.

Consider adding a brief Javadoc comment to explain the purpose of this Polish message provider.

+/**
+ * Polish implementation of server capacity messages.
+ */
 @Getter
 @Accessors(fluent = true)
 @Contextual
 public class PLServerCapacityMessages implements ServerCapacityMessages {

12-12: Consider making the field private and moving the message to a resource file.

A few suggestions to improve this:

  1. Make the field private to better encapsulate it
  2. Consider moving the message text to a resource bundle for easier maintenance
-    public Notice slotSaved = Notice.chat("<green>► <white>Sloty serwera zostały ustawione na <green>{SLOTS} <white>i zapisane!");
+    private final Notice slotSaved = Notice.chat("<green>► <white>Sloty serwera zostały ustawione na <green>{SLOTS} <white>i zapisane!");
eternalcore-core/src/main/java/com/eternalcode/core/util/ReflectUtil.java (1)

100-107: Consider enhancing getDeclaredMethod to support methods with parameters.

The method currently only works for no-arg methods. This might be too limiting for general use.

Here's a simple way to make it more flexible:

-    public static Method getDeclaredMethod(Class<?> clazz, String methodName) {
+    public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
         try {
-            return clazz.getDeclaredMethod(methodName);
+            return clazz.getDeclaredMethod(methodName, parameterTypes);
         }
         catch (NoSuchMethodException exception) {
             throw new IllegalArgumentException("Method " + methodName + " not found in class " + clazz.getName(), exception);
         }
     }
eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityService.java (1)

24-33: Set capacity logic is direct.

Consider logging when the capacity is changed to help diagnose issues quickly.

eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacitySaver.java (1)

18-24: Consider a more descriptive constant name.

The constant name could be more specific to better indicate its purpose.

-    private static final String MAX_PLAYERS_PROPERTY = "max-players";
+    private static final String SERVER_MAX_PLAYERS_PROPERTY = "max-players";
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4db61f0 and 972a7aa.

📒 Files selected for processing (10)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityCommand.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacitySaver.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityService.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/ENServerCapacityMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/PLServerCapacityMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/ServerCapacityMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java (2 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java (2 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java (2 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/util/ReflectUtil.java (2 hunks)
🔇 Additional comments (14)
eternalcore-core/src/main/java/com/eternalcode/core/util/ReflectUtil.java (1)

109-116: Add security checks before method invocation.

Unrestricted reflection access could pose security risks. Consider adding checks for the calling context.

Let's check if this utility is used safely:

eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityService.java (6)

11-12: Clean class definition.

This service annotation and class naming look consistent and clear.


14-16: Good job initializing fields.

The final fields keep the class thread-safe.


18-22: Constructor usage is straightforward.

Injecting the saver here keeps things nicely organized.


35-41: Reflection call is concise.

This method neatly encapsulates the reflection to get the player list object.


42-60: Clever field detection.

Relying on the current max player check can be risky if multiple int fields share the same value. You might want a stricter approach or naming check.


62-69: Setting the new value.

Exception handling is appropriate here. Looks good overall.

eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/ServerCapacityMessages.java (1)

5-7: Interface is well-defined.

A simple contract for capacity messages keeps it flexible.

eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/messages/ENServerCapacityMessages.java (1)

1-13: Clear and friendly messaging.

Fluent accessors and a contextual approach keep this easy to understand.

eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityCommand.java (2)

14-28: Looking good! Clean command setup.

The command structure follows best practices with proper dependency injection and clear permission control.


34-40: Nice notification implementation!

The code follows the builder pattern consistently and handles placeholders well.

eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java (1)

449-450: Clean interface addition!

The new method follows the existing pattern and is well-placed in the interface.

eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java (1)

849-851: Perfect translation implementation!

The server capacity messages are properly integrated with clear documentation.

eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java (1)

873-875: Looks good!

The new server capacity messages section is properly integrated into the translations.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (2)
eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityCommand.java (2)

14-16: Consider making the permission more specific.

The current permission 'eternalcore.servercapacity' could be more descriptive. Consider using 'eternalcore.servercapacity.set' to better reflect that this command modifies server settings.


30-48: Add an upper limit check for server slots.

Thanks for adding the validation for negative numbers! To prevent potential server issues, it would be good to add an upper limit too (e.g., 1000 slots).

 public void execute(@Context Viewer viewer, @Arg int slots) {
     if (slots <= 0) {
         this.noticeService.create()
             .notice(notice -> notice.argument().numberBiggerThanOrEqualZero())
             .viewer(viewer)
             .send();
         return;
     }
+    if (slots > 1000) {
+        this.noticeService.create()
+            .notice(notice -> notice.argument().numberTooBig())
+            .viewer(viewer)
+            .send();
+        return;
+    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 972a7aa and b0d4b56.

📒 Files selected for processing (1)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityCommand.java (1 hunks)
🔇 Additional comments (2)
eternalcore-core/src/main/java/com/eternalcode/core/feature/servercapacity/ServerCapacityCommand.java (2)

1-13: Looking good! All the right imports are here.

The imports are well-organized and include everything needed for the command to work.


18-28: Nice work with dependency injection!

The constructor and fields are well-structured, using final fields and proper dependency injection.

@Rollczi
Copy link
Member

Rollczi commented Feb 5, 2025

🐒

Copy link
Contributor

@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: 1

🧹 Nitpick comments (5)
eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/ENSetSlotMessages.java (1)

12-12: Consider making the field private

The field could be private since we have @Getter already.

-    public Notice slotSaved = Notice.chat("<green>► <white>Server slots have been set to <green>{SLOTS} <white>and saved!");
+    private Notice slotSaved = Notice.chat("<green>► <white>Server slots have been set to <green>{SLOTS} <white>and saved!");
eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/PLSetSlotMessages.java (1)

12-12: Consider making the field private

Just like in ENSetSlotMessages, the field could be private since @Getter is used.

-    public Notice slotSaved = Notice.chat("<green>► <white>Sloty serwera zostały ustawione na <green>{SLOTS} <white>i zapisane!");
+    private Notice slotSaved = Notice.chat("<green>► <white>Sloty serwera zostały ustawione na <green>{SLOTS} <white>i zapisane!");
eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotCommand.java (1)

30-48: Consider adding a constant for the minimum slot value.

Instead of using the magic number 0 directly, it would be clearer to define it as a constant.

Here's a simple way to improve it:

+    private static final int MIN_SLOTS = 0;
+
     @Execute
     @Async
     @DescriptionDocs(description = "Set the max players on the server")
     public void execute(@Context Viewer viewer, @Arg int slots) {
-        if (slots <= 0) {
+        if (slots <= MIN_SLOTS) {
eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotService.java (1)

35-40: Potential method rename risk
The reflect call may fail if “getHandle” is changed in future server updates.

eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotSaver.java (1)

27-27: Consider making the properties file path configurable

Using a hardcoded path might cause issues if the server is run from a different directory.

-        File propertiesFile = new File("server.properties");
+        File propertiesFile = new File(server.getWorldContainer(), "server.properties");
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b0d4b56 and 44e43ce.

📒 Files selected for processing (9)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotCommand.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotSaver.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotService.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/ENSetSlotMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/PLSetSlotMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/SetSlotMessages.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java (2 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java (2 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java
  • eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java
🔇 Additional comments (10)
eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/messages/SetSlotMessages.java (1)

5-7: Nice and clean interface!

The interface is simple and focused, which is exactly what we want.

eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotCommand.java (3)

1-13: Looking good! All imports are in order.

Everything is properly organized and includes just what we need.


18-28: Nice work with dependency injection!

The constructor is clean and follows best practices.


14-16: Let's double-check the permission naming.

The permission "eternalcore.setslot" looks good, but let's make sure it follows the project's standard pattern.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Check other permission patterns in the codebase
rg "@Permission\(" -A 1

Length of output: 30360


Permission naming verified

The permission "eternalcore.setslot" adheres to the same naming pattern found across the project. Comparing it with other commands (like "eternalcore.afk", "eternalcore.kill", and "eternalcore.spawn") confirms that it matches the established convention. No changes are needed.

eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotService.java (5)

1-10: Neat import structure
This is clear and organized. Good work.


11-23: Class and dependency injection look solid
The service annotation and constructor injection are straightforward and tidy.


24-33: Reflection in setCapacity
Watch out for concurrency if multiple calls try to update this at once.


42-60: Field discovery by default value
This is clever, but be aware if server defaults shift, it might not locate the right field.


62-69: Exception handling is well-done
Errors will be clear and won’t go unnoticed.

eternalcore-core/src/main/java/com/eternalcode/core/feature/setslot/SetSlotSaver.java (1)

1-24: Nice work on the class setup!

The class is well-organized with proper dependency injection and clear naming.

@vLuckyyy vLuckyyy merged commit b208ad9 into master Feb 6, 2025
3 checks passed
@vLuckyyy vLuckyyy deleted the server-capacity branch February 6, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add server capacity change command.
4 participants