personthecat.catlib.command.CommandSuggestions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of catlib-quilt Show documentation
Show all versions of catlib-quilt Show documentation
Utilities for serialization, commands, noise generation, IO, and some new data types.
The newest version!
package personthecat.catlib.command;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import personthecat.catlib.command.arguments.JsonArgument;
import personthecat.catlib.command.arguments.PathArgument;
import personthecat.catlib.serialization.json.JsonPath;
import personthecat.catlib.serialization.json.XjsUtils;
import personthecat.catlib.util.LibReference;
import xjs.core.JsonValue;
import java.util.concurrent.CompletableFuture;
import net.minecraft.class_2168;
import net.minecraft.class_2172;
import net.minecraft.class_2321;
import net.minecraft.class_2960;
public class CommandSuggestions {
/** A suggestion provider suggesting that any value is acceptable. */
public static final SuggestionProvider ANY_VALUE =
register("any_suggestion", (ctx, builder) -> suggestAny(builder));
/** A suggestion provider suggesting that any integer is acceptable. */
public static final SuggestionProvider ANY_INT =
register("integer_suggestion", "[]", "-1", "0", "1");
/** A suggestion provider suggesting that any decimal is acceptable. */
public static final SuggestionProvider ANY_DECIMAL =
register("decimal_suggestion", "[]", "0.5", "1.0", "1.5");
/**
* A suggestion provider suggesting either the current JSON value or any.
*
* Note that this provider requires two previous arguments:
*
* - A {@link JsonArgument} named
file
* - A {@link PathArgument} named
path
*
* If the current value is missing, "any value" will be suggested.
*/
public static final SuggestionProvider CURRENT_JSON =
register("current_json_suggestion", (ctx, builder) -> {
final JsonArgument.Result json = CommandUtils.getLastArg(ctx, JsonArgument.class, JsonArgument.Result.class).orElse(null);
if (json == null) return suggestAny(builder);
final JsonPath path = CommandUtils.getLastArg(ctx, PathArgument.class, JsonPath.class).orElse(null);
if (path == null) return suggestAny(builder);
final JsonValue value = XjsUtils.getValueFromPath(json.json.get(), path).orElse(null);
if (value == null) return suggestAny(builder);
if (value.isObject()) return class_2172.method_9253(new String[] { "{ ... }" }, builder);
if (value.isArray()) return class_2172.method_9253(new String[] { "[ ... ]" }, builder);
return class_2172.method_9253(new String[] { value.toString() }, builder);
});
private static CompletableFuture suggestAny(final SuggestionsBuilder builder) {
return class_2172.method_9253(new String[] { "[]" }, builder);
}
private static SuggestionProvider register(final String name, final String... suggestions) {
return register(name, (ctx, builder) -> class_2172.method_9253(suggestions, builder));
}
private static SuggestionProvider register(final String name, final SuggestionProvider provider) {
return class_2321.method_10022(new class_2960(LibReference.MOD_ID, name), provider);
}
}