我目前正试图为我的一个Streamer朋友想要运行的服务器编写一个Minecraft插件。我需要的是:
我需要一个函数或方法,让我让玩家ping到服务器。因此玩家在聊天框中输入/ping
并从服务器获得类似[Server]: Your current Ping is 62ms.
的响应我已经在Bukkit文档中查找了http://docs.codelanx.com/Bukkit/1.7.10/或http://bukkit.luricos.de/api/1.7.9-R0.2/,但我找不到适合我需要的东西。
我Plugin.yml:
name: Beginning
version: ${project.version}
main: de.nightcore.beginning.Main
api-version: 1.16
commands:
date:
description: Shows the current time and Date
ping:
description: Gives the Player its current ping to the server
My Main Class:
package de.nightcore.beginning;
import de.nightcore.beginning.commands.DateCommand;
import de.nightcore.beginning.commands.PingCommand;
import de.nightcore.beginning.listeners.JoinListener;
import de.nightcore.beginning.listeners.QuitListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public final class Main extends JavaPlugin {
@Override
public void onEnable()
{
Bukkit.getLogger().fine("Plugin wird geladen!");
listenerRegistration();
commandRegistration();
}
@Override
public void onDisable()
{
Bukkit.getLogger().fine("Plugin wird beendet!");
}
public static String getPrefix()
{
return ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Server" + ChatColor.DARK_GRAY + "] " + ChatColor.WHITE;
}
private void listenerRegistration()
{
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new JoinListener(), this);
pluginManager.registerEvents(new QuitListener(), this);
}
private void commandRegistration()
{
getCommand("date").setExecutor(new DateCommand());
getCommand("ping").setExecutor(new PingCommand());
}
}
最后我的PingCommand类:
package de.nightcore.beginning.commands;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
//TODO: Ping Command / Command that provides Connection Info
public class PingCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = sender.getServer().getPlayer("%UUID%"); ;
Server server = sender.getServer();
//Source Code to provide information about the players current ping
return false;
}
}
你需要将Player转换为CraftPlayer,然后使用craftplayer# getHandler()获得EntityPlayer,在那里你可以将播放器ping作为int值返回。
((CraftPlayer) player).getHandle().ping;
你还需要添加spigot依赖项(API似乎没有包含它)。
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>