Use Google commons
This commit is contained in:
parent
7aee354eb2
commit
490233e832
@ -1,13 +1,15 @@
|
||||
package eu.mikroskeem.utils.bukkit;
|
||||
|
||||
import eu.mikroskeem.utils.reflect.Reflect;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static eu.mikroskeem.utils.bukkit.ServerUtils.getNmsVersion;
|
||||
|
||||
public class PlayerUtils {
|
||||
/**
|
||||
* Return player ping (via NMS reflection)
|
||||
@ -18,31 +20,19 @@ public class PlayerUtils {
|
||||
* @return Player's ping (-1 if reflection failed)
|
||||
*/
|
||||
public static int getNMSPing(@NotNull Player player){
|
||||
int ping = -1;
|
||||
|
||||
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName();
|
||||
String nmsVer = bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1);
|
||||
|
||||
Class<?> craftPlayerClass = Reflect.getClass(
|
||||
String.format("org.bukkit.craftbukkit.%s.entity.CraftPlayer", nmsVer));
|
||||
Class<?> craftEntityPlayerClass = Reflect.getClass(
|
||||
String.format("net.minecraft.server.%s.EntityPlayer", nmsVer));
|
||||
|
||||
if(craftPlayerClass != null && craftEntityPlayerClass != null){
|
||||
Method getHandle = Reflect.getMethod(craftPlayerClass, "getHandle");
|
||||
if(getHandle != null){
|
||||
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;
|
||||
try {
|
||||
String nmsVer = getNmsVersion();
|
||||
Class<?> craftPlayerClass = checkNotNull(Reflect.getClass(
|
||||
String.format("org.bukkit.craftbukkit.%s.entity.CraftPlayer", nmsVer)));
|
||||
Class<?> craftEntityPlayerClass = checkNotNull(Reflect.getClass(
|
||||
String.format("net.minecraft.server.%s.EntityPlayer", nmsVer)));
|
||||
Method getHandle = checkNotNull(Reflect.getMethod(craftPlayerClass, "getHandle"));
|
||||
Object handle = checkNotNull(Reflect.invokeMethod(getHandle, player));
|
||||
Field pingField = checkNotNull(Reflect.getField(craftEntityPlayerClass, "ping"));
|
||||
return (Integer) checkNotNull(Reflect.readField(pingField, handle));
|
||||
}
|
||||
catch (NullPointerException ignored){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ping;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user