From c898a2341d20b7957915b401e92f147541abcbf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 22:05:49 +0200 Subject: [PATCH] Implement basic tab-completion --- .../wtf/beatrice/nounspicker/NounsPicker.java | 2 + .../nounspicker/objects/TabCompletion.java | 37 +++++++++++++++++++ .../wtf/beatrice/nounspicker/utils/Cache.java | 4 ++ .../nounspicker/utils/DatabaseManager.java | 8 ++++ 4 files changed, 51 insertions(+) create mode 100644 src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index a79a45f..0ac545b 100644 --- a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java +++ b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java @@ -5,6 +5,7 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import wtf.beatrice.nounspicker.commands.NounsCommand; import wtf.beatrice.nounspicker.objects.PAPIManager; +import wtf.beatrice.nounspicker.objects.TabCompletion; import wtf.beatrice.nounspicker.utils.Cache; import wtf.beatrice.nounspicker.utils.ConsoleLogger; import wtf.beatrice.nounspicker.utils.DatabaseManager; @@ -39,6 +40,7 @@ public class NounsPicker extends JavaPlugin logger.log("Registering commands..."); getCommand("nouns").setExecutor(new NounsCommand(this)); + getCommand("nouns").setTabCompleter(new TabCompletion()); logger.log("Commands registered!"); logger.log("Checking files..."); diff --git a/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java b/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java new file mode 100644 index 0000000..7b64360 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java @@ -0,0 +1,37 @@ +package wtf.beatrice.nounspicker.objects; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.util.StringUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TabCompletion implements org.bukkit.command.TabCompleter +{ + + private final List mainSubCommands = new ArrayList<>(){{ + add("set"); + add("create"); + add("delete"); + add("update"); + }}; + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + List completions = new ArrayList<>(); + + if(args.length == 0) return null; + + String mainArg = args[0].toLowerCase(); + if(args.length == 1) + { + StringUtil.copyPartialMatches(mainArg, mainSubCommands, completions); + } + + + + return completions; + } + +} diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java b/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java index 60b5b18..1cb3a91 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java @@ -3,10 +3,14 @@ package wtf.beatrice.nounspicker.utils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; public class Cache { public static DatabaseManager dbManager; + + public static List pronouns = new ArrayList<>(); } diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index dd3d442..33264f9 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -146,6 +146,11 @@ public class DatabaseManager pStatement.setString(2, format); pStatement.setInt(3, pronounId); pStatement.executeUpdate(); + + // add to cache + Cache.pronouns.add(pronoun); + + // announce success return true; } catch (SQLException e) { @@ -209,6 +214,9 @@ public class DatabaseManager } } + // remove from cache + Cache.pronouns.remove(pronoun); + // announce success return true; }