Implement list command and fix caching issue
All checks were successful
continuous-integration/drone/push Build is passing

Cache was not initialized correctly by loading pronouns from database at startup.
This commit is contained in:
Bea 2022-10-24 22:14:42 +02:00
parent c898a2341d
commit 7b3f0941e1
5 changed files with 50 additions and 4 deletions

View File

@ -60,6 +60,8 @@ public class NounsPicker extends JavaPlugin
{ {
logger.log("Database connection initialized!"); logger.log("Database connection initialized!");
Cache.dbManager = dbManager; Cache.dbManager = dbManager;
Cache.pronouns = dbManager.getAllPronouns();
logger.log("Database data loaded into memory!");
} else { } else {
logger.err("Error initializing database connection!"); logger.err("Error initializing database connection!");
} }

View File

@ -5,10 +5,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import wtf.beatrice.nounspicker.NounsPicker; import wtf.beatrice.nounspicker.NounsPicker;
import wtf.beatrice.nounspicker.commands.subcommands.NounsCreateSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.*;
import wtf.beatrice.nounspicker.commands.subcommands.NounsDeleteSubCommand;
import wtf.beatrice.nounspicker.commands.subcommands.NounsSetSubCommand;
import wtf.beatrice.nounspicker.commands.subcommands.NounsUpdateSubCmd;
public class NounsCommand implements CommandExecutor public class NounsCommand implements CommandExecutor
{ {
@ -42,6 +39,8 @@ public class NounsCommand implements CommandExecutor
return NounsDeleteSubCommand.run(sender, args); return NounsDeleteSubCommand.run(sender, args);
case "update": case "update":
return NounsUpdateSubCmd.run(sender, args); return NounsUpdateSubCmd.run(sender, args);
case "list":
return NounsListSubCommand.run(sender);
} }

View File

@ -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;
}
}

View File

@ -12,6 +12,7 @@ public class TabCompletion implements org.bukkit.command.TabCompleter
private final List<String> mainSubCommands = new ArrayList<>(){{ private final List<String> mainSubCommands = new ArrayList<>(){{
add("set"); add("set");
add("list");
add("create"); add("create");
add("delete"); add("delete");
add("update"); add("update");

View File

@ -312,5 +312,24 @@ public class DatabaseManager
return false; return false;
} }
public List<String> getAllPronouns() {
String query = "SELECT pronoun FROM pronouns;";
List<String> 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;
}
} }