Skip to content

Commit

Permalink
fix captions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jul 18, 2024
1 parent 2274144 commit 24fcda7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public final class BukkitCaptionKeys {
*/
public static final Caption ARGUMENT_PARSE_FAILURE_NAMESPACED_KEY_NEED_NAMESPACE =
of("argument.parse.failure.namespacedkey.need_namespace");
/**
* Variables: {@code <input>}, {@code <registry>}
*
* @since 2.0.0
*/
public static final Caption ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING =
of("argument.parse.failure.registry_entry.missing");

private BukkitCaptionKeys() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public final class BukkitDefaultCaptionsProvider<C> extends DelegatingCaptionPro
*/
public static final String ARGUMENT_PARSE_FAILURE_NAMESPACED_KEY_NEED_NAMESPACE =
"Invalid input '<input>', requires an explicit namespace.";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING}
*/
public static final String ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING =
"No such entry '<input>' in '<registry>' registry.";

private static final CaptionProvider<?> PROVIDER = CaptionProvider.constantProvider()
.putCaption(
Expand Down Expand Up @@ -120,6 +125,10 @@ public final class BukkitDefaultCaptionsProvider<C> extends DelegatingCaptionPro
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_NAMESPACED_KEY_NEED_NAMESPACE,
ARGUMENT_PARSE_FAILURE_NAMESPACED_KEY_NEED_NAMESPACE
)
.putCaption(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING,
ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING
)
.build();

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public final class RegistryEntryParser<C, E extends Keyed>
*
* @param registryKey registry key
* @param elementType registry element type
* @param <C> command sender type
* @param <E> registry element type
* @param <C> command sender type
* @param <E> registry element type
* @return the created parser
* @since 2.0.0
*/
Expand Down Expand Up @@ -90,6 +90,7 @@ public RegistryEntryParser(final RegistryKey<E> registryKey) {
this.registryKey = registryKey;
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public @NonNull ArgumentParseResult<RegistryEntry<@NonNull E>> parse(
final @NonNull CommandContext<@NonNull C> commandContext,
Expand All @@ -100,7 +101,7 @@ public RegistryEntryParser(final RegistryKey<E> registryKey) {

final E value = registry.get(key);
if (value == null) {
return ArgumentParseResult.failure(new ParseException(key.asString(), commandContext));
return ArgumentParseResult.failure(new ParseException(key.asString(), (RegistryKey) this.registryKey, commandContext));
}

return ArgumentParseResult.success(RegistryEntryImpl.of(value, key));
Expand All @@ -117,24 +118,29 @@ public RegistryEntryParser(final RegistryKey<E> registryKey) {
*/
public static final class ParseException extends ParserException {
private final String input;
private final RegistryKey<Object> registryKey;

/**
* Creates a new {@link ParseException}.
*
* @param input input string
* @param registryKey registry key
* @param context command context
*/
public ParseException(
final @NonNull String input,
final @NonNull RegistryKey<Object> registryKey,
final @NonNull CommandContext<?> context
) {
super(
RegistryEntryParser.class,
context,
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_WORLD,
CaptionVariable.of("input", input)
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_MISSING,
CaptionVariable.of("input", input),
CaptionVariable.of("registry", registryKey.key().asString())
);
this.input = input;
this.registryKey = registryKey;
}

/**
Expand All @@ -145,6 +151,15 @@ public ParseException(
public @NonNull String input() {
return this.input;
}

/**
* Returns the registry key.
*
* @return registry key
*/
public @NonNull RegistryKey<Object> registryKey() {
return this.registryKey;
}
}

@Override
Expand Down

0 comments on commit 24fcda7

Please sign in to comment.