Fix Brigadier suggestions for KeyedWorldArgument

This commit is contained in:
Jason Penilla 2021-08-31 13:51:04 -07:00 committed by Jason
parent 4447ab451f
commit b4492e9bc5
3 changed files with 48 additions and 15 deletions

View file

@ -169,6 +169,24 @@ public final class BukkitBrigadierMapper<C> {
public <T extends ArgumentParser<C, ?>> void mapSimpleNMS(
final @NonNull TypeToken<T> type,
final @NonNull String argumentId
) {
this.mapSimpleNMS(type, argumentId, false);
}
/**
* Attempt to register a mapping between a cloud argument parser type and an NMS brigadier argument type which
* has a no-args constructor.
*
* @param type Type to map
* @param <T> argument parser type
* @param argumentId network id of argument type
* @param useCloudSuggestions whether to use cloud suggestions
* @since 1.6.0
*/
public <T extends ArgumentParser<C, ?>> void mapSimpleNMS(
final @NonNull TypeToken<T> type,
final @NonNull String argumentId,
final boolean useCloudSuggestions
) {
final Constructor<?> constructor;
try {
@ -182,21 +200,26 @@ public final class BukkitBrigadierMapper<C> {
);
return;
}
this.brigadierManager.registerMapping(type, builder -> builder.to(argument -> {
try {
return (ArgumentType<?>) constructor.newInstance();
} catch (final ReflectiveOperationException e) {
this.commandManager.getOwningPlugin().getLogger().log(
Level.WARNING,
String.format(
"Failed to create instance of brigadier argument type '%s'.",
GenericTypeReflector.erase(type.getType()).getCanonicalName()
),
e
);
return fallbackType();
this.brigadierManager.registerMapping(type, builder -> {
builder.to(argument -> {
try {
return (ArgumentType<?>) constructor.newInstance();
} catch (final ReflectiveOperationException e) {
this.commandManager.getOwningPlugin().getLogger().log(
Level.WARNING,
String.format(
"Failed to create instance of brigadier argument type '%s'.",
GenericTypeReflector.erase(type.getType()).getCanonicalName()
),
e
);
return fallbackType();
}
});
if (useCloudSuggestions) {
builder.cloudSuggestions();
}
}));
});
}
/**