Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Nov 24, 2024
1 parent 0a4a845 commit 4a81e69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.network.chat.MutableComponent;

import static net.earthcomputer.clientcommands.command.arguments.FormattedComponentArgument.*;
import static net.earthcomputer.clientcommands.command.arguments.ExtendedMarkdownArgument.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;

public class NoteCommand {

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(literal("cnote")
.then(argument("message", formattedComponent())
.executes(ctx -> note(ctx.getSource(), getFormattedComponent(ctx, "message")))));
.then(argument("message", extendedMarkdown())
.executes(ctx -> note(ctx.getSource(), getExtendedMarkdown(ctx, "message")))));
}

private static int note(FabricClientCommandSource source, MutableComponent message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
import java.util.function.Consumer;
import java.util.function.Function;

public class FormattedComponentArgument implements ArgumentType<MutableComponent> {
public class ExtendedMarkdownArgument implements ArgumentType<MutableComponent> {
private static final Collection<String> EXAMPLES = Arrays.asList("Earth", "bold{xpple}", "red{hello blue{world}!}", "*italic*");
private static final SimpleCommandExceptionType TOO_DEEPLY_NESTED = new SimpleCommandExceptionType(Component.translatable("commands.client.componentTooDeeplyNested"));
private static final DynamicCommandExceptionType INVALID_CLICK_ACTION = new DynamicCommandExceptionType(action -> Component.translatable("commands.client.invalidClickAction", action));
private static final DynamicCommandExceptionType INVALID_HOVER_ACTION = new DynamicCommandExceptionType(action -> Component.translatable("commands.client.invalidHoverAction", action));
private static final DynamicCommandExceptionType INVALID_HOVER_EVENT = new DynamicCommandExceptionType(event -> Component.translatable("commands.client.invalidHoverEvent", event));
private static final SimpleCommandExceptionType TOO_DEEPLY_NESTED_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.client.componentTooDeeplyNested"));
private static final DynamicCommandExceptionType INVALID_CLICK_ACTION_EXCEPTION = new DynamicCommandExceptionType(action -> Component.translatable("commands.client.invalidClickAction", action));
private static final DynamicCommandExceptionType INVALID_HOVER_ACTION_EXCEPTION = new DynamicCommandExceptionType(action -> Component.translatable("commands.client.invalidHoverAction", action));
private static final DynamicCommandExceptionType INVALID_HOVER_EVENT_EXCEPTION = new DynamicCommandExceptionType(event -> Component.translatable("commands.client.invalidHoverEvent", event));

private FormattedComponentArgument() {
private ExtendedMarkdownArgument() {
}

public static FormattedComponentArgument formattedComponent() {
return new FormattedComponentArgument();
public static ExtendedMarkdownArgument extendedMarkdown() {
return new ExtendedMarkdownArgument();
}

public static MutableComponent getFormattedComponent(CommandContext<FabricClientCommandSource> context, String arg) {
public static MutableComponent getExtendedMarkdown(CommandContext<FabricClientCommandSource> context, String arg) {
return context.getArgument(arg, MutableComponent.class);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public MutableComponent parse() throws CommandSyntaxException {

private MutableComponent parse(int end, int depth) throws CommandSyntaxException {
if (depth > MAX_NESTING) {
throw TOO_DEEPLY_NESTED.createWithContext(reader);
throw TOO_DEEPLY_NESTED_EXCEPTION.createWithContext(reader);
}

StringBuilder plainText = new StringBuilder();
Expand Down Expand Up @@ -269,7 +269,8 @@ private MutableComponent parse(int end, int depth) throws CommandSyntaxException
components.add(Component.literal(plainText.toString()));
plainText.setLength(0);
}
components.add(linkComponent.withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, linkHref))
components.add(linkComponent.withStyle(style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, linkHref))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(linkHref)))
.withColor(ChatFormatting.BLUE)
.withUnderlined(true)));
Expand Down Expand Up @@ -432,19 +433,19 @@ interface StylerFunc {
private static ClickEvent parseClickEvent(String name, String value) throws CommandSyntaxException {
ClickEvent.Action action = CLICK_EVENT_ACTION_BY_NAME.apply(name);
if (action == null) {
throw INVALID_CLICK_ACTION.create(name);
throw INVALID_CLICK_ACTION_EXCEPTION.create(name);
}
return new ClickEvent(action, value);
}

private static HoverEvent parseHoverEvent(String name, String value) throws CommandSyntaxException {
HoverEvent.Action<?> action = HoverEvent.Action.UNSAFE_CODEC.parse(JsonOps.INSTANCE, new JsonPrimitive(name)).result().orElse(null);
if (action == null) {
throw INVALID_HOVER_ACTION.create(name);
throw INVALID_HOVER_ACTION_EXCEPTION.create(name);
}

JsonElement component = ComponentSerialization.CODEC.encodeStart(JsonOps.INSTANCE, Component.nullToEmpty(value)).getOrThrow();
HoverEvent.TypedHoverEvent<?> eventData = action.legacyCodec.codec().parse(JsonOps.INSTANCE, component).getOrThrow(error -> INVALID_HOVER_EVENT.create(value));
HoverEvent.TypedHoverEvent<?> eventData = action.legacyCodec.codec().parse(JsonOps.INSTANCE, component).getOrThrow(error -> INVALID_HOVER_EVENT_EXCEPTION.create(value));
return new HoverEvent(eventData);
}

Expand Down

0 comments on commit 4a81e69

Please sign in to comment.