Java 如何获得播放器';插口中的s ping 1.16.4
我尝试以多种方式使用java反射来获得玩家的ping。但在100%时,它返回0毫秒 我已经找了很久了,所以。。。有人能帮我吗 尝试1:Java 如何获得播放器';插口中的s ping 1.16.4,java,minecraft,ping,java-reflection,Java,Minecraft,Ping,Java Reflection,我尝试以多种方式使用java反射来获得玩家的ping。但在100%时,它返回0毫秒 我已经找了很久了,所以。。。有人能帮我吗 尝试1: public static int getPing(Player p) { try { Object craftPlayer = (CraftPlayer) p; return (int) craftPlayer.getClass().getField("ping").ge
public static int getPing(Player p) {
try {
Object craftPlayer = (CraftPlayer) p;
return (int) craftPlayer.getClass().getField("ping").get(craftPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
throw new Error(e);
}
}
public static int getPing(Player p) {
EntityPlayer player = ((CraftPlayer) p).getHandle();
return player.ping;
}
int ping = 0;
Class<?>[] parameterTypes = null;
Class<CraftPlayer> metadata = CraftPlayer.class;
Method[] methods = metadata.getDeclaredMethods();
for(Method method : methods) {
if(method.getName().equalsIgnoreCase("getHandle")) {
parameterTypes = method.getParameterTypes();
}
}
try {
Object entityPlayer = p.getClass().getDeclaredMethod("getHandle",
parameterTypes).invoke((CraftPlayer) p);
ping = (int) entityPlayer.getClass().getDeclaredField("ping").get(entityPlayer);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | NoSuchFieldException e) {
e.printStackTrace();
}
int ping = 0;
try {
Object entityPlayer = p.getClass().getMethod("getHandle").invoke(p);
ping = entityPlayer.getClass().getField("ping").getInt(entityPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
e.printStackTrace();
}
Try2:
public static int getPing(Player p) {
try {
Object craftPlayer = (CraftPlayer) p;
return (int) craftPlayer.getClass().getField("ping").get(craftPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
throw new Error(e);
}
}
public static int getPing(Player p) {
EntityPlayer player = ((CraftPlayer) p).getHandle();
return player.ping;
}
int ping = 0;
Class<?>[] parameterTypes = null;
Class<CraftPlayer> metadata = CraftPlayer.class;
Method[] methods = metadata.getDeclaredMethods();
for(Method method : methods) {
if(method.getName().equalsIgnoreCase("getHandle")) {
parameterTypes = method.getParameterTypes();
}
}
try {
Object entityPlayer = p.getClass().getDeclaredMethod("getHandle",
parameterTypes).invoke((CraftPlayer) p);
ping = (int) entityPlayer.getClass().getDeclaredField("ping").get(entityPlayer);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | NoSuchFieldException e) {
e.printStackTrace();
}
int ping = 0;
try {
Object entityPlayer = p.getClass().getMethod("getHandle").invoke(p);
ping = entityPlayer.getClass().getField("ping").getInt(entityPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
e.printStackTrace();
}
尝试3:
public static int getPing(Player p) {
try {
Object craftPlayer = (CraftPlayer) p;
return (int) craftPlayer.getClass().getField("ping").get(craftPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
throw new Error(e);
}
}
public static int getPing(Player p) {
EntityPlayer player = ((CraftPlayer) p).getHandle();
return player.ping;
}
int ping = 0;
Class<?>[] parameterTypes = null;
Class<CraftPlayer> metadata = CraftPlayer.class;
Method[] methods = metadata.getDeclaredMethods();
for(Method method : methods) {
if(method.getName().equalsIgnoreCase("getHandle")) {
parameterTypes = method.getParameterTypes();
}
}
try {
Object entityPlayer = p.getClass().getDeclaredMethod("getHandle",
parameterTypes).invoke((CraftPlayer) p);
ping = (int) entityPlayer.getClass().getDeclaredField("ping").get(entityPlayer);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | NoSuchFieldException e) {
e.printStackTrace();
}
int ping = 0;
try {
Object entityPlayer = p.getClass().getMethod("getHandle").invoke(p);
ping = entityPlayer.getClass().getField("ping").getInt(entityPlayer);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException e) {
e.printStackTrace();
}
还有3个。。。(与此类似或显然不适合使用)
我正在VPS托管的1.16.4PC服务器上测试我的插件
在专用机器上可能会有所不同
我尝试使用“紧凑”代码,如下所示:
int ping = ((CraftPlayer) p).getHandle().ping;
。。。但它再次返回0毫秒
请注意,我是一个真正的Java新手。我在练习提高我的水平
我不是在寻找最终的解决方案,但如果我的方案可行的话,我只是在寻找答案。如果没有,这是一个很好的方法。最后,它起作用了
问题是我的FAI:几天前,我为UDP/TCP打开了25565个端口,我还更改了其他参数
由于我关闭了所有这些选项,我的ping神奇地回到了8-12毫秒
感谢kahveci提出的编辑建议,并对由此带来的不便表示歉意