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

feat: enable bossbar by default & support for custom title texts #259

Merged
merged 4 commits into from
Feb 4, 2025

Conversation

Phoenix616
Copy link

@Phoenix616 Phoenix616 commented Jan 31, 2025

User description

This fixes that the bossbar would not be rendered properly when added via the /bossbar command/plugin API and wasn't using the component rendering system but manual mapping. Also updates to the boss bar would not get rendered before.


PR Type

Bug fix, Enhancement


Description

  • Fixed boss bar titles not displaying correctly.

  • Updated boss bar title handling to support various formats.

  • Refactored boss bar management with dedicated add/remove functions.

  • Improved boss bar update logic for better rendering.


Changes walkthrough 📝

Relevant files
Enhancement
BossBarOverlay.tsx
Refactor boss bar title handling and styles                           

src/react/BossBarOverlay.tsx

  • Changed boss bar title handling to support multiple formats.
  • Removed manual translation mapping for titles.
  • Updated state initialization for boss bar title.
  • Adjusted logic for setting boss bar styles.
  • +4/-8     
    BossBarOverlayProvider.tsx
    Refactor boss bar management with utility functions           

    src/react/BossBarOverlayProvider.tsx

  • Added addBossBar and removeBossBar utility functions.
  • Simplified boss bar event handling with new functions.
  • Improved boss bar update logic with a timeout mechanism.
  • +15/-5   

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

    codesandbox bot commented Jan 31, 2025

    Review or Edit in CodeSandbox

    Open the branch in Web EditorVS CodeInsiders

    Open Preview

    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

    Race Condition

    The setTimeout with 1ms delay in bossBarUpdated event handler could lead to race conditions. Consider using React state updates properly instead of this workaround.

    setTimeout(() => addBossBar(bossBar as BossBarType), 1)
    Type Safety

    The title type allows any Record<string,any> which is too permissive and could lead to runtime errors. Consider using a more specific type definition.

    title: string | Record<string, any> | null,
    _title: string | Record<string, any> | null,

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Remove unnecessary state update delay

    Remove the unnecessary timeout when updating boss bars. The removeBossBar followed
    by a delayed addBossBar creates a visual flicker and race condition risk. Instead,
    directly update the boss bar state.

    src/react/BossBarOverlayProvider.tsx [24-27]

     bot.on('bossBarUpdated', (bossBar) => {
    -  removeBossBar(bossBar as BossBarType)
    -  setTimeout(() => addBossBar(bossBar as BossBarType), 1)
    +  addBossBar(bossBar as BossBarType)
     })
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The current implementation using setTimeout creates unnecessary visual flicker and potential race conditions. Direct state update is cleaner and more reliable.

    8
    General
    Add proper type safety

    Initialize the title state with a proper type annotation matching the possible title
    formats (string | Record<string, any> | null) to prevent type errors.

    src/react/BossBarOverlay.tsx [19]

    -const [title, setTitle] = useState({})
    +const [title, setTitle] = useState<string | Record<string, any> | null>(null)
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly aligns the useState type with the BossBarType interface definition, preventing potential type-related bugs and improving code maintainability.

    7

    @zardoy
    Copy link
    Owner

    zardoy commented Jan 31, 2025

    /deploy

    Copy link

    Deployed to Vercel Preview: https://prismarine-p2emo882d-zaro.vercel.app
    Playground
    Storybook

    Copy link
    Owner

    @zardoy zardoy left a comment

    Choose a reason for hiding this comment

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

    cant get it working, @Phoenix616 please specify versions on which you test your fixes

    })
    bot.on('bossBarUpdated', (bossBar) => {
    setBossBars(prevBossBars => new Map(prevBossBars.set(bossBar.entityUUID, bossBar as BossBarType)))
    removeBossBar(bossBar as BossBarType)
    setTimeout(() => addBossBar(bossBar as BossBarType), 1)
    Copy link
    Owner

    Choose a reason for hiding this comment

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

    why do we need timeout? would not create it flickering, so weird

    Copy link
    Author

    @Phoenix616 Phoenix616 Jan 31, 2025

    Choose a reason for hiding this comment

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

    It will otherwise not update the displayed text/values/styles, most likely because the element already exists? It was the only solution I found but I don't know how react works so there might be a better way to force an update of the changed element.

    @zardoy
    Copy link
    Owner

    zardoy commented Jan 31, 2025

    ok, my apologies, it is not visible only after rejoining.

    though IMO further polishing is possible (wdyt?):
    current:
    image
    vanilla: image
    mobile current:
    image

    IMO need to:

    • bossbar-title class: set font size to 9px (make a bit bigger), add margin-bottom to at least 1px (can leave as 1px for now)
    • when touch active: .bossBars top = 18px , when not 1px
    • add pointerEvents none to bossBars
    • lets enable them by default

    @Phoenix616
    Copy link
    Author

    Style changes make sense, yeah.

    I'm unsure what you mean by "lets enable them by default" though. I didn't adjust any code regarding the behavior or when and how it displays, it instantly shows them as soon as the mineflayer bot event is called which should be whenever a packet is received.

    @zardoy
    Copy link
    Owner

    zardoy commented Jan 31, 2025

    Style changes make sense, yeah.

    I'm unsure what you mean by "lets enable them by default" though. I didn't adjust any code regarding the behavior or when and how it displays, it instantly shows them as soon as the mineflayer bot event is called which should be whenever a packet is received.

    no worries I can these changes myself, I just have hundreds of tasks that I try to do at the same and had to write these here so I don't forget

    @Phoenix616 most importantly I wanted to know your opinion on these changes and if you have any other feedback

    - Adjusts the font size and gabs
    - Fixes that it would overlap the mobile buttons
    - Fixes that touching where the boss bar is would not move the camera
    - Better match the behaviour of really wide bossbar titles
    @Phoenix616
    Copy link
    Author

    Phoenix616 commented Feb 3, 2025

    Did some adjustments to the bossbar styles to make them behave pretty close to Java (imo) and also fixed the mobile issues at the same time.

    Copy link

    github-actions bot commented Feb 3, 2025

    Deployed to Vercel Preview: https://prismarine-3lkbca6zf-zaro.vercel.app
    Playground
    Storybook

    @zardoy
    Copy link
    Owner

    zardoy commented Feb 4, 2025

    @Phoenix616 again, i still do not understand the intention to for setTimeout, will ltry to figure out as well will try to fix the re-login issue

    @zardoy
    Copy link
    Owner

    zardoy commented Feb 4, 2025

    ok dont have time, feel free to help, need a plugin in src/mineflayer/plugins/bossbars.ts so bossbars are preserved into mineflayer bot state

    Copy link

    github-actions bot commented Feb 4, 2025

    Deployed to Vercel Preview: https://prismarine-9w6g5l9b1-zaro.vercel.app
    Playground
    Storybook

    @zardoy zardoy changed the title fix: bossbar does not display custom titles feat: enable bossbar by default & support for custom title texts Feb 4, 2025
    @zardoy zardoy merged commit e2a093b into zardoy:next Feb 4, 2025
    0 of 2 checks passed
    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