![JAR search and dependency download from the Maven repository](/logo.png)
personthecat.catlib.command.arguments.RegistryArgument Maven / Gradle / Ivy
package personthecat.catlib.command.arguments;
import com.google.common.collect.ImmutableList;
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.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import personthecat.catlib.data.Lazy;
import personthecat.catlib.registry.CommonRegistries;
import personthecat.catlib.registry.RegistryHandle;
import personthecat.catlib.util.LibReference;
import personthecat.catlib.registry.RegistryUtils;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.class_2172;
import net.minecraft.class_2316;
import net.minecraft.class_2319;
import net.minecraft.class_2960;
import static personthecat.catlib.exception.Exceptions.cmdSyntax;
public class RegistryArgument implements ArgumentType {
public static void register() {
class_2316.method_10017(LibReference.MOD_ID + ":registry_argument", RegistryArgument.class,
new class_2319<>(() -> new RegistryArgument<>(CommonRegistries.BLOCKS)));
}
private static final Map, RegistryArgument>> ARGUMENTS_BY_TYPE = new ConcurrentHashMap<>();
private final RegistryHandle handle;
private final Lazy> suggestions;
public RegistryArgument(final RegistryHandle handle) {
this.handle = handle;
this.suggestions = Lazy.of(() -> computeSuggestions(handle));
}
@SuppressWarnings("unchecked")
public static RegistryArgument getOrThrow(final Class clazz) {
return (RegistryArgument) ARGUMENTS_BY_TYPE.computeIfAbsent(clazz, c ->
new RegistryArgument<>(RegistryUtils.getByType(c)));
}
@Override
public T parse(final StringReader reader) throws CommandSyntaxException {
final class_2960 id = class_2960.method_12835(reader);
if (!this.handle.isRegistered(id)) {
throw cmdSyntax(reader, "Feature not found");
}
return this.handle.lookup(id);
}
@Override
public CompletableFuture listSuggestions(final CommandContext ctx, final SuggestionsBuilder builder) {
return class_2172.method_9265(this.suggestions.get(), builder);
}
private static List computeSuggestions(final RegistryHandle> handle) {
final ImmutableList.Builder builder = ImmutableList.builder();
handle.forEach((id, value) -> {
if ("minecraft".equals(id.method_12836())) builder.add(id.method_12832());
builder.add(id.toString());
});
return builder.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy