From 7b3f0941e13aef407d0a46bdf81b719626bbf48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 22:14:42 +0200 Subject: [PATCH] Implement list command and fix caching issue Cache was not initialized correctly by loading pronouns from database at startup. --- .../wtf/beatrice/nounspicker/NounsPicker.java | 2 ++ .../nounspicker/commands/NounsCommand.java | 7 +++--- .../subcommands/NounsListSubCommand.java | 25 +++++++++++++++++++ .../nounspicker/objects/TabCompletion.java | 1 + .../nounspicker/utils/DatabaseManager.java | 19 ++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsListSubCommand.java diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index 0ac545b..0240eb9 100644 --- a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java +++ b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java @@ -60,6 +60,8 @@ public class NounsPicker extends JavaPlugin { logger.log("Database connection initialized!"); Cache.dbManager = dbManager; + Cache.pronouns = dbManager.getAllPronouns(); + logger.log("Database data loaded into memory!"); } else { logger.err("Error initializing database connection!"); } diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java index a8eed16..3dd1b7b 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java @@ -5,10 +5,7 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import wtf.beatrice.nounspicker.NounsPicker; -import wtf.beatrice.nounspicker.commands.subcommands.NounsCreateSubCommand; -import wtf.beatrice.nounspicker.commands.subcommands.NounsDeleteSubCommand; -import wtf.beatrice.nounspicker.commands.subcommands.NounsSetSubCommand; -import wtf.beatrice.nounspicker.commands.subcommands.NounsUpdateSubCmd; +import wtf.beatrice.nounspicker.commands.subcommands.*; public class NounsCommand implements CommandExecutor { @@ -42,6 +39,8 @@ public class NounsCommand implements CommandExecutor return NounsDeleteSubCommand.run(sender, args); case "update": return NounsUpdateSubCmd.run(sender, args); + case "list": + return NounsListSubCommand.run(sender); } diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsListSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsListSubCommand.java new file mode 100644 index 0000000..3a29aa1 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsListSubCommand.java @@ -0,0 +1,25 @@ +package wtf.beatrice.nounspicker.commands.subcommands; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.nounspicker.utils.Cache; + +public class NounsListSubCommand +{ + + // todo: pagination + + public static boolean run(@NotNull CommandSender sender) + { + + sender.sendMessage("Pronouns List:"); + for(int i = 0; i < Cache.pronouns.size(); i++) { + String pronoun = Cache.pronouns.get(i); + int pronounId = Cache.dbManager.getPronounId(pronoun); + String format = Cache.dbManager.getPronounFormat(pronounId); + sender.sendMessage(i+1 + " - " + format); + } + + return true; + } +} diff --git a/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java b/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java index 7b64360..f4e4327 100644 --- a/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java +++ b/src/main/java/wtf/beatrice/nounspicker/objects/TabCompletion.java @@ -12,6 +12,7 @@ public class TabCompletion implements org.bukkit.command.TabCompleter private final List mainSubCommands = new ArrayList<>(){{ add("set"); + add("list"); add("create"); add("delete"); add("update"); diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 33264f9..3e4d9ac 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -312,5 +312,24 @@ public class DatabaseManager return false; } + public List getAllPronouns() { + String query = "SELECT pronoun FROM pronouns;"; + + List allPronouns = new ArrayList<>(); + + try(Statement stmt = dbConnection.createStatement()) { + ResultSet rSet = stmt.executeQuery(query); + + if(rSet.isClosed()) return allPronouns; + + while(rSet.next()) { + allPronouns.add(rSet.getString("pronoun")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return allPronouns; + } }