From 304f521e518697f39dd319095237178a90208ab3 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 20 Aug 2018 12:46:45 +0200 Subject: [PATCH] Simplify callback handling --- .../voxelmodpack/hdskins/HDSkinManager.java | 56 ++++++------------- .../hdskins/ISkinAvailableCallback.java | 12 ++++ .../hdskins/ImageBufferDownloadHD.java | 26 +++++++-- .../hdskins/PreviewTextureManager.java | 39 ++++--------- .../voxelmodpack/hdskins/gui/GuiSkins.java | 2 +- .../hdskins/skins/BethlehemSkinServer.java | 2 - 6 files changed, 64 insertions(+), 73 deletions(-) create mode 100644 src/hdskins/java/com/voxelmodpack/hdskins/ISkinAvailableCallback.java diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java index 50159b52..3fd8f771 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java @@ -26,8 +26,6 @@ import com.voxelmodpack.hdskins.skins.ServerType; import com.voxelmodpack.hdskins.skins.SkinServer; import com.voxelmodpack.hdskins.skins.ValhallaSkinServer; import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.IImageBuffer; -import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.DefaultPlayerSkin; import net.minecraft.client.resources.IResourceManager; @@ -59,8 +57,6 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; -import javax.annotation.Nonnull; - public final class HDSkinManager implements IResourceManagerReloadListener { private static final Logger logger = LogManager.getLogger(); @@ -170,41 +166,25 @@ public final class HDSkinManager implements IResourceManagerReloadListener { } private void loadTexture(GameProfile profile, final Type type, final SkinAvailableCallback callback) { - if (profile.getId() != null) { - Map data = getProfileData(profile); - final MinecraftProfileTexture texture = data.get(type); - - String skinDir = type.toString().toLowerCase() + "s/"; - final ResourceLocation skin = new ResourceLocation("hdskins", skinDir + texture.getHash()); - File file2 = new File(LiteLoader.getAssetsDirectory(), "hd/" + skinDir + texture.getHash().substring(0, 2) + "/" + texture.getHash()); - - final IImageBuffer imagebufferdownload = type == Type.SKIN ? new ImageBufferDownloadHD() : null; - - ITextureObject texObject = new ThreadDownloadImageETag(file2, bustCache(texture.getUrl()), - DefaultPlayerSkin.getDefaultSkinLegacy(), - new IImageBuffer() { - @Nonnull - @Override - public BufferedImage parseUserSkin(@Nonnull BufferedImage image) { - BufferedImage image1 = image; - if (imagebufferdownload != null) { - image1 = imagebufferdownload.parseUserSkin(image); - } - return image1 == null ? image : image1; - } - - @Override - public void skinAvailable() { - if (imagebufferdownload != null) { - imagebufferdownload.skinAvailable(); - } - callback.skinAvailable(type, skin, texture); - } - }); - - // schedule texture loading on the main thread. - TextureLoader.loadTexture(skin, texObject); + if (profile.getId() == null) { + return; } + + String skinDir = type.toString().toLowerCase() + "s/"; + + final MinecraftProfileTexture texture = getProfileData(profile).get(type); + final ResourceLocation resource = new ResourceLocation("hdskins", skinDir + texture.getHash()); + + ISkinAvailableCallback buffs = new ImageBufferDownloadHD(type, () -> { + callback.skinAvailable(type, resource, texture); + }); + + // schedule texture loading on the main thread. + TextureLoader.loadTexture(resource, new ThreadDownloadImageETag( + new File(LiteLoader.getAssetsDirectory(), "hd/" + skinDir + texture.getHash().substring(0, 2) + "/" + texture.getHash()), + bustCache(texture.getUrl()), + DefaultPlayerSkin.getDefaultSkinLegacy(), + buffs)); } private Map loadProfileData(GameProfile profile) { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/ISkinAvailableCallback.java b/src/hdskins/java/com/voxelmodpack/hdskins/ISkinAvailableCallback.java new file mode 100644 index 00000000..c4659a36 --- /dev/null +++ b/src/hdskins/java/com/voxelmodpack/hdskins/ISkinAvailableCallback.java @@ -0,0 +1,12 @@ +package com.voxelmodpack.hdskins; + +import net.minecraft.client.renderer.IImageBuffer; + +import java.awt.image.BufferedImage; + +@FunctionalInterface +public interface ISkinAvailableCallback extends IImageBuffer { + default BufferedImage parseUserSkin(BufferedImage image) { + return image; + } +} \ No newline at end of file diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/ImageBufferDownloadHD.java b/src/hdskins/java/com/voxelmodpack/hdskins/ImageBufferDownloadHD.java index a8257be3..ae47b008 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/ImageBufferDownloadHD.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/ImageBufferDownloadHD.java @@ -1,24 +1,39 @@ package com.voxelmodpack.hdskins; -import net.minecraft.client.renderer.IImageBuffer; +import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import javax.annotation.Nullable; import java.awt.Graphics; import java.awt.image.BufferedImage; -public class ImageBufferDownloadHD implements IImageBuffer { +public class ImageBufferDownloadHD implements ISkinAvailableCallback { private int scale; private Graphics graphics; private BufferedImage image; + private ISkinAvailableCallback callback = null; + + private Type skinType = Type.SKIN; + + public ImageBufferDownloadHD() { + + } + + public ImageBufferDownloadHD(Type type, ISkinAvailableCallback callback) { + this.callback = callback; + this.skinType = type; + } + @Override @Nullable @SuppressWarnings({"SuspiciousNameCombination", "NullableProblems"}) public BufferedImage parseUserSkin(@Nullable BufferedImage downloadedImage) { - if (downloadedImage == null) { - return null; + // TODO: Do we want to convert other skin types? + if (downloadedImage == null || skinType != Type.SKIN) { + return downloadedImage; } + int imageWidth = downloadedImage.getWidth(); int imageHeight = downloadedImage.getHeight(); if (imageHeight == imageWidth) { @@ -61,5 +76,8 @@ public class ImageBufferDownloadHD implements IImageBuffer { @Override public void skinAvailable() { + if (callback != null) { + callback.skinAvailable(); + } } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java index d3a6462a..7848be5e 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java @@ -1,17 +1,12 @@ 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.yggdrasil.response.MinecraftTexturesPayload; -import com.voxelmodpack.hdskins.skins.SkinServer; -import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.resources.SkinManager; import net.minecraft.util.ResourceLocation; -import java.awt.image.BufferedImage; import java.util.Map; -import java.util.concurrent.CompletableFuture; import javax.annotation.Nullable; @@ -23,40 +18,28 @@ public class PreviewTextureManager { private final Map textures; - private PreviewTextureManager(MinecraftTexturesPayload payload) { + public PreviewTextureManager(MinecraftTexturesPayload payload) { this.textures = payload.getTextures(); } @Nullable - public PreviewTexture getPreviewTexture(ResourceLocation location, MinecraftProfileTexture.Type type, ResourceLocation def, - @Nullable SkinManager.SkinAvailableCallback callback) { + public PreviewTexture getPreviewTexture(ResourceLocation location, MinecraftProfileTexture.Type type, ResourceLocation def, @Nullable SkinManager.SkinAvailableCallback callback) { if (!textures.containsKey(type)) { return null; } - MinecraftProfileTexture texture = textures.get(type); - IImageBuffer buffer = new ImageBufferDownloadHD(); - PreviewTexture skinTexture = new PreviewTexture(texture.getMetadata("model"), texture.getUrl(), def, - type == MinecraftProfileTexture.Type.SKIN ? new IImageBuffer() { - @Override - @Nullable - public BufferedImage parseUserSkin(BufferedImage image) { - return buffer.parseUserSkin(image); - } - @Override - public void skinAvailable() { - if (callback != null) { - callback.skinAvailable(type, location, new MinecraftProfileTexture(texture.getUrl(), Maps.newHashMap())); - } - } - } : null); + MinecraftProfileTexture texture = textures.get(type); + + ISkinAvailableCallback buff = new ImageBufferDownloadHD(type, () -> { + if (callback != null) { + callback.skinAvailable(type, location, new MinecraftProfileTexture(texture.getUrl(), Maps.newHashMap())); + } + }); + + PreviewTexture skinTexture = new PreviewTexture(texture.getMetadata("model"), texture.getUrl(), def, buff); TextureLoader.loadTexture(location, skinTexture); return skinTexture; } - - public static CompletableFuture load(SkinServer server, GameProfile profile) { - return server.getPreviewTextures(profile).thenApply(PreviewTextureManager::new); - } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java index 2630ee19..b05e66b0 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java @@ -603,7 +603,7 @@ public class GuiSkins extends GameGui { } CompletableFuture loadTextures(GameProfile profile) { - return PreviewTextureManager.load(this.gateway, profile); + return gateway.getPreviewTextures(profile).thenApply(PreviewTextureManager::new); } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java index 0ae82762..4f00db30 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java @@ -28,7 +28,6 @@ public class BethlehemSkinServer implements SkinServer { @Override public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException { - // TODO: Fix this try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) { if (!response.ok()) { throw new IOException(response.getResponse().getStatusLine().getReasonPhrase()); @@ -43,7 +42,6 @@ public class BethlehemSkinServer implements SkinServer { return CallableFutures.asyncFailableFuture(() -> { SkinServer.verifyServerConnection(session, SERVER_ID); - // TODO: Fix this NetClient client = new NetClient("POST", address); client.putHeaders(createHeaders(session, upload));