Read desc

Added methods for other plugins to request GameProfiles
Changed GameProfiles to lookup UUID's with a different API as the old
one barfed
This commit is contained in:
libraryaddict 2014-04-19 08:14:02 +12:00
parent b0f1a756cc
commit 74d5dbce9e
3 changed files with 144 additions and 66 deletions

View File

@ -126,17 +126,6 @@ public class DisguiseUtilities {
// + " on a entity. Removed the old disguise."); // + " on a entity. Removed the old disguise.");
disguiseItel.remove(); disguiseItel.remove();
/* if (name != null) {
if (!disguise.getObservers().contains(name)) {
d.setViewDisguise(name);
}
} else {
for (String playername : d.getObservers()) {
if (!disguise.getObservers().contains(playername)) {
d.setViewDisguise(playername);
}
}
}*/
} }
} }
} }
@ -230,6 +219,10 @@ public class DisguiseUtilities {
return new TargetedDisguise[0]; return new TargetedDisguise[0];
} }
public static Object getGameProfile(String playerName) {
return gameProfiles.get(playerName);
}
public static TargetedDisguise getMainDisguise(UUID entityId) { public static TargetedDisguise getMainDisguise(UUID entityId) {
TargetedDisguise toReturn = null; TargetedDisguise toReturn = null;
if (getDisguises().containsKey(entityId)) { if (getDisguises().containsKey(entityId)) {
@ -271,55 +264,52 @@ public class DisguiseUtilities {
} }
public static Object getProfile(final String playerName) { public static Object getProfile(final String playerName) {
Player player = Bukkit.getPlayerExact(playerName); if (gameProfiles.containsKey(playerName)) {
if (player != null) { if (gameProfiles.get(playerName) != null) {
return ReflectionManager.getGameProfile(player); return gameProfiles.get(playerName);
}
} else { } else {
if (gameProfiles.containsKey(playerName)) { Player player = Bukkit.getPlayerExact(playerName);
if (gameProfiles.get(playerName) != null) { if (player != null) {
return gameProfiles.get(playerName); Object gameProfile = ReflectionManager.getGameProfile(player);
if (ReflectionManager.hasSkinBlob(gameProfile)) {
gameProfiles.put(playerName, gameProfile);
return gameProfile;
} }
} else { }
// Add null so that if this is called again. I already know I'm doing something about it // Add null so that if this is called again. I already know I'm doing something about it
gameProfiles.put(playerName, null); gameProfiles.put(playerName, null);
Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() { Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() {
public void run() { public void run() {
try { try {
Object gameprofile = ReflectionManager.grabUUID(ReflectionManager.getGameProfile(null, playerName, final Object gameProfile = lookupGameProfile(playerName);
false)); Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
if (gameprofile != null) { public void run() {
final Object gameProfile = ReflectionManager.grabSkullBlob(gameprofile); if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() { gameProfiles.put(playerName, gameProfile);
public void run() { for (HashSet<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) {
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) { for (TargetedDisguise disguise : disguises) {
gameProfiles.put(playerName, gameProfile); if (disguise.getType() == DisguiseType.PLAYER
} && ((PlayerDisguise) disguise).getName().equals(playerName)) {
for (HashSet<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) { DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise);
for (TargetedDisguise disguise : disguises) { if (disguise.getEntity() instanceof Player && disguise.isSelfDisguiseVisible()) {
if (disguise.getType() == DisguiseType.PLAYER DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
&& ((PlayerDisguise) disguise).getName().equals(playerName)) {
DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise);
if (disguise.getEntity() instanceof Player
&& disguise.isSelfDisguiseVisible()) {
DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(),
disguise);
}
} }
} }
} }
} }
}); }
} }
} catch (Exception e) { });
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) { } catch (Exception e) {
gameProfiles.remove(playerName); if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
} gameProfiles.remove(playerName);
System.out.print("[LibsDisguises] Error when fetching " + playerName + "'s uuid from mojang: "
+ e.getMessage());
} }
System.out.print("[LibsDisguises] Error when fetching " + playerName + "'s uuid from mojang: "
+ e.getMessage());
} }
}); }
} });
} }
return ReflectionManager.getGameProfile(null, playerName); return ReflectionManager.getGameProfile(null, playerName);
} }
@ -351,6 +341,10 @@ public class DisguiseUtilities {
return selfDisguisesIds; return selfDisguisesIds;
} }
public static boolean hasGameProfile(String playerName) {
return getGameProfile(playerName) != null;
}
public static void init(LibsDisguises disguises) { public static void init(LibsDisguises disguises) {
libsDisguises = disguises; libsDisguises = disguises;
} }
@ -363,6 +357,39 @@ public class DisguiseUtilities {
return false; return false;
} }
/**
* This is called on a thread as it is thread blocking
*/
public static Object lookupGameProfile(String playerName) {
Object gameprofile = ReflectionManager.grabProfileAddUUID(playerName);
return ReflectionManager.grabSkullBlob(gameprofile);
}
/**
* This is safe to call from the main thread
*/
public static void lookupGameProfileAndStore(final String playerName) {
if (!gameProfiles.containsKey(playerName)) {
gameProfiles.put(playerName, null);
Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
try {
final Object gameProfile = lookupGameProfile(playerName);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
gameProfiles.put(playerName, gameProfile);
}
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}
/** /**
* @param Resends * @param Resends
* the entity to all the watching players, which is where the magic begins * the entity to all the watching players, which is where the magic begins

View File

@ -0,0 +1,33 @@
package me.libraryaddict.disguise.utilities;
import java.lang.reflect.Field;
import java.util.UUID;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.ProfileLookupCallback;
public class LibsProfileLookupCaller implements ProfileLookupCallback {
private GameProfile gameProfile;
public GameProfile getGameProfile() {
return gameProfile;
}
@Override
public void onProfileLookupFailed(GameProfile gameProfile, Exception arg1) {
try {
Field field = GameProfile.class.getDeclaredField("id");
field.setAccessible(true);
field.set(gameProfile, UUID.randomUUID());
} catch (Exception e) {
e.printStackTrace();
}
this.onProfileLookupSucceeded(gameProfile);
}
@Override
public void onProfileLookupSucceeded(GameProfile profile) {
gameProfile = profile;
}
}

View File

@ -218,15 +218,11 @@ public class ReflectionManager {
} }
public static Object getGameProfile(UUID uuid, String playerName) { 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 {
try { try {
return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile")
.getConstructor(UUID.class, String.class) .getConstructor(UUID.class, String.class)
.newInstance(uuid != null || !createUuid ? uuid : UUID.randomUUID(), playerName); .newInstance(uuid != null ? uuid : UUID.randomUUID(), playerName);
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile")
.getConstructor(String.class, String.class).newInstance(uuid != null ? uuid.toString() : "", playerName); .getConstructor(String.class, String.class).newInstance(uuid != null ? uuid.toString() : "", playerName);
@ -303,6 +299,28 @@ public class ReflectionManager {
return null; return null;
} }
public static Object grabProfileAddUUID(String playername) {
try {
Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null);
for (Method method : getNmsClass("MinecraftServer").getMethods()) {
if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) {
Object profileRepo = method.invoke(minecraftServer);
Object agent = Class.forName("net.minecraft.util.com.mojang.authlib.Agent").getField("MINECRAFT").get(null);
LibsProfileLookupCaller callback = new LibsProfileLookupCaller();
profileRepo
.getClass()
.getMethod("findProfilesByNames", String[].class, agent.getClass(),
Class.forName("net.minecraft.util.com.mojang.authlib.ProfileLookupCallback"))
.invoke(profileRepo, new String[] { playername }, agent, callback);
return callback.getGameProfile();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Object grabSkullBlob(Object gameProfile) { public static Object grabSkullBlob(Object gameProfile) {
try { try {
Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null);
@ -319,20 +337,20 @@ public class ReflectionManager {
return null; return null;
} }
public static Object grabUUID(Object gameProfile) { public static boolean hasSkinBlob(Object gameprofile) {
try { try {
Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); Field propField = gameprofile.getClass().getDeclaredField("properties");
for (Method method : getNmsClass("MinecraftServer").getMethods()) { propField.setAccessible(true);
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { Object propMap = propField.get(gameprofile);
Object session = method.invoke(minecraftServer); propField = propMap.getClass().getDeclaredField("properties");
return session.getClass().getMethod("hasJoinedServer", gameProfile.getClass(), String.class) propField.setAccessible(true);
.invoke(session, gameProfile, null); propMap = propField.get(propMap);
} return !(Boolean) propMap.getClass().getMethod("isEmpty").invoke(propMap);
} } catch (NoSuchFieldException ex) {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
return null; return true;
} }
public static void setAllowSleep(Player player) { public static void setAllowSleep(Player player) {