mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
nickname command for real this time
This commit is contained in:
1 parent
c5dc95eba3
commit
c63ba3651d
4 files changed
+196
-20
No files matched your search
+116
@@ -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<C> implements ArgumentParser<C, String> {
|
||||
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 + "'";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+47
-17
@@ -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();
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
}
|
||||
@@ -11,5 +11,9 @@ reply.target.offline=<red>That person is offline.
|
||||
reply.target.self=<red>You cannot whisper to yourself.
|
||||
continue.target.missing=<red>You have no one to whisper.
|
||||
continue.target.offline=<red>That person is offline.
|
||||
nickname.set=<green>Your nickname has been set to </green><nickname>
|
||||
nickname.set.others=<green>You set </green><target><green>'s nickname to </green> <nickname>
|
||||
nickname.set=<green>Your nickname has been set to </green><nickname><green>.</green>
|
||||
nickname.set.others=<green>You set </green><target><green>'s nickname to </green> <nickname><green>.</green>
|
||||
nickname.show.others=<target><green>'s nickname is </green><nickname><green>.</green>
|
||||
nickname.show.others.unset=<target><red> does not have a nickname set.</red>
|
||||
nickname.show=<green>Your nickname is </green><nickname><green>.</green>
|
||||
nickname.show.unset=<red>You do not have a nickname set.
|
||||
Reference in new issue
Block a user