Simplify callback handling

This commit is contained in:
Sollace 2018-08-20 12:46:45 +02:00
parent e26e21b160
commit 304f521e51
6 changed files with 64 additions and 73 deletions

View file

@ -26,8 +26,6 @@ import com.voxelmodpack.hdskins.skins.ServerType;
import com.voxelmodpack.hdskins.skins.SkinServer; import com.voxelmodpack.hdskins.skins.SkinServer;
import com.voxelmodpack.hdskins.skins.ValhallaSkinServer; import com.voxelmodpack.hdskins.skins.ValhallaSkinServer;
import net.minecraft.client.Minecraft; 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.renderer.texture.TextureManager;
import net.minecraft.client.resources.DefaultPlayerSkin; import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.IResourceManager;
@ -59,8 +57,6 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
public final class HDSkinManager implements IResourceManagerReloadListener { public final class HDSkinManager implements IResourceManagerReloadListener {
private static final Logger logger = LogManager.getLogger(); 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) { private void loadTexture(GameProfile profile, final Type type, final SkinAvailableCallback callback) {
if (profile.getId() != null) { if (profile.getId() == null) {
Map<Type, MinecraftProfileTexture> data = getProfileData(profile); return;
final MinecraftProfileTexture texture = data.get(type); }
String skinDir = type.toString().toLowerCase() + "s/"; 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; final MinecraftProfileTexture texture = getProfileData(profile).get(type);
final ResourceLocation resource = new ResourceLocation("hdskins", skinDir + texture.getHash());
ITextureObject texObject = new ThreadDownloadImageETag(file2, bustCache(texture.getUrl()), ISkinAvailableCallback buffs = new ImageBufferDownloadHD(type, () -> {
DefaultPlayerSkin.getDefaultSkinLegacy(), callback.skinAvailable(type, resource, texture);
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. // schedule texture loading on the main thread.
TextureLoader.loadTexture(skin, texObject); 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<Type, MinecraftProfileTexture> loadProfileData(GameProfile profile) { private Map<Type, MinecraftProfileTexture> loadProfileData(GameProfile profile) {

View file

@ -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;
}
}

View file

@ -1,24 +1,39 @@
package com.voxelmodpack.hdskins; package com.voxelmodpack.hdskins;
import net.minecraft.client.renderer.IImageBuffer; import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class ImageBufferDownloadHD implements IImageBuffer { public class ImageBufferDownloadHD implements ISkinAvailableCallback {
private int scale; private int scale;
private Graphics graphics; private Graphics graphics;
private BufferedImage image; 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 @Override
@Nullable @Nullable
@SuppressWarnings({"SuspiciousNameCombination", "NullableProblems"}) @SuppressWarnings({"SuspiciousNameCombination", "NullableProblems"})
public BufferedImage parseUserSkin(@Nullable BufferedImage downloadedImage) { public BufferedImage parseUserSkin(@Nullable BufferedImage downloadedImage) {
if (downloadedImage == null) { // TODO: Do we want to convert other skin types?
return null; if (downloadedImage == null || skinType != Type.SKIN) {
return downloadedImage;
} }
int imageWidth = downloadedImage.getWidth(); int imageWidth = downloadedImage.getWidth();
int imageHeight = downloadedImage.getHeight(); int imageHeight = downloadedImage.getHeight();
if (imageHeight == imageWidth) { if (imageHeight == imageWidth) {
@ -61,5 +76,8 @@ public class ImageBufferDownloadHD implements IImageBuffer {
@Override @Override
public void skinAvailable() { public void skinAvailable() {
if (callback != null) {
callback.skinAvailable();
}
} }
} }

View file

@ -1,17 +1,12 @@
package com.voxelmodpack.hdskins; package com.voxelmodpack.hdskins;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; 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.client.resources.SkinManager;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import java.awt.image.BufferedImage;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -23,40 +18,28 @@ public class PreviewTextureManager {
private final Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> textures; private final Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> textures;
private PreviewTextureManager(MinecraftTexturesPayload payload) { public PreviewTextureManager(MinecraftTexturesPayload payload) {
this.textures = payload.getTextures(); this.textures = payload.getTextures();
} }
@Nullable @Nullable
public PreviewTexture getPreviewTexture(ResourceLocation location, MinecraftProfileTexture.Type type, ResourceLocation def, public PreviewTexture getPreviewTexture(ResourceLocation location, MinecraftProfileTexture.Type type, ResourceLocation def, @Nullable SkinManager.SkinAvailableCallback callback) {
@Nullable SkinManager.SkinAvailableCallback callback) {
if (!textures.containsKey(type)) { if (!textures.containsKey(type)) {
return null; 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 MinecraftProfileTexture texture = textures.get(type);
public void skinAvailable() {
ISkinAvailableCallback buff = new ImageBufferDownloadHD(type, () -> {
if (callback != null) { if (callback != null) {
callback.skinAvailable(type, location, new MinecraftProfileTexture(texture.getUrl(), Maps.newHashMap())); callback.skinAvailable(type, location, new MinecraftProfileTexture(texture.getUrl(), Maps.newHashMap()));
} }
} });
} : null);
PreviewTexture skinTexture = new PreviewTexture(texture.getMetadata("model"), texture.getUrl(), def, buff);
TextureLoader.loadTexture(location, skinTexture); TextureLoader.loadTexture(location, skinTexture);
return skinTexture; return skinTexture;
} }
public static CompletableFuture<PreviewTextureManager> load(SkinServer server, GameProfile profile) {
return server.getPreviewTextures(profile).thenApply(PreviewTextureManager::new);
}
} }

View file

@ -603,7 +603,7 @@ public class GuiSkins extends GameGui {
} }
CompletableFuture<PreviewTextureManager> loadTextures(GameProfile profile) { CompletableFuture<PreviewTextureManager> loadTextures(GameProfile profile) {
return PreviewTextureManager.load(this.gateway, profile); return gateway.getPreviewTextures(profile).thenApply(PreviewTextureManager::new);
} }
} }

View file

@ -28,7 +28,6 @@ public class BethlehemSkinServer implements SkinServer {
@Override @Override
public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException { public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException {
// TODO: Fix this
try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) { try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) {
if (!response.ok()) { if (!response.ok()) {
throw new IOException(response.getResponse().getStatusLine().getReasonPhrase()); throw new IOException(response.getResponse().getStatusLine().getReasonPhrase());
@ -43,7 +42,6 @@ public class BethlehemSkinServer implements SkinServer {
return CallableFutures.asyncFailableFuture(() -> { return CallableFutures.asyncFailableFuture(() -> {
SkinServer.verifyServerConnection(session, SERVER_ID); SkinServer.verifyServerConnection(session, SERVER_ID);
// TODO: Fix this
NetClient client = new NetClient("POST", address); NetClient client = new NetClient("POST", address);
client.putHeaders(createHeaders(session, upload)); client.putHeaders(createHeaders(session, upload));