-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch actual profiles from Mojang's public api for players in offline…
… servers this way, Mojang's skin servers can be used in offline servers bump version
- Loading branch information
Showing
6 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/lain/mods/skinport/providers/MojangService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); | ||
|
||
} |