Support signing messages from command arguments (#434)

* Initial signed command argument impl

* Implement on Paper and map to greedy string (unsigned) on velocity

* Use cloud signed arguments

* add relocations

* fabric: include instead of shade cloud-signed-arguments

* Make whispers between players on the same server signed

* Update cloud & loom

---------

Co-authored-by: Draycia <[email protected]>
This commit is contained in:
Jason PenillaandDraycia authored and GitHub committed 2024-05-14 16:13:57 -07:00
1 parent a53d2e3d84
commit a816dcd738
17 files changed
+147 -36

No files matched your search

@@ -0,0 +1,35 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Injection binding annotation for the raw chat type key.
*/
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
public @interface RawChat {
}
@@ -48,6 +48,7 @@ import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent;
import net.draycia.carbon.api.event.events.ChannelSwitchEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.RawChat;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.config.ConfigManager;
@@ -61,6 +62,7 @@ import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.common.util.Exceptions;
import net.draycia.carbon.common.util.FileUtil;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.key.Key;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -69,12 +71,13 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.minecraft.signed.SignedString;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;
import static org.incendo.cloud.minecraft.signed.SignedGreedyStringParser.signedGreedyStringParser;
@Singleton
@DefaultQualifier(NonNull.class)
@@ -87,6 +90,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
private @MonotonicNonNull Key defaultKey;
private final CarbonMessages carbonMessages;
private final CarbonEventHandler eventHandler;
private final Key rawChatKey;
private final Map<String, SpecialHandler<?>> handlers = new HashMap<>();
private record SpecialHandler<T extends ConfigChatChannel>(Class<T> cls, Supplier<T> defaultSupplier) {}
@@ -111,7 +115,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final Logger logger,
final ConfigManager config,
final CarbonMessages carbonMessages,
final CarbonEventHandler events
final CarbonEventHandler events,
@RawChat final Key rawChatKey
) {
super(events, carbonMessages, config);
this.configChannelDir = dataDirectory.resolve("channels");
@@ -120,6 +125,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.config = config;
this.carbonMessages = carbonMessages;
this.eventHandler = events;
this.rawChatKey = rawChatKey;
if (config.primaryConfig().partyChat().enabled) {
this.registerSpecialConfigChannel(PartyChatChannel.FILE_NAME, PartyChatChannel.class);
@@ -327,22 +333,22 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final ChatChannel channel,
final String plainMessage
) {
this.sendMessageInChannel(new ConsoleCarbonPlayer(sender), channel, plainMessage);
this.sendMessageInChannel(new ConsoleCarbonPlayer(sender), channel, SignedString.unsigned(plainMessage));
}
private void sendMessageInChannel(
final CarbonPlayer sender,
final ChatChannel channel,
final String plainMessage
final SignedString message
) {
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, message.string(), message.signedMessage(), channel);
if (chatEvent == null || chatEvent.cancelled()) {
return;
}
for (final Audience recipient : chatEvent.recipients()) {
recipient.sendMessage(chatEvent.renderFor(recipient));
message.sendMessage(recipient, ChatType.chatType(this.rawChatKey), chatEvent.renderFor(recipient));
}
}
@@ -355,7 +361,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
Command.Builder<Commander> builder = commandManager.commandBuilder(channel.commandName(),
channel.commandAliases(), commandManager.createDefaultCommandMeta())
.optional("message", greedyStringParser());
.optional("message", signedGreedyStringParser());
if (channel.permission() != null) {
builder = builder.permission(channel.permission());
@@ -377,10 +383,10 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
if (!(commander instanceof PlayerCommander playerCommander)) {
if (chatChannel != null && handler.contains("message")) {
final String message = handler.get("message");
final SignedString message = handler.get("message");
// TODO: trigger platform events related to chat
this.sendMessageInChannelAsConsole(commander, chatChannel, message);
this.sendMessageInChannelAsConsole(commander, chatChannel, message.string());
}
return;
@@ -397,7 +403,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.carbonMessages.channelJoined(player);
}
if (handler.contains("message")) {
final String message = handler.get("message");
final SignedString message = handler.get("message");
// TODO: trigger platform events related to chat
this.sendMessageInChannel(player, chatChannel, message);
@@ -102,4 +102,5 @@ public final class CarbonPlayerParser implements ArgumentParser.FutureArgumentPa
}
}
}
@@ -34,9 +34,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.minecraft.signed.SignedString;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;
import static org.incendo.cloud.minecraft.signed.SignedGreedyStringParser.signedGreedyStringParser;
@DefaultQualifier(NonNull.class)
public final class ContinueCommand extends CarbonCommand {
@@ -72,7 +73,7 @@ public final class ContinueCommand extends CarbonCommand {
@Override
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.required("message", greedyStringParser(), richDescription(this.messages.commandContinueArgumentMessage()))
.required("message", signedGreedyStringParser(), richDescription(this.messages.commandContinueArgumentMessage()))
.permission("carbon.whisper.continue")
.senderType(PlayerCommander.class)
.commandDescription(richDescription(this.messages.commandContinueDescription()))
@@ -84,7 +85,7 @@ public final class ContinueCommand extends CarbonCommand {
return;
}
final String message = ctx.get("message");
final SignedString message = ctx.get("message");
final @Nullable UUID whisperTarget = sender.lastWhisperTarget();
if (whisperTarget == null) {
@@ -34,9 +34,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.minecraft.signed.SignedString;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;
import static org.incendo.cloud.minecraft.signed.SignedGreedyStringParser.signedGreedyStringParser;
@DefaultQualifier(NonNull.class)
public final class ReplyCommand extends CarbonCommand {
@@ -72,7 +73,7 @@ public final class ReplyCommand extends CarbonCommand {
@Override
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.required("message", greedyStringParser(), richDescription(this.messages.commandReplyArgumentMessage()))
.required("message", signedGreedyStringParser(), richDescription(this.messages.commandReplyArgumentMessage()))
.permission("carbon.whisper.reply")
.senderType(PlayerCommander.class)
.commandDescription(richDescription(this.messages.commandReplyDescription()))
@@ -84,7 +85,7 @@ public final class ReplyCommand extends CarbonCommand {
return;
}
final String message = ctx.get("message");
final SignedString message = ctx.get("message");
final @Nullable UUID replyTarget = sender.whisperReplyTarget();
if (replyTarget == null) {
@@ -26,6 +26,7 @@ import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.RawChat;
import net.draycia.carbon.common.command.CarbonCommand;
import net.draycia.carbon.common.command.CommandSettings;
import net.draycia.carbon.common.command.Commander;
@@ -41,6 +42,7 @@ import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.messaging.packets.WhisperPacket;
import net.draycia.carbon.common.users.NetworkUsers;
import net.draycia.carbon.common.util.CloudUtils;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
@@ -49,9 +51,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.minecraft.signed.SignedString;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;
import static org.incendo.cloud.minecraft.signed.SignedGreedyStringParser.signedGreedyStringParser;
@DefaultQualifier(NonNull.class)
public final class WhisperCommand extends CarbonCommand {
@@ -88,7 +91,7 @@ public final class WhisperCommand extends CarbonCommand {
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.required("player", this.parserFactory.carbonPlayer(), richDescription(this.carbonMessages.commandWhisperArgumentPlayer()))
.required("message", greedyStringParser(), richDescription(this.carbonMessages.commandWhisperArgumentMessage()))
.required("message", signedGreedyStringParser(), richDescription(this.carbonMessages.commandWhisperArgumentMessage()))
.permission("carbon.whisper.message")
.senderType(PlayerCommander.class)
.commandDescription(richDescription(this.carbonMessages.commandWhisperDescription()))
@@ -100,7 +103,7 @@ public final class WhisperCommand extends CarbonCommand {
return;
}
final String message = ctx.get("message");
final SignedString message = ctx.get("message");
final CarbonPlayer recipient = ctx.get("player");
this.whisper.whisper(sender, recipient, message, ctx.parsingContext("player").consumedInput());
@@ -121,6 +124,7 @@ public final class WhisperCommand extends CarbonCommand {
private final CarbonServer server;
private final CarbonEventHandler events;
private final NetworkUsers network;
private final Key rawChatKey;
@Inject
private WhisperHandler(
@@ -132,7 +136,8 @@ public final class WhisperCommand extends CarbonCommand {
final UserManager<?> userManager,
final CarbonServer server,
final CarbonEventHandler events,
final NetworkUsers network
final NetworkUsers network,
@RawChat final Key rawChatKey
) {
this.logger = logger;
this.messages = messages;
@@ -143,12 +148,13 @@ public final class WhisperCommand extends CarbonCommand {
this.server = server;
this.events = events;
this.network = network;
this.rawChatKey = rawChatKey;
}
public void whisper(
final CarbonPlayer sender,
final CarbonPlayer recipient,
final String message
final SignedString message
) {
this.whisper(sender, recipient, message, null);
}
@@ -156,7 +162,7 @@ public final class WhisperCommand extends CarbonCommand {
public void whisper(
final CarbonPlayer sender,
final CarbonPlayer recipient,
final String message,
final SignedString message,
final @Nullable String recipientInputString
) {
if (sender.equals(recipient)) {
@@ -199,7 +205,7 @@ public final class WhisperCommand extends CarbonCommand {
final Component senderDisplayName = sender.displayName();
final Component recipientDisplayName = recipient.displayName();
final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message));
final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message.string()));
this.events.emit(privateChatEvent);
if (privateChatEvent.cancelled()) {
@@ -208,9 +214,17 @@ public final class WhisperCommand extends CarbonCommand {
}
final String senderUsername = sender.username();
this.messages.whisperSender(SourcedAudience.of(sender, sender), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, privateChatEvent.message());
message.sendMessage(
sender,
ChatType.chatType(this.rawChatKey),
this.messages.whisperSender(SourcedAudience.of(sender, sender), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, privateChatEvent.message())
);
if (localRecipient) {
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, privateChatEvent.message());
message.sendMessage(
recipient,
ChatType.chatType(this.rawChatKey),
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, privateChatEvent.message())
);
}
this.messages.whisperConsoleLog(this.server.console(), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, privateChatEvent.message());
@@ -243,7 +257,9 @@ public final class WhisperCommand extends CarbonCommand {
final Component recipientDisplayName = recipient.displayName();
recipient.whisperReplyTarget(sender.uuid());
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, packet.message());
SourcedAudience.of(sender, recipient).sendMessage(
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, packet.message())
);
this.messages.whisperConsoleLog(this.server.console(), senderUsername, senderDisplayName, recipientUsername, recipientDisplayName, packet.message());
final @Nullable Sound messageSound = this.configManager.primaryConfig().messageSound();
if (messageSound != null) {
@@ -108,8 +108,8 @@ public interface CarbonMessages {
*/
@Message("whisper.to")
void whisperSender(
SourcedAudience audience,
Component whisperSender(
@NotPlaceholder SourcedAudience audience,
String senderUsername,
Component senderDisplayName,
String recipientUsername,
@@ -118,8 +118,8 @@ public interface CarbonMessages {
);
@Message("whisper.from")
void whisperRecipient(
SourcedAudience audience,
Component whisperRecipient(
@NotPlaceholder SourcedAudience audience,
String senderUsername,
Component senderDisplayName,
String recipientUsername,
@@ -0,0 +1,32 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.messages;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target({ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotPlaceholder {
}
@@ -81,7 +81,7 @@ public final class StandardPlaceholderResolverStrategyButDifferent<R, I, F> impl
final Parameter parameter = methodParameters[idx];
final @Nullable Object value = parameters[idx];
// Don't resolve Audiences for now
if (value == null || parameter.getType() == Audience.class) {
if (value == null || parameter.getType() == Audience.class || parameter.getAnnotation(NotPlaceholder.class) != null) {
// Nothing to resolve with.
continue;
}