rikka0w0 / LanServerProperties

Enhance the vanilla "Open to LAN" Gui for listening port customization and removal of enforced authentication
https://www.curseforge.com/minecraft/mc-mods/lan-server-properties
110 stars 18 forks source link

Add "Preserve UUID in offline mode" feature #19

Closed rikka0w0 closed 2 years ago

rikka0w0 commented 2 years ago

If offline mode is enabled, the game uses locally calculated UUIDs for identifying players. If the server owner changes the offline mode during the gameplay (either enable or disable), the UUIDs won't match. This causes loss of inventories and ender chest.

  1. In offline mode, Minecraft uses net.minecraft.world.entity.player.Player.createPlayerUUID(String) to generate a local UUID for each player, including those who logged in with Microsoft account. Its implementation ensures each player name will have its unique corresponding UUID: return UUID.nameUUIDFromBytes(("OfflinePlayer:" + p_36284_).getBytes(StandardCharsets.UTF_8));.
  2. Upon start-up, Minecraft checks if the launch argument contains a UUID, if not, it will use the above method to generate a local one.
  3. When a second player joins the game, Minecraft calls net.minecraft.world.entity.player.Player.createPlayerUUID(GameProfile) to map his UUID:

    public static UUID createPlayerUUID(GameProfile p_36199_) {
      UUID uuid = p_36199_.getId();
      if (uuid == null) {
         uuid = createPlayerUUID(p_36199_.getName());
      }
    
      return uuid;
    }
    
    public static UUID createPlayerUUID(String p_36284_) {
      return UUID.nameUUIDFromBytes(("OfflinePlayer:" + p_36284_).getBytes(StandardCharsets.UTF_8));
    }

To address this issue, the author proposed a new way of generating the UUID:

  1. Call https://api.mojang.com/users/profiles/minecraft/<username>
  2. If succeeded, use the returned UUID from the previous API.
  3. If not, falls back to the local UUID.

See: https://bukkit.org/threads/how-to-convert-uuid-to-name-and-name-to-uuid-uising-mojang-api.460828/

rikka0w0 commented 2 years ago

Properly implemented since:

https://github.com/rikka0w0/LanServerProperties/commit/57fbd3678cad74f2afe52499e513f679f618ebb0