From 1ea7ed0c6bf16e58456c6a244b37b2397c674830 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 26 Jul 2018 16:00:33 +0200 Subject: [PATCH] Asynchronous fetch preview textures --- .../voxelmodpack/hdskins/HDSkinManager.java | 2 +- .../voxelmodpack/hdskins/LocalTexture.java | 4 +++- .../hdskins/PreviewTextureManager.java | 23 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java index 2420b505..78f18c38 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java @@ -88,7 +88,7 @@ public final class HDSkinManager implements IResourceManagerReloadListener { private Class skinsClass = null; public static PreviewTextureManager getPreviewTextureManager(GameProfile profile) { - return new PreviewTextureManager(INSTANCE.getGatewayServer().getPreviewTextures(profile)); + return new PreviewTextureManager(profile); } private HDSkinManager() { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/LocalTexture.java b/src/hdskins/java/com/voxelmodpack/hdskins/LocalTexture.java index 6b2ee1a7..a027ba9f 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/LocalTexture.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/LocalTexture.java @@ -76,7 +76,9 @@ public class LocalTexture { public void setRemote(PreviewTextureManager ptm, SkinAvailableCallback callback) { clearRemote(); - remote = ptm.getPreviewTexture(remoteResource, type, blank.getBlankSkin(type), callback); + ptm.getPreviewTexture(remoteResource, type, blank.getBlankSkin(type), callback).thenAccept(texture -> { + remote = texture; + }); } public void setLocal(File file) { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java index 4b5ccd8d..06edfcf3 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java @@ -1,8 +1,10 @@ package com.voxelmodpack.hdskins; import com.google.common.collect.Maps; +import com.mojang.authlib.GameProfile; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; +import com.voxelmodpack.hdskins.skins.CallableFutures; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.IImageBuffer; @@ -10,6 +12,7 @@ import net.minecraft.client.resources.SkinManager.SkinAvailableCallback; import net.minecraft.util.ResourceLocation; import java.util.Map; +import java.util.concurrent.CompletableFuture; import javax.annotation.Nullable; @@ -19,14 +22,26 @@ import javax.annotation.Nullable; */ public class PreviewTextureManager { - private final Map textures; + private final GameProfile profile; - PreviewTextureManager(Map textures) { - this.textures = textures; + private Map textures = null; + + PreviewTextureManager(GameProfile profile) { + this.profile = profile; + } + + public CompletableFuture getPreviewTexture(ResourceLocation location, Type type, ResourceLocation def, @Nullable SkinAvailableCallback callback) { + return CallableFutures.asyncFailableFuture(() -> + loadPreviewTexture(location, type, def, callback) + , HDSkinManager.skinUploadExecutor); } @Nullable - public PreviewTexture getPreviewTexture(ResourceLocation location, Type type, ResourceLocation def, @Nullable SkinAvailableCallback callback) { + private PreviewTexture loadPreviewTexture(ResourceLocation location, Type type, ResourceLocation def, @Nullable SkinAvailableCallback callback) { + if (textures == null) { + textures = HDSkinManager.INSTANCE.getGatewayServer().getPreviewTextures(profile); + } + if (!textures.containsKey(type)) { return null; }