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

Added writeJsObject() #44

Closed
wants to merge 10 commits into from

Conversation

GroobleDierne
Copy link
Contributor

Added a function that takes a javascript object, convert it to the nbt format and write it.

This is an example of the result of a convertion:

let parsedObj = parseJsObjectt({
  Data: {
    RandomSeed: '0L',
    Difficulty: '0b',
    BorderSize: '60000000.0d',
    GameRules: {
      announceAdvancements: true
    },
    ServerBrands : [],
    RandomArray: ['hello', 'I am', 'a test array']
})
console.log(JSON.stringify(parsedObj))

will result in

{
  "type": "compound",
  "value": {
    "Data": {
      "type": "compound",
      "value": {
        "RandomSeed": {
          "type": "long",
          "value": [
            0,
            "0"
          ]
        },
        "Difficulty": {
          "type": "byte",
          "value": "0"
        },
        "BorderSize": {
          "type": "double",
          "value": "60000000.0"
        },
        "GameRules": {
          "type": "compound",
          "value": {
            "announceAdvancements": {
              "type": "string",
              "value": "true"
            }
          }
        },
        "ServerBrands": {
          "type": "list",
          "value": {
            "type": "end",
            "value": []
          }
        },
        "RandomArray": {
          "type": "list",
          "value": {
            "type": "string",
            "value": [
              "hello",
              "I am",
              "a test array"
            ]
          }
        }
      }
    }
  }
}

@u9g
Copy link
Member

u9g commented Apr 22, 2021

when adding to the docs, make sure to note that if you are doing a byte or something that you need to add the 'b' to the end of the string

@GroobleDierne
Copy link
Contributor Author

Of course, I'll do a proper documentation and add tests.

@extremeheat
Copy link
Member

extremeheat commented May 16, 2021

I think this is okay, having a shorter syntax can be nice. But it also does add some fragmentation, we have the protodef object format used internally, simplify format which removes encapsulation, then this the opposite of simplify, but requires manually writing things out as strings with type suffix. They’re not really interoperable. So I’m not sure there’s much practical benefit. There is #39 to return something from simplify that can be written back, but not a simple thing to solve.

@GroobleDierne
Copy link
Contributor Author

GroobleDierne commented May 16, 2021

Yeah as javascript isn't strongly typed it's very difficult to do that without adding extra informations such as suffixes or extra fields with type informations.

@rom1504
Copy link
Member

rom1504 commented May 16, 2021

I'm not very convinced by this.
This is a variation of https://www.npmjs.com/package/mojangson
what use case are you trying to solve with this ?

@GroobleDierne
Copy link
Contributor Author

Avoiding to use the wordy syntaxe when you work with nbt from javascript, what I want to say is that when you load existing NBTs, and modify some values on them and write them back it can be ok using the actual syntaxe but when you are creating complex NBTs, from scratch, based on variables in javascript it force you to use a extremely wordy syntaxe with specific tricks that you have to know (for example if an array is empty you have to put an end tag in it). It becomes even more complicated if you want to add fields only in certain cases.

@extremeheat
Copy link
Member

The array issue sounds like something that should be fixed separately instead. Using a custom string-based format like here isn't reliable. What if you want it to be a string and not be type coerced? For clarity, why not just wrapper functions?

var nbt = {
  short(value: number) { return { type: 'short', value } },
  byte(value: number) { return { type: 'byte', value } },
  string(value: string) { return { type: 'string', value } },
  comp(value) { return { type: 'compound', value } },
  list(...value) {
    const type = value[0]?.type ?? 'end'
    return { type: 'list', value: { type, value } }
  }
}

writePlayerNbt({
  Air: nbt.short(300),
  Armor: nbt.list(
    nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
    nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
    nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
  ),
})

@GroobleDierne
Copy link
Contributor Author

Yes, It can be added so users have the choice and can combine both.

@extremeheat
Copy link
Member

That sounds pretty bad

@rom1504
Copy link
Member

rom1504 commented May 31, 2021

I like the NBT builder helper function idea.
This could be a good solution to that making it easy to build and edit NBT use case.

@rom1504 rom1504 closed this Jun 16, 2021
@GroobleDierne
Copy link
Contributor Author

Why has it been closed?

@GroobleDierne
Copy link
Contributor Author

I don't have much time because of my exams, but I haven't abandoned the pr.

@rom1504
Copy link
Member

rom1504 commented Jun 19, 2021

It would need to be completely rewritten to avoid integrating this format and instead provide builder functions.
I can reopen if you want to do this in this PR

@rom1504 rom1504 reopened this Jun 19, 2021
@GroobleDierne
Copy link
Contributor Author

Ok I'll do that

@rom1504
Copy link
Member

rom1504 commented Jul 6, 2021

#48 is a better solution for this

@rom1504 rom1504 closed this Jul 6, 2021
@GroobleDierne GroobleDierne deleted the writeJsObject branch July 7, 2021 16:36
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.

4 participants