Guest User

Untitled

a guest
Jul 15th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package com.negafinity.skinrestorer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.Reader;
  8. import java.net.URL;
  9. import java.nio.charset.Charset;
  10. import java.util.Map;
  11. import java.util.UUID;
  12.  
  13. import org.apache.commons.codec.binary.Base64;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import com.google.common.collect.Maps;
  19. import com.mojang.authlib.minecraft.MinecraftProfileTexture;
  20. import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
  21.  
  22. import cpw.mods.fml.common.eventhandler.SubscribeEvent;
  23. import net.minecraft.client.Minecraft;
  24. import net.minecraft.client.entity.EntityPlayerSP;
  25. import net.minecraft.util.ResourceLocation;
  26. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  27.  
  28. public class PlayerJoinWorldListener
  29. {
  30.     private Map<UUID, ResourceLocation> resources = Maps.newHashMap();
  31.  
  32.     @SubscribeEvent
  33.     public void onJoinWorld(EntityJoinWorldEvent event)
  34.     {
  35.         if (event.entity instanceof EntityPlayerSP)
  36.         {
  37.             EntityPlayerSP player = (EntityPlayerSP) event.entity;
  38.  
  39.             if (!resources.containsKey(player.getUniqueID()))
  40.             {
  41.                 resources.put(player.getUniqueID(), Minecraft.getMinecraft().getSkinManager().loadSkin(new MinecraftProfileTexture(this.getPlayerTexture(player)), Type.SKIN));
  42.             }
  43.  
  44.             player.onSkinAvailable(Type.SKIN, resources.get(player.getUniqueID()));
  45.         }
  46.     }
  47.  
  48.     private String getPlayerTexture(EntityPlayerSP player)
  49.     {
  50.         try
  51.         {
  52.             byte[] decoded = Base64.decodeBase64(this.getPlayerTextureBase64(player.getUniqueID()));
  53.             String json = new String(decoded, "UTF-8");
  54.             String url = new JSONObject(json).getJSONObject("textures").getJSONObject("SKIN").getString("url");
  55.             return url;
  56.         }
  57.         catch (JSONException | IOException e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.  
  62.         return null;
  63.     }
  64.  
  65.     private String getPlayerTextureBase64(UUID uuid) throws IOException, JSONException
  66.     {
  67.         String url = ("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString()).replaceAll("-", "");
  68.         InputStream is = new URL(url).openStream();
  69.  
  70.         try
  71.         {
  72.             BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  73.             String jsonText = this.readAll(rd);
  74.             JSONArray jsonArray = new JSONObject(jsonText).getJSONArray("properties");
  75.             return jsonArray.getJSONObject(0).getString("value");
  76.         }
  77.         finally
  78.         {
  79.             is.close();
  80.         }
  81.     }
  82.  
  83.     private String readAll(Reader rd) throws IOException
  84.     {
  85.         StringBuilder sb = new StringBuilder();
  86.         int cp;
  87.         while ((cp = rd.read()) != -1)
  88.         {
  89.             sb.append((char) cp);
  90.         }
  91.         return sb.toString();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment