2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.client;
|
|
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.client.renderer.IImageBuffer;
|
|
|
|
|
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 org.apache.commons.io.FileUtils;
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2025-03-13 17:14:45 -04:00
|
|
|
public class ImageDownloadAlt extends SimpleTexture {
|
2018-09-13 12:03:28 -04:00
|
|
|
private static final Logger logger = LogManager.getLogger();
|
|
|
|
|
private static final AtomicInteger threadDownloadCounter = new AtomicInteger(0);
|
|
|
|
|
private final File cacheFile;
|
|
|
|
|
private final String imageUrl;
|
|
|
|
|
private final IImageBuffer imageBuffer;
|
|
|
|
|
private BufferedImage bufferedImage;
|
|
|
|
|
private Thread imageThread;
|
|
|
|
|
private boolean textureUploaded;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public ImageDownloadAlt(File file, String url, ResourceLocation resource, IImageBuffer buffer) {
|
2018-09-13 12:03:28 -04:00
|
|
|
super(resource);
|
|
|
|
|
this.cacheFile = file;
|
|
|
|
|
this.imageUrl = url;
|
|
|
|
|
this.imageBuffer = buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
private void checkTextureUploaded() {
|
|
|
|
|
if (!this.textureUploaded) {
|
|
|
|
|
if (this.bufferedImage != null) {
|
|
|
|
|
if (this.textureLocation != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.deleteGlTexture();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureUtil.uploadTextureImage(super.getGlTextureId(), this.bufferedImage);
|
|
|
|
|
this.textureUploaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getGlTextureId() {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.checkTextureUploaded();
|
|
|
|
|
return super.getGlTextureId();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void setBufferedImage(BufferedImage p_147641_1_) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.bufferedImage = p_147641_1_;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (this.imageBuffer != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.imageBuffer.func_152634_a();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public BufferedImage getBufferedImage() {
|
2022-05-06 00:22:28 -05:00
|
|
|
return this.bufferedImage;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void loadTexture(IResourceManager resourceManager) throws IOException {
|
|
|
|
|
if (this.bufferedImage == null && this.textureLocation != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
super.loadTexture(resourceManager);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (this.imageThread == null) {
|
|
|
|
|
if (this.cacheFile != null && this.cacheFile.isFile()) {
|
|
|
|
|
logger.debug("Loading http texture from local cache ({})", new Object[]{this.cacheFile});
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
try {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.bufferedImage = ImageIO.read(this.cacheFile);
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (this.imageBuffer != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.setBufferedImage(this.imageBuffer.parseUserSkin(this.bufferedImage));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} catch (IOException ioexception) {
|
2018-09-13 12:03:28 -04:00
|
|
|
logger.error("Couldn\'t load skin " + this.cacheFile, ioexception);
|
|
|
|
|
this.loadTextureFromServer();
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.loadTextureFromServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
protected void loadTextureFromServer() {
|
|
|
|
|
this.imageThread = new Thread("Texture Downloader #" + threadDownloadCounter.incrementAndGet()) {
|
|
|
|
|
public void run() {
|
2018-09-13 12:03:28 -04:00
|
|
|
HttpURLConnection connection = null;
|
2025-03-13 17:14:45 -04:00
|
|
|
try {
|
2024-02-29 00:57:49 -05:00
|
|
|
URL url = new URL(ImageDownloadAlt.this.imageUrl);
|
2025-03-13 17:14:45 -04:00
|
|
|
connection = (HttpURLConnection) (url).openConnection(Minecraft.getMinecraft().getProxy());
|
2018-09-13 12:03:28 -04:00
|
|
|
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");
|
2024-03-08 05:16:48 -05:00
|
|
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
2024-02-29 00:57:49 -05:00
|
|
|
|
2024-03-08 05:16:48 -05:00
|
|
|
// Modify Accept Redirect
|
2024-02-29 00:57:49 -05:00
|
|
|
if (isImgurLink(url)) {
|
2024-03-08 05:16:48 -05:00
|
|
|
connection.setRequestProperty("Accept", "*/*");
|
2024-02-29 00:57:49 -05:00
|
|
|
}
|
2024-03-08 05:16:48 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
connection.connect();
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (connection.getResponseCode() / 100 != 2) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferedImage bufferedimage;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (ImageDownloadAlt.this.cacheFile != null) {
|
2025-07-23 05:43:50 -04:00
|
|
|
try (InputStream in = connection.getInputStream()) {
|
|
|
|
|
FileUtils.copyInputStreamToFile(in, ImageDownloadAlt.this.cacheFile);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
bufferedimage = ImageIO.read(ImageDownloadAlt.this.cacheFile);
|
2025-03-13 17:14:45 -04:00
|
|
|
} else {
|
2025-07-23 05:43:50 -04:00
|
|
|
try (InputStream in = connection.getInputStream()) {
|
|
|
|
|
bufferedimage = ImageIO.read(in);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (ImageDownloadAlt.this.imageBuffer != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
bufferedimage = ImageDownloadAlt.this.imageBuffer.parseUserSkin(bufferedimage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageDownloadAlt.this.setBufferedImage(bufferedimage);
|
2025-03-13 17:14:45 -04:00
|
|
|
} catch (MalformedURLException ignored) {
|
|
|
|
|
} catch (Exception exception) {
|
2024-03-08 05:16:48 -05:00
|
|
|
ImageDownloadAlt.logger.error("Couldn\'t download http texture", exception);
|
2025-03-13 17:14:45 -04:00
|
|
|
} finally {
|
|
|
|
|
if (connection != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.imageThread.setDaemon(true);
|
|
|
|
|
this.imageThread.start();
|
|
|
|
|
}
|
2024-02-29 00:57:49 -05:00
|
|
|
|
2024-03-08 05:16:48 -05:00
|
|
|
|
2024-02-29 00:57:49 -05:00
|
|
|
// Check if the URL is an Imgur link
|
|
|
|
|
private static boolean isImgurLink(URL url) {
|
|
|
|
|
return url.getHost().endsWith("imgur.com");
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|