HumanSkinLoader.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package me.km.entities;
  2. import java.awt.image.BufferedImage;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.function.Consumer;
  9. import javax.imageio.ImageIO;
  10. import net.minecraft.client.Minecraft;
  11. import net.minecraft.client.renderer.ImageBufferDownload;
  12. import net.minecraft.client.renderer.texture.DynamicTexture;
  13. import net.minecraft.client.renderer.texture.TextureManager;
  14. import net.minecraft.util.ResourceLocation;
  15. import net.minecraftforge.fml.relauncher.Side;
  16. import net.minecraftforge.fml.relauncher.SideOnly;
  17. @SideOnly(Side.CLIENT)
  18. public class HumanSkinLoader
  19. {
  20. public final static HumanSkinLoader INSTANCE = new HumanSkinLoader();
  21. private static final ResourceLocation TEXTURE_STEVE = new ResourceLocation("textures/entity/steve.png");
  22. private final HashMap<String, ResourceLocation> skins;
  23. private final HashSet<String> loading;
  24. private final HashMap<String, ArrayList<Consumer<ResourceLocation>>> queue;
  25. private TextureManager manager;
  26. private ImageBufferDownload converter;
  27. public HumanSkinLoader()
  28. {
  29. this.skins = new HashMap<>();
  30. this.loading = new HashSet<>();
  31. this.queue = new HashMap<>();
  32. this.manager = Minecraft.getMinecraft().getRenderManager().renderEngine;
  33. this.converter = new ImageBufferDownload();
  34. }
  35. public ResourceLocation getTexture(String name, Consumer<ResourceLocation> delayed)
  36. {
  37. ResourceLocation loc = skins.get(name);
  38. if(loc == null)
  39. {
  40. if(loading.add(name))
  41. {
  42. downloadSkin(name, image ->
  43. {
  44. ResourceLocation rloc = manager.getDynamicTextureLocation("skin_" + name, new DynamicTexture(image));
  45. skins.put(name, rloc);
  46. loading.remove(name);
  47. delayed.accept(rloc);
  48. ArrayList<Consumer<ResourceLocation>> list = queue.get(name);
  49. if(list != null)
  50. {
  51. list.forEach(c -> c.accept(rloc));
  52. list.clear();
  53. queue.remove(name);
  54. }
  55. });
  56. }
  57. else
  58. {
  59. ArrayList<Consumer<ResourceLocation>> list = queue.get(name);
  60. if(list == null)
  61. {
  62. list = new ArrayList<>();
  63. queue.put(name, list);
  64. }
  65. list.add(delayed);
  66. }
  67. return TEXTURE_STEVE;
  68. }
  69. return loc;
  70. }
  71. private void downloadSkin(String name, Consumer<BufferedImage> delayed)
  72. {
  73. new Thread(() ->
  74. {
  75. HttpURLConnection httpurlconnection = null;
  76. try
  77. {
  78. //httpurlconnection = (HttpURLConnection)(new URL("http://skins.minecraft.net/MinecraftSkins/" + name + ".png"))
  79. httpurlconnection = (HttpURLConnection) (new URL("http://skins.hammerle.me/" + name + ".png"))
  80. .openConnection(Minecraft.getMinecraft().getProxy());
  81. httpurlconnection.setDoInput(true);
  82. httpurlconnection.setDoOutput(false);
  83. httpurlconnection.connect();
  84. int code = httpurlconnection.getResponseCode();
  85. if(code != 200)
  86. {
  87. // Failed own server, trying mojangs
  88. httpurlconnection.disconnect();
  89. httpurlconnection = (HttpURLConnection) (new URL("http://skins.minecraft.net/MinecraftSkins/" + name + ".png"))
  90. .openConnection(Minecraft.getMinecraft().getProxy());
  91. httpurlconnection.setDoInput(true);
  92. httpurlconnection.setDoOutput(false);
  93. httpurlconnection.connect();
  94. code = httpurlconnection.getResponseCode();
  95. if(code != 200)
  96. {
  97. System.out.println("Server response code did return " + code + ", skin servers might be down.");
  98. return;
  99. }
  100. }
  101. BufferedImage image = converter.parseUserSkin(ImageIO.read(httpurlconnection.getInputStream()));
  102. Minecraft.getMinecraft().addScheduledTask(() -> delayed.accept(image));
  103. }
  104. catch (Exception ex)
  105. {
  106. System.out.println("Error occurred when downloading skin, however, skin servers seem to be up.");
  107. }
  108. finally
  109. {
  110. if (httpurlconnection != null)
  111. {
  112. httpurlconnection.disconnect();
  113. }
  114. }
  115. }).start();
  116. }
  117. }