Skip to content

Instantly share code, notes, and snippets.

@games647
Last active May 7, 2024 10:10
Show Gist options
  • Save games647/2b6a00a8fc21fd3b88375f03c9e2e603 to your computer and use it in GitHub Desktop.
Save games647/2b6a00a8fc21fd3b88375f03c9e2e603 to your computer and use it in GitHub Desktop.
Generate an offline minecraft UUID v3 based on the case sensitive player name
<?
/**
* Generates a offline-mode player UUID.
*
* @param $username string
* @return string
*/
public static function constructOfflinePlayerUuid($username) {
//extracted from the java code:
//new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)), name));
$data = hex2bin(md5("OfflinePlayer:" . $username));
//set the version to 3 -> Name based md5 hash
$data[6] = chr(ord($data[6]) & 0x0f | 0x30);
//IETF variant
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return self::createJavaUuid(bin2hex($data));
}
public static function createJavaUuid($striped) {
//example: 069a79f4-44e9-4726-a5be-fca90e38aaf5
$components = array(
substr($striped, 0, 8),
substr($striped, 8, 4),
substr($striped, 12, 4),
substr($striped, 16, 4),
substr($striped, 20),
);
return implode('-', $components);
}
?>
@Wector11211
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment