Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ShutdownCommand message styling #1427

Open
wants to merge 3 commits into
base: dev/3.0.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

package com.velocitypowered.proxy.command.builtin;

import com.google.gson.JsonSyntaxException;
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.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

/**
* Shuts down the proxy.
Expand All @@ -52,11 +54,22 @@ public static BrigadierCommand command(final VelocityServer server) {
StringArgumentType.greedyString())
.executes(context -> {
String reason = context.getArgument("reason", String.class);
server.shutdown(true, MiniMessage.miniMessage().deserialize(
MiniMessage.miniMessage().serialize(
LegacyComponentSerializer.legacy('&').deserialize(reason)
)
));
Component reasonComponent = null;

if (reason.startsWith("{") || reason.startsWith("[") || reason.startsWith("\"")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should do it like this?

        char firstChar = reason.charAt(0);
        char lastChar = reason.charAt(reason.length() - 1);

        if ((firstChar == '{' && lastChar == '}')
                || (firstChar == '[' && lastChar == ']')
                || (firstChar == '"' && lastChar == '"')) {
                try {
                    reasonComponent = GsonComponentSerializer.gson()
                            .deserializeOrNull(reason);
                } catch (JsonSyntaxException e) {
                    log.warn("Failed to parse reason as JSON object: {}", reason, e);
                }
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally not fond of printing a warning for that kinda thing, and generally not sure what exactly this would solve vs the current approach

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally not fond of printing a warning for that kinda thing, and generally not sure what exactly this would solve vs the current approach

this will allow server administrators to know the exact error when formatting is incorrect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command accepts json or MiniMessage, the intent is that it will silently fall through to parsing the alt format rather than blowing up

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command accepts json or MiniMessage, the intent is that it will silently fall through to parsing the alt format rather than blowing up

How then to quickly determine where the error occurred? Maybe in that case it is worth trying log.debug?

try {
reasonComponent = GsonComponentSerializer.gson()
.deserializeOrNull(reason);
} catch (JsonSyntaxException ignored) {

}
}

if (reasonComponent == null) {
reasonComponent = MiniMessage.miniMessage().deserialize(reason);
}

server.shutdown(true, reasonComponent);
return Command.SINGLE_SUCCESS;
})
).build());
Expand Down
Loading