-
Notifications
You must be signed in to change notification settings - Fork 29
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
Conversation
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 |
Of course, I'll do a proper documentation and add tests. |
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. |
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. |
I'm not very convinced by this. |
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. |
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("") }),
),
}) |
Yes, It can be added so users have the choice and can combine both. |
That sounds pretty bad |
I like the NBT builder helper function idea. |
Why has it been closed? |
I don't have much time because of my exams, but I haven't abandoned the pr. |
It would need to be completely rewritten to avoid integrating this format and instead provide builder functions. |
Ok I'll do that |
#48 is a better solution for this |
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:
will result in