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

Use ExtendedMarkdownArgument for /cwe #464

Merged
merged 8 commits into from
Jan 4, 2025
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.earthcomputer.clientcommands.c2c;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
Expand All @@ -12,8 +13,9 @@
import net.earthcomputer.clientcommands.c2c.packets.StartTwoPlayerGameC2CPacket;
import net.earthcomputer.clientcommands.command.ConnectFourCommand;
import net.earthcomputer.clientcommands.command.ListenCommand;
import net.earthcomputer.clientcommands.features.TwoPlayerGame;
import net.earthcomputer.clientcommands.command.TicTacToeCommand;
import net.earthcomputer.clientcommands.command.arguments.ExtendedMarkdownArgument;
import net.earthcomputer.clientcommands.features.TwoPlayerGame;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
Expand Down Expand Up @@ -190,12 +192,18 @@ public static boolean handleC2CPacket(String content, String sender, UUID sender
public void onMessageC2CPacket(MessageC2CPacket packet) {
String sender = packet.sender();
String message = packet.message();
Component formattedComponent;
try {
formattedComponent = ExtendedMarkdownArgument.extendedMarkdown().parse(new StringReader(message));
} catch (CommandSyntaxException e) {
formattedComponent = Component.nullToEmpty(message);
}
MutableComponent prefix = Component.empty();
prefix.append(Component.literal("[").withStyle(ChatFormatting.DARK_GRAY));
prefix.append(Component.literal("/cwe").withStyle(ChatFormatting.AQUA));
prefix.append(Component.literal("]").withStyle(ChatFormatting.DARK_GRAY));
prefix.append(Component.literal(" "));
Component component = prefix.append(Component.translatable("c2cpacket.messageC2CPacket.incoming", sender, message).withStyle(ChatFormatting.GRAY));
Component component = prefix.append(Component.translatable("c2cpacket.messageC2CPacket.incoming", sender, formattedComponent));
Minecraft.getInstance().gui.getChat().addMessage(component);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.earthcomputer.clientcommands.c2c.C2CPacketHandler;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.command.arguments.WithStringArgument;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.ChatFormatting;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;

import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static dev.xpple.clientarguments.arguments.CGameProfileArgument.*;
import static net.earthcomputer.clientcommands.command.arguments.ExtendedMarkdownArgument.*;
import static net.earthcomputer.clientcommands.command.arguments.WithStringArgument.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;

public class WhisperEncryptedCommand {
Expand All @@ -24,24 +26,24 @@ public class WhisperEncryptedCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(literal("cwe")
.then(argument("player", gameProfile(true))
.then(argument("message", greedyString())
.executes((ctx) -> whisper(ctx.getSource(), getSingleProfileArgument(ctx, "player"), getString(ctx, "message"))))));
.then(argument("message", withString(extendedMarkdown()))
.executes((ctx) -> whisper(ctx.getSource(), getSingleProfileArgument(ctx, "player"), getWithString(ctx, "message", MutableComponent.class))))));
}

private static int whisper(FabricClientCommandSource source, GameProfile player, String message) throws CommandSyntaxException {
private static int whisper(FabricClientCommandSource source, GameProfile player, WithStringArgument.Result<MutableComponent> result) throws CommandSyntaxException {
PlayerInfo recipient = source.getClient().getConnection().getPlayerInfo(player.getId());
if (recipient == null) {
throw PLAYER_NOT_FOUND_EXCEPTION.create();
}

MessageC2CPacket packet = new MessageC2CPacket(source.getClient().getConnection().getLocalGameProfile().getName(), message);
MessageC2CPacket packet = new MessageC2CPacket(source.getClient().getConnection().getLocalGameProfile().getName(), result.string());
C2CPacketHandler.getInstance().sendPacket(packet, recipient);
MutableComponent prefix = Component.empty();
prefix.append(Component.literal("[").withStyle(ChatFormatting.DARK_GRAY));
prefix.append(Component.literal("/cwe").withStyle(ChatFormatting.AQUA));
prefix.append(Component.literal("]").withStyle(ChatFormatting.DARK_GRAY));
prefix.append(Component.literal(" "));
Component component = prefix.append(Component.translatable("c2cpacket.messageC2CPacket.outgoing", recipient.getProfile().getName(), message).withStyle(ChatFormatting.GRAY));
Component component = prefix.append(Component.translatable("c2cpacket.messageC2CPacket.outgoing", recipient.getProfile().getName(), result.value()));
source.sendFeedback(component);
return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
Expand Down
Loading