|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.cache_profile_texture_url; |
| 2 | + |
| 3 | +import com.google.common.cache.Cache; |
| 4 | +import com.google.common.cache.CacheBuilder; |
| 5 | +import com.mojang.authlib.minecraft.MinecraftProfileTexture; |
| 6 | +import net.minecraft.client.resources.SkinManager; |
| 7 | +import org.spongepowered.asm.mixin.Mixin; |
| 8 | +import org.spongepowered.asm.mixin.Unique; |
| 9 | +import org.spongepowered.asm.mixin.injection.At; |
| 10 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 11 | + |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +@Mixin(SkinManager.class) |
| 16 | +public class SkinManagerMixin { |
| 17 | + @Unique |
| 18 | + private final Cache<MinecraftProfileTexture, String> mfix$hashCache = CacheBuilder.newBuilder() |
| 19 | + .expireAfterAccess(60, TimeUnit.SECONDS) |
| 20 | + .concurrencyLevel(1) |
| 21 | + .build(); |
| 22 | + |
| 23 | + @Redirect(method = "registerTexture(Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;Lcom/mojang/authlib/minecraft/MinecraftProfileTexture$Type;Lnet/minecraft/client/resources/SkinManager$SkinTextureCallback;)Lnet/minecraft/resources/ResourceLocation;", |
| 24 | + at = @At(value = "INVOKE", target = "Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;getHash()Ljava/lang/String;", remap = false)) |
| 25 | + private String useCachedHash(MinecraftProfileTexture texture) { |
| 26 | + // avoid lambda allocation for common case |
| 27 | + String hash = mfix$hashCache.getIfPresent(texture); |
| 28 | + if(hash != null) |
| 29 | + return hash; |
| 30 | + |
| 31 | + try { |
| 32 | + return mfix$hashCache.get(texture, texture::getHash); |
| 33 | + } catch (ExecutionException e) { |
| 34 | + throw new RuntimeException(e); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
0 commit comments