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;
|
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;
|
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;
|
|
|
|
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
|
|
|
import com.mojang.brigadier.suggestion.Suggestions;
|
|
|
|
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
|
|
|
|
|
|
|
import net.minecraft.commands.SharedSuggestionProvider;
|
|
|
|
import net.minecraft.network.chat.TranslatableComponent;
|
2023-04-03 08:58:28 +02:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
public enum BackendArgument implements ArgumentType<Backend> {
|
2022-01-24 19:46:17 +01:00
|
|
|
INSTANCE;
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
private static final List<String> STRING_IDS = Backend.REGISTRY.getAllIds().stream().map(ResourceLocation::toString).toList();
|
|
|
|
|
2021-12-23 06:35:48 +01:00
|
|
|
private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> {
|
2021-12-23 07:29:52 +01:00
|
|
|
// TODO: don't steal lang
|
2021-12-23 06:35:48 +01:00
|
|
|
return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found);
|
|
|
|
});
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
public static BackendArgument getInstance() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2021-12-23 06:35:48 +01:00
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
@Override
|
|
|
|
public Backend parse(StringReader reader) throws CommandSyntaxException {
|
|
|
|
ResourceLocation id = ResourceLocation.read(reader);
|
|
|
|
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) {
|
|
|
|
throw INVALID.createWithContext(reader, id.toString(), STRING_IDS);
|
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-04-03 08:58:28 +02:00
|
|
|
return SharedSuggestionProvider.suggest(STRING_IDS, builder);
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Collection<String> getExamples() {
|
2023-04-03 08:58:28 +02:00
|
|
|
return STRING_IDS;
|
2021-12-23 06:35:48 +01:00
|
|
|
}
|
|
|
|
}
|