package me.km.entities; import java.awt.image.BufferedImage; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.function.Consumer; import javax.imageio.ImageIO; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.ImageBufferDownload; import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class HumanSkinLoader { public final static HumanSkinLoader INSTANCE = new HumanSkinLoader(); private static final ResourceLocation TEXTURE_STEVE = new ResourceLocation("textures/entity/steve.png"); private final HashMap skins; private final HashSet loading; private final HashMap>> queue; private TextureManager manager; private ImageBufferDownload converter; public HumanSkinLoader() { this.skins = new HashMap<>(); this.loading = new HashSet<>(); this.queue = new HashMap<>(); this.manager = Minecraft.getMinecraft().getRenderManager().renderEngine; this.converter = new ImageBufferDownload(); } public ResourceLocation getTexture(String name, Consumer delayed) { ResourceLocation loc = skins.get(name); if(loc == null) { if(loading.add(name)) { downloadSkin(name, image -> { ResourceLocation rloc = manager.getDynamicTextureLocation("skin_" + name, new DynamicTexture(image)); skins.put(name, rloc); loading.remove(name); delayed.accept(rloc); ArrayList> list = queue.get(name); if(list != null) { list.forEach(c -> c.accept(rloc)); list.clear(); queue.remove(name); } }); } else { ArrayList> list = queue.get(name); if(list == null) { list = new ArrayList<>(); queue.put(name, list); } list.add(delayed); } return TEXTURE_STEVE; } return loc; } private void downloadSkin(String name, Consumer delayed) { new Thread(() -> { HttpURLConnection httpurlconnection = null; try { //httpurlconnection = (HttpURLConnection)(new URL("http://skins.minecraft.net/MinecraftSkins/" + name + ".png")) httpurlconnection = (HttpURLConnection) (new URL("http://skins.hammerle.me/" + name + ".png")) .openConnection(Minecraft.getMinecraft().getProxy()); httpurlconnection.setDoInput(true); httpurlconnection.setDoOutput(false); httpurlconnection.connect(); int code = httpurlconnection.getResponseCode(); if(code != 200) { // Failed own server, trying mojangs httpurlconnection.disconnect(); httpurlconnection = (HttpURLConnection) (new URL("http://skins.minecraft.net/MinecraftSkins/" + name + ".png")) .openConnection(Minecraft.getMinecraft().getProxy()); httpurlconnection.setDoInput(true); httpurlconnection.setDoOutput(false); httpurlconnection.connect(); code = httpurlconnection.getResponseCode(); if(code != 200) { System.out.println("Server response code did return " + code + ", skin servers might be down."); return; } } BufferedImage image = converter.parseUserSkin(ImageIO.read(httpurlconnection.getInputStream())); Minecraft.getMinecraft().addScheduledTask(() -> delayed.accept(image)); } catch (Exception ex) { System.out.println("Error occurred when downloading skin, however, skin servers seem to be up."); } finally { if (httpurlconnection != null) { httpurlconnection.disconnect(); } } }).start(); } }