Skip to content

Commit

Permalink
fix: title would not get rendered and rendering times be wrong (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 authored Jan 28, 2025
1 parent a442acf commit 41684bc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/react/ChatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default () => {

useEffect(() => {
bot.addListener('message', (jsonMsg, position) => {
if (position === 'game_info') return // ignore action bar messages, they are handled by the TitleProvider
const parts = formatMessage(jsonMsg)

setMessages(m => {
Expand Down
6 changes: 3 additions & 3 deletions src/react/Title.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const Primary: Story = {
text: 'Action bar text'
},
transitionTimes: {
fadeIn: 2500,
stay: 17_500,
fadeOut: 5000
fadeIn: 500,
stay: 3500,
fadeOut: 1000
}
}
}
19 changes: 10 additions & 9 deletions src/react/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const Title = ({
const [mounted, setMounted] = useState(false)
const [useEnterTransition, setUseEnterTransition] = useState(true)

const defaultDuration = 500
const defaultFadeIn = 500
const defaultFadeOut = 1000
const startStyle = {
opacity: 1,
transition: `${transitionTimes.fadeIn}ms ease-in-out all` }
Expand All @@ -54,10 +55,10 @@ const Title = ({
<div className='title-container'>
<Transition
in={openTitle}
timeout={transitionTimes ? {
enter: transitionTimes.fadeIn,
exit: transitionTimes.fadeOut,
} : defaultDuration}
timeout={{
enter: transitionTimes?.fadeIn ?? defaultFadeIn,
exit: transitionTimes?.fadeOut ?? defaultFadeOut,
}}
mountOnEnter
unmountOnExit
enter={useEnterTransition}
Expand All @@ -83,10 +84,10 @@ const Title = ({
</Transition>
<Transition
in={openActionBar}
timeout={transitionTimes ? {
enter: transitionTimes.fadeIn,
exit: transitionTimes.fadeOut,
} : defaultDuration}
timeout={{
enter: transitionTimes?.fadeIn ?? defaultFadeIn,
exit: transitionTimes?.fadeOut ?? defaultFadeOut,
}}
mountOnEnter
unmountOnExit
// enter={useEnterTransition}
Expand Down
25 changes: 21 additions & 4 deletions src/react/TitleProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useEffect, useMemo, useState } from 'react'
import mojangson from 'mojangson'
import nbt from 'prismarine-nbt'
import type { ClientOnMap } from '../generatedServerPackets'
import Title from './Title'
import type { AnimationTimes } from './Title'


const defaultText: Record<string, any> = { 'text': '' }
const defaultTimings: AnimationTimes = { fadeIn: 400, stay: 3800, fadeOut: 800 }
const defaultTimings: AnimationTimes = { fadeIn: 500, stay: 3500, fadeOut: 1000 }

const ticksToMs = (ticks: AnimationTimes) => {
ticks.fadeIn *= 50
Expand All @@ -14,6 +16,20 @@ const ticksToMs = (ticks: AnimationTimes) => {
return ticks
}

const getComponent = (input: string | any) => {
if (typeof input === 'string') {
// raw json is sent
return mojangson.simplify(mojangson.parse(input))
} else if (input.type === 'string') {
// this is used for simple chat components without any special properties
return { 'text': input.value }
} else if (input.type === 'compound') {
// this is used for complex chat components with special properties
return nbt.simplify(input)
}
return input
}

export default () => {
const [title, setTitle] = useState<string | Record<string, any>>(defaultText)
const [subtitle, setSubtitle] = useState<string | Record<string, any>>(defaultText)
Expand All @@ -25,14 +41,14 @@ export default () => {
useMemo(() => {
// todo move to mineflayer
bot._client.on('set_title_text', (packet) => {
setTitle(JSON.parse(packet.text))
setTitle(getComponent(packet.text))
setOpenTitle(true)
})
bot._client.on('set_title_subtitle', (packet) => {
setSubtitle(JSON.parse(packet.text))
setSubtitle(getComponent(packet.text))
})
bot._client.on('action_bar', (packet) => {
setActionBar(JSON.parse(packet.text))
setActionBar(getComponent(packet.text))
setOpenActionBar(true)
})
bot._client.on('set_title_time', (packet) => {
Expand All @@ -51,6 +67,7 @@ export default () => {


bot.on('actionBar', (packet) => {
setAnimTimes({ fadeIn: 0, stay: 2000, fadeOut: 1000 })
setActionBar(packet)
setOpenActionBar(true)
})
Expand Down

0 comments on commit 41684bc

Please sign in to comment.