2013-11-22 21:10:20 +01:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
2013-11-18 04:24:25 +01:00
|
|
|
|
2014-06-15 04:30:47 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2013-11-18 12:49:04 +01:00
|
|
|
import java.lang.reflect.Field;
|
2013-11-18 04:24:25 +01:00
|
|
|
import java.lang.reflect.Method;
|
2013-11-18 20:25:48 +01:00
|
|
|
import java.lang.reflect.Modifier;
|
2014-06-14 23:58:49 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2014-01-15 11:14:21 +01:00
|
|
|
import java.util.UUID;
|
2014-06-14 23:58:49 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2013-11-18 04:24:25 +01:00
|
|
|
|
2014-06-15 04:30:47 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
2014-06-15 07:38:16 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
2013-11-18 12:49:04 +01:00
|
|
|
import org.bukkit.Art;
|
2013-11-18 04:24:25 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2013-12-31 06:33:42 +01:00
|
|
|
import org.bukkit.Location;
|
2013-11-18 12:49:04 +01:00
|
|
|
import org.bukkit.Sound;
|
2013-11-18 20:25:48 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Entity;
|
2014-01-05 01:13:49 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2013-11-18 04:24:25 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
|
|
|
|
2013-11-18 04:24:25 +01:00
|
|
|
public class ReflectionManager {
|
2014-06-15 01:06:02 +02:00
|
|
|
|
2014-01-18 20:21:55 +01:00
|
|
|
public enum LibVersion {
|
2014-04-12 07:19:46 +02:00
|
|
|
V1_6, V1_7;
|
2014-05-13 12:26:34 +02:00
|
|
|
private static LibVersion currentVersion = LibVersion.V1_7;
|
2014-01-18 20:21:55 +01:00
|
|
|
static {
|
|
|
|
if (getBukkitVersion().startsWith("v1_")) {
|
|
|
|
try {
|
2014-05-12 03:46:29 +02:00
|
|
|
int version = Integer.parseInt(getBukkitVersion().split("_")[1]);
|
2014-01-18 20:21:55 +01:00
|
|
|
if (version == 7) {
|
2014-04-12 07:19:46 +02:00
|
|
|
currentVersion = LibVersion.V1_7;
|
2014-01-18 20:21:55 +01:00
|
|
|
} else {
|
|
|
|
if (version < 7) {
|
|
|
|
currentVersion = LibVersion.V1_6;
|
|
|
|
} else {
|
|
|
|
currentVersion = LibVersion.V1_7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2014-04-12 07:07:27 +02:00
|
|
|
ex.printStackTrace();
|
2014-01-18 20:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LibVersion getGameVersion() {
|
|
|
|
return currentVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean is1_6() {
|
|
|
|
return getGameVersion() == V1_6;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean is1_7() {
|
2014-04-12 07:19:46 +02:00
|
|
|
return getGameVersion() == V1_7;
|
2014-01-18 20:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 01:06:02 +02:00
|
|
|
private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
|
|
|
|
private static final boolean isForge = Bukkit.getServer().getName().equalsIgnoreCase("Cauldron");
|
|
|
|
|
2014-06-14 23:58:49 +02:00
|
|
|
/**
|
|
|
|
* Map of mc-dev simple class name to fully qualified Forge class name.
|
|
|
|
*/
|
|
|
|
private static Map<String, String> ForgeClassMappings;
|
|
|
|
/**
|
|
|
|
* Map of Forge fully qualified class names to a map from mc-dev field names to Forge field names.
|
|
|
|
*/
|
|
|
|
private static Map<String, Map<String, String>> ForgeFieldMappings;
|
|
|
|
/**
|
2014-06-15 07:38:16 +02:00
|
|
|
* Map of Forge fully qualified class names to a map from mc-dev method names to a map from method signatures to Forge method names.
|
2014-06-14 23:58:49 +02:00
|
|
|
*/
|
2014-06-15 07:38:16 +02:00
|
|
|
private static Map<String, Map<String, Map<String, String>>> ForgeMethodMappings;
|
|
|
|
private static Map<Class<?>, String> primitiveTypes;
|
2014-01-20 17:29:14 +01:00
|
|
|
|
2013-11-18 04:24:25 +01:00
|
|
|
static {
|
2014-06-15 04:30:47 +02:00
|
|
|
final String nameseg_class = "a-zA-Z0-9$_";
|
2014-06-15 07:38:16 +02:00
|
|
|
final String fqn_class = nameseg_class + "/";
|
|
|
|
|
|
|
|
primitiveTypes = ImmutableMap.<Class<?>, String>builder()
|
|
|
|
.put(boolean.class,"Z")
|
|
|
|
.put(byte.class, "B")
|
|
|
|
.put(char.class, "C")
|
|
|
|
.put(short.class, "S")
|
|
|
|
.put(int.class, "I")
|
|
|
|
.put(long.class, "J")
|
|
|
|
.put(float.class, "F")
|
|
|
|
.put(double.class, "D")
|
|
|
|
.put(void.class, "V").build();
|
2014-06-15 04:30:47 +02:00
|
|
|
|
2014-06-14 23:58:49 +02:00
|
|
|
if (isForge) {
|
|
|
|
// Initialize the maps by reading the srg file
|
|
|
|
ForgeClassMappings = new HashMap<String, String>();
|
|
|
|
ForgeFieldMappings = new HashMap<String, Map<String, String>>();
|
2014-06-15 07:38:16 +02:00
|
|
|
ForgeMethodMappings = new HashMap<String, Map<String, Map<String, String>>>();
|
2014-06-14 23:58:49 +02:00
|
|
|
try {
|
|
|
|
InputStream stream = Class.forName("net.minecraftforge.common.MinecraftForge").getClassLoader()
|
|
|
|
.getResourceAsStream("mappings/" + getBukkitVersion() + "/cb2numpkg.srg");
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
2014-06-15 04:30:47 +02:00
|
|
|
|
2014-06-14 23:58:49 +02:00
|
|
|
// 1: cb-simpleName
|
|
|
|
// 2: forge-fullName (Needs dir2fqn())
|
2014-06-15 04:30:47 +02:00
|
|
|
Pattern classPattern = Pattern.compile("^CL: net/minecraft/server/([" + nameseg_class + "]+) ([" + fqn_class + "]+)$");
|
2014-06-14 23:58:49 +02:00
|
|
|
// 1: cb-simpleName
|
|
|
|
// 2: cb-fieldName
|
|
|
|
// 3: forge-fullName (Needs dir2fqn())
|
|
|
|
// 4: forge-fieldName
|
2014-06-15 04:30:47 +02:00
|
|
|
Pattern fieldPattern = Pattern.compile("^FD: net/minecraft/server/([" + nameseg_class + "]+)/([" + nameseg_class + "]+) ([" + fqn_class + "]+)/([" + nameseg_class + "]+)$");
|
|
|
|
// 1: cb-simpleName
|
|
|
|
// 2: cb-methodName
|
|
|
|
// 3: cb-signature-args
|
|
|
|
// 4: cb-signature-ret
|
|
|
|
// 5: forge-fullName (Needs dir2fqn())
|
|
|
|
// 6: forge-methodName
|
|
|
|
// 7: forge-signature-args
|
|
|
|
// 8: forge-signature-ret
|
|
|
|
Pattern methodPattern = Pattern.compile("^MD: net/minecraft/server/([" + fqn_class + "]+)/([" + nameseg_class + "]+) \\(([;\\[" + fqn_class + "]*)\\)([;\\[" + fqn_class + "]+) " +
|
|
|
|
"([" + fqn_class + "]+)/([" + nameseg_class + "]+) \\(([;\\[" + fqn_class + "]*)\\)([;\\[" + fqn_class + "]+)$");
|
2014-06-14 23:58:49 +02:00
|
|
|
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
Matcher classMatcher = classPattern.matcher(line);
|
|
|
|
if (classMatcher.matches()) {
|
2014-06-15 05:19:56 +02:00
|
|
|
// by CB class name
|
2014-06-14 23:58:49 +02:00
|
|
|
ForgeClassMappings.put(classMatcher.group(1), dir2fqn(classMatcher.group(2)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Matcher fieldMatcher = fieldPattern.matcher(line);
|
|
|
|
if (fieldMatcher.matches()) {
|
2014-06-15 05:19:56 +02:00
|
|
|
// by CB class name
|
2014-06-14 23:58:49 +02:00
|
|
|
Map<String, String> innerMap = ForgeFieldMappings.get(dir2fqn(fieldMatcher.group(3)));
|
|
|
|
if (innerMap == null) {
|
|
|
|
innerMap = new HashMap<String, String>();
|
|
|
|
ForgeFieldMappings.put(dir2fqn(fieldMatcher.group(3)), innerMap);
|
|
|
|
}
|
2014-06-15 05:19:56 +02:00
|
|
|
// by CB field name to Forge field name
|
2014-06-14 23:58:49 +02:00
|
|
|
innerMap.put(fieldMatcher.group(2), fieldMatcher.group(4));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Matcher methodMatcher = methodPattern.matcher(line);
|
|
|
|
if (methodMatcher.matches()) {
|
2014-06-15 05:19:56 +02:00
|
|
|
// get by CB class name
|
2014-06-15 07:38:16 +02:00
|
|
|
Map<String, Map<String, String>> middleMap = ForgeMethodMappings.get(dir2fqn(methodMatcher.group(5)));
|
2014-06-15 05:19:56 +02:00
|
|
|
if (middleMap == null) {
|
2014-06-15 07:38:16 +02:00
|
|
|
middleMap = new HashMap<String, Map<String, String>>();
|
2014-06-15 05:19:56 +02:00
|
|
|
ForgeMethodMappings.put(dir2fqn(methodMatcher.group(5)), middleMap);
|
|
|
|
}
|
|
|
|
// get by CB method name
|
2014-06-15 07:38:16 +02:00
|
|
|
Map<String, String> innerMap = middleMap.get(methodMatcher.group(2));
|
2014-06-15 05:19:56 +02:00
|
|
|
if (innerMap == null) {
|
2014-06-15 07:38:16 +02:00
|
|
|
innerMap = new HashMap<String, String>();
|
2014-06-15 05:19:56 +02:00
|
|
|
middleMap.put(methodMatcher.group(2), innerMap);
|
|
|
|
}
|
2014-06-15 07:38:16 +02:00
|
|
|
// store the parameter strings
|
|
|
|
innerMap.put(methodMatcher.group(3), methodMatcher.group(6));
|
|
|
|
innerMap.put(methodMatcher.group(7), methodMatcher.group(6));
|
2014-06-14 23:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-15 05:19:56 +02:00
|
|
|
System.out.println("[LibsDisguises] Loaded in Cauldron/Forge mode");
|
2014-06-15 07:38:16 +02:00
|
|
|
System.out.println("[LibsDisguises] Loaded " + ForgeClassMappings.size() + " Cauldron class mappings");
|
|
|
|
System.out.println("[LibsDisguises] Loaded " + ForgeFieldMappings.size() + " Cauldron field mappings");
|
|
|
|
System.out.println("[LibsDisguises] Loaded " + ForgeMethodMappings.size() + " Cauldron method mappings");
|
2014-06-14 23:58:49 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
2014-06-15 07:38:16 +02:00
|
|
|
System.err.println("Warning: Running on Cauldron server, but couldn't load mappings file. LibsDisguises will likely crash!");
|
2014-06-14 23:58:49 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2014-06-15 07:38:16 +02:00
|
|
|
System.err.println("Warning: Running on Cauldron server, but couldn't load mappings file. LibsDisguises will likely crash!");
|
2014-06-14 23:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-15 05:19:56 +02:00
|
|
|
}
|
2014-06-14 23:58:49 +02:00
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
private static final Class<?> craftItemClass;
|
2014-06-15 05:19:56 +02:00
|
|
|
private static final Field pingField;
|
|
|
|
private static final Field trackerField;
|
|
|
|
private static final Field entitiesField;
|
|
|
|
private static final Method ihmGet;
|
|
|
|
private static Method damageAndIdleSoundMethod;
|
|
|
|
|
|
|
|
static {
|
2013-11-18 20:25:48 +01:00
|
|
|
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
|
|
|
|
try {
|
|
|
|
if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers())
|
|
|
|
&& method.getParameterTypes().length == 0) {
|
2014-01-18 20:39:23 +01:00
|
|
|
Object entity = createEntityInstance("Cow");
|
2013-11-18 20:25:48 +01:00
|
|
|
method.setAccessible(true);
|
2014-01-18 20:39:23 +01:00
|
|
|
float value = (Float) method.invoke(entity);
|
|
|
|
if (value == 0.4F) {
|
|
|
|
damageAndIdleSoundMethod = method;
|
2013-11-18 20:25:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2014-06-15 05:19:56 +02:00
|
|
|
craftItemClass = getCraftClass("inventory.CraftItemStack");
|
|
|
|
pingField = getNmsField("EntityPlayer", "ping");
|
|
|
|
trackerField = getNmsField("WorldServer", "tracker");
|
|
|
|
entitiesField = getNmsField("EntityTracker", "trackedEntities");
|
|
|
|
ihmGet = getNmsMethod("IntHashMap", "get", int.class);
|
2014-01-05 01:13:49 +01:00
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
Method m = getNmsMethod("Item", "getItemOf", getNmsClass("Block"));
|
|
|
|
System.out.println(m);
|
2014-06-15 04:30:47 +02:00
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
DisguiseType.ARROW.isMisc();
|
2014-06-15 04:30:47 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
private static String dir2fqn(String s) {
|
|
|
|
return s.replaceAll("/", ".");
|
2014-06-15 04:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ===
|
|
|
|
|
2013-12-22 01:03:47 +01:00
|
|
|
public static Object createEntityInstance(String entityName) {
|
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
Class<?> entityClass = getNmsClass("Entity" + entityName);
|
2013-12-22 01:03:47 +01:00
|
|
|
Object entityObject;
|
|
|
|
Object world = getWorld(Bukkit.getWorlds().get(0));
|
|
|
|
if (entityName.equals("Player")) {
|
2014-06-15 01:06:02 +02:00
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2013-12-22 01:03:47 +01:00
|
|
|
Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World"))
|
|
|
|
.newInstance(world);
|
2014-01-18 20:21:55 +01:00
|
|
|
if (LibVersion.is1_7()) {
|
2014-06-04 22:40:15 +02:00
|
|
|
WrappedGameProfile gameProfile = getGameProfile(null, "LibsDisguises");
|
2013-12-22 01:03:47 +01:00
|
|
|
entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
|
2014-06-04 22:40:15 +02:00
|
|
|
gameProfile.getHandleType(), playerinteractmanager.getClass()).newInstance(minecraftServer, world,
|
|
|
|
gameProfile.getHandle(), playerinteractmanager);
|
2013-12-22 01:03:47 +01:00
|
|
|
} else {
|
|
|
|
entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("World"), String.class,
|
|
|
|
playerinteractmanager.getClass()).newInstance(minecraftServer, world, "LibsDisguises",
|
|
|
|
playerinteractmanager);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entityObject = entityClass.getConstructor(getNmsClass("World")).newInstance(world);
|
|
|
|
}
|
|
|
|
return entityObject;
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:36:06 +01:00
|
|
|
public static FakeBoundingBox getBoundingBox(Entity entity) {
|
|
|
|
try {
|
2014-06-14 23:58:49 +02:00
|
|
|
Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity));
|
2013-12-22 04:58:49 +01:00
|
|
|
double x = 0, y = 0, z = 0;
|
|
|
|
int stage = 0;
|
|
|
|
for (Field field : boundingBox.getClass().getFields()) {
|
|
|
|
if (field.getType().getSimpleName().equals("double")) {
|
|
|
|
stage++;
|
|
|
|
switch (stage) {
|
|
|
|
case 1:
|
|
|
|
x -= field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
y -= field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
z -= field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
x += field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
y += field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
z += field.getDouble(boundingBox);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Error while setting the bounding box, more doubles than I thought??");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new FakeBoundingBox(x, y, z);
|
|
|
|
|
2013-12-22 00:36:06 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:55:03 +01:00
|
|
|
public static Entity getBukkitEntity(Object nmsEntity) {
|
|
|
|
try {
|
2014-06-15 07:38:16 +02:00
|
|
|
return (Entity) getNmsMethod("Entity", "getBukkitEntity").invoke(nmsEntity);
|
2013-11-27 04:55:03 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static ItemStack getBukkitItem(Object nmsItem) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
2013-11-22 20:52:15 +01:00
|
|
|
e.printStackTrace();
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-18 20:21:55 +01:00
|
|
|
public static String getBukkitVersion() {
|
|
|
|
return bukkitVersion;
|
|
|
|
}
|
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
public static Class<?> getCraftClass(String className) {
|
2013-11-18 20:25:48 +01:00
|
|
|
try {
|
2014-05-12 03:46:29 +02:00
|
|
|
return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
|
2013-11-18 20:25:48 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static String getCraftSound(Sound sound) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2014-06-15 07:38:16 +02:00
|
|
|
return (String) getCraftClass("CraftSound").getMethod("getSound", Sound.class).invoke(null, sound);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception ex) {
|
2013-11-22 20:52:15 +01:00
|
|
|
ex.printStackTrace();
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static String getEnumArt(Art art) {
|
2013-11-18 20:25:48 +01:00
|
|
|
try {
|
2014-06-15 07:38:16 +02:00
|
|
|
Object enumArt = getCraftClass("CraftArt").getMethod("BukkitToNotch", Art.class).invoke(null, art);
|
2013-11-22 20:52:15 +01:00
|
|
|
for (Field field : enumArt.getClass().getFields()) {
|
|
|
|
if (field.getType() == String.class) {
|
|
|
|
return (String) field.get(enumArt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-15 01:06:02 +02:00
|
|
|
static Object getEntityTrackerEntry(Entity target) throws Exception {
|
|
|
|
Object world = getWorld(target.getWorld());
|
|
|
|
Object tracker = trackerField.get(world);
|
|
|
|
Object trackedEntities = entitiesField.get(tracker);
|
|
|
|
return ihmGet.invoke(trackedEntities, target.getEntityId());
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getGameProfile(Player player) {
|
2014-04-12 07:19:46 +02:00
|
|
|
if (LibVersion.is1_7()) {
|
2014-06-04 22:40:15 +02:00
|
|
|
return WrappedGameProfile.fromPlayer(player);
|
2014-04-12 07:07:27 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) {
|
2013-12-05 09:05:58 +01:00
|
|
|
try {
|
2014-06-04 22:40:15 +02:00
|
|
|
return new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
|
2013-12-05 09:05:58 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, WrappedGameProfile profileWithSkin) {
|
2014-05-31 20:47:05 +02:00
|
|
|
try {
|
2014-06-04 22:40:15 +02:00
|
|
|
WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
|
|
|
|
gameProfile.getProperties().putAll(profileWithSkin.getProperties());
|
2014-05-31 20:47:05 +02:00
|
|
|
return gameProfile;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Class getNmsClass(String className) {
|
2014-06-15 07:38:16 +02:00
|
|
|
if (isForge) {
|
|
|
|
String forgeName = ForgeClassMappings.get(className);
|
|
|
|
if (forgeName != null) {
|
|
|
|
try {
|
|
|
|
return Class.forName(forgeName);
|
|
|
|
} catch (ClassNotFoundException ignored) {
|
2014-06-14 23:58:49 +02:00
|
|
|
}
|
2014-06-15 07:38:16 +02:00
|
|
|
} else
|
|
|
|
throw new RuntimeException("Missing Forge mapping for " + className);
|
|
|
|
}
|
|
|
|
try {
|
2014-05-12 03:46:29 +02:00
|
|
|
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
2014-06-14 23:58:49 +02:00
|
|
|
e.printStackTrace();
|
2013-11-27 04:38:51 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Object getNmsEntity(Entity entity) {
|
|
|
|
try {
|
|
|
|
return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object getNmsItem(ItemStack itemstack) {
|
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
return craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-14 23:58:49 +02:00
|
|
|
public static Field getNmsField(String className, String fieldName) {
|
|
|
|
return getNmsField(getNmsClass(className), fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Field getNmsField(Class clazz, String fieldName) {
|
2014-06-15 07:38:16 +02:00
|
|
|
if (isForge) {
|
|
|
|
Map<String, String> fieldMap = ForgeFieldMappings.get(clazz.getName());
|
|
|
|
if (fieldMap != null) {
|
2014-06-15 05:19:56 +02:00
|
|
|
String forgeName = fieldMap.get(fieldName);
|
2014-06-15 07:38:16 +02:00
|
|
|
if (forgeName != null) {
|
|
|
|
try {
|
|
|
|
return clazz.getField(forgeName);
|
|
|
|
} catch (NoSuchFieldException ignored) {
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
throw new RuntimeException("No field mapping for " + clazz.getName() + "." + fieldName);
|
|
|
|
} else
|
|
|
|
throw new RuntimeException("No field mappings for " + clazz.getName());
|
|
|
|
}
|
|
|
|
try {
|
2014-06-14 23:58:49 +02:00
|
|
|
return clazz.getField(fieldName);
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Method getNmsMethod(String className, String methodName, Class<?>... parameters) {
|
|
|
|
return getNmsMethod(getNmsClass(className), methodName, parameters);
|
|
|
|
}
|
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
private static String methodSignaturePart(Class<?> param) {
|
|
|
|
if (param.isArray()) {
|
|
|
|
return "[" + methodSignaturePart(param.getComponentType());
|
|
|
|
} else if (param.isPrimitive()) {
|
|
|
|
return primitiveTypes.get(param);
|
|
|
|
} else {
|
|
|
|
return "L" + param.getName().replaceAll("\\.", "/") + ";";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 01:06:02 +02:00
|
|
|
public static Method getNmsMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
|
2014-06-15 07:38:16 +02:00
|
|
|
if (isForge) {
|
|
|
|
Map<String, Map<String, String>> middleMap = ForgeMethodMappings.get(clazz.getName());
|
|
|
|
if (middleMap != null) {
|
|
|
|
Map<String, String> innerMap = middleMap.get(methodName);
|
|
|
|
if (innerMap != null) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (Class<?> cl : parameters) {
|
|
|
|
sb.append(methodSignaturePart(cl));
|
|
|
|
}
|
|
|
|
String trName = innerMap.get(sb.toString());
|
|
|
|
if (trName != null) {
|
|
|
|
try {
|
|
|
|
return clazz.getMethod(trName, parameters);
|
|
|
|
} catch (NoSuchMethodException ignored) {
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
throw new RuntimeException("No method mapping for " + clazz.getName() + "." + methodName + "(" + sb.toString() + ")");
|
|
|
|
} else
|
|
|
|
throw new RuntimeException("No method mapping for " + clazz.getName() + "." + methodName);
|
|
|
|
} else
|
|
|
|
throw new RuntimeException("No method mappings for " + clazz.getName());
|
|
|
|
}
|
2014-06-14 23:58:49 +02:00
|
|
|
try {
|
|
|
|
return clazz.getMethod(methodName, parameters);
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:01:49 +01:00
|
|
|
public static double getPing(Player player) {
|
|
|
|
try {
|
|
|
|
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return 0D;
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:57 +01:00
|
|
|
public static float[] getSize(Entity entity) {
|
|
|
|
try {
|
2014-06-14 23:58:49 +02:00
|
|
|
float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity));
|
2014-06-15 01:06:02 +02:00
|
|
|
float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity));
|
|
|
|
float height = getNmsField("Entity", "height").getFloat(getNmsEntity(entity));
|
2013-12-22 05:35:57 +01:00
|
|
|
return new float[] { length, width, height };
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) {
|
2014-04-23 01:44:12 +02:00
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2014-04-23 01:44:12 +02:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getMethods()) {
|
|
|
|
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) {
|
|
|
|
Object session = method.invoke(minecraftServer);
|
2014-06-04 22:40:15 +02:00
|
|
|
return WrappedGameProfile.fromHandle(session.getClass()
|
|
|
|
.getMethod("fillProfileProperties", gameProfile.getHandleType(), boolean.class)
|
|
|
|
.invoke(session, gameProfile.getHandle(), true));
|
2014-04-23 01:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Float getSoundModifier(Object entity) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2014-01-18 20:39:23 +01:00
|
|
|
damageAndIdleSoundMethod.setAccessible(true);
|
|
|
|
return (Float) damageAndIdleSoundMethod.invoke(entity);
|
2013-11-22 20:52:15 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object getWorld(World world) {
|
|
|
|
try {
|
|
|
|
return getCraftClass("CraftWorld").getMethod("getHandle").invoke(world);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile grabProfileAddUUID(String playername) {
|
2014-04-14 16:26:31 +02:00
|
|
|
try {
|
2014-06-15 07:38:16 +02:00
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2014-04-14 16:26:31 +02:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getMethods()) {
|
2014-04-18 22:14:02 +02:00
|
|
|
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();
|
2014-04-14 16:26:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static boolean hasSkinBlob(WrappedGameProfile gameProfile) {
|
|
|
|
return !gameProfile.getProperties().isEmpty();
|
2014-04-18 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-18 20:21:55 +01:00
|
|
|
public static void setAllowSleep(Player player) {
|
|
|
|
try {
|
|
|
|
Object nmsEntity = getNmsEntity(player);
|
2014-06-15 01:06:02 +02:00
|
|
|
Object connection = getNmsField(nmsEntity.getClass(), "playerConnection").get(nmsEntity);
|
|
|
|
Field check = getNmsField(connection.getClass(), "checkMovement");
|
2014-01-18 20:21:55 +01:00
|
|
|
check.setBoolean(connection, true);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2013-12-07 19:51:37 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 03:21:10 +02:00
|
|
|
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
|
2013-12-22 01:03:47 +01:00
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity));
|
2013-12-22 01:03:47 +01:00
|
|
|
int stage = 0;
|
2013-12-31 06:33:42 +01:00
|
|
|
Location loc = entity.getLocation();
|
2013-12-22 01:03:47 +01:00
|
|
|
for (Field field : boundingBox.getClass().getFields()) {
|
2013-12-22 02:30:15 +01:00
|
|
|
if (field.getType().getSimpleName().equals("double")) {
|
2013-12-22 01:03:47 +01:00
|
|
|
stage++;
|
|
|
|
switch (stage) {
|
|
|
|
case 1:
|
2013-12-31 06:33:42 +01:00
|
|
|
field.setDouble(boundingBox, loc.getX() - newBox.getX());
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-12-31 06:37:28 +01:00
|
|
|
// field.setDouble(boundingBox, loc.getY() - newBox.getY());
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2013-12-31 06:33:42 +01:00
|
|
|
field.setDouble(boundingBox, loc.getZ() - newBox.getZ());
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2013-12-31 06:33:42 +01:00
|
|
|
field.setDouble(boundingBox, loc.getX() + newBox.getX());
|
2013-12-22 04:23:55 +01:00
|
|
|
break;
|
2013-12-22 01:03:47 +01:00
|
|
|
case 5:
|
2013-12-31 08:08:27 +01:00
|
|
|
field.setDouble(boundingBox, loc.getY() + newBox.getY());
|
2013-12-22 04:23:55 +01:00
|
|
|
break;
|
2013-12-22 01:03:47 +01:00
|
|
|
case 6:
|
2013-12-31 06:33:42 +01:00
|
|
|
field.setDouble(boundingBox, loc.getZ() + newBox.getZ());
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Error while setting the bounding box, more doubles than I thought??");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|