From c63ba3651d34f91c2b524eff935240cb861302d3 Mon Sep 17 00:00:00 2001 From: Draycia Date: Sat, 25 Sep 2021 22:07:47 -0700 Subject: [PATCH] nickname command for real this time --- .../command/arguments/OptionValueParser.java | 116 ++++++++++++++++++ .../command/commands/NicknameCommand.java | 64 +++++++--- .../common/messages/CarbonMessageService.java | 28 ++++- .../locale/messages-en_US.properties | 8 +- 4 files changed, 196 insertions(+), 20 deletions(-) create mode 100644 common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java diff --git a/common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java b/common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java new file mode 100644 index 00000000..0ce175da --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java @@ -0,0 +1,116 @@ +/* + * PermissionsEx + * Copyright (C) zml and PermissionsEx contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.draycia.carbon.common.command.arguments; + +import cloud.commandframework.arguments.parser.ArgumentParseResult; +import cloud.commandframework.arguments.parser.ArgumentParser; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.exceptions.parsing.NoInputProvidedException; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Queue; + +public final class OptionValueParser implements ArgumentParser { + private static final char ESCAPE = '\\'; + private static final String FLAG_STARTER = "-"; + + public OptionValueParser() { + } + + @Override + public @NonNull ArgumentParseResult<@NonNull String> parse( + @NonNull final CommandContext<@NonNull C> commandContext, + @NonNull final Queue<@NonNull String> inputQueue + ) { + final @Nullable String input = inputQueue.peek(); + if (input == null || input.startsWith(FLAG_STARTER)) { + return ArgumentParseResult.failure(new NoInputProvidedException( + OptionValueParser.class, + commandContext + )); + } + + final char startChar = input.charAt(0); + + // If quoted string: read until a word ending with a quote + if (startChar == '\'' || startChar == '\"') { + final StringBuilder result = new StringBuilder(); + result.append(inputQueue.remove(), 1, input.length()); + @Nullable String next; + while ((next = inputQueue.peek()) != null) { + result.append(" "); + if (next.length() > 0 && next.charAt(next.length() - 1) == startChar) { + // We've found the end of a quoted string, maybe + // if escaped, append without escape then continue + if (next.charAt(next.length() - 1) == ESCAPE) { + result.append(inputQueue.remove(), 0, next.length() - 2) + .append(startChar); + continue; + } else { + result.append(inputQueue.remove(), 0, next.length() - 1); + // then return our full quoted string + return ArgumentParseResult.success(result.toString()); + } + } + result.append(inputQueue.remove()); + } + + // If we made it to the end without finding an end quote, throw an error + return ArgumentParseResult.failure(new MissingEndQuoteException(input)); + } else { // otherwise, read until end of line, or a word starting with '-' + final StringBuilder result = new StringBuilder(); + result.append(inputQueue.remove()); + @Nullable String next; + while ((next = inputQueue.peek()) != null) { + if (next.startsWith(FLAG_STARTER)) { + break; + } + result.append(" ").append(inputQueue.remove()); + } + + return ArgumentParseResult.success(result.toString()); + } + } + + @Override + public boolean isContextFree() { + return true; + } + + public static final class MissingEndQuoteException extends RuntimeException { + + private static final long serialVersionUID = 2969516711506622423L; + private final String input; + + public MissingEndQuoteException( + final @NonNull String input + ) { + this.input = input; + } + + public @NonNull String input() { + return this.input; + } + + @Override + public String getMessage() { + return "Missing end quote for input: '" + this.input + "'"; + } + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java index de9e7e81..c0a1976e 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java @@ -8,8 +8,8 @@ import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; +import net.draycia.carbon.common.command.arguments.OptionValueParser; import net.draycia.carbon.common.messages.CarbonMessageService; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; public class NicknameCommand { @@ -22,33 +22,63 @@ public class NicknameCommand { ) { var command = commandManager.commandBuilder("nickname", "nick") .argument(StringArgument.greedy("nickname")) + //.flag(commandManager.flagBuilder("duration") + // .withAliases("d") + // .withArgument() + // .withPermission(Permission.of("carbon.nickname.duration")) + //) .flag(commandManager.flagBuilder("player") - .withAliases("p", "target", "t") + .withAliases("p") .withArgument(carbonPlayerArgument.newInstance(true, "recipient")) .withPermission(Permission.of("carbon.nickname.others")) ) + .flag(commandManager.flagBuilder("nickname") + .withAliases("n") + .withArgument(StringArgument.newBuilder("nickname").greedy().withParser(new OptionValueParser<>())) + .withPermission(Permission.of("carbon.nickname.set")) + ) .permission("carbon.nickname.self") .senderType(PlayerCommander.class) .handler(handler -> { CarbonPlayer sender = ((PlayerCommander)handler.getSender()).carbonPlayer(); - final var nickname = MiniMessage.get().parse(handler.get("nickname")); + long expirationTime = -1; // TODO: implement timed nicknames - if (handler.contains("player")) { - final CarbonPlayer target = handler.get("player"); - target.displayName(nickname); - messageService.nicknameSet(target, nickname); - - if (target.hasCustomDisplayName()) { - messageService.nicknameSetOthers(sender, target.displayName(), nickname); - } else { - messageService.nicknameSetOthers(sender, Component.text(target.username()), nickname); - } - - return; + if (handler.contains("duration")) { + expirationTime = handler.get("duration"); } - sender.displayName(nickname); - messageService.nicknameSet(sender, nickname); + // Setting nickname + if (handler.contains("nickname")) { + final var nickname = MiniMessage.get().parse(handler.get("nickname")); + + // Setting other player's nickname + if (handler.contains("player")) { + final CarbonPlayer target = handler.get("player"); + target.displayName(nickname); + messageService.nicknameSet(target, nickname); + messageService.nicknameSetOthers(sender, target.username(), nickname); + } else { + // Setting own nickname + sender.displayName(nickname); + messageService.nicknameSet(sender, nickname); + } + } else if (handler.contains("player")) { + // Checking other player's nickname + final CarbonPlayer target = handler.get("player"); + + if (target.displayName() != null) { + messageService.nicknameShowOthers(sender, target.username(), target.displayName()); + } else { + messageService.nicknameShowOthersUnset(sender, target.username()); + } + } else { + // Checking own nickname + if (sender.displayName() != null) { + messageService.nicknameShow(sender, sender.username(), sender.displayName()); + } else { + messageService.nicknameShowUnset(sender, sender.username()); + } + } }) .build(); diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java index 83a74933..eaa8b2d5 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java @@ -97,8 +97,34 @@ public interface CarbonMessageService { @Message("nickname.set.others") void nicknameSetOthers( final Audience audience, - @Placeholder Component target, + @Placeholder String target, @Placeholder Component nickname ); + @Message("nickname.show.others") + void nicknameShowOthers( + final Audience audience, + @Placeholder String target, + @Placeholder Component nickname + ); + + @Message("nickname.show.others.unset") + void nicknameShowOthersUnset( + final Audience audience, + @Placeholder String target + ); + + @Message("nickname.show") + void nicknameShow( + final Audience audience, + @Placeholder String target, + @Placeholder Component nickname + ); + + @Message("nickname.show.unset") + void nicknameShowUnset( + final Audience audience, + @Placeholder String target + ); + } diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 57582677..2bdef74f 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -11,5 +11,9 @@ reply.target.offline=That person is offline. reply.target.self=You cannot whisper to yourself. continue.target.missing=You have no one to whisper. continue.target.offline=That person is offline. -nickname.set=Your nickname has been set to -nickname.set.others=You set 's nickname to +nickname.set=Your nickname has been set to . +nickname.set.others=You set 's nickname to . +nickname.show.others='s nickname is . +nickname.show.others.unset= does not have a nickname set. +nickname.show=Your nickname is . +nickname.show.unset=You do not have a nickname set.