From 08860dd13e0cf4ea166f63c18834763210275954 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 25 Dec 2016 20:27:55 -0500 Subject: [PATCH] Fix hanging when loading several skins at once. Fixes #25 --- .../voxelmodpack/hdskins/HDSkinManager.java | 26 ++++++++++++------- .../hdskins/mixin/MixinPlayerInfo.java | 20 +++++++------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java index 08566e76..61c67eb9 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java @@ -32,6 +32,7 @@ import net.minecraft.util.ResourceLocation; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.awt.*; import java.awt.image.BufferedImage; @@ -118,7 +119,7 @@ public final class HDSkinManager implements IResourceManagerReloadListener { private void loadTexture(GameProfile profile, final Type type, final SkinAvailableCallback callback) { if (profile != null && profile.getId() != null) { - Map data = getProfileData(profile); + Map data = loadProfileData(profile); final MinecraftProfileTexture texture = data.get(type); if (texture == null) { return; @@ -131,8 +132,9 @@ public final class HDSkinManager implements IResourceManagerReloadListener { ThreadDownloadImageData threaddownloadimagedata = new ThreadDownloadImageData(file2, texture.getUrl(), DefaultPlayerSkin.getDefaultSkinLegacy(), new IImageBuffer() { + @Nonnull @Override - public BufferedImage parseUserSkin(BufferedImage image) { + public BufferedImage parseUserSkin(@Nonnull BufferedImage image) { if (imagebufferdownload != null) return imagebufferdownload.parseUserSkin(image); return image; @@ -154,11 +156,15 @@ public final class HDSkinManager implements IResourceManagerReloadListener { } } - public Map getProfileData(GameProfile profile) { + public Optional> getProfileData(GameProfile profile) { if (!enabled) - return ImmutableMap.of(); - Map textures = this.profileTextures.get(profile.getId()); - if (textures == null) { + return Optional.of(ImmutableMap.of()); + return Optional.ofNullable(this.profileTextures.get(profile.getId())); + + } + + private Map loadProfileData(final GameProfile profile) { + return getProfileData(profile).orElseGet(() -> { String uuid = UUIDTypeAdapter.fromUUID(profile.getId()); @@ -170,10 +176,10 @@ public final class HDSkinManager implements IResourceManagerReloadListener { builder.put(type, new HDProfileTexture(url, hash, null)); } - textures = builder.build(); - this.profileTextures.put(profile.getId(), textures); - } - return textures; + Map textures = builder.build(); + this.profileTextures.put(profile.getId(), textures); + return textures; + }); } private static Map getTexturesForProfile(GameProfile profile) { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/mixin/MixinPlayerInfo.java b/src/hdskins/java/com/voxelmodpack/hdskins/mixin/MixinPlayerInfo.java index 1cfd8946..ce5271c7 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/mixin/MixinPlayerInfo.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/mixin/MixinPlayerInfo.java @@ -1,7 +1,6 @@ package com.voxelmodpack.hdskins.mixin; import com.mojang.authlib.GameProfile; -import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import com.voxelmodpack.hdskins.HDSkinManager; import net.minecraft.client.network.NetworkPlayerInfo; @@ -54,15 +53,16 @@ public abstract class MixinPlayerInfo { cancellable = true, at = @At("RETURN")) private void getSkinType(CallbackInfoReturnable ci) { - MinecraftProfileTexture data = HDSkinManager.INSTANCE.getProfileData(getGameProfile()).get(Type.SKIN); - if (data != null) { - String type = data.getMetadata("model"); - if (type == null) - type = "default"; - String type1 = type; - Optional texture = HDSkinManager.INSTANCE.getSkinLocation(getGameProfile(), Type.SKIN, false); + HDSkinManager.INSTANCE.getProfileData(getGameProfile()) + .map(m -> m.get(Type.SKIN)) + .ifPresent(data -> { + String type = data.getMetadata("model"); + if (type == null) + type = "default"; + String type1 = type; + Optional texture = HDSkinManager.INSTANCE.getSkinLocation(getGameProfile(), Type.SKIN, false); - texture.ifPresent((res) -> ci.setReturnValue(type1)); - } + texture.ifPresent((res) -> ci.setReturnValue(type1)); + }); } }