Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yushijinhun/69f68397c5bb5bee76e80d192295f6e0 to your computer and use it in GitHub Desktop.
Save yushijinhun/69f68397c5bb5bee76e80d192295f6e0 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);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment