Fix a ClassNotFoundException with PlayerInfoData

Fixes #59
This commit is contained in:
Dan Mulloy 2015-03-15 13:10:36 -04:00
parent ac14843d96
commit c7b9600c21
2 changed files with 49 additions and 59 deletions

View File

@ -2,16 +2,16 @@
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland * Copyright (C) 2012 Kristian S. Stangeland
* *
* This program is free software; you can redistribute it and/or modify it under the terms of the * This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of * GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along with this program; * You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA * 02111-1307 USA
*/ */
@ -67,13 +67,13 @@ class CachedPackage {
if (result == null) { if (result == null) {
// Look up the class dynamically // Look up the class dynamically
result = source.loadClass(combine(packageName, className)); result = source.loadClass(combine(packageName, className));
if (result == null) if (result == null)
throw new IllegalArgumentException("Source " + source + " returned NULL for " + className); throw new IllegalArgumentException("Source " + source + " returned NULL for " + className);
cache.put(className, result); cache.put(className, result);
} }
return result; return result;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find class " + className, e); throw new RuntimeException("Cannot find class " + className, e);
} }

View File

@ -420,10 +420,8 @@ public class MinecraftReflection {
/** /**
* Determine if a given object is a ChunkPosition. * Determine if a given object is a ChunkPosition.
* @param obj - the object to test. * @param obj - the object to test.
* @deprecated ChunkPosition no longer exists
* @return TRUE if it can, FALSE otherwise. * @return TRUE if it can, FALSE otherwise.
*/ */
@Deprecated
public static boolean isChunkPosition(Object obj) { public static boolean isChunkPosition(Object obj) {
Class<?> chunkPosition = getChunkPositionClass(); Class<?> chunkPosition = getChunkPositionClass();
return obj != null && chunkPosition != null && chunkPosition.isAssignableFrom(obj.getClass()); return obj != null && chunkPosition != null && chunkPosition.isAssignableFrom(obj.getClass());
@ -567,16 +565,15 @@ public class MinecraftReflection {
return getMinecraftClass("EntityPlayer"); return getMinecraftClass("EntityPlayer");
} catch (RuntimeException e) { } catch (RuntimeException e) {
try { try {
// A fairly stable method // Grab CraftPlayer's handle
Method detect = FuzzyReflection.fromClass(getCraftBukkitClass("CraftServer")). Method getHandle = FuzzyReflection
getMethodByName("detectListNameConflict"); .fromClass(getCraftBukkitClass("entity.CraftPlayer"))
.getMethodByName("getHandle");
// EntityPlayer is then the first parameter // EntityPlayer is the return type
return detect.getParameterTypes()[0]; return setMinecraftClass("EntityPlayer", getHandle.getReturnType());
} catch (IllegalArgumentException e1) {
} catch (IllegalArgumentException ex) { throw new RuntimeException("Could not find EntityPlayer class.", e1);
// Last resort
return fallbackMethodReturn("EntityPlayer", "entity.CraftPlayer", "getHandle");
} }
} }
} }
@ -609,7 +606,7 @@ public class MinecraftReflection {
*/ */
public static Class<?> getEntityClass() { public static Class<?> getEntityClass() {
try { try {
return getMinecraftClass("Entity"); return getMinecraftClass("Entity");
} catch (RuntimeException e) { } catch (RuntimeException e) {
return fallbackMethodReturn("Entity", "entity.CraftEntity", "getHandle"); return fallbackMethodReturn("Entity", "entity.CraftEntity", "getHandle");
} }
@ -828,13 +825,14 @@ public class MinecraftReflection {
} catch (RuntimeException e) { } catch (RuntimeException e) {
Class<?> serverPing = getServerPingClass(); Class<?> serverPing = getServerPingClass();
// Find a server ping object for (Field field : FuzzyReflection.fromClass(serverPing, true).getFields()) {
AbstractFuzzyMatcher<Class<?>> serverDataContract = FuzzyClassContract.newBuilder(). Class<?> clazz = field.getType();
constructor(FuzzyMethodContract.newBuilder().parameterExactArray(String.class, int.class)). if (clazz.getName().contains("ServerData")) {
build(). return setMinecraftClass("ServerData", clazz);
and(getMinecraftObjectMatcher()); }
}
return setMinecraftClass("ServerPingServerData", getTypeFromField(serverPing, serverDataContract)); throw new IllegalStateException("Could not find ServerData class.");
} }
} }
@ -1172,30 +1170,27 @@ public class MinecraftReflection {
/** /**
* Retrieves the ChunkPosition class. * Retrieves the ChunkPosition class.
* *
* @deprecated ChunkPosition no longer exists. Replaced by BlockPosition.
* @return The ChunkPosition class. * @return The ChunkPosition class.
*/ */
@Deprecated
public static Class<?> getChunkPositionClass() { public static Class<?> getChunkPositionClass() {
try { try {
return getMinecraftClass("ChunkPosition"); return getMinecraftClass("ChunkPosition");
} catch (RuntimeException e) { } catch (RuntimeException e) {
return null; Class<?> normalChunkGenerator = getCraftBukkitClass("generator.NormalChunkGenerator");
// Class<?> normalChunkGenerator = getCraftBukkitClass("generator.NormalChunkGenerator");
// // ChunkPosition a(net.minecraft.server.World world, String string, int i, int i1, int i2) {
// // ChunkPosition a(net.minecraft.server.World world, String string, int i, int i1, int i2) { FuzzyMethodContract selected = FuzzyMethodContract.newBuilder()
// FuzzyMethodContract selected = FuzzyMethodContract.newBuilder() .banModifier(Modifier.STATIC)
// .banModifier(Modifier.STATIC) .parameterMatches(getMinecraftObjectMatcher(), 0)
// .parameterMatches(getMinecraftObjectMatcher(), 0) .parameterExactType(String.class, 1)
// .parameterExactType(String.class, 1) .parameterExactType(int.class, 2)
// .parameterExactType(int.class, 2) .parameterExactType(int.class, 3)
// .parameterExactType(int.class, 3) .parameterExactType(int.class, 4)
// .parameterExactType(int.class, 4) .build();
// .build();
// return setMinecraftClass("ChunkPosition",
// return setMinecraftClass("ChunkPosition", FuzzyReflection.fromClass(normalChunkGenerator).getMethod(selected).getReturnType());
// FuzzyReflection.fromClass(normalChunkGenerator).getMethod(selected).getReturnType());
} }
} }
@ -1223,8 +1218,8 @@ public class MinecraftReflection {
} }
/** /**
* Retrieves the Vec3d class. * Retrieves the Vec3D class.
* @return The Vec3d class. * @return The Vec3D class.
*/ */
public static Class<?> getVec3DClass() { public static Class<?> getVec3DClass() {
try { try {
@ -1239,7 +1234,6 @@ public class MinecraftReflection {
* Retrieve the ChunkCoordinates class. * Retrieve the ChunkCoordinates class.
* @return The ChunkPosition class. * @return The ChunkPosition class.
*/ */
@Deprecated
public static Class<?> getChunkCoordinatesClass() { public static Class<?> getChunkCoordinatesClass() {
try { try {
return getMinecraftClass("ChunkCoordinates"); return getMinecraftClass("ChunkCoordinates");
@ -1886,7 +1880,7 @@ public class MinecraftReflection {
* @return The PlayerInfoData class * @return The PlayerInfoData class
*/ */
public static Class<?> getPlayerInfoDataClass() { public static Class<?> getPlayerInfoDataClass() {
return getMinecraftClass("PlayerInfoData"); return getMinecraftClass("PacketPlayOutPlayerInfo$PlayerInfoData", "PlayerInfoData");
} }
/** /**
@ -1936,8 +1930,7 @@ public class MinecraftReflection {
* @return Class object. * @return Class object.
* @throws RuntimeException If we are unable to find the given class. * @throws RuntimeException If we are unable to find the given class.
*/ */
@SuppressWarnings("rawtypes") public static Class<?> getCraftBukkitClass(String className) {
public static Class getCraftBukkitClass(String className) {
if (craftbukkitPackage == null) if (craftbukkitPackage == null)
craftbukkitPackage = new CachedPackage(getCraftBukkitPackage(), getClassSource()); craftbukkitPackage = new CachedPackage(getCraftBukkitPackage(), getClassSource());
return craftbukkitPackage.getPackageClass(className); return craftbukkitPackage.getPackageClass(className);
@ -1990,6 +1983,7 @@ public class MinecraftReflection {
// Just use the default class loader // Just use the default class loader
classSource = ClassSource.fromClassLoader(); classSource = ClassSource.fromClassLoader();
} }
return classSource; return classSource;
} }
@ -2004,7 +1998,7 @@ public class MinecraftReflection {
try { try {
// Try the main class first // Try the main class first
return getMinecraftClass(className); return getMinecraftClass(className);
} catch (RuntimeException e1) { } catch (RuntimeException e) {
Class<?> success = null; Class<?> success = null;
// Try every alias too // Try every alias too
@ -2012,8 +2006,8 @@ public class MinecraftReflection {
try { try {
success = getMinecraftClass(alias); success = getMinecraftClass(alias);
break; break;
} catch (RuntimeException e2) { } catch (RuntimeException e1) {
// Swallov // Just swallow it...
} }
} }
@ -2023,11 +2017,7 @@ public class MinecraftReflection {
return success; return success;
} else { } else {
// Hack failed // Hack failed
throw new RuntimeException( throw new RuntimeException(String.format("Unable to find %s (%s)", className, Joiner.on(", ").join(aliases)));
String.format("Unable to find %s (%s)",
className,
Joiner.on(", ").join(aliases))
);
} }
} }
} }