From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Fri, 13 Mar 2020 22:29:10 -0500
Subject: [PATCH] Add ping command


diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
index 59881c845cc88508736cc3d2a61860512d28964d..3a1e1ff3c9154db6b1b23d1898c8e6ec198f103f 100644
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
@@ -198,6 +198,7 @@ public class Commands {
             SetPlayerIdleTimeoutCommand.register(this.dispatcher);
             StopCommand.register(this.dispatcher);
             WhitelistCommand.register(this.dispatcher);
+            net.pl3x.purpur.command.PingCommand.register(this.dispatcher); // Purpur
         }
 
         if (environment.includeIntegrated) {
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index 03b88ddc349fc7cfa48573ec93c22cd9cef3b58d..8f84848950c2bab5123766edd6131e98be97a92a 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -162,12 +162,14 @@ public class PurpurConfig {
     public static String afkBroadcastBack = "<yellow><italic>%s is no longer AFK";
     public static String afkTabListPrefix = "[AFK] ";
     public static String afkTabListSuffix = "";
+    public static String pingCommandOutput = "<green>%s's ping is %sms";
     private static void messages() {
         cannotRideMob = getString("settings.messages.cannot-ride-mob", cannotRideMob);
         afkBroadcastAway = getString("settings.messages.afk-broadcast-away", afkBroadcastAway);
         afkBroadcastBack = getString("settings.messages.afk-broadcast-back", afkBroadcastBack);
         afkTabListPrefix = getString("settings.messages.afk-tab-list-prefix", afkTabListPrefix);
         afkTabListSuffix = getString("settings.messages.afk-tab-list-suffix", afkTabListSuffix);
+        pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
     }
 
     public static String serverModName = "Purpur";
diff --git a/src/main/java/net/pl3x/purpur/command/PingCommand.java b/src/main/java/net/pl3x/purpur/command/PingCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..6de4f71aa5d501f3f5da93cd631da654a6e264e0
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/command/PingCommand.java
@@ -0,0 +1,32 @@
+package net.pl3x.purpur.command;
+
+import com.mojang.brigadier.CommandDispatcher;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.commands.Commands;
+import net.minecraft.commands.arguments.EntityArgument;
+import net.minecraft.server.level.ServerPlayer;
+import net.pl3x.purpur.PurpurConfig;
+import org.bukkit.craftbukkit.util.CraftChatMessage;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class PingCommand {
+    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
+        dispatcher.register(Commands.literal("ping")
+                .requires((listener) -> listener.hasPermission(2))
+                .executes((context) -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
+                .then(Commands.argument("targets", EntityArgument.players())
+                        .executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets")))
+                )
+        ).setPermission("bukkit.command.ping");
+    }
+
+    private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
+        for (ServerPlayer player : targets) {
+            String output = String.format(PurpurConfig.pingCommandOutput, player.getGameProfile().getName(), player.latency);
+            sender.sendSuccess(output, false);
+        }
+        return targets.size();
+    }
+}
