Skip to content

Commit

Permalink
add class javadoc and reorder inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jul 19, 2024
1 parent 24fcda7 commit a8994ce
Showing 1 changed file with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;

/**
* Parser for {@link Registry} {@link RegistryEntry entries}.
*
* @param <C> command sender type
* @param <E> registry element type
*/
@API(status = API.Status.EXPERIMENTAL)
public final class RegistryEntryParser<C, E extends Keyed>
implements ArgumentParser<C, RegistryEntryParser.RegistryEntry<E>>, SuggestionProvider<C>,
Expand Down Expand Up @@ -113,6 +119,47 @@ public RegistryEntryParser(final RegistryKey<E> registryKey) {
return this.keyParser.parser();
}

@Override
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput input
) {
final List<Suggestion> completions = new ArrayList<>();
final Registry<E> registry = RegistryAccess.registryAccess().getRegistry(this.registryKey);
registry.stream()
.map(registry::getKeyOrThrow)
.forEach(key -> {
if (input.hasRemainingInput() && key.getNamespace().equals(NamespacedKey.MINECRAFT_NAMESPACE)) {
completions.add(Suggestion.suggestion(key.getKey()));
}
completions.add(Suggestion.suggestion(key.getNamespace() + ':' + key.getKey()));
});
return CompletableFuture.completedFuture(completions);
}

/**
* Holds a registry value and it's key.
*
* @param <E> value type
*/
@ImmutableImpl
@Value.Immutable
public interface RegistryEntry<E> {
/**
* Returns the value.
*
* @return the value
*/
E value();

/**
* Returns the key.
*
* @return the key
*/
NamespacedKey key();
}

/**
* Exception when there is no registry entry for the provided key.
*/
Expand All @@ -123,9 +170,9 @@ public static final class ParseException extends ParserException {
/**
* Creates a new {@link ParseException}.
*
* @param input input string
* @param input input string
* @param registryKey registry key
* @param context command context
* @param context command context
*/
public ParseException(
final @NonNull String input,
Expand Down Expand Up @@ -161,45 +208,4 @@ public ParseException(
return this.registryKey;
}
}

@Override
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput input
) {
final List<Suggestion> completions = new ArrayList<>();
final Registry<E> registry = RegistryAccess.registryAccess().getRegistry(this.registryKey);
registry.stream()
.map(registry::getKeyOrThrow)
.forEach(key -> {
if (input.hasRemainingInput() && key.getNamespace().equals(NamespacedKey.MINECRAFT_NAMESPACE)) {
completions.add(Suggestion.suggestion(key.getKey()));
}
completions.add(Suggestion.suggestion(key.getNamespace() + ':' + key.getKey()));
});
return CompletableFuture.completedFuture(completions);
}

/**
* Holds a registry value and it's key.
*
* @param <E> value type
*/
@ImmutableImpl
@Value.Immutable
public interface RegistryEntry<E> {
/**
* Returns the value.
*
* @return the value
*/
E value();

/**
* Returns the key.
*
* @return the key
*/
NamespacedKey key();
}
}

0 comments on commit a8994ce

Please sign in to comment.