From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: BillyGalbreath <blake.galbreath@gmail.com>
Date: Mon, 30 Nov 2020 03:12:04 -0600
Subject: [PATCH] Add demo command


diff --git a/src/main/java/net/minecraft/commands/CommandDispatcher.java b/src/main/java/net/minecraft/commands/CommandDispatcher.java
index 2566c5ea7ba2bbeed43ba74944c038b53f7f9fde..d402a896a0094e6c379d71c63d744b4ee443c097 100644
--- a/src/main/java/net/minecraft/commands/CommandDispatcher.java
+++ b/src/main/java/net/minecraft/commands/CommandDispatcher.java
@@ -191,6 +191,7 @@ public class CommandDispatcher {
             CommandIdleTimeout.a(this.b);
             CommandStop.a(this.b);
             CommandWhitelist.a(this.b);
+            net.pl3x.purpur.command.DemoCommand.register(dispatcher()); // Purpur
             net.pl3x.purpur.command.PingCommand.register(dispatcher()); // Purpur
         }
 
diff --git a/src/main/java/net/minecraft/network/protocol/game/PacketPlayOutGameStateChange.java b/src/main/java/net/minecraft/network/protocol/game/PacketPlayOutGameStateChange.java
index edb6c0ab2826051b04e025a713d794dbc5de4792..0161657748d398b6827ef8bc2b00b8a63bf37c55 100644
--- a/src/main/java/net/minecraft/network/protocol/game/PacketPlayOutGameStateChange.java
+++ b/src/main/java/net/minecraft/network/protocol/game/PacketPlayOutGameStateChange.java
@@ -13,7 +13,7 @@ public class PacketPlayOutGameStateChange implements Packet<PacketListenerPlayOu
     public static final PacketPlayOutGameStateChange.a c = new PacketPlayOutGameStateChange.a(2);
     public static final PacketPlayOutGameStateChange.a d = new PacketPlayOutGameStateChange.a(3);
     public static final PacketPlayOutGameStateChange.a e = new PacketPlayOutGameStateChange.a(4);
-    public static final PacketPlayOutGameStateChange.a f = new PacketPlayOutGameStateChange.a(5);
+    public static final PacketPlayOutGameStateChange.a f = new PacketPlayOutGameStateChange.a(5); public static PacketPlayOutGameStateChange.a demo() { return f; } // Purpur - OBFHELPER
     public static final PacketPlayOutGameStateChange.a g = new PacketPlayOutGameStateChange.a(6);
     public static final PacketPlayOutGameStateChange.a h = new PacketPlayOutGameStateChange.a(7);
     public static final PacketPlayOutGameStateChange.a i = new PacketPlayOutGameStateChange.a(8);
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index b005025e49f5b45bf5a28e5a38cffc9b70689b0f..f99566da1ea88cfddf14fd73cba98643707bb893 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -137,6 +137,7 @@ public class PurpurConfig {
     public static String afkBroadcastBack = "§e§o%s is no longer AFK";
     public static String afkTabListPrefix = "[AFK] ";
     public static String afkTabListSuffix = "";
+    public static String demoCommandOutput = "§a%s has been shown the demo screen";
     public static String pingCommandOutput = "§a%s's ping is %sms";
     public static String cannotRideMob = "§cYou cannot mount that mob";
     private static void messages() {
@@ -144,6 +145,7 @@ public class PurpurConfig {
         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);
+        demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
         pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
         cannotRideMob = getString("settings.messages.cannot-ride-mob", cannotRideMob);
     }
diff --git a/src/main/java/net/pl3x/purpur/command/DemoCommand.java b/src/main/java/net/pl3x/purpur/command/DemoCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..d621dab3c40f5d093474444eccecd8e7c9e301a1
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/command/DemoCommand.java
@@ -0,0 +1,39 @@
+package net.pl3x.purpur.command;
+
+import java.util.Collection;
+import java.util.Collections;
+import net.minecraft.commands.arguments.ArgumentEntity;
+import net.minecraft.commands.CommandDispatcher;
+import net.minecraft.commands.CommandListenerWrapper;
+import net.minecraft.network.protocol.game.PacketPlayOutGameStateChange;
+import net.minecraft.server.level.EntityPlayer;
+import net.pl3x.purpur.PurpurConfig;
+import org.bukkit.craftbukkit.util.CraftChatMessage;
+
+public class DemoCommand {
+    public static void register(com.mojang.brigadier.CommandDispatcher<CommandListenerWrapper> dispatcher) {
+        dispatcher.register(CommandDispatcher.literal("demo")
+                .requires((listener) -> {
+                    return listener.hasPermission(2);
+                })
+                .executes((context) -> {
+                    return execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException()));
+                })
+                .then(CommandDispatcher.argument("targets", ArgumentEntity.players())
+                        .executes((context) -> {
+                            return execute(context.getSource(), ArgumentEntity.getPlayers(context, "targets"));
+                        })
+                )
+        ).setPermission("bukkit.command.demo");
+    }
+
+    private static int execute(CommandListenerWrapper sender, Collection<EntityPlayer> targets) {
+        for (EntityPlayer player : targets) {
+            PacketPlayOutGameStateChange packet = new PacketPlayOutGameStateChange(PacketPlayOutGameStateChange.demo(), 0);
+            player.playerConnection.sendPacket(packet);
+            String output = String.format(PurpurConfig.demoCommandOutput, player.getProfile().getName(), player.ping);
+            sender.sendMessage(CraftChatMessage.fromStringOrNull(output), false);
+        }
+        return targets.size();
+    }
+}
