| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.authlib.GameProfile;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import com.mojang.serialization.Lifecycle;
|
| 10 | import com.mojang.util.UndashedUuid;
|
| 11 | import io.netty.buffer.ByteBuf;
|
| 12 | import java.nio.ByteBuffer;
|
| 13 | import java.nio.ByteOrder;
|
| 14 | import java.nio.charset.StandardCharsets;
|
| 15 | import java.util.Arrays;
|
| 16 | import java.util.Set;
|
| 17 | import java.util.UUID;
|
| 18 | import net.minecraft.network.FriendlyByteBuf;
|
| 19 | import net.minecraft.network.codec.StreamCodec;
|
| 20 | import net.minecraft.util.Util;
|
| 21 |
|
| 22 | public final class UUIDUtil {
|
| 23 | public static final Codec<UUID> CODEC = Codec.INT_STREAM
|
| 24 | .comapFlatMap(list -> Util.fixedSize(list, 4).map(UUIDUtil::uuidFromIntArray), uuid -> Arrays.stream(uuidToIntArray(uuid)));
|
| 25 | public static final Codec<Set<UUID>> CODEC_SET = Codec.list(CODEC).xmap(Sets::newHashSet, Lists::newArrayList);
|
| 26 | public static final Codec<Set<UUID>> CODEC_LINKED_SET = Codec.list(CODEC).xmap(Sets::newLinkedHashSet, Lists::newArrayList);
|
| 27 | public static final Codec<UUID> STRING_CODEC = Codec.STRING.comapFlatMap(s -> {
|
| 28 | try {
|
| 29 | return DataResult.success(UUID.fromString(s), Lifecycle.stable());
|
| 30 | } catch (IllegalArgumentException e) {
|
| 31 | return DataResult.error(() -> "Invalid UUID " + s + ": " + e.getMessage());
|
| 32 | }
|
| 33 | }, UUID::toString);
|
| 34 | public static final Codec<UUID> AUTHLIB_CODEC = Codec.withAlternative(Codec.STRING.comapFlatMap(s -> {
|
| 35 | try {
|
| 36 | return DataResult.success(UndashedUuid.fromStringLenient(s), Lifecycle.stable());
|
| 37 | } catch (IllegalArgumentException e) {
|
| 38 | return DataResult.error(() -> "Invalid UUID " + s + ": " + e.getMessage());
|
| 39 | }
|
| 40 | }, UndashedUuid::toString), CODEC);
|
| 41 | public static final Codec<UUID> LENIENT_CODEC = Codec.withAlternative(CODEC, STRING_CODEC);
|
| 42 | public static final StreamCodec<ByteBuf, UUID> STREAM_CODEC = new StreamCodec<ByteBuf, UUID>() {
|
| 43 | public UUID decode(final ByteBuf input) {
|
| 44 | return FriendlyByteBuf.readUUID(input);
|
| 45 | }
|
| 46 |
|
| 47 | public void encode(final ByteBuf output, final UUID value) {
|
| 48 | FriendlyByteBuf.writeUUID(output, value);
|
| 49 | }
|
| 50 | };
|
| 51 | public static final int UUID_BYTES = 16;
|
| 52 | private static final String UUID_PREFIX_OFFLINE_PLAYER = "OfflinePlayer:";
|
| 53 |
|
| 54 | private UUIDUtil() {
|
| 55 | }
|
| 56 |
|
| 57 | public static UUID uuidFromIntArray(final int[] intArray) {
|
| 58 | return new UUID((long)intArray[0] << 32 | intArray[1] & 4294967295L, (long)intArray[2] << 32 | intArray[3] & 4294967295L);
|
| 59 | }
|
| 60 |
|
| 61 | public static int[] uuidToIntArray(final UUID uuid) {
|
| 62 | long mostSignificantBits = uuid.getMostSignificantBits();
|
| 63 | long leastSignificantBits = uuid.getLeastSignificantBits();
|
| 64 | return leastMostToIntArray(mostSignificantBits, leastSignificantBits);
|
| 65 | }
|
| 66 |
|
| 67 | private static int[] leastMostToIntArray(final long mostSignificantBits, final long leastSignificantBits) {
|
| 68 | return new int[]{(int)(mostSignificantBits >> 32), (int)mostSignificantBits, (int)(leastSignificantBits >> 32), (int)leastSignificantBits};
|
| 69 | }
|
| 70 |
|
| 71 | public static byte[] uuidToByteArray(final UUID uuid) {
|
| 72 | byte[] bytes = new byte[16];
|
| 73 | ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
|
| 74 | return bytes;
|
| 75 | }
|
| 76 |
|
| 77 | public static UUID readUUID(final Dynamic<?> input) {
|
| 78 | int[] intArray = input.asIntStream().toArray();
|
| 79 | if (intArray.length != 4) {
|
| 80 | throw new IllegalArgumentException("Could not read UUID. Expected int-array of length 4, got " + intArray.length + ".");
|
| 81 | } else {
|
| 82 | return uuidFromIntArray(intArray);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public static UUID createOfflinePlayerUUID(final String playerName) {
|
| 87 | return UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(StandardCharsets.UTF_8));
|
| 88 | }
|
| 89 |
|
| 90 | public static GameProfile createOfflineProfile(final String playerName) {
|
| 91 | UUID id = createOfflinePlayerUUID(playerName);
|
| 92 | return new GameProfile(id, playerName);
|
| 93 | }
|
| 94 | }
|
| 95 |
|