Remove old code for player disguises, support 1.7.9 code for disguises!

This commit is contained in:
libraryaddict 2014-04-15 02:26:31 +12:00
parent 5801356fbf
commit 6ce864ae6c
5 changed files with 157 additions and 28 deletions

View File

@ -83,12 +83,6 @@
<artifactId>ProtocolLib</artifactId> <artifactId>ProtocolLib</artifactId>
<version>3.1.0</version> <version>3.1.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.3</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<version>8.0.8-SNAPSHOT</version> <version>8.0.8-SNAPSHOT</version>

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -18,7 +19,6 @@ import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -36,12 +36,10 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
public class DisguiseUtilities { public class DisguiseUtilities {
private static HashMap<Integer, HashSet<TargetedDisguise>> futureDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>(); private static HashMap<Integer, HashSet<TargetedDisguise>> futureDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>();
private static HashMap<String, Object> gameProfiles = new HashMap<String, Object>();
private static LibsDisguises libsDisguises; private static LibsDisguises libsDisguises;
// A internal storage of fake entity ID's I can use. // A internal storage of fake entity ID's I can use.
// Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID // Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID
@ -272,6 +270,60 @@ public class DisguiseUtilities {
return players; return players;
} }
public static Object getProfile(final Disguise disguise, final String playerName) {
Player player = null;
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.getName().equalsIgnoreCase(playerName)) {
player = p;
break;
}
}
if (player != null) {
return ReflectionManager.getGameProfile(player);
} else if (disguise != null) {
if (gameProfiles.containsKey(playerName)) {
if (gameProfiles.get(playerName) != null) {
return gameProfiles.get(playerName);
}
} else {
// Add null so that if this is called again. I already know I'm doing something about it
gameProfiles.put(playerName, null);
Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
try {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(playerName));
HashMap<String, UUID> map = fetcher.call();
if (map.containsKey(playerName)) {
Object gameprofile = ReflectionManager.getGameProfile(map.get(playerName), playerName);
final Object gameProfile = ReflectionManager.grabSkullBlob(gameprofile);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
gameProfiles.put(playerName, gameProfile);
}
if (DisguiseUtilities.isDisguiseInUse(disguise)) {
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) {
gameProfiles.remove(playerName);
}
System.out.print("[LibsDisguises] Error when fetching " + playerName + "'s uuid from mojang: "
+ e.getMessage());
}
}
});
}
}
return ReflectionManager.getGameProfile(null, playerName);
}
public static List<TargetedDisguise> getSeenDisguises(String viewer) { public static List<TargetedDisguise> getSeenDisguises(String viewer) {
List<TargetedDisguise> dis = new ArrayList<TargetedDisguise>(); List<TargetedDisguise> dis = new ArrayList<TargetedDisguise>();
for (HashSet<TargetedDisguise> disguises : getDisguises().values()) { for (HashSet<TargetedDisguise> disguises : getDisguises().values()) {
@ -299,15 +351,6 @@ public class DisguiseUtilities {
return selfDisguisesIds; return selfDisguisesIds;
} }
public static UUID getUUID() {
if (LibVersion.is1_7()) {
EthernetAddress addr = EthernetAddress.fromInterface();
TimeBasedGenerator uuidGenerator = Generators.timeBasedGenerator(addr);
return uuidGenerator.generate();
}
return null;
}
public static void init(LibsDisguises disguises) { public static void init(LibsDisguises disguises) {
libsDisguises = disguises; libsDisguises = disguises;
} }

View File

@ -201,14 +201,7 @@ public class PacketsManager {
} }
} else { } else {
Object gameProfile = null; Object gameProfile = null;
if (disguisedEntity instanceof Player gameProfile = DisguiseUtilities.getProfile(disguise, ((PlayerDisguise) disguise).getName());
&& ((Player) disguisedEntity).getName().equals(((PlayerDisguise) disguise).getName())
&& disguisedEntity != observer) {
gameProfile = ReflectionManager.getGameProfile((Player) disguisedEntity);
} else {
gameProfile = ReflectionManager.getGameProfile(DisguiseUtilities.getUUID(),
((PlayerDisguise) disguise).getName());
}
spawnPackets[0].getModifier().write(1, gameProfile); spawnPackets[0].getModifier().write(1, gameProfile);
} }
StructureModifier<Integer> intMods = spawnPackets[0].getIntegers(); StructureModifier<Integer> intMods = spawnPackets[0].getIntegers();

View File

@ -222,7 +222,7 @@ public class ReflectionManager {
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 ? uuid : DisguiseUtilities.getUUID(), 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);
@ -299,6 +299,22 @@ public class ReflectionManager {
return null; return null;
} }
public static Object grabSkullBlob(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("fillProfileProperties", gameProfile.getClass(), boolean.class)
.invoke(session, gameProfile, true);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void setAllowSleep(Player player) { public static void setAllowSleep(Player player) {
try { try {
Object nmsEntity = getNmsEntity(player); Object nmsEntity = getNmsEntity(player);

View File

@ -0,0 +1,83 @@
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<HashMap<String, UUID>> {
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<String> names) {
List<JSONObject> lookups = new ArrayList<JSONObject>();
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<String> names;
public UUIDFetcher(List<String> names) {
this.names = ImmutableList.copyOf(names);
}
public HashMap<String, UUID> call() throws Exception {
HashMap<String, UUID> uuidMap = new HashMap<String, UUID>();
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;
}
}