From 02bc96d76c2f4eaea4ecf9dd991dc419119c1d59 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 15 Jul 2018 12:21:47 +0200 Subject: [PATCH] General cleanup --- .../hdskins/DynamicTextureImage.java | 4 +- .../voxelmodpack/hdskins/HDSkinManager.java | 18 ++--- .../voxelmodpack/hdskins/PreviewTexture.java | 17 +++-- .../hdskins/PreviewTextureManager.java | 51 ++++++++------ .../voxelmodpack/hdskins/TextureLoader.java | 8 ++- .../hdskins/ThreadDownloadImageETag.java | 69 +++++++++---------- .../hdskins/gui/GuiItemStackButton.java | 2 +- 7 files changed, 95 insertions(+), 74 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/DynamicTextureImage.java b/src/hdskins/java/com/voxelmodpack/hdskins/DynamicTextureImage.java index d7598b08..87d7c945 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/DynamicTextureImage.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/DynamicTextureImage.java @@ -10,7 +10,7 @@ public class DynamicTextureImage extends DynamicTexture { public DynamicTextureImage(BufferedImage bufferedImage) { super(bufferedImage); - this.image = bufferedImage; + image = bufferedImage; } public BufferedImage getImage() { @@ -20,7 +20,7 @@ public class DynamicTextureImage extends DynamicTexture { @Override public void deleteGlTexture() { super.deleteGlTexture(); - this.image = null; + image = null; } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java index b200c3ef..d7b0d901 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java @@ -82,6 +82,7 @@ public final class HDSkinManager implements IResourceManagerReloadListener { private Map> skinCache = Maps.newHashMap(); + // TODO: Garggling marbles private LoadingCache> skins = CacheBuilder.newBuilder() .initialCapacity(20) .maximumSize(100) @@ -248,16 +249,16 @@ public final class HDSkinManager implements IResourceManagerReloadListener { } public Class getSkinServerClass(String type) { - return this.skinServerTypes.get(type); + return skinServerTypes.get(type); } public void addSkinServer(SkinServer skinServer) { - this.skinServers.add(skinServer); + skinServers.add(skinServer); } @Deprecated public SkinServer getGatewayServer() { - return this.skinServers.get(0); + return skinServers.get(0); } public void setEnabled(boolean enabled) { @@ -278,9 +279,10 @@ public final class HDSkinManager implements IResourceManagerReloadListener { try { FileUtils.deleteDirectory(new File(LiteLoader.getAssetsDirectory(), "skins")); FileUtils.deleteDirectory(new File(LiteLoader.getAssetsDirectory(), "hd")); + TextureManager textures = Minecraft.getMinecraft().getTextureManager(); - INSTANCE.skinCache.values().stream() - .flatMap(m -> m.values().stream()) + + INSTANCE.skinCache.values().stream().flatMap(m -> m.values().stream()) .forEach(textures::deleteTexture); INSTANCE.skinCache.clear(); INSTANCE.skins.invalidateAll(); @@ -313,13 +315,11 @@ public final class HDSkinManager implements IResourceManagerReloadListener { } public void convertSkin(BufferedImage image, Graphics dest) { - for (ISkinModifier skin : skinModifiers) { - skin.convertSkin(image, dest); - } + skinModifiers.forEach(a -> a.convertSkin(image, dest)); } @Override public void onResourceManagerReload(IResourceManager resourceManager) { - this.resources.onResourceManagerReload(resourceManager); + resources.onResourceManagerReload(resourceManager); } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTexture.java b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTexture.java index e5430ee2..336657ec 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTexture.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTexture.java @@ -6,16 +6,21 @@ import net.minecraft.util.ResourceLocation; import javax.annotation.Nullable; +import com.mojang.authlib.minecraft.MinecraftProfileTexture; + public class PreviewTexture extends ThreadDownloadImageData { private boolean uploaded; private String model; - public PreviewTexture(@Nullable String model, String url, ResourceLocation fallbackTexture, @Nullable IImageBuffer imageBuffer) { - super(null, url, fallbackTexture, imageBuffer); + public PreviewTexture(MinecraftProfileTexture texture, ResourceLocation fallbackTexture, @Nullable IImageBuffer imageBuffer) { + super(null, texture.getUrl(), fallbackTexture, imageBuffer); - this.model = model == null ? "default" : model; + model = texture.getMetadata("model"); + if (model == null) { + model = "default"; + } } public boolean isTextureUploaded() { @@ -25,7 +30,7 @@ public class PreviewTexture extends ThreadDownloadImageData { @Override public void deleteGlTexture() { super.deleteGlTexture(); - this.uploaded = true; + uploaded = true; } public boolean hasModel() { @@ -35,4 +40,8 @@ public class PreviewTexture extends ThreadDownloadImageData { public boolean usesThinArms() { return "thin".equals(model); } + + public String getModel() { + return model; + } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java index 057fd56f..23159208 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/PreviewTextureManager.java @@ -2,6 +2,8 @@ package com.voxelmodpack.hdskins; import com.google.common.collect.Maps; import com.mojang.authlib.minecraft.MinecraftProfileTexture; +import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; + import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.resources.SkinManager; @@ -18,35 +20,44 @@ import javax.annotation.Nullable; */ public class PreviewTextureManager { - private final Map textures; + private final Map textures; - PreviewTextureManager(Map textures) { + PreviewTextureManager(Map textures) { this.textures = textures; } + private IImageBuffer getPreviewImageBuffer(MinecraftProfileTexture texture, ResourceLocation location, Type type, SkinManager.SkinAvailableCallback callback) { + if (type != Type.SKIN) { + return null; + } + + IImageBuffer buffer = new ImageBufferDownloadHD(); + + return 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())); + } + } + }; + } + @Nullable - public PreviewTexture getPreviewTexture(ResourceLocation location, MinecraftProfileTexture.Type type, ResourceLocation def, - @Nullable SkinManager.SkinAvailableCallback callback) { + public PreviewTexture getPreviewTexture(ResourceLocation location, 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); + PreviewTexture skinTexture = new PreviewTexture(texture, def, getPreviewImageBuffer(texture, location, type, callback)); + Minecraft.getMinecraft().getTextureManager().loadTexture(location, skinTexture); return skinTexture; diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/TextureLoader.java b/src/hdskins/java/com/voxelmodpack/hdskins/TextureLoader.java index 959e0131..92dbd8db 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/TextureLoader.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/TextureLoader.java @@ -6,9 +6,11 @@ import net.minecraft.util.ResourceLocation; public class TextureLoader { - private static Minecraft mc = Minecraft.getMinecraft(); + private static final Minecraft mc = Minecraft.getMinecraft(); - public static void loadTexture(final ResourceLocation textureLocation, final ITextureObject textureObj) { - mc.addScheduledTask((Runnable) () -> mc.getTextureManager().loadTexture(textureLocation, textureObj)); + public static void loadTexture(ResourceLocation textureLocation, ITextureObject textureObj) { + mc.addScheduledTask(() -> { + mc.getTextureManager().loadTexture(textureLocation, textureObj); + }); } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java b/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java index 514aac1a..96c9fb69 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java @@ -12,7 +12,6 @@ import org.apache.http.Header; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; @@ -36,6 +35,7 @@ public class ThreadDownloadImageETag extends SimpleTexture { private final File cacheFile; private final File eTagFile; private final String imageUrl; + @Nullable private final IImageBuffer imageBuffer; @@ -47,35 +47,32 @@ public class ThreadDownloadImageETag extends SimpleTexture { public ThreadDownloadImageETag(@Nonnull File cacheFileIn, String imageUrlIn, ResourceLocation defLocation, @Nullable IImageBuffer imageBufferIn) { super(defLocation); - this.cacheFile = cacheFileIn; - this.eTagFile = new File(cacheFile.getParentFile(), cacheFile.getName() + ".etag"); - this.imageUrl = imageUrlIn; - this.imageBuffer = imageBufferIn; - } - - private void checkTextureUploaded() { - if (!this.textureUploaded) { - if (this.bufferedImage != null) { - if (this.textureLocation != null) { - this.deleteGlTexture(); - } - - TextureUtil.uploadTextureImage(super.getGlTextureId(), this.bufferedImage); - this.textureUploaded = true; - } - } + cacheFile = cacheFileIn; + eTagFile = new File(cacheFile.getParentFile(), cacheFile.getName() + ".etag"); + imageUrl = imageUrlIn; + imageBuffer = imageBufferIn; } public int getGlTextureId() { - this.checkTextureUploaded(); + if (!textureUploaded) { + if (bufferedImage != null) { + if (textureLocation != null) { + deleteGlTexture(); + } + + TextureUtil.uploadTextureImage(super.getGlTextureId(), bufferedImage); + textureUploaded = true; + } + } + return super.getGlTextureId(); } private void setBufferedImage(@Nonnull BufferedImage bufferedImageIn) { - this.bufferedImage = bufferedImageIn; + bufferedImage = bufferedImageIn; - if (this.imageBuffer != null) { - this.imageBuffer.skinAvailable(); + if (imageBuffer != null) { + imageBuffer.skinAvailable(); } } @@ -85,22 +82,22 @@ public class ThreadDownloadImageETag extends SimpleTexture { } public void loadTexture(IResourceManager resourceManager) throws IOException { - if (this.bufferedImage == null && this.textureLocation != null) { + if (bufferedImage == null && textureLocation != null) { super.loadTexture(resourceManager); } - if (this.imageThread == null) { - this.imageThread = new Thread(this::loadTexture, "Texture Downloader #" + THREAD_ID.incrementAndGet()); - this.imageThread.setDaemon(true); - this.imageThread.start(); + if (imageThread == null) { + imageThread = new Thread(this::loadTexture, "Texture Downloader #" + THREAD_ID.incrementAndGet()); + imageThread.setDaemon(true); + imageThread.start(); } } private void loadTexture() { HttpResponse response = null; try { - HttpClient client = HttpClientBuilder.create().build(); - response = client.execute(new HttpGet(imageUrl)); + response = HttpClientBuilder.create().build().execute(new HttpGet(imageUrl)); + int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_NOT_FOUND) { // delete the cache files in case we can't connect in the future @@ -133,8 +130,9 @@ public class ThreadDownloadImageETag extends SimpleTexture { } LOGGER.error("Couldn't load skin {} ", imageUrl, e); } finally { - if (response != null) + if (response != null) { EntityUtils.consumeQuietly(response.getEntity()); + } } } @@ -149,8 +147,8 @@ public class ThreadDownloadImageETag extends SimpleTexture { } private void clearCache() { - FileUtils.deleteQuietly(this.cacheFile); - FileUtils.deleteQuietly(this.eTagFile); + FileUtils.deleteQuietly(cacheFile); + FileUtils.deleteQuietly(eTagFile); } private boolean checkETag(HttpResponse response) { @@ -158,14 +156,15 @@ public class ThreadDownloadImageETag extends SimpleTexture { if (cacheFile.isFile()) { String localETag = Files.readFirstLine(eTagFile, Charsets.UTF_8); Header remoteETag = response.getFirstHeader(HttpHeaders.ETAG); + // true if no remote etag or does match return remoteETag == null || localETag.equals(remoteETag.getValue()); } - return false; } catch (IOException e) { - // it failed, so re-fetch. - return false; + } + + return false; // it failed, so re-fetch. } private void loadTextureFromServer(HttpResponse response) { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiItemStackButton.java b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiItemStackButton.java index d4d7eede..79c88a13 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiItemStackButton.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiItemStackButton.java @@ -17,6 +17,6 @@ public class GuiItemStackButton extends GuiButton { public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) { super.drawButton(mc, mouseX, mouseY, partialTicks); - mc.getRenderItem().renderItemIntoGUI(itemStack, this.x + 2, this.y + 2); + mc.getRenderItem().renderItemIntoGUI(itemStack, x + 2, y + 2); } }