From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Encode42 <me@encode42.dev>
Date: Fri, 30 Apr 2021 14:03:06 -0400
Subject: [PATCH] Add credits command


diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
index 880efd832a2029bdb7a404e27797b316cbbe5703..a5712108fbbe5ddc61cdfd390f667ab013ba609d 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.CreditsCommand.register(this.dispatcher); // Purpur
             net.pl3x.purpur.command.DemoCommand.register(this.dispatcher); // Purpur
             net.pl3x.purpur.command.PingCommand.register(this.dispatcher); // Purpur
         }
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index 72cfd435cc68ee42ea28b0afc5b42c75e1e2813a..35be2f773d33b1c13f8732217c5520b384305b18 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -162,6 +162,7 @@ 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 creditsCommandOutput = "<green>%s has been shown the end credits";
     public static String demoCommandOutput = "<green>%s has been shown the demo screen";
     public static String pingCommandOutput = "<green>%s's ping is %sms";
     private static void messages() {
@@ -170,6 +171,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);
+        creditsCommandOutput = getString("settings.messages.credits-command-output", creditsCommandOutput);
         demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
         pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
     }
diff --git a/src/main/java/net/pl3x/purpur/command/CreditsCommand.java b/src/main/java/net/pl3x/purpur/command/CreditsCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..282be3e0e0cb3137829a88bd40fa18772dc344c2
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/command/CreditsCommand.java
@@ -0,0 +1,35 @@
+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.network.protocol.game.ClientboundGameEventPacket;
+import net.minecraft.server.level.ServerPlayer;
+import net.pl3x.purpur.PurpurConfig;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class CreditsCommand {
+    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
+        dispatcher.register(Commands.literal("credits")
+                .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.credits");
+    }
+
+    private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
+        for (ServerPlayer player : targets) {
+            ClientboundGameEventPacket packet = new ClientboundGameEventPacket(ClientboundGameEventPacket.WIN_GAME, 1F);
+            player.wonGame = true;
+            player.connection.send(packet);
+            String output = String.format(PurpurConfig.creditsCommandOutput, player.getGameProfile().getName());
+            sender.sendSuccess(output, false);
+        }
+        return targets.size();
+    }
+}
