PlayerCapeLoader.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package me.km.overrides;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.UUID;
  6. import java.util.function.Consumer;
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.client.renderer.texture.DynamicTexture;
  9. import net.minecraft.client.renderer.texture.NativeImage;
  10. import net.minecraft.client.renderer.texture.TextureManager;
  11. import net.minecraft.util.ResourceLocation;
  12. import net.minecraftforge.api.distmarker.Dist;
  13. import net.minecraftforge.api.distmarker.OnlyIn;
  14. import org.apache.logging.log4j.LogManager;
  15. @OnlyIn(Dist.CLIENT)
  16. public class PlayerCapeLoader
  17. {
  18. private static class Data
  19. {
  20. private ResourceLocation cape;
  21. }
  22. public final static PlayerCapeLoader INSTANCE = new PlayerCapeLoader();
  23. private final HashMap<UUID, Data> skins = new HashMap<>();
  24. private final TextureManager manager = Minecraft.getInstance().getRenderManager().textureManager;
  25. private PlayerCapeLoader()
  26. {
  27. }
  28. public ResourceLocation getTexture(UUID uuid)
  29. {
  30. Data oldData = skins.get(uuid);
  31. if(oldData != null)
  32. {
  33. return oldData.cape;
  34. }
  35. Data newData = new Data();
  36. skins.put(uuid, newData);
  37. downloadCape(uuid, image ->
  38. {
  39. newData.cape = manager.getDynamicTextureLocation("cape_" + uuid, new DynamicTexture(image));
  40. });
  41. return null;
  42. }
  43. private void downloadCape(UUID uuid, Consumer<NativeImage> delayed)
  44. {
  45. new Thread(() ->
  46. {
  47. HttpURLConnection httpurlconnection = null;
  48. try
  49. {
  50. org.apache.logging.log4j.LogManager.getLogger().warn(String.format("http://skins.hammerle.me/cape_%s.png", uuid));
  51. httpurlconnection = (HttpURLConnection) (new URL("http://skins.hammerle.me/cape.png"))
  52. .openConnection(Minecraft.getInstance().getProxy());
  53. httpurlconnection.setDoInput(true);
  54. httpurlconnection.setDoOutput(false);
  55. httpurlconnection.connect();
  56. int code = httpurlconnection.getResponseCode();
  57. if(code != 200)
  58. {
  59. return;
  60. }
  61. NativeImage image = NativeImage.read(httpurlconnection.getInputStream());
  62. Minecraft.getInstance().enqueue(() -> delayed.accept(image));
  63. }
  64. catch(Exception ex)
  65. {
  66. LogManager.getLogger().warn("Error occurred when downloading skin, however, skin servers seem to be up.");
  67. }
  68. finally
  69. {
  70. if(httpurlconnection != null)
  71. {
  72. httpurlconnection.disconnect();
  73. }
  74. }
  75. }).start();
  76. }
  77. }