From 80ab2eaae9052a97142c2064d6849744a49e4be8 Mon Sep 17 00:00:00 2001 From: somehussar <148253652+somehussar@users.noreply.github.com> Date: Sat, 28 Feb 2026 02:23:47 +0100 Subject: [PATCH] Change image download headers. - Simplified connection headers. - Added a config to fall back to the original connection headers. --- .../noppes/npcs/client/ImageDownloadAlt.java | 53 +++++++++++++++---- .../java/noppes/npcs/config/ConfigClient.java | 3 ++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main/java/noppes/npcs/client/ImageDownloadAlt.java b/src/main/java/noppes/npcs/client/ImageDownloadAlt.java index 7d986948f..f8d3b4857 100644 --- a/src/main/java/noppes/npcs/client/ImageDownloadAlt.java +++ b/src/main/java/noppes/npcs/client/ImageDownloadAlt.java @@ -8,6 +8,7 @@ import net.minecraft.client.renderer.texture.SimpleTexture; import net.minecraft.client.renderer.texture.TextureUtil; import net.minecraft.client.resources.IResourceManager; import net.minecraft.util.ResourceLocation; +import noppes.npcs.config.ConfigClient; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -101,18 +102,10 @@ public class ImageDownloadAlt extends SimpleTexture { HttpURLConnection connection = null; try { URL url = new URL(ImageDownloadAlt.this.imageUrl); - connection = (HttpURLConnection) (url).openConnection(Minecraft.getMinecraft().getProxy()); - connection.setDoInput(true); - connection.setDoOutput(false); - connection.setRequestProperty("Content-Type", "image/png"); - connection.setRequestProperty("Expect", "100-continue"); - //connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0"); - connection.setRequestProperty("User-Agent", "Mozilla/5.0"); - // Modify Accept Redirect - if (isImgurLink(url)) { - connection.setRequestProperty("Accept", "*/*"); - } + // Adding a config so people can switch back if needed. + // TODO: Remove config option if improved works better + connection = ConfigClient.ImprovedImageDownloadConnection ? setupConnectionImproved(url) : setupConnectionOld(url); connection.connect(); @@ -152,6 +145,44 @@ public class ImageDownloadAlt extends SimpleTexture { this.imageThread.start(); } + private HttpURLConnection setupConnectionOld(URL url) throws IOException { + HttpURLConnection connection = (HttpURLConnection) (url).openConnection(Minecraft.getMinecraft().getProxy()); + // setups stuff which basically tells the browser if this a get or post request + connection.setDoInput(true); + connection.setDoOutput(false); + + // ??? I believe these are used to tell the receiver what you are sending to them + // Not what you expect to receive from them. + connection.setRequestProperty("Content-Type", "image/png"); + connection.setRequestProperty("Expect", "100-continue"); + //connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0"); + connection.setRequestProperty("User-Agent", "Mozilla/5.0"); + + // Modify Accept Redirect + if (isImgurLink(url)) { + connection.setRequestProperty("Accept", "*/*"); + } + return connection; + } + + private HttpURLConnection setupConnectionImproved(URL url) throws IOException { + HttpURLConnection connection = (HttpURLConnection) (url).openConnection(Minecraft.getMinecraft().getProxy()); + + connection.setRequestMethod("GET"); + // Basically redundant because I set it as a GET request? + // Better safe than sorry ig. + connection.setDoInput(true); + connection.setDoOutput(false); + + // Random ass user agent I grabbed from my snooping on imgur + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0"); + // I tell the server what I want to receive. + // TODO: Should this just accept PNGs? + connection.setRequestProperty("Accept", "image/png,image/*"); + + return connection; + } + // Check if the URL is an Imgur link private static boolean isImgurLink(URL url) { diff --git a/src/main/java/noppes/npcs/config/ConfigClient.java b/src/main/java/noppes/npcs/config/ConfigClient.java index 79a647026..ef0650db0 100644 --- a/src/main/java/noppes/npcs/config/ConfigClient.java +++ b/src/main/java/noppes/npcs/config/ConfigClient.java @@ -157,6 +157,7 @@ public class ConfigClient { public static boolean LegacyTallLamp = false; public static boolean LegacyPedestal = false; public static boolean LegacyMailbox = false; + public static boolean ImprovedImageDownloadConnection = true; public static void init(File configFile) { config = new Configuration(configFile); @@ -303,6 +304,7 @@ public class ConfigClient { BannerAlerts = BannerAlertsProperty.getBoolean(true); WoodTextures = config.get(TEXTURE, "Wood Textures", false, "Models like Chairs and Stools will use default MC Wood Textures").getBoolean(false); + ImprovedImageDownloadConnection = config.get(TEXTURE, "DEBUG: Better handling of image downloads: ", true).getBoolean(true); LegacyCampfire = config.get(MODEL, "Legacy Campfire Model", false).getBoolean(false); LegacyBanner = config.get(MODEL, "Legacy Banner Model", false).getBoolean(false); @@ -333,6 +335,7 @@ public class ConfigClient { FontSize = LegacyConfig.FontSize; FontSizeProperty.set(FontSize); } + } catch (Exception e) { FMLLog.log(Level.ERROR, e, "CNPC+ has had a problem loading its client configuration"); } finally {