如何在子接口1.16.4中获得玩家的ping [英] How to get player's ping in spigot 1.16.4
问题描述
我试图以多种方式使用Java反射来获得播放器的ping.但是在100%时,它返回0ms.
I tried to use java reflection in many ways to get the ping of a player. But at 100%, it returns 0ms.
我已经搜索了很长时间,所以...有人可以帮我吗?
I've searched for a long time, so... can someone help me?
尝试1:
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);
}
}
Try2:
public static int getPing(Player p) {
EntityPlayer player = ((CraftPlayer) p).getHandle();
return player.ping;
}
尝试3:
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();
}
尝试4:
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个...(类似于此功能,或者绝对不适合使用)
And 3 more... (Similar to this or definitively not good for usage)
我正在VPS托管的1.16.4 PaperMC服务器上测试我的插件.
I'm testing my plugin on a VPS-Hosted 1.16.4 PaperMC server.
也许在专用计算机上会有所不同.
Maybe it will be different on a dedicated machine.
我尝试使用紧凑"像这样的代码:
I tried to use "compact" code like this :
int ping = ((CraftPlayer) p).getHandle().ping;
...但是它再次返回0ms.
... but it returns 0ms again.
请注意,我是一名真正的Java新手.我正在努力提高自己的水平.
Please note that I'm a real Java novice. I'm practicing to improve my level.
我不是在寻找最终的解决方案,而是如果我的方法还可以的话,只是一个答案.如果没有,那么是执行此操作的好方法.
I'm not searching the final solution, but just an answer if my aproach is OK. If not, a good way to do this.
推荐答案
最后,它可行.
问题是我的FAI:几天前,我为UDP/TCP打开了25565个端口,并且还更改了其他参数.
The problem is my FAI : A few days ago, I opened 25565 ports for UDP/TCP and I also change other parameters.
自从我关闭所有这些选项以来,我的ping神奇地恢复了8-12毫秒.
Since I turn off all these options, my ping was magically back to 8-12 ms.
感谢kahveci的编辑建议,不便之处,敬请谅解.
Thanks kahveci for your edit suggestion and sorry for the inconvenience.
这篇关于如何在子接口1.16.4中获得玩家的ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!