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

Fixed block interaction logic for adventure mode! #289

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

Mqxx
Copy link

@Mqxx Mqxx commented Feb 25, 2025

PR Type

Bug fix


Description

  • Fixed block interaction logic for adventure mode.

  • Adjusted conditions for cursorBlockDiggable assignment.

  • Prevented block placement in adventure mode.


Changes walkthrough 📝

Relevant files
Bug fix
worldInteractions.ts
Refine block interaction and placement logic                         

src/worldInteractions.ts

  • Refined logic for cursorBlockDiggable assignment.
  • Added checks to prevent block placement in adventure mode.
  • Adjusted conditions for block interaction and activation.
  • +4/-2     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Summary by CodeRabbit

    • Bug Fixes
      • Refined block interaction behavior so that actions like digging and placing respond correctly based on the player's mode and block state, ensuring more consistent gameplay in modes such as adventure and creative.

    Copy link

    codesandbox bot commented Feb 25, 2025

    Review or Edit in CodeSandbox

    Open the branch in Web EditorVS CodeInsiders

    Open Preview

    Copy link

    coderabbitai bot commented Feb 25, 2025

    Walkthrough

    The changes modify the WorldInteraction class to refine the logic for determining whether a block is diggable and managing block interactions. Instead of a single condition, it now performs sequential checks: verifying the cursor block’s existence, evaluating if the player is in adventure mode, and ensuring the bot’s capability to dig when not in creative mode. Additionally, the block placement or interaction logic now includes a check for adventure mode before proceeding.

    Changes

    File(s) Change Summary
    src/worldInteractions.ts Refactored block diggability logic with a series of conditional statements based on the cursor block status and player game mode, and updated block interaction checks to prevent actions in adventure mode.

    Sequence Diagram(s)

    sequenceDiagram
      participant P as Player
      participant W as WorldInteraction
      participant B as CursorBlock
    
      P->>W: Initiate block dig/check
      alt cursorBlock is null
        W->>W: Set diggable to null
      else
        alt Player in adventure mode?
          W->>W: Set diggable to null
        else
          alt Cannot dig block and not in creative mode?
             W->>W: Set diggable to null
          else
             W->>W: Mark block as diggable
          end
        end
      end
    
    sequenceDiagram
      participant P as Player
      participant W as WorldInteraction
    
      P->>W: Attempt block placement/interaction
      alt Player in adventure mode?
        W->>W: Prevent block interaction
      else
        W->>W: Process block interaction
      end
    

    Poem

    I'm a rabbit in a code-filled glen,
    Hopping through logic again and again.
    With conditions clear and paths refined,
    My digital burrow's streamlined and kind.
    Cheers to new checks—let's hop to the beat! 🐇✨

    ✨ Finishing Touches
    • 📝 Generate Docstrings (Beta)

    🪧 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.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Logic Flow

    The nested if-else conditions for cursorBlockDiggable could be simplified and made more readable by combining conditions. Current implementation may be harder to maintain.

    let cursorBlockDiggable = cursorBlock
    if (!cursorBlock) cursorBlockDiggable = null
    else if (inAdventure) cursorBlockDiggable = null
    else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    Error Handling

    The ts-expect-error comment suggests potential type issues with the intersect property access that should be properly typed rather than suppressed.

    //@ts-expect-error
    const delta = cursorBlock.intersect.minus(cursorBlock.position)

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Simplify complex conditional logic

    Consolidate the multiple null checks for cursorBlockDiggable into a single
    condition using logical operators to improve readability and maintainability.

    src/worldInteractions.ts [281-284]

    -let cursorBlockDiggable = cursorBlock
    -if (!cursorBlock) cursorBlockDiggable = null
    -else if (inAdventure) cursorBlockDiggable = null
    -else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    +let cursorBlockDiggable = cursorBlock && !inAdventure && (bot.canDigBlock(cursorBlock) || bot.game.gameMode === 'creative') ? cursorBlock : null
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    __

    Why: The suggestion effectively consolidates multiple conditional checks into a single, more readable expression. While this improves code maintainability and reduces line count, it doesn't fix any bugs or critical issues.

    Low
    Use strict equality comparison

    Use strict equality operator ('===') instead of loose equality ('!=') for
    type-safe comparison with 'creative' string.

    src/worldInteractions.ts [284]

    -else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    +else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode !== 'creative') cursorBlockDiggable = null
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    __

    Why: While using strict equality (===) is a TypeScript best practice, the current loose equality (!=) doesn't pose any significant risks in this context since we're comparing with a string literal. The improvement is minor.

    Low
    • More

    @Mqxx Mqxx changed the title Update worldInteractions.ts Fixed block interaction logic for adventure mode. Feb 25, 2025
    @Mqxx Mqxx changed the title Fixed block interaction logic for adventure mode. Fixed block interaction logic for adventure mode! Feb 25, 2025
    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: 1

    📜 Review details

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

    📥 Commits

    Reviewing files that changed from the base of the PR and between 2833b33 and 75224f6.

    📒 Files selected for processing (1)
    • src/worldInteractions.ts (2 hunks)
    🧰 Additional context used
    🪛 ESLint
    src/worldInteractions.ts

    [error] 283-283: This branch's code block is the same as the block for the branch on line 282.

    (sonarjs/no-duplicated-branches)


    [error] 284-284: Expected '!==' and instead saw '!='.

    (eqeqeq)


    [error] 284-284: This branch's code block is the same as the block for the branch on line 283.

    (sonarjs/no-duplicated-branches)

    ⏰ Context from checks skipped due to timeout of 90000ms (1)
    • GitHub Check: build-and-deploy
    🔇 Additional comments (1)
    src/worldInteractions.ts (1)

    323-323: LGTM: Adventure mode check correctly implemented.

    The addition of the adventure mode check before placing blocks aligns with Minecraft's game rules, where players in adventure mode cannot place blocks unless specifically allowed to.

    Comment on lines +282 to +284
    if (!cursorBlock) cursorBlockDiggable = null
    else if (inAdventure) cursorBlockDiggable = null
    else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    Copy link

    Choose a reason for hiding this comment

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

    🛠️ Refactor suggestion

    Improve the conditional checks for better code quality.

    The refactoring of the cursor block diggability logic into sequential checks improves readability, but there are two issues that should be addressed:

    1. Line 284 uses != instead of the strict equality operator !== which is the recommended practice in TypeScript.
    2. The three conditions have identical code blocks which could be refactored for cleaner code.
    -    if (!cursorBlock) cursorBlockDiggable = null
    -    else if (inAdventure) cursorBlockDiggable = null
    -    else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    +    cursorBlockDiggable = (
    +      !cursorBlock || 
    +      inAdventure || 
    +      (!bot.canDigBlock(cursorBlock) && bot.game.gameMode !== 'creative')
    +    ) ? null : cursorBlock;
    📝 Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    Suggested change
    if (!cursorBlock) cursorBlockDiggable = null
    else if (inAdventure) cursorBlockDiggable = null
    else if (!bot.canDigBlock(cursorBlock) && bot.game.gameMode != 'creative') cursorBlockDiggable = null
    cursorBlockDiggable = (
    !cursorBlock ||
    inAdventure ||
    (!bot.canDigBlock(cursorBlock) && bot.game.gameMode !== 'creative')
    ) ? null : cursorBlock;
    🧰 Tools
    🪛 ESLint

    [error] 283-283: This branch's code block is the same as the block for the branch on line 282.

    (sonarjs/no-duplicated-branches)


    [error] 284-284: Expected '!==' and instead saw '!='.

    (eqeqeq)


    [error] 284-284: This branch's code block is the same as the block for the branch on line 283.

    (sonarjs/no-duplicated-branches)

    @zardoy
    Copy link
    Owner

    zardoy commented Feb 25, 2025

    already fixed in #286 , will be merged soon (I removed worldInteractions.ts completely ther)

    @@ -318,7 +320,7 @@ class WorldInteraction {
    }
    }
    // todo placing with offhand
    if (cursorBlock && !activate && !stop) {
    if (!inAdventure && cursorBlock && !activate && !stop) {
    Copy link
    Owner

    @zardoy zardoy Feb 25, 2025

    Choose a reason for hiding this comment

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

    @Mqxx oh i missed this part. I believe this is not correct since it also disables block interactions (like crafting table, buttons!), you are free to help implement this fix in github.com/zardoy/mineflayer-mouse

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants