Serializing UnsafeData from JSON from String (argument)


#1

Pretty basic question really, i want to make a plugin that pretty much perfectly mimics minecrafts /give commands, but adds permission nodes for nbt and giving to others.

I am using google GSON to serializing the last group of the argument (not how it’s done, but subjectively it’s an improvement to not have accidental spaces make the command invalid) and i have gotten it to work mostly, except for stuff that is a boolean or integer, has a b appended to the end, it is expressed as 1b, or so. now, i could just ommit the b and it would work, but i wanted commands that would work in vanilla to work for this aswell

I was thinking of using a regex pattern like “^(/d+)b$” and omitting the data using a recursive function (I actually already wrote one that works well before discovering DataFormats.JSON.read(), Oof)

Is there any better ways of doing this, though? like, can i just straight up turn the string to nbt, or am i parsing it wrongly which is why the b’s remain?


#2

If you’re wanting complete compatibility with vanilla JSON format then it’s probably best to use the server internals and call net.minecraft.nbt.JsonToNBT.getTagFromJson() directly.
It’s obviously not ideal having to use internals so I think a case can be made to add DataFormats.MOJANGSON or something.

Edit: looks like an issue is open for a string/nbt conversion:


#3

I mean, mojangson is pretty much json with some sort of type security on the numbers, no?
More so asking because i am extremely new to java, though i have done some short amount of C# before, just enough to be able to learn my way into making sponge plugins. Just making sure i am not missing something incredibly obvious, haha.

Edit:
Yeah managed to get it all working, just made a json using google GSON then recursively iterated through the nodes and pattern matched any non-json-objects to turn them into their correct typed. works wonders. only issue, was i had problems with the arrays however i don’t need them. Turned out to not actually require many lines of code either, less then 50 lines anyway.