From d1f6eb6b9516b49d56c15d577ac75e2681292047 Mon Sep 17 00:00:00 2001 From: Mighty_Squid <103967@gmail.com> Date: Sun, 4 Aug 2024 15:49:05 +0000 Subject: [PATCH] Update `wiki` command to new URL and add tab-completion (#23) Reviewed-on: https://git.jojodev.com/Minecraft/QoL/pulls/23 Co-authored-by: Mighty_Squid <103967@gmail.com> Co-committed-by: Mighty_Squid <103967@gmail.com> --- pom.xml | 10 +-- .../xyz/etztech/qol/commands/WikiCommand.java | 81 ++++++++++++++----- src/main/resources/config.yml | 2 + 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index e9202bb..1d8a0dd 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xyz.etztech QoL - 1.16 + 1.17 jar @@ -42,7 +42,7 @@ us.dynmap dynmap-api - 1.9.4 + 3.5 provided @@ -53,7 +53,7 @@ com.discordsrv discordsrv - 1.23.0 + 1.27.0 provided @@ -81,7 +81,7 @@ birbmc-repo - https://mvn.birbmc.com + https://mvn.jojodev.com dynmap-repo @@ -105,7 +105,7 @@ shopkeepers-repo - https://nexus.lichtspiele.org/repository/releases/ + https://repo.projectshard.dev/repository/releases/ diff --git a/src/main/java/xyz/etztech/qol/commands/WikiCommand.java b/src/main/java/xyz/etztech/qol/commands/WikiCommand.java index c8d4742..cc00363 100644 --- a/src/main/java/xyz/etztech/qol/commands/WikiCommand.java +++ b/src/main/java/xyz/etztech/qol/commands/WikiCommand.java @@ -1,25 +1,24 @@ package xyz.etztech.qol.commands; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.bukkit.ChatColor; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; +import org.bukkit.command.TabExecutor; import org.bukkit.command.CommandSender; import xyz.etztech.qol.QoL; import xyz.etztech.qol.EtzTechUtil; import xyz.etztech.qol.Lang; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; -import java.util.Date; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; -public class WikiCommand implements CommandExecutor { +public class WikiCommand implements TabExecutor { QoL plugin; public WikiCommand(QoL paramQoL) { @@ -28,7 +27,7 @@ public class WikiCommand implements CommandExecutor { } @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { + public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) { if (!commandSender.hasPermission("qol.wiki")) { EtzTechUtil.sms(commandSender, Lang.NO_PERMISSION.getDef()); return true; @@ -38,23 +37,61 @@ public class WikiCommand implements CommandExecutor { return true; } - try { - Date changedToAt; - JsonObject obj; - InputStream response = new URL(String.format("https://minecraft.gamepedia.com/api.php?action=opensearch&format=json&formatversion=2&search=%s&namespace=0&limit=1", args[0])).openStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(response)); - JsonArray jsonArray = new JsonParser().parse(reader.readLine()).getAsJsonArray(); - StringBuilder message = new StringBuilder(ChatColor.GREEN + jsonArray.get(1).getAsJsonArray().get(0).getAsString()); - message.append(": "); - message.append(ChatColor.WHITE + jsonArray.get(3).getAsJsonArray().get(0).getAsString()); + String query = String.join(" ", args); + List> result = getWikiResults(query, 1); - EtzTechUtil.sms(commandSender, message.toString()); - } catch (IOException e) { - EtzTechUtil.sms(commandSender, ChatColor.RED + "Minecraft wiki API returned nothing."); - } catch (IndexOutOfBoundsException e) { - EtzTechUtil.sms(commandSender, ChatColor.RED + String.format("Nothing was found on the wiki using: %s", args[0])); + if (result.isEmpty()) + { + EtzTechUtil.sms(commandSender, ChatColor.RED + String.format("Nothing was found on the wiki using: %s", query)); + return true; } - + + String message = ChatColor.GREEN + result.get(0).get(0) + ": " + ChatColor.WHITE + result.get(0).get(1); + EtzTechUtil.sms(commandSender, message); + return true; } + + @Override + public List onTabComplete(CommandSender commandSender, Command command, String label, String[] args) { + String query = String.join(" ", args); + + if (query.isEmpty()) return null; + + List> results = getWikiResults(query, 10); + + if (results.isEmpty()) return null; + + List completions = new ArrayList<>(); + results.forEach((result) -> completions.add(result.get(0))); + return completions; + } + + private List> getWikiResults(String query, Integer numOfResults) { + try { + InputStream response = new URL(String.format(plugin.getConfig().getString("wiki-base-url", "https://minecraft.wiki:") + "/api.php?action=opensearch&format=json&formatversion=2&search=%s&namespace=0&limit=%d", query, numOfResults)).openStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(response)); + JsonArray jsonArray = new JsonParser().parse(reader.readLine()).getAsJsonArray(); + + JsonArray names = jsonArray.get(1).getAsJsonArray(); + JsonArray links = jsonArray.get(3).getAsJsonArray(); + + // Check if we got any result + if (names.size() < 1) + return Collections.emptyList(); + + List> results = new ArrayList<>(); + for (int i = 0; i < names.size(); i++) + { + List e = new ArrayList<>(); + e.add(names.get(i).getAsString()); + e.add(links.get(i).getAsString()); + results.add(e); + } + + return results; + } catch(Exception e) { + return Collections.emptyList(); + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6f949ef..1c8f706 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -124,3 +124,5 @@ head_shop: # Diamond price of a head in the shop, default is 2 diamonds price: 2 +# Base URL for the /wiki command. Should support a https://www.mediawiki.org/wiki/API:Opensearch request +wiki-base-url: 'https://minecraft.wiki'