Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.negafinity.skinrestorer;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.util.Map;
- import java.util.UUID;
- import org.apache.commons.codec.binary.Base64;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.google.common.collect.Maps;
- import com.mojang.authlib.minecraft.MinecraftProfileTexture;
- import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
- import cpw.mods.fml.common.eventhandler.SubscribeEvent;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.entity.EntityPlayerSP;
- import net.minecraft.util.ResourceLocation;
- import net.minecraftforge.event.entity.EntityJoinWorldEvent;
- public class PlayerJoinWorldListener
- {
- private Map<UUID, ResourceLocation> resources = Maps.newHashMap();
- @SubscribeEvent
- public void onJoinWorld(EntityJoinWorldEvent event)
- {
- if (event.entity instanceof EntityPlayerSP)
- {
- EntityPlayerSP player = (EntityPlayerSP) event.entity;
- if (!resources.containsKey(player.getUniqueID()))
- {
- resources.put(player.getUniqueID(), Minecraft.getMinecraft().getSkinManager().loadSkin(new MinecraftProfileTexture(this.getPlayerTexture(player)), Type.SKIN));
- }
- player.onSkinAvailable(Type.SKIN, resources.get(player.getUniqueID()));
- }
- }
- private String getPlayerTexture(EntityPlayerSP player)
- {
- try
- {
- byte[] decoded = Base64.decodeBase64(this.getPlayerTextureBase64(player.getUniqueID()));
- String json = new String(decoded, "UTF-8");
- String url = new JSONObject(json).getJSONObject("textures").getJSONObject("SKIN").getString("url");
- return url;
- }
- catch (JSONException | IOException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- private String getPlayerTextureBase64(UUID uuid) throws IOException, JSONException
- {
- String url = ("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString()).replaceAll("-", "");
- InputStream is = new URL(url).openStream();
- try
- {
- BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
- String jsonText = this.readAll(rd);
- JSONArray jsonArray = new JSONObject(jsonText).getJSONArray("properties");
- return jsonArray.getJSONObject(0).getString("value");
- }
- finally
- {
- is.close();
- }
- }
- private String readAll(Reader rd) throws IOException
- {
- StringBuilder sb = new StringBuilder();
- int cp;
- while ((cp = rd.read()) != -1)
- {
- sb.append((char) cp);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment