Last active
November 6, 2024 17:09
-
-
Save storycraft/7813dd0186e85daa393e1df9cfa19f2a to your computer and use it in GitHub Desktop.
Convert uuid string to minecraft nbt uuid (int array)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toNBTUUID(uuid) { | |
return `[I;${uuid.replace(/-/g, '').match(/.{8}/g).map(str => Number.parseInt(str, 16)).map(num => num & 0x80000000 ? num - 0xffffffff - 1 : num).join(',')}]`; | |
} | |
// toNBTUUID('0e9b65dd-3dce-4a3f-8a13-3299a91ceac7'); | |
// -> [I;245065181,1036929599,-1978453351,-1457722681] |
What a life-saver! :) I'll add my Java-implementation here too, as a reference for others, as well as my future self, xD.
private int[] uniqueIdToNbtFormat(UUID id) {
var result = new int[4];
var msb = id.getMostSignificantBits();
var lsb = id.getLeastSignificantBits();
result[0] = (int) ((msb >> 32) & 0xFFFFFFFFL);
result[1] = (int) (msb & 0xFFFFFFFFL);
result[2] = (int) ((lsb >> 32) & 0xFFFFFFFFL);
result[3] = (int) (lsb & 0xFFFFFFFFL);
// As per https://gist.github.com/storycraft/7813dd0186e85daa393e1df9cfa19f2a
for (var i = 0; i < result.length; ++i) {
if ((result[i] & 0x80000000 /* 2^31 */) != 0)
result[i] = result[i] - 0xFFFFFFFF /* 2^32 - 1 */ - 1;
}
return result;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks dude ❤