2021-12-23 06:35:48 +01:00
|
|
|
package com.jozufozu.flywheel.config;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2023-04-03 08:58:28 +02:00
|
|
|
import java.util.List;
|
2023-11-30 05:03:26 +01:00
|
|
|
import java.util.Locale;
|
2021-12-23 06:35:48 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
import com.jozufozu.flywheel.api.backend.Backend;
|
2023-11-26 21:07:05 +01:00
|
|
|
import com.jozufozu.flywheel.lib.util.ResourceUtil;
|
2021-12-23 06:35:48 +01:00
|
|
|
import com.mojang.brigadier.StringReader;
|
|
|
|
import com.mojang.brigadier.arguments.ArgumentType;
|
|
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
2023-04-08 01:01:03 +02:00
|
|
|
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
2021-12-23 06:35:48 +01:00
|
|
|
import com.mojang.brigadier.suggestion.Suggestions;
|
|
|
|
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
|
|
|
|
|
|
|
import net.minecraft.commands.SharedSuggestionProvider;
|
2023-11-23 06:30:58 +01:00
|
|
|
import net.minecraft.network.chat.Component;
|
2023-04-03 08:58:28 +02:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-06 03:03:25 +02:00
|
|
|
public class BackendArgument implements ArgumentType<Backend> {
|
2023-11-30 05:03:26 +01:00
|
|
|
private static final List<String> EXAMPLES = List.of("off", "flywheel:off", "instancing");
|
2023-04-03 08:58:28 +02:00
|
|
|
|
2023-11-30 05:03:26 +01:00
|
|
|
private static final DynamicCommandExceptionType ERROR_UNKNOWN_BACKEND = new DynamicCommandExceptionType(arg -> {
|
2023-11-23 06:30:58 +01:00
|
|
|
return Component.literal("Unknown backend '" + arg + "'");
|
2021-12-23 06:35:48 +01:00
|
|
|
});
|
|
|
|
|
2023-04-06 03:03:25 +02:00
|
|
|
public static final BackendArgument INSTANCE = new BackendArgument();
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
@Override
|
|
|
|
public Backend parse(StringReader reader) throws CommandSyntaxException {
|
2023-11-30 05:03:26 +01:00
|
|
|
ResourceLocation id = ResourceUtil.readFlywheelDefault(reader);
|
2023-04-03 08:58:28 +02:00
|
|
|
Backend backend = Backend.REGISTRY.get(id);
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
if (backend == null) {
|
2023-04-08 01:01:03 +02:00
|
|
|
throw ERROR_UNKNOWN_BACKEND.createWithContext(reader, id.toString());
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
return backend;
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
@Override
|
2021-12-23 06:35:48 +01:00
|
|
|
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
2023-11-30 05:03:26 +01:00
|
|
|
String input = builder.getRemaining().toLowerCase(Locale.ROOT);
|
|
|
|
for (ResourceLocation id : Backend.REGISTRY.getAllIds()) {
|
|
|
|
String idStr = id.toString();
|
|
|
|
if (SharedSuggestionProvider.matchesSubStr(input, idStr) || SharedSuggestionProvider.matchesSubStr(input, id.getPath())) {
|
|
|
|
builder.suggest(idStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.buildFuture();
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Collection<String> getExamples() {
|
2023-11-30 05:03:26 +01:00
|
|
|
return EXAMPLES;
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
}
|