| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.mojang.authlib.GameProfile;
|
| 7 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 8 | import java.time.Duration;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.gui.font.GlyphRenderTypes;
|
| 15 | import net.minecraft.client.renderer.blockentity.SkullBlockRenderer;
|
| 16 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 17 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 18 | import net.minecraft.client.resources.DefaultPlayerSkin;
|
| 19 | import net.minecraft.client.resources.SkinManager;
|
| 20 | import net.minecraft.server.players.ProfileResolver;
|
| 21 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 22 | import net.minecraft.world.entity.player.PlayerSkin.Patch;
|
| 23 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class PlayerSkinRenderCache {
|
| 28 | public static final RenderType DEFAULT_PLAYER_SKIN_RENDER_TYPE = playerSkinRenderType(DefaultPlayerSkin.getDefaultSkin());
|
| 29 | public static final Duration CACHE_DURATION = Duration.ofMinutes(5L);
|
| 30 | private final LoadingCache<ResolvableProfile, CompletableFuture<Optional<PlayerSkinRenderCache.RenderInfo>>> renderInfoCache = CacheBuilder.newBuilder()
|
| 31 | .expireAfterAccess(CACHE_DURATION)
|
| 32 | .build(
|
| 33 | new CacheLoader<ResolvableProfile, CompletableFuture<Optional<PlayerSkinRenderCache.RenderInfo>>>() {
|
| 34 | public CompletableFuture<Optional<PlayerSkinRenderCache.RenderInfo>> load(final ResolvableProfile profile) {
|
| 35 | return profile.resolveProfile(PlayerSkinRenderCache.this.profileResolver)
|
| 36 | .thenCompose(
|
| 37 | resolvedProfile -> PlayerSkinRenderCache.this.skinManager
|
| 38 | .get(resolvedProfile)
|
| 39 | .thenApply(playerSkin -> playerSkin.map(skin -> PlayerSkinRenderCache.this.new RenderInfo(resolvedProfile, skin, profile.skinPatch())))
|
| 40 | );
|
| 41 | }
|
| 42 | }
|
| 43 | );
|
| 44 | private final LoadingCache<ResolvableProfile, PlayerSkinRenderCache.RenderInfo> defaultSkinCache = CacheBuilder.newBuilder()
|
| 45 | .expireAfterAccess(CACHE_DURATION)
|
| 46 | .build(new CacheLoader<ResolvableProfile, PlayerSkinRenderCache.RenderInfo>() {
|
| 47 | public PlayerSkinRenderCache.RenderInfo load(final ResolvableProfile profile) {
|
| 48 | GameProfile temporaryProfile = profile.partialProfile();
|
| 49 | return PlayerSkinRenderCache.this.new RenderInfo(temporaryProfile, DefaultPlayerSkin.get(temporaryProfile), profile.skinPatch());
|
| 50 | }
|
| 51 | });
|
| 52 | private final TextureManager textureManager;
|
| 53 | private final SkinManager skinManager;
|
| 54 | private final ProfileResolver profileResolver;
|
| 55 |
|
| 56 | public PlayerSkinRenderCache(final TextureManager textureManager, final SkinManager skinManager, final ProfileResolver profileResolver) {
|
| 57 | this.textureManager = textureManager;
|
| 58 | this.skinManager = skinManager;
|
| 59 | this.profileResolver = profileResolver;
|
| 60 | }
|
| 61 |
|
| 62 | public PlayerSkinRenderCache.RenderInfo getOrDefault(final ResolvableProfile profile) {
|
| 63 | PlayerSkinRenderCache.RenderInfo result = this.lookup(profile).getNow(Optional.empty()).orElse(null);
|
| 64 | return result != null ? result : this.defaultSkinCache.getUnchecked(profile);
|
| 65 | }
|
| 66 |
|
| 67 | public Supplier<PlayerSkinRenderCache.RenderInfo> createLookup(final ResolvableProfile profile) {
|
| 68 | PlayerSkinRenderCache.RenderInfo defaultForProfile = this.defaultSkinCache.getUnchecked(profile);
|
| 69 | CompletableFuture<Optional<PlayerSkinRenderCache.RenderInfo>> future = this.renderInfoCache.getUnchecked(profile);
|
| 70 | Optional<PlayerSkinRenderCache.RenderInfo> currentValue = future.getNow(null);
|
| 71 | if (currentValue != null) {
|
| 72 | PlayerSkinRenderCache.RenderInfo finalValue = currentValue.orElse(defaultForProfile);
|
| 73 | return () -> finalValue;
|
| 74 | } else {
|
| 75 | return () -> future.getNow(Optional.empty()).orElse(defaultForProfile);
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | public CompletableFuture<Optional<PlayerSkinRenderCache.RenderInfo>> lookup(final ResolvableProfile profile) {
|
| 80 | return this.renderInfoCache.getUnchecked(profile);
|
| 81 | }
|
| 82 |
|
| 83 | private static RenderType playerSkinRenderType(final PlayerSkin playerSkin) {
|
| 84 | return SkullBlockRenderer.getPlayerSkinRenderType(playerSkin.body().texturePath());
|
| 85 | }
|
| 86 |
|
| 87 | @Environment(EnvType.CLIENT)
|
| 88 | public final class RenderInfo {
|
| 89 | private final GameProfile gameProfile;
|
| 90 | private final PlayerSkin playerSkin;
|
| 91 | private @Nullable RenderType itemRenderType;
|
| 92 | private @Nullable GpuTextureView textureView;
|
| 93 | private @Nullable GlyphRenderTypes glyphRenderTypes;
|
| 94 |
|
| 95 | public RenderInfo(final GameProfile gameProfile, final PlayerSkin playerSkin, final Patch patch) {
|
| 96 | this.gameProfile = gameProfile;
|
| 97 | this.playerSkin = playerSkin.with(patch);
|
| 98 | }
|
| 99 |
|
| 100 | public GameProfile gameProfile() {
|
| 101 | return this.gameProfile;
|
| 102 | }
|
| 103 |
|
| 104 | public PlayerSkin playerSkin() {
|
| 105 | return this.playerSkin;
|
| 106 | }
|
| 107 |
|
| 108 | public RenderType renderType() {
|
| 109 | if (this.itemRenderType == null) {
|
| 110 | this.itemRenderType = PlayerSkinRenderCache.playerSkinRenderType(this.playerSkin);
|
| 111 | }
|
| 112 |
|
| 113 | return this.itemRenderType;
|
| 114 | }
|
| 115 |
|
| 116 | public GpuTextureView textureView() {
|
| 117 | if (this.textureView == null) {
|
| 118 | this.textureView = PlayerSkinRenderCache.this.textureManager.getTexture(this.playerSkin.body().texturePath()).getTextureView();
|
| 119 | }
|
| 120 |
|
| 121 | return this.textureView;
|
| 122 | }
|
| 123 |
|
| 124 | public GlyphRenderTypes glyphRenderTypes() {
|
| 125 | if (this.glyphRenderTypes == null) {
|
| 126 | this.glyphRenderTypes = GlyphRenderTypes.createForColorTexture(this.playerSkin.body().texturePath());
|
| 127 | }
|
| 128 |
|
| 129 | return this.glyphRenderTypes;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public boolean equals(final Object o) {
|
| 134 | return this == o
|
| 135 | || o instanceof PlayerSkinRenderCache.RenderInfo that && this.gameProfile.equals(that.gameProfile) && this.playerSkin.equals(that.playerSkin);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public int hashCode() {
|
| 140 | int result = 1;
|
| 141 | result = 31 * result + this.gameProfile.hashCode();
|
| 142 | return 31 * result + this.playerSkin.hashCode();
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|