From 2a39b2477030f725ce1571cefd96a71a4e9b5739 Mon Sep 17 00:00:00 2001 From: 4drian3d <68704415+4drian3d@users.noreply.github.com> Date: Fri, 8 Apr 2022 21:56:41 -0500 Subject: [PATCH] Shutdown command improvements (#680) - Migrate to Brigadier usage - Added MiniMessage support for custom shutdown reason --- .../velocitypowered/proxy/VelocityServer.java | 2 +- .../command/builtin/ShutdownCommand.java | 54 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index d00af4e91..82093801e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -209,7 +209,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { // Initialize commands first commandManager.register("velocity", new VelocityCommand(this)); commandManager.register("server", new ServerCommand(this)); - commandManager.register("shutdown", new ShutdownCommand(this),"end"); + commandManager.register("shutdown", ShutdownCommand.command(this), "end"); new GlistCommand(this).register(); this.doStartupConfigLoad(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ShutdownCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ShutdownCommand.java index 47394384a..5d3bec66d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ShutdownCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ShutdownCommand.java @@ -17,30 +17,42 @@ package com.velocitypowered.proxy.command.builtin; -import com.velocitypowered.api.command.RawCommand; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.proxy.VelocityServer; + +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -public class ShutdownCommand implements RawCommand { +public final class ShutdownCommand { + private ShutdownCommand(){} - private final VelocityServer server; - - public ShutdownCommand(VelocityServer server) { - this.server = server; - } - - @Override - public void execute(final Invocation invocation) { - String reason = invocation.arguments(); - if (reason.isEmpty() || reason.trim().isEmpty()) { - server.shutdown(true); - } else { - server.shutdown(true, LegacyComponentSerializer.legacy('&').deserialize(reason)); - } - } - - @Override - public boolean hasPermission(final Invocation invocation) { - return invocation.source() == server.getConsoleCommandSource(); + /** + * Creates a Velocity Shutdown Command. + * @param server the proxy instance + * @return the Shutdown Command + */ + public static BrigadierCommand command(final VelocityServer server) { + return new BrigadierCommand(LiteralArgumentBuilder.literal("shutdown") + .requires(source -> source == server.getConsoleCommandSource()) + .executes(context -> { + server.shutdown(true); + return Command.SINGLE_SUCCESS; + }) + .then(RequiredArgumentBuilder.argument("reason", StringArgumentType.greedyString()) + .executes(context -> { + String reason = context.getArgument("reason", String.class); + server.shutdown(true, MiniMessage.miniMessage().deserialize( + MiniMessage.miniMessage().serialize( + LegacyComponentSerializer.legacy('&').deserialize(reason) + ) + )); + return Command.SINGLE_SUCCESS; + }) + ).build()); } }