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

feat(minecraft-extras): Allow platforms to override ComponentMessageThrowable conversions #87

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ private static <C> Component getMessage(final ComponentCaptionFormatter<C> forma
if (throwable instanceof ParserException) {
return ((ParserException) throwable).formatCaption(formatter);
}
final Component msg = ComponentMessageThrowable.getOrConvertMessage(throwable);
final Component msg = ComponentMessageThrowable.getOrConvertMessage(
ComponentMessageThrowableConverterHolder.instance.maybeConvert(throwable)
);
return msg == null ? NULL : msg;
}

Expand Down Expand Up @@ -472,4 +474,45 @@ public interface Decorator<C> {
@NonNull Component message
);
}

/**
* Converts Throwables to ComponentMessageThrowables when possible.
*/
@API(status = API.Status.INTERNAL)
@FunctionalInterface
public interface ComponentMessageThrowableConverter {
/**
* Converts a Throwable to a ComponentMessageThrowable if possible.
*
* @param thr throwable
* @return possibly converted throwable
*/
Throwable maybeConvert(Throwable thr);
}

/**
* Default implementation and holder for {@link ComponentMessageThrowableConverter}.
*/
@API(status = API.Status.INTERNAL)
public static final class ComponentMessageThrowableConverterHolder implements ComponentMessageThrowableConverter {
private static ComponentMessageThrowableConverter instance = new ComponentMessageThrowableConverterHolder();

/**
* Replaces the converter. Mainly useful for platforms that need custom logic to transform vanilla CommandSyntaxExceptions
* into an Adventure representation.
*
* @param converter converter
*/
public static void converter(final ComponentMessageThrowableConverter converter) {
instance = Objects.requireNonNull(converter, "converter");
}

private ComponentMessageThrowableConverterHolder() {
}

@Override
public Throwable maybeConvert(final Throwable thr) {
return thr;
}
}
}