SHARE
TWEET

Untitled

a guest Jan 28th, 2020 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.minecraft.client.resources;
  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.google.common.collect.Maps;
  7. import com.mojang.authlib.GameProfile;
  8. import com.mojang.authlib.minecraft.InsecureTextureException;
  9. import com.mojang.authlib.minecraft.MinecraftProfileTexture;
  10. import com.mojang.authlib.minecraft.MinecraftSessionService;
  11. import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
  12. import java.awt.image.BufferedImage;
  13. import java.io.File;
  14. import java.util.Map;
  15. import java.util.concurrent.ExecutorService;
  16. import java.util.concurrent.LinkedBlockingQueue;
  17. import java.util.concurrent.ThreadPoolExecutor;
  18. import java.util.concurrent.TimeUnit;
  19. import net.minecraft.client.Minecraft;
  20. import net.minecraft.client.renderer.IImageBuffer;
  21. import net.minecraft.client.renderer.ImageBufferDownload;
  22. import net.minecraft.client.renderer.ThreadDownloadImageData;
  23. import net.minecraft.client.renderer.texture.ITextureObject;
  24. import net.minecraft.client.renderer.texture.TextureManager;
  25. import net.minecraft.util.ResourceLocation;
  26.  
  27. public class SkinManager {
  28.    private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(0, 2, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue());
  29.    private final TextureManager textureManager;
  30.    private final File skinCacheDir;
  31.    private final MinecraftSessionService sessionService;
  32.    private final LoadingCache<GameProfile, Map<Type, MinecraftProfileTexture>> skinCacheLoader;
  33.  
  34.    public SkinManager(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService) {
  35.       this.textureManager = textureManagerInstance;
  36.       this.skinCacheDir = skinCacheDirectory;
  37.       this.sessionService = sessionService;
  38.       this.skinCacheLoader = CacheBuilder.newBuilder().expireAfterAccess(15L, TimeUnit.SECONDS).<GameProfile, Map<Type, MinecraftProfileTexture>>build(new CacheLoader<GameProfile, Map<Type, MinecraftProfileTexture>>() {
  39.          public Map<Type, MinecraftProfileTexture> load(GameProfile p_load_1_) throws Exception {
  40.             return Minecraft.getMinecraft().getSessionService().getTextures(p_load_1_, false);
  41.          }
  42.       });
  43.    }
  44.  
  45.    public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, Type p_152792_2_) {
  46.       return this.loadSkin(profileTexture, p_152792_2_, (SkinManager.SkinAvailableCallback)null);
  47.    }
  48.  
  49.    public ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final Type p_152789_2_, final SkinManager.SkinAvailableCallback skinAvailableCallback) {
  50.       final ResourceLocation resourcelocation = new ResourceLocation("skins/" + profileTexture.getHash());
  51.       ITextureObject itextureobject = this.textureManager.getTexture(resourcelocation);
  52.       if(itextureobject != null) {
  53.          if(skinAvailableCallback != null) {
  54.             skinAvailableCallback.skinAvailable(p_152789_2_, resourcelocation, profileTexture);
  55.          }
  56.       } else {
  57.          File file1 = new File(this.skinCacheDir, profileTexture.getHash().length() > 2?profileTexture.getHash().substring(0, 2):"xx");
  58.          File file2 = new File(file1, profileTexture.getHash());
  59.          final IImageBuffer iimagebuffer = p_152789_2_ == Type.SKIN?new ImageBufferDownload():null;
  60.          ThreadDownloadImageData threaddownloadimagedata = new ThreadDownloadImageData(file2, profileTexture.getUrl(), DefaultPlayerSkin.getDefaultSkinLegacy(), new IImageBuffer() {
  61.             public BufferedImage parseUserSkin(BufferedImage image) {
  62.                if(iimagebuffer != null) {
  63.                   image = iimagebuffer.parseUserSkin(image);
  64.                }
  65.  
  66.                return image;
  67.             }
  68.  
  69.             public void skinAvailable() {
  70.                if(iimagebuffer != null) {
  71.                   iimagebuffer.skinAvailable();
  72.                }
  73.  
  74.                if(skinAvailableCallback != null) {
  75.                   skinAvailableCallback.skinAvailable(p_152789_2_, resourcelocation, profileTexture);
  76.                }
  77.             }
  78.          });
  79.          this.textureManager.loadTexture(resourcelocation, threaddownloadimagedata);
  80.       }
  81.  
  82.       return resourcelocation;
  83.    }
  84.  
  85.    public void loadProfileTextures(final GameProfile profile, final SkinManager.SkinAvailableCallback skinAvailableCallback, final boolean requireSecure) {
  86.       THREAD_POOL.submit(new Runnable() {
  87.          public void run() {
  88.             final Map<Type, MinecraftProfileTexture> map = Maps.<Type, MinecraftProfileTexture>newHashMap();
  89.  
  90.             try {
  91.                map.putAll(SkinManager.this.sessionService.getTextures(profile, requireSecure));
  92.             } catch (InsecureTextureException var3) {
  93.                ;
  94.             }
  95.  
  96.             if(map.isEmpty() && profile.getId().equals(Minecraft.getMinecraft().getSession().getProfile().getId())) {
  97.                profile.getProperties().clear();
  98.                profile.getProperties().putAll(Minecraft.getMinecraft().func_181037_M());
  99.                map.putAll(SkinManager.this.sessionService.getTextures(profile, false));
  100.             }
  101.  
  102.             Minecraft.getMinecraft().addScheduledTask(new Runnable() {
  103.                public void run() {
  104.                   if(map.containsKey(Type.SKIN)) {
  105.                      SkinManager.this.loadSkin((MinecraftProfileTexture)map.get(Type.SKIN), Type.SKIN, skinAvailableCallback);
  106.                   }
  107.  
  108.                   if(map.containsKey(Type.CAPE)) {
  109.                      SkinManager.this.loadSkin((MinecraftProfileTexture)map.get(Type.CAPE), Type.CAPE, skinAvailableCallback);
  110.                   }
  111.                }
  112.             });
  113.          }
  114.       });
  115.    }
  116.  
  117.    public Map<Type, MinecraftProfileTexture> loadSkinFromCache(GameProfile profile) {
  118.       return (Map)this.skinCacheLoader.getUnchecked(profile);
  119.    }
  120.  
  121.    public interface SkinAvailableCallback {
  122.       void skinAvailable(Type p_180521_1_, ResourceLocation location, MinecraftProfileTexture profileTexture);
  123.    }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top