ResolvableProfile .java

net.minecraft.world.item.component.ResolvableProfile (class): members: createUnresolved, createResolved, partialProfile, resolveProfile, skinPatch, Static.

net.minecraft.world.item.componentclass200 linescommon8.2 KB

Implements

API Surface (18)

ResolvableProfile createResolved(final GameProfile gameProfile)
ResolvableProfile createUnresolved(final String name)
ResolvableProfile createUnresolved(final UUID id)
GameProfile partialProfile()
PlayerSkin.Patch skinPatch()
Optional<String> name()
boolean equals(final Object o)
int hashCode()
Either<GameProfile, ResolvableProfile.Partial> unpack()
CompletableFuture<GameProfile> resolveProfile(final ProfileResolver profileResolver)
void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components)
record Partial(Optional<String> name, Optional<UUID> id, PropertyMap properties)
CompletableFuture<GameProfile> resolveProfile(final ProfileResolver profileResolver)
Either<GameProfile, ResolvableProfile.Partial> unpack()
Optional<String> name()
boolean equals(final Object o)
int hashCode()
void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components)

Fields & Constants (5)

FULL_CODEC
CODEC
STREAM_CODEC
partialProfile
skinPatch

String literals (2)

"component.profile.dynamic"
"properties"

Full Source

ResolvableProfile.java200 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.authlib.GameProfile;
4
import com.mojang.authlib.properties.PropertyMap;
5
import com.mojang.datafixers.util.Either;
6
import com.mojang.serialization.Codec;
7
import com.mojang.serialization.MapCodec;
8
import com.mojang.serialization.codecs.RecordCodecBuilder;
9
import io.netty.buffer.ByteBuf;
10
import java.util.Optional;
11
import java.util.UUID;
12
import java.util.concurrent.CompletableFuture;
13
import java.util.function.Consumer;
14
import net.minecraft.ChatFormatting;
15
import net.minecraft.core.UUIDUtil;
16
import net.minecraft.core.component.DataComponentGetter;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.network.codec.ByteBufCodecs;
19
import net.minecraft.network.codec.StreamCodec;
20
import net.minecraft.server.players.ProfileResolver;
21
import net.minecraft.util.ExtraCodecs;
22
import net.minecraft.util.Util;
23
import net.minecraft.world.entity.player.PlayerSkin;
24
import net.minecraft.world.item.Item;
25
import net.minecraft.world.item.TooltipFlag;
26
27
public abstract sealed class ResolvableProfile implements TooltipProvider permits ResolvableProfile.Static, ResolvableProfile.Dynamic {
28
	private static final Codec<ResolvableProfile> FULL_CODEC = RecordCodecBuilder.create(
29
		i -> i.group(
30
				Codec.mapEither(ExtraCodecs.STORED_GAME_PROFILE, ResolvableProfile.Partial.MAP_CODEC).forGetter(ResolvableProfile::unpack),
31
				PlayerSkin.Patch.MAP_CODEC.forGetter(ResolvableProfile::skinPatch)
32
			)
33
			.apply(i, ResolvableProfile::create)
34
	);
35
	public static final Codec<ResolvableProfile> CODEC = Codec.withAlternative(FULL_CODEC, ExtraCodecs.PLAYER_NAME, ResolvableProfile::createUnresolved);
36
	public static final StreamCodec<ByteBuf, ResolvableProfile> STREAM_CODEC = StreamCodec.composite(
37
		ByteBufCodecs.either(ByteBufCodecs.GAME_PROFILE, ResolvableProfile.Partial.STREAM_CODEC),
38
		ResolvableProfile::unpack,
39
		PlayerSkin.Patch.STREAM_CODEC,
40
		ResolvableProfile::skinPatch,
41
		ResolvableProfile::create
42
	);
43
	protected final GameProfile partialProfile;
44
	protected final PlayerSkin.Patch skinPatch;
45
46
	private static ResolvableProfile create(final Either<GameProfile, ResolvableProfile.Partial> value, final PlayerSkin.Patch patch) {
47
		return value.map(
48
			full -> new ResolvableProfile.Static(Either.left(full), patch),
49
			partial -> partial.properties.isEmpty() && partial.id.isPresent() != partial.name.isPresent()
50
				? partial.name
51
					.<ResolvableProfile>map(s -> new ResolvableProfile.Dynamic(Either.left(s), patch))
52
					.orElseGet(() -> new ResolvableProfile.Dynamic(Either.right(partial.id.get()), patch))
53
				: new ResolvableProfile.Static(Either.right(partial), patch)
54
		);
55
	}
56
57
	public static ResolvableProfile createResolved(final GameProfile gameProfile) {
58
		return new ResolvableProfile.Static(Either.left(gameProfile), PlayerSkin.Patch.EMPTY);
59
	}
60
61
	public static ResolvableProfile createUnresolved(final String name) {
62
		return new ResolvableProfile.Dynamic(Either.left(name), PlayerSkin.Patch.EMPTY);
63
	}
64
65
	public static ResolvableProfile createUnresolved(final UUID id) {
66
		return new ResolvableProfile.Dynamic(Either.right(id), PlayerSkin.Patch.EMPTY);
67
	}
68
69
	protected abstract Either<GameProfile, ResolvableProfile.Partial> unpack();
70
71
	protected ResolvableProfile(final GameProfile partialProfile, final PlayerSkin.Patch skinPatch) {
72
		this.partialProfile = partialProfile;
73
		this.skinPatch = skinPatch;
74
	}
75
76
	public abstract CompletableFuture<GameProfile> resolveProfile(ProfileResolver profileResolver);
77
78
	public GameProfile partialProfile() {
79
		return this.partialProfile;
80
	}
81
82
	public PlayerSkin.Patch skinPatch() {
83
		return this.skinPatch;
84
	}
85
86
	private static GameProfile createPartialProfile(final Optional<String> maybeName, final Optional<UUID> maybeId, final PropertyMap properties) {
87
		String name = maybeName.orElse("");
88
		UUID id = maybeId.orElseGet(() -> maybeName.map(UUIDUtil::createOfflinePlayerUUID).orElse(Util.NIL_UUID));
89
		return new GameProfile(id, name, properties);
90
	}
91
92
	public abstract Optional<String> name();
93
94
	public static final class Dynamic extends ResolvableProfile {
95
		private static final Component DYNAMIC_TOOLTIP = Component.translatable("component.profile.dynamic").withStyle(ChatFormatting.GRAY);
96
		private final Either<String, UUID> nameOrId;
97
98
		private Dynamic(final Either<String, UUID> nameOrId, final PlayerSkin.Patch skinPatch) {
99
			super(ResolvableProfile.createPartialProfile(nameOrId.left(), nameOrId.right(), PropertyMap.EMPTY), skinPatch);
100
			this.nameOrId = nameOrId;
101
		}
102
103
		@Override
104
		public Optional<String> name() {
105
			return this.nameOrId.left();
106
		}
107
108
		@Override
109
		public boolean equals(final Object o) {
110
			return this == o || o instanceof ResolvableProfile.Dynamic that && this.nameOrId.equals(that.nameOrId) && this.skinPatch.equals(that.skinPatch);
111
		}
112
113
		@Override
114
		public int hashCode() {
115
			int result = 31 + this.nameOrId.hashCode();
116
			return 31 * result + this.skinPatch.hashCode();
117
		}
118
119
		@Override
120
		protected Either<GameProfile, ResolvableProfile.Partial> unpack() {
121
			return Either.right(new ResolvableProfile.Partial(this.nameOrId.left(), this.nameOrId.right(), PropertyMap.EMPTY));
122
		}
123
124
		@Override
125
		public CompletableFuture<GameProfile> resolveProfile(final ProfileResolver profileResolver) {
126
			return CompletableFuture.supplyAsync(() -> profileResolver.fetchByNameOrId(this.nameOrId).orElse(this.partialProfile), Util.nonCriticalIoPool());
127
		}
128
129
		@Override
130
		public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
131
			consumer.accept(DYNAMIC_TOOLTIP);
132
		}
133
	}
134
135
	protected record Partial(Optional<String> name, Optional<UUID> id, PropertyMap properties) {
136
		public static final ResolvableProfile.Partial EMPTY = new ResolvableProfile.Partial(Optional.empty(), Optional.empty(), PropertyMap.EMPTY);
137
		private static final MapCodec<ResolvableProfile.Partial> MAP_CODEC = RecordCodecBuilder.mapCodec(
138
			i -> i.group(
139
					ExtraCodecs.PLAYER_NAME.optionalFieldOf("name").forGetter(ResolvableProfile.Partial::name),
140
					UUIDUtil.CODEC.optionalFieldOf("id").forGetter(ResolvableProfile.Partial::id),
141
					ExtraCodecs.PROPERTY_MAP.optionalFieldOf("properties", PropertyMap.EMPTY).forGetter(ResolvableProfile.Partial::properties)
142
				)
143
				.apply(i, ResolvableProfile.Partial::new)
144
		);
145
		public static final StreamCodec<ByteBuf, ResolvableProfile.Partial> STREAM_CODEC = StreamCodec.composite(
146
			ByteBufCodecs.PLAYER_NAME.apply(ByteBufCodecs::optional),
147
			ResolvableProfile.Partial::name,
148
			UUIDUtil.STREAM_CODEC.apply(ByteBufCodecs::optional),
149
			ResolvableProfile.Partial::id,
150
			ByteBufCodecs.GAME_PROFILE_PROPERTIES,
151
			ResolvableProfile.Partial::properties,
152
			ResolvableProfile.Partial::new
153
		);
154
155
		private GameProfile createProfile() {
156
			return ResolvableProfile.createPartialProfile(this.name, this.id, this.properties);
157
		}
158
	}
159
160
	public static final class Static extends ResolvableProfile {
161
		public static final ResolvableProfile.Static EMPTY = new ResolvableProfile.Static(Either.right(ResolvableProfile.Partial.EMPTY), PlayerSkin.Patch.EMPTY);
162
		private final Either<GameProfile, ResolvableProfile.Partial> contents;
163
164
		private Static(final Either<GameProfile, ResolvableProfile.Partial> contents, final PlayerSkin.Patch skinPatch) {
165
			super(contents.map(gameProfile -> (GameProfile)gameProfile, ResolvableProfile.Partial::createProfile), skinPatch);
166
			this.contents = contents;
167
		}
168
169
		@Override
170
		public CompletableFuture<GameProfile> resolveProfile(final ProfileResolver profileResolver) {
171
			return CompletableFuture.completedFuture(this.partialProfile);
172
		}
173
174
		@Override
175
		protected Either<GameProfile, ResolvableProfile.Partial> unpack() {
176
			return this.contents;
177
		}
178
179
		@Override
180
		public Optional<String> name() {
181
			return this.contents.map(gameProfile -> Optional.of(gameProfile.name()), partial -> partial.name);
182
		}
183
184
		@Override
185
		public boolean equals(final Object o) {
186
			return this == o || o instanceof ResolvableProfile.Static that && this.contents.equals(that.contents) && this.skinPatch.equals(that.skinPatch);
187
		}
188
189
		@Override
190
		public int hashCode() {
191
			int result = 31 + this.contents.hashCode();
192
			return 31 * result + this.skinPatch.hashCode();
193
		}
194
195
		@Override
196
		public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
197
		}
198
	}
199
}
200

Decompiled Minecraft 26.3 source of ResolvableProfile (net/minecraft/world/item/component/ResolvableProfile.java) with Mojang official mappings, stored verbatim on the site.