123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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<String, ResourceLocation> skins;
- private final HashSet<String> loading;
-
- private final HashMap<String, ArrayList<Consumer<ResourceLocation>>> 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<ResourceLocation> 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<Consumer<ResourceLocation>> list = queue.get(name);
- if(list != null)
- {
- list.forEach(c -> c.accept(rloc));
- list.clear();
- queue.remove(name);
- }
- });
- }
- else
- {
- ArrayList<Consumer<ResourceLocation>> 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<BufferedImage> 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();
- }
- }
|