Skip to content

Commit

Permalink
fetch actual profiles from Mojang's public api for players in offline…
Browse files Browse the repository at this point in the history
… servers

this way, Mojang's skin servers can be used in offline servers
bump version
  • Loading branch information
zlainsama committed Feb 20, 2016
1 parent 0bbe529 commit d716dc0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

apply plugin: 'forge'

version = "1.7.10-v7a"
version = "1.7.10-v8"
group= "lain.mods.skinport"
archivesBaseName = "SkinPort"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import lain.mods.skinport.PlayerUtils;
import lain.mods.skinport.SkinData;
import lain.mods.skinport.api.ISkin;
Expand Down Expand Up @@ -190,7 +190,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
t = -1;
}

HttpsURLConnection conn = (HttpsURLConnection) remote.openConnection(proxy);
HttpURLConnection conn = (HttpURLConnection) remote.openConnection(proxy);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
if (etag != null && System.currentTimeMillis() < t)
Expand All @@ -206,7 +206,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
FileUtils.copyInputStreamToFile(conn.getInputStream(), file1);
if (etag != null)
FileUtils.writeStringToFile(file2, etag, "UTF-8");
if (t > -1)
if (t > 0)
FileUtils.writeStringToFile(file3, Long.toString(t), "UTF-8");
}
catch (IOException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import lain.mods.skinport.LegacyConversion;
import lain.mods.skinport.PlayerUtils;
import lain.mods.skinport.SkinData;
Expand Down Expand Up @@ -195,7 +195,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
t = -1;
}

HttpsURLConnection conn = (HttpsURLConnection) remote.openConnection(proxy);
HttpURLConnection conn = (HttpURLConnection) remote.openConnection(proxy);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
if (etag != null && System.currentTimeMillis() < t)
Expand All @@ -211,7 +211,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
FileUtils.copyInputStreamToFile(conn.getInputStream(), file1);
if (etag != null)
FileUtils.writeStringToFile(file2, etag, "UTF-8");
if (t > -1)
if (t > 0)
FileUtils.writeStringToFile(file3, Long.toString(t), "UTF-8");
}
catch (IOException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
import lain.mods.skinport.PlayerUtils;
import lain.mods.skinport.SkinData;
import lain.mods.skinport.api.ISkin;
import lain.mods.skinport.api.ISkinProvider;
Expand Down Expand Up @@ -45,12 +46,16 @@ public ISkin getSkin(AbstractClientPlayer player)
{
final SkinData data = new SkinData();
data.profile = player.getGameProfile();
final boolean flag = PlayerUtils.isOfflinePlayer(player);
pool.execute(new Runnable()
{

@Override
public void run()
{
if (flag)
data.profile = MojangService.getProfile(data.profile.getName(), data.profile);

BufferedImage image = null;
UUID uuid = data.profile.getId();

Expand Down Expand Up @@ -196,7 +201,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
FileUtils.copyInputStreamToFile(conn.getInputStream(), file1);
if (etag != null)
FileUtils.writeStringToFile(file2, etag, "UTF-8");
if (t > -1)
if (t > 0)
FileUtils.writeStringToFile(file3, Long.toString(t), "UTF-8");
}
catch (IOException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
import lain.mods.skinport.LegacyConversion;
import lain.mods.skinport.PlayerUtils;
import lain.mods.skinport.SkinData;
import lain.mods.skinport.api.ISkin;
import lain.mods.skinport.api.ISkinProvider;
Expand Down Expand Up @@ -46,12 +47,16 @@ public ISkin getSkin(AbstractClientPlayer player)
{
final SkinData data = new SkinData();
data.profile = player.getGameProfile();
final boolean flag = PlayerUtils.isOfflinePlayer(player);
pool.execute(new Runnable()
{

@Override
public void run()
{
if (flag)
data.profile = MojangService.getProfile(data.profile.getName(), data.profile);

BufferedImage image = null;
UUID uuid = data.profile.getId();

Expand Down Expand Up @@ -201,7 +206,7 @@ private BufferedImage readImageCached(File workDir, String local, URL remote, Pr
FileUtils.copyInputStreamToFile(conn.getInputStream(), file1);
if (etag != null)
FileUtils.writeStringToFile(file2, etag, "UTF-8");
if (t > -1)
if (t > 0)
FileUtils.writeStringToFile(file3, Long.toString(t), "UTF-8");
}
catch (IOException e)
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/lain/mods/skinport/providers/MojangService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package lain.mods.skinport.providers;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import net.minecraft.client.Minecraft;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.mojang.authlib.GameProfile;
import com.mojang.util.UUIDTypeAdapter;

public class MojangService
{

public static GameProfile getProfile(String username)
{
return getProfile(username, DUMMY);
}

public static GameProfile getProfile(String username, GameProfile defaultValue)
{
try
{
return cachedProfiles.get(username).or(defaultValue);
}
catch (ExecutionException ignored)
{
return defaultValue;
}
}

private static final LoadingCache<String, Optional<GameProfile>> cachedProfiles = CacheBuilder.newBuilder().expireAfterWrite(6, TimeUnit.HOURS).build(new CacheLoader<String, Optional<GameProfile>>()
{

Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();

@Override
public Optional<GameProfile> load(String key) throws Exception
{
URL url = new URL(new StringBuilder().append("https://api.mojang.com/users/profiles/minecraft/").append(key).toString());

HttpURLConnection conn = (HttpURLConnection) url.openConnection(Minecraft.getMinecraft().getProxy());
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setUseCaches(false);
InputStream in = null;
try
{
in = conn.getInputStream();
}
catch (IOException e)
{
IOUtils.closeQuietly(in);
in = conn.getErrorStream();
}

try
{
if (in != null)
return Optional.of(Minecraft.getMinecraft().func_152347_ac().fillProfileProperties(gson.fromJson(IOUtils.toString(in, Charsets.UTF_8), GameProfile.class), false));
}
catch (JsonSyntaxException ignored)
{
}
catch (IOException ignored)
{
}
finally
{
IOUtils.closeQuietly(in);
}
return Optional.absent();
}

});

private static final GameProfile DUMMY = new GameProfile(UUID.fromString("fed3a6ca-d7de-11e5-b5d2-0a1d41d68578"), "[Dummy]");

}

0 comments on commit d716dc0

Please sign in to comment.