Skip to content

Instantly share code, notes, and snippets.

@storycraft
Last active November 6, 2024 17:09
Show Gist options
  • Save storycraft/7813dd0186e85daa393e1df9cfa19f2a to your computer and use it in GitHub Desktop.
Save storycraft/7813dd0186e85daa393e1df9cfa19f2a to your computer and use it in GitHub Desktop.
Convert uuid string to minecraft nbt uuid (int array)
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]
@Wolphwood
Copy link

Thanks dude ❤

@BlvckBytes
Copy link

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