From 1a10b6a774fc8ce7279db887056668a0d8dab47e Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Tue, 15 Apr 2014 02:54:04 +1200 Subject: [PATCH] Use nms to get the uuid --- .../disguise/utilities/DisguiseUtilities.java | 8 +- .../disguise/utilities/ReflectionManager.java | 22 ++++- .../disguise/utilities/UUIDFetcher.java | 83 ------------------- 3 files changed, 24 insertions(+), 89 deletions(-) delete mode 100644 src/me/libraryaddict/disguise/utilities/UUIDFetcher.java diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 241587ec..7de55268 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -4,7 +4,6 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -285,10 +284,9 @@ public class DisguiseUtilities { Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() { public void run() { try { - UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(playerName)); - HashMap map = fetcher.call(); - if (map.containsKey(playerName)) { - Object gameprofile = ReflectionManager.getGameProfile(map.get(playerName), playerName); + Object gameprofile = ReflectionManager.grabUUID(ReflectionManager.getGameProfile(null, playerName, + false)); + if (gameprofile != null) { final Object gameProfile = ReflectionManager.grabSkullBlob(gameprofile); Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() { public void run() { diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index ca2f6f55..b0de4ecd 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -218,11 +218,15 @@ public class ReflectionManager { } public static Object getGameProfile(UUID uuid, String playerName) { + return getGameProfile(uuid, playerName, true); + } + + public static Object getGameProfile(UUID uuid, String playerName, boolean createUuid) { try { try { return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") .getConstructor(UUID.class, String.class) - .newInstance(uuid != null ? uuid : UUID.randomUUID(), playerName); + .newInstance(uuid != null || !createUuid ? uuid : UUID.randomUUID(), playerName); } catch (NoSuchMethodException ex) { return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") .getConstructor(String.class, String.class).newInstance(uuid != null ? uuid.toString() : "", playerName); @@ -315,6 +319,22 @@ public class ReflectionManager { return null; } + public static Object grabUUID(Object gameProfile) { + try { + Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); + for (Method method : getNmsClass("MinecraftServer").getMethods()) { + if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { + Object session = method.invoke(minecraftServer); + return session.getClass().getMethod("hasJoinedServer", gameProfile.getClass(), String.class) + .invoke(session, gameProfile, null); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + public static void setAllowSleep(Player player) { try { Object nmsEntity = getNmsEntity(player); diff --git a/src/me/libraryaddict/disguise/utilities/UUIDFetcher.java b/src/me/libraryaddict/disguise/utilities/UUIDFetcher.java deleted file mode 100644 index 275b1814..00000000 --- a/src/me/libraryaddict/disguise/utilities/UUIDFetcher.java +++ /dev/null @@ -1,83 +0,0 @@ -package me.libraryaddict.disguise.utilities; - -import com.google.common.collect.ImmutableList; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; -import org.json.simple.parser.JSONParser; - -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.Callable; - -public class UUIDFetcher implements Callable> { - private static final String AGENT = "minecraft"; - private static final int MAX_SEARCH = 100; - private static final String PROFILE_URL = "https://api.mojang.com/profiles/page/"; - @SuppressWarnings("unchecked") - private static String buildBody(List names) { - List lookups = new ArrayList(); - for (String name : names) { - JSONObject obj = new JSONObject(); - obj.put("name", name); - obj.put("agent", AGENT); - lookups.add(obj); - } - return JSONValue.toJSONString(lookups); - } - private static HttpURLConnection createConnection(int page) throws Exception { - URL url = new URL(PROFILE_URL + page); - 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 void writeBody(HttpURLConnection connection, String body) throws Exception { - DataOutputStream writer = new DataOutputStream(connection.getOutputStream()); - writer.write(body.getBytes()); - writer.flush(); - writer.close(); - } - - private final JSONParser jsonParser = new JSONParser(); - - private final List names; - - public UUIDFetcher(List names) { - this.names = ImmutableList.copyOf(names); - } - - public HashMap call() throws Exception { - HashMap uuidMap = new HashMap(); - String body = buildBody(names); - for (int i = 1; i < MAX_SEARCH; i++) { - HttpURLConnection connection = createConnection(i); - writeBody(connection, body); - JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); - JSONArray array = (JSONArray) jsonObject.get("profiles"); - Number count = (Number) jsonObject.get("size"); - if (count.intValue() == 0) { - break; - } - for (Object profile : array) { - JSONObject jsonProfile = (JSONObject) profile; - String id = (String) jsonProfile.get("id"); - String name = (String) jsonProfile.get("name"); - UUID uuid = UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" - + id.substring(16, 20) + "-" + id.substring(20, 32)); - uuidMap.put(name, uuid); - } - } - return uuidMap; - } -}