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

1.20.3 nbt chat handling #103

Merged
merged 5 commits into from
Feb 24, 2024
Merged

1.20.3 nbt chat handling #103

merged 5 commits into from
Feb 24, 2024

Conversation

extremeheat
Copy link
Member

Expose a processNbtMessage method for 1.20.3 chat handling, called in fromNotch

// mcpc 1.20.3 uses NBT instead of JSON in some places to store chat, so the schema is a bit different over network
// processNbtMessage normalizes the JS object obtained from nbt derealization to the old JSON schema

@extremeheat
Copy link
Member Author

Having a toNotch would be helpful for server-side code/mineflayer test code that'd turn the JSON into NBT schema

Some basic code to do it, but untested

// Some but not all packet fields on 1.20.3 use NBT
    toNotch (asNbt = false) {
      if (asNbt && registry.supportFeature('chatPacketsUseNbtComponents')) {
        // TODO: handle recursive json features
        // const json = JSON.stringify(this.json, (key, value) => {
        //   if (typeof value === 'object' && !Array.isArray(value) && !(value.type && value.value)) return nbt.comp(value)
        //   if (key === 'id' && typeof value === 'string') return intArrayFromUUID(value)
        //   if (typeof value === 'string') return nbt.string(value)
        //   if (typeof value === 'number') return nbt.double(value)
        //   if (typeof value === 'boolean') return nbt.bool(value)
        // })
       // return nbt.comp(JSON.stringify(json))
        return nbt.comp(this.json)
      } else {
        return JSON.stringify(this.json)
      }
    }

Probably can be done in future PR. Since not all packet fields use the NBT schema the caller would have to specify if the packet this is being written to is expecting an NBT field or not. Using the JSON replacer with stringify-parse isn't great here but it's a bit simpler than writing a recursive function

@rom1504
Copy link
Member

rom1504 commented Feb 24, 2024

would be good to add tests to make sure we convert things properly

but this can be in future PRs

filing #104

@rom1504 rom1504 merged commit 1066a76 into PrismarineJS:master Feb 24, 2024
3 checks passed
@rom1504
Copy link
Member

rom1504 commented Feb 24, 2024

/makerelease

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.

2 participants