Created
July 31, 2014 11:57
-
-
Save mooman219/bd825ea6731d8ca69ce5 to your computer and use it in GitHub Desktop.
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
public static void setSkin(Player target, final String sourceName) { | |
if(TextHelper.isEmpty(sourceName)) { | |
LogHelper.warning("[SkinHelper] setSkin(): Invalid sourceName."); | |
return; | |
} else if(target == null) { | |
LogHelper.warning("[SkinHelper] setSkin(): Invalid target."); | |
return; | |
} | |
final UUID targetUUID = target.getUniqueId(); | |
TaskManager.get().runPlugin(() -> { | |
try { | |
System.out.println("GET sourceUUID"); | |
final UUID sourceUUID = UUIDFetcher.getUUIDOf(sourceName); | |
System.out.println("GOT sourceUUID"); | |
if(sourceUUID == null) { | |
LogHelper.warning("[SkinHelper] setSkin(): Unable to fetch UUID for '" + sourceName + "'."); | |
return; | |
} | |
final Property skin = SkinFetcher.getSkinOf(sourceUUID); | |
queueSkin(targetUUID, skin); | |
} catch(Exception ex) { | |
LogHelper.warning("[SkinHelper] setSkin(): Error fetching skin for '" + sourceName + "'."); | |
ex.printStackTrace(); | |
} | |
}); | |
} |
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
package com.gmail.mooman219.api.skin; | |
import com.google.common.cache.CacheBuilder; | |
import com.google.common.cache.CacheLoader; | |
import com.google.common.cache.LoadingCache; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.Lists; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.nio.ByteBuffer; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.UUID; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.TimeUnit; | |
public class UUIDFetcher implements Callable<Map<String, UUID>> { | |
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; | |
private static final int PROFILES_PER_REQUEST = 100; | |
private static final boolean RATE_LIMITING = true; | |
private static final LoadingCache<String, UUID> uuidCache = buildCache(); | |
private final List<String> names; | |
public UUIDFetcher(List<String> names) { | |
this.names = ImmutableList.copyOf(names); | |
} | |
@Override | |
public Map<String, UUID> call() throws Exception { | |
System.out.println("call"); | |
Map<String, UUID> uuidMap = new HashMap<>(); | |
if (names.size() == 1 && names.get(0) != null) { | |
uuidMap.put(names.get(0), uuidCache.get(names.get(0))); | |
} else { | |
uuidMap.putAll(uuidCache.getAll(names)); | |
} | |
return uuidMap; | |
} | |
private static LoadingCache<String, UUID> buildCache() { | |
return CacheBuilder.newBuilder() | |
.maximumSize(1000) | |
.expireAfterAccess(30, TimeUnit.MINUTES) | |
.build(new CacheLoader<String, UUID>() { | |
@Override | |
public UUID load(String name) throws Exception { | |
System.out.println("Singular UUID lookup for " + name); | |
HttpURLConnection connection = createConnection(); | |
writeBody(connection, new StringBuilder(name.length()).append("[\"").append(name).append("\"]").toString()); | |
JSONArray array = (JSONArray) new JSONParser().parse(new InputStreamReader(connection.getInputStream())); | |
String id = (String) ((JSONObject) array.get(0)).get("id"); | |
return UUIDFetcher.getUUID(id); | |
} | |
@Override | |
public Map<String, UUID> loadAll(Iterable<? extends String> namesRaw) throws Exception { | |
Map<String, UUID> uuidMap = new HashMap<>(); | |
List<String> names = Lists.newArrayList(namesRaw); | |
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); | |
for (int i = 0; i < requests; i++) { | |
HttpURLConnection connection = createConnection(); | |
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); | |
writeBody(connection, body); | |
JSONArray array = (JSONArray) new JSONParser().parse(new InputStreamReader(connection.getInputStream())); | |
for (Object profile : array) { | |
JSONObject jsonProfile = (JSONObject) profile; | |
String id = (String) jsonProfile.get("id"); | |
String name = (String) jsonProfile.get("name"); | |
UUID uuid = UUIDFetcher.getUUID(id); | |
uuidMap.put(name, uuid); | |
} | |
if (RATE_LIMITING && i != requests - 1) { | |
Thread.sleep(100L); | |
} | |
} | |
return uuidMap; | |
} | |
}); | |
} | |
private static void writeBody(HttpURLConnection connection, String body) throws Exception { | |
OutputStream stream = connection.getOutputStream(); | |
stream.write(body.getBytes()); | |
stream.flush(); | |
stream.close(); | |
} | |
private static HttpURLConnection createConnection() throws Exception { | |
URL url = new URL(PROFILE_URL); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setUseCaches(false); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
return connection; | |
} | |
private static UUID getUUID(String id) { | |
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32)); | |
} | |
public static byte[] toBytes(UUID uuid) { | |
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); | |
byteBuffer.putLong(uuid.getMostSignificantBits()); | |
byteBuffer.putLong(uuid.getLeastSignificantBits()); | |
return byteBuffer.array(); | |
} | |
public static UUID fromBytes(byte[] array) { | |
if (array.length != 16) { | |
throw new IllegalArgumentException("Illegal byte array length: " + array.length); | |
} | |
ByteBuffer byteBuffer = ByteBuffer.wrap(array); | |
long mostSignificant = byteBuffer.getLong(); | |
long leastSignificant = byteBuffer.getLong(); | |
return new UUID(mostSignificant, leastSignificant); | |
} | |
public static UUID getUUIDOf(String name) throws Exception { | |
System.out.println("getUUIDOf"); | |
return new UUIDFetcher(Arrays.asList(name)).call().get(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment