Skip to content

Conversation

@vLuckyyy
Copy link
Member

@vLuckyyy vLuckyyy commented May 23, 2025

No description provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 23, 2025

Walkthrough

The changes remove the old command that teleported a player to a random online player. In its place, a new service is added to track teleport history between players with a 30-minute expiry. A new command uses this service to teleport a player to someone they haven’t teleported to recently, respecting configuration settings. The command also sends notifications to the player about success or failure. This update splits the teleport logic into a dedicated service and a command class, replacing the previous single-command approach.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8fbcae7 and 5aeee39.

📒 Files selected for processing (1)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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
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: 6

🧹 Nitpick comments (6)
eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (3)

23-23: Consider memory management for the teleportation history.

The static map will grow indefinitely as players join the server, but entries are never removed. This could lead to a memory leak over time.

Consider adding a cleanup mechanism, such as:

  • Removing entries for players who haven't been online for a certain period
  • Using a cache with size/time-based eviction
  • Clearing entries when the server restarts

40-57: Verify the player selection logic handles edge cases.

The method looks correct but let's ensure it handles all scenarios properly. The logic should work but could be simplified.

Consider this cleaner approach:

public Player findLeastRecentlyTeleportedPlayer() {
    List<Player> eligiblePlayers = getEligiblePlayers();
    
    if (eligiblePlayers.isEmpty()) {
        return null;
    }
    
    return eligiblePlayers.stream()
        .min(Comparator.comparing(
            p -> TELEPORTATION_HISTORY.getOrDefault(p.getUniqueId(), Instant.EPOCH)
        ))
        .orElse(null);
}

The nullsLast comparator isn't needed since getOrDefault handles missing entries.


47-49: Simplify single player case handling.

The early return for single player is good, but the logic could be cleaner.

-if (eligiblePlayers.size() == 1) {
-    return eligiblePlayers.get(0);
-}
+if (eligiblePlayers.size() <= 1) {
+    return eligiblePlayers.isEmpty() ? null : eligiblePlayers.get(0);
+}

This consolidates the empty and single player cases.

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java (3)

44-44: Consider validating target player is still online.

There's a small chance the target player could disconnect between selection and teleportation.

Add a quick online check before teleporting:

+if (!targetPlayer.isOnline()) {
+    this.noticeService.create()
+        .player(player.getUniqueId())
+        .notice(translation -> translation.teleport().randomPlayerNotFound())
+        .send();
+    return;
+}
+
player.teleport(targetPlayer);

32-40: Consider checking if target is the same as executor.

The command might teleport a player to themselves if they're the only eligible player, which could be confusing.

Add a check after finding the target:

Player targetPlayer = this.teleportRandomPlayerService.findLeastRecentlyTeleportedPlayer();

+if (targetPlayer != null && targetPlayer.equals(player)) {
+    this.noticeService.create()
+        .player(player.getUniqueId())
+        .notice(translation -> translation.teleport().cannotTeleportToSelf())
+        .send();
+    return;
+}
+
if (targetPlayer == null) {

31-32: Consider filtering out the executing player.

The current implementation could teleport a player to themselves if they're the only eligible player.

Consider modifying the service call to exclude the current player:

Player targetPlayer = this.teleportRandomPlayerService.findLeastRecentlyTeleportedPlayer(player);

And update the service method to filter out the executing player from eligible targets.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between da18b63 and ee09810.

📒 Files selected for processing (3)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java (0 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java (1 hunks)
💤 Files with no reviewable changes (1)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java
👮 Files not reviewed due to content moderation or server errors (2)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java
🔇 Additional comments (11)
eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (5)

34-38: Good implementation of player filtering.

The eligible players logic correctly filters based on the operator configuration setting.


59-61: Good history tracking implementation.

The update method correctly records the current timestamp for the teleported player.


51-56: Good implementation of least recently teleported logic.

The comparator logic correctly handles the case where players haven't been teleported before by using Instant.EPOCH as default.


40-57: Logic looks good but consider self-teleportation edge case.

The method correctly finds the least recently teleported player using a clean comparator approach.

However, consider what happens if the executing player is the only eligible player - they would teleport to themselves. You might want to filter out the executing player from eligible targets.


52-54: Good use of Instant.EPOCH as default value.

Using Instant.EPOCH as the default ensures players who haven't been teleported to will be prioritized, which is exactly the intended behavior.

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java (6)

13-14: Good command setup with clear naming.

The command name and alias are appropriate, and the permission is properly configured.


29-51: Good use of service pattern and error handling.

The command properly uses the service to find targets and handles the null case with appropriate user feedback.


46-50: Good notice implementation with placeholder.

The success notice correctly uses the target player's name as a placeholder, providing clear feedback to the user.


29-31: Excellent documentation and command setup.

The description clearly explains the fair distribution logic, and the command annotation is properly configured.


32-40: Good error handling for no available targets.

The null check and user notification is well implemented.


42-44: Correct order of operations.

Updating the teleportation history before the actual teleport is the right approach to ensure accurate tracking.

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 (1)
eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (1)

46-48: Add validation for sender and target.

Consider adding a check to ensure sender and target are different players to prevent unnecessary cache entries.

 public void updateTeleportationHistory(Player sender, Player target) {
+    if (sender.equals(target)) {
+        return;
+    }
     teleportationHistory.put(new HistoryKey(sender.getUniqueId(), target.getUniqueId()), Instant.now());
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ee09810 and dd71f92.

📒 Files selected for processing (2)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (1 hunks)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportToRandomPlayerCommand.java
🔇 Additional comments (3)
eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrandomplayer/TeleportRandomPlayerService.java (3)

23-25: Great improvement with Caffeine cache!

The cache with 30-minute expiration nicely solves the memory management issues from previous implementations.


42-44: Good use of cache default value.

Using Instant.EPOCH as the default ensures players with no teleportation history are prioritized correctly.


50-51: Clean record implementation.

The HistoryKey record provides a clear and efficient way to store teleportation pairs in the cache.

Copy link
Member

@Rollczi Rollczi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done code review via commit. Check out my changes, everythink looks good? I added the key because we want to handle multiple admins.

Copy link
Member

@CitralFlo CitralFlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor this and that. I like usage of Caffeine.

Tprp will work better than spotify random play (I still get the same messages)

@vLuckyyy vLuckyyy merged commit 47a44b7 into master May 25, 2025
3 checks passed
@vLuckyyy vLuckyyy deleted the fix-tprp branch July 4, 2025 19:53
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.

5 participants