Skip to content

Instantly share code, notes, and snippets.

@Ensamisten
Last active February 18, 2024 05:04
Show Gist options
  • Save Ensamisten/f5c9de7be616a7af4475d77a6ae2bb82 to your computer and use it in GitHub Desktop.
Save Ensamisten/f5c9de7be616a7af4475d77a6ae2bb82 to your computer and use it in GitHub Desktop.
AurCommand. Usage: /aur search <package> <value>
package io.github.ensamisten.forgient.client.command;
import com.google.gson.Gson;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Objects;
/**
* Usage: /aur search <package> <value> (Where <package> is a query and <value> is ...
* AurCommand. Search packages within Arch User Repository
* License: Free for all use.
**/
public class AurCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("aur")
.then(Commands.literal("search")
.then(Commands.argument("package", StringArgumentType.string())
.then(Commands.argument("value", StringArgumentType.string())
.executes(context -> (int) searchAur(context,
StringArgumentType.getString(context, "value"),
StringArgumentType.getString(context, "package")))))));
}
public static class SearchResult {
private int resultcount;
private Result[] results;
private String type;
private int version;
@Override
public String toString() {
return "SearchResult{" +
"resultcount=" + resultcount +
", results=" + (results == null ? null : Arrays.asList(results)) +
", type='" + type + '\'' +
", version=" + version +
'}';
}
}
public static class Result {
private String Description;
private long FirstSubmitted;
private int ID;
private long LastModified;
private String Maintainer;
private String Name;
private int NumVotes;
private String OutOfDate;
private String PackageBase;
private int PackageBaseID;
private String Popularity;
private String URL;
private String URLPath;
private String Version;
public int getPopularity() {
// Parse the Popularity field into an integer
return Integer.parseInt(Popularity);
}
@Override
public String toString() {
return "Result{" +
"Description='" + Description + '\'' +
", FirstSubmitted=" + FirstSubmitted +
", ID=" + ID +
", LastModified=" + LastModified +
", Maintainer='" + Maintainer + '\'' +
", Name='" + Name + '\'' +
", NumVotes=" + NumVotes +
", OutOfDate='" + OutOfDate + '\'' +
", PackageBase='" + PackageBase + '\'' +
", PackageBaseID=" + PackageBaseID +
", Popularity=" + Popularity +
", URL='" + URL + '\'' +
", URLPath='" + URLPath + '\'' +
", Version='" + Version + '\'' +
'}';
}
}
private static String sendGetRequest(String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
} else {
throw new IOException("HTTP GET request failed with response code " + responseCode);
}
}
public static float searchAur(CommandContext<CommandSourceStack> context, String packageInfo, String aPackage) {
String packageName = StringArgumentType.getString(context, "package");
try {
String API_URL = "https://aur.archlinux.org/rpc/?v=5&type=search&arg=" + packageName;
String response = sendGetRequest(API_URL);
Gson gson = new Gson();
SearchResult searchResult = gson.fromJson(response, SearchResult.class);
System.out.println(searchResult);
if (searchResult.results.length == 0) {
context.getSource().sendFailure(Component.literal("No results found for package: " + packageName));
return 0;
}
ServerPlayer player = context.getSource().getPlayerOrException();
ItemStack heldItem = player.getMainHandItem();
if (heldItem.getItem() == Items.WRITABLE_BOOK) {
ListTag pages = new ListTag();
StringBuilder currentPage = new StringBuilder();
String currentType = null;
// Iterate through the results and display the chosen field
for (Result result : searchResult.results) {
String firstSubmitted = "First Submitted:\n" + result.FirstSubmitted;
String description = "Description:\n" + result.Description;
String name = "Name:\n" + result.Name;
String id = "ID:\n" + result.ID;
String lastModified = "Last Modified:\n" + result.LastModified;
String maintainer = "Maintainer:\n" + result.Maintainer;
String numVotes = "Num Votes:\n" + result.NumVotes;
String outOfDate = "Out of Date:\n" + result.OutOfDate;
String packageBase = "Package Base:\n" + result.PackageBase;
String packageBaseID = "Package Base ID:\n" + result.PackageBaseID;
String popularity = "Popularity:\n" + result.Popularity;
String url = "URL:\n" + result.URL;
String urlPath = "URL Path:\n" + result.URLPath;
String version = "Version:\n" + result.Version;
String fullResult = "Full Result:\n" + result.toString(); // If you want to include all fields in a single string
// Check if the type has changed
if (!Objects.equals(currentType, result.Name)) {
// If it has, start a new page
if (!currentPage.isEmpty()) {
pages.add(StringTag.valueOf(currentPage.toString()));
currentPage = new StringBuilder();
}
// Update the current type
currentType = result.Name;
}
// Format the result into a string that can be added to the book
String formattedResult;
// Format the result into a string that can be added to the book
switch (packageInfo.toLowerCase()) {
case "firstsubmitted":
formattedResult = String.format("%-7s%s", "", firstSubmitted.trim());
break;
case "id":
formattedResult = String.format("%-7s%s", "", id.trim());
break;
case "name":
formattedResult = String.format("%-7s%s", "", name.trim());
break;
case "version":
formattedResult = String.format("%-7s%s", "", version.trim());
break;
case "description":
formattedResult = String.format("%-7s%s", "", description.trim());
break;
case "lastmodified":
formattedResult = String.format("%-7s%s", "", lastModified.trim());
break;
case "maintainer":
formattedResult = String.format("%-7s%s", "", maintainer.trim());
break;
case "numvotes":
formattedResult = String.format("%-7s%s", "", numVotes.trim());
break;
case "outofdate":
formattedResult = String.format("%-7s%s", "", outOfDate.trim());
break;
case "packagebase":
formattedResult = String.format("%-7s%s", "", packageBase.trim());
break;
case "packagebaseid":
formattedResult = String.format("%-7s%s", "", packageBaseID.trim());
break;
case "popularity":
formattedResult = String.format("%-7s%s", "", popularity.trim());
break;
case "url":
formattedResult = String.format("%-7s%s", "", url.trim());
break;
case "urlpath":
formattedResult = String.format("%-7s%s", "", urlPath.trim());
break;
default:
formattedResult = "Invalid field";
context.getSource().sendFailure(Component.literal("Invalid field specified: " + packageInfo));
break;
}
// Check if adding the formatted result to the current page exceeds the character limit
if (currentPage.length() + formattedResult.length() > 798) {
// If it exceeds, start a new page
pages.add(StringTag.valueOf(currentPage.toString()));
currentPage = new StringBuilder();
}
// Add the formatted result to the current page
currentPage.append(formattedResult);
}
// Assuming this part of the code is outside the loop, add the last page if it's not empty
if (!currentPage.isEmpty()) {
pages.add(StringTag.valueOf(currentPage.toString()));
}
// Assuming this part of the code is outside the loop, add the pages to the book
CompoundTag bookTag = heldItem.getOrCreateTag();
bookTag.put("pages", pages);
context.getSource().sendSuccess(() -> Component.literal("Text written to the book!"), true);} else {
context.getSource().sendFailure(Component.literal("You need to hold a writable book in your hand!"));
return 0;
}
} catch (IOException | CommandSyntaxException e) {
context.getSource().sendFailure(Component.literal(""));
}
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment