Use Google commons

This commit is contained in:
Mark Vainomaa 2016-12-22 03:51:43 +02:00
parent 7aee354eb2
commit 490233e832

View File

@ -1,13 +1,15 @@
package eu.mikroskeem.utils.bukkit; package eu.mikroskeem.utils.bukkit;
import eu.mikroskeem.utils.reflect.Reflect; import eu.mikroskeem.utils.reflect.Reflect;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static com.google.common.base.Preconditions.checkNotNull;
import static eu.mikroskeem.utils.bukkit.ServerUtils.getNmsVersion;
public class PlayerUtils { public class PlayerUtils {
/** /**
* Return player ping (via NMS reflection) * Return player ping (via NMS reflection)
@ -18,31 +20,19 @@ public class PlayerUtils {
* @return Player's ping (-1 if reflection failed) * @return Player's ping (-1 if reflection failed)
*/ */
public static int getNMSPing(@NotNull Player player){ public static int getNMSPing(@NotNull Player player){
int ping = -1; try {
String nmsVer = getNmsVersion();
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName(); Class<?> craftPlayerClass = checkNotNull(Reflect.getClass(
String nmsVer = bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1); String.format("org.bukkit.craftbukkit.%s.entity.CraftPlayer", nmsVer)));
Class<?> craftEntityPlayerClass = checkNotNull(Reflect.getClass(
Class<?> craftPlayerClass = Reflect.getClass( String.format("net.minecraft.server.%s.EntityPlayer", nmsVer)));
String.format("org.bukkit.craftbukkit.%s.entity.CraftPlayer", nmsVer)); Method getHandle = checkNotNull(Reflect.getMethod(craftPlayerClass, "getHandle"));
Class<?> craftEntityPlayerClass = Reflect.getClass( Object handle = checkNotNull(Reflect.invokeMethod(getHandle, player));
String.format("net.minecraft.server.%s.EntityPlayer", nmsVer)); Field pingField = checkNotNull(Reflect.getField(craftEntityPlayerClass, "ping"));
return (Integer) checkNotNull(Reflect.readField(pingField, handle));
if(craftPlayerClass != null && craftEntityPlayerClass != null){ }
Method getHandle = Reflect.getMethod(craftPlayerClass, "getHandle"); catch (NullPointerException ignored){
if(getHandle != null){ return -1;
Object handle = Reflect.invokeMethod(getHandle, player);
if(handle != null){
Field pingField = Reflect.getField(craftEntityPlayerClass, "ping");
if(pingField != null){
Integer nmsPing = (Integer)Reflect.readField(pingField, handle);
if(nmsPing != null){
return nmsPing;
}
}
}
}
} }
return ping;
} }
} }