Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created March 1, 2024 04:53
Show Gist options
  • Select an option

  • Save Densamisten/9cc32453aeaed5b477d450869eadb04d to your computer and use it in GitHub Desktop.

Select an option

Save Densamisten/9cc32453aeaed5b477d450869eadb04d to your computer and use it in GitHub Desktop.
Shows a way to do brigadier suggestions
package io.github.densamisten.troubleshootingmod.command;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class TestCommand {
// Define enum for repositories
public enum Repository {
CORE("Core"),
CORE_TESTING("Core Testing"),
EXTRA("Extra"),
EXTRA_TESTING("Extra Testing"),
MULTILIB("Multilib"),
MULTILIB_TESTING("Multilib Testing");
private final String value;
Repository(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
public TestCommand(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("searchRepository")
.then(Commands.argument("search", StringArgumentType.string())
.then(Commands.argument("repo", StringArgumentType.string())
.suggests((context, builder) -> {
// Provide repository values as suggestions
for (Repository repository : Repository.values()) {
builder.suggest(repository.getValue());
}
return builder.buildFuture();
})
.executes(context -> queryOrwiWithRepo(context,
StringArgumentType.getString(context, "search"),
StringArgumentType.getString(context, "repo")
))
)
)
);
}
private int queryOrwiWithRepo(CommandContext<CommandSourceStack> context, String query, String repo) {
try {
String response = sendGetRequest("https://archlinux.org/packages/search/json/?q=" + query + "&repo=" + repo);
// Parse the JSON response
JsonObject jsonObject = parseResponse(response);
// Build the URL with the query and repo parameters
// Extract and display package names
if (jsonObject.has("results")) {
for (JsonElement element : jsonObject.getAsJsonArray("results")) {
if (element.isJsonObject()) {
JsonObject resultObject = element.getAsJsonObject();
String packageName = resultObject.get("pkgname").getAsString();
String packageDescription = resultObject.get("pkgdesc").getAsString();
String packageRepository = resultObject.get("pkgreo").getAsString();
context.getSource().sendSuccess(() -> Component.literal(packageName).withStyle(ChatFormatting.WHITE), false);
context.getSource().sendSuccess(() -> Component.literal(packageDescription).withStyle(ChatFormatting.GREEN), false);
}
}
}
return 1;
} catch (IOException e) {
throw new RuntimeException(e);
}
// Placeholder return value
}
// Method to send GET request and retrieve response
private String sendGetRequest(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.connect();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} finally {
connection.disconnect();
}
}
// Method to parse JSON response
private JsonObject parseResponse(String response) {
JsonElement jsonResponse = JsonParser.parseString(response);
return jsonResponse.getAsJsonObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment