Untitled
unknown
plain_text
a year ago
2.7 kB
5
Indexable
package me.mz.pingdisplay;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class PingDisplayPlugin extends JavaPlugin {
@Override
public void onEnable() {
// Register the /ping command
this.getCommand("ping").setExecutor(new PingCommand());
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
// Command executor for the /ping command
public class PingCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// اگر کامند توسط یک پلیر زده شده باشد
if (sender instanceof Player) {
Player player = (Player) sender;
// بررسی اینکه آیا آرگومنتی برای نام پلیر داده شده است
if (args.length > 0) {
// گرفتن بازیکن هدف از نام وارد شده
Player target = Bukkit.getPlayer(args[0]);
// بررسی اینکه بازیکن مورد نظر آنلاین است یا خیر
if (target != null && target.isOnline()) {
int ping = getPing(target);
player.sendMessage(target.getName() + "'s ping: " + ping + " ms");
} else {
player.sendMessage("Player not found or not online.");
}
} else {
// اگر هیچ آرگومنتی وارد نشده باشد، پینگ خود بازیکن نمایش داده میشود
int ping = getPing(player);
player.sendMessage("Your ping: " + ping + " ms");
}
return true;
} else {
sender.sendMessage("This command can only be used by players.");
return true;
}
}
// Method to get a player's ping
private int getPing(Player player) {
try {
Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
return (int) entityPlayer.getClass().getField("ping").get(entityPlayer);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
}
Editor is loading...
Leave a Comment