From e7e57506c42ffa612d50b86f8629e4d03fbb7e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 20:35:57 +0200 Subject: [PATCH] Start implementing add command --- .../wtf/beatrice/nounspicker/NounsPicker.java | 2 + .../subcommands/NounsAddSubCommand.java | 30 +++++++++++ .../wtf/beatrice/nounspicker/utils/Cache.java | 18 +------ .../nounspicker/utils/DatabaseManager.java | 52 +++++++++++++------ 4 files changed, 70 insertions(+), 32 deletions(-) create mode 100644 src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index 036ea4f..a79a45f 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.utils.Cache; import wtf.beatrice.nounspicker.utils.ConsoleLogger; import wtf.beatrice.nounspicker.utils.DatabaseManager; import wtf.beatrice.nounspicker.utils.FileManager; @@ -56,6 +57,7 @@ public class NounsPicker extends JavaPlugin if(dbManager.connect() && dbManager.initDb()) { logger.log("Database connection initialized!"); + Cache.dbManager = dbManager; } else { logger.err("Error initializing database connection!"); } diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java new file mode 100644 index 0000000..445b361 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java @@ -0,0 +1,30 @@ +package wtf.beatrice.nounspicker.commands.subcommands; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.nounspicker.utils.Cache; + +public class NounsAddSubCommand +{ + + public static boolean run(@NotNull CommandSender sender, + @NotNull String[] args) + { + + + if(args.length < 3) + { + sender.sendMessage("usage: /nouns add "); + return true; + } + + String pronoun = args[1].toLowerCase(); + + if(Cache.dbManager.getPronounId(pronoun) >= 0) + { + sender.sendMessage("Pronoun already exists!"); + } + + return false; + } +} diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java b/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java index 8d4d5dd..60b5b18 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/Cache.java @@ -8,21 +8,5 @@ import java.util.HashMap; public class Cache { - // map to store pronouns' "raw" names and how they appear in game. - private static final HashMap pronouns = new HashMap<>(); - - public static @Nullable String getPronoun(String pronoun) - { - return pronouns.get(pronoun); - } - - public static void addPronoun(@NotNull String pronoun, @NotNull String appearance) - { - pronouns.put(pronoun, appearance); - } - - public static boolean isPronounValid(String pronoun) - { - return getPronoun(pronoun) != null; - } + public static DatabaseManager dbManager; } diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index b3a4484..3e24839 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -1,9 +1,6 @@ package wtf.beatrice.nounspicker.utils; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -68,10 +65,11 @@ public class DatabaseManager "secondary_pronoun_id integer" + ");"); - newTables.add("CREATE TABLE IF NOT EXISTS pronouns (\" +\n" + - " \"pronoun text,\" +\n" + - " \"id integer\" +\n" + - " \");"); + newTables.add("CREATE TABLE IF NOT EXISTS pronouns (" + + "pronoun text," + + "format text," + + "\"id integer" + + ");"); for(String sql : newTables) { @@ -99,14 +97,38 @@ public class DatabaseManager * * * TABLE 2: PRONOUNS - * -------------------------- - * | pronoun | id | - * -------------------------- - * | she | 1 | - * | her | 2 | - * | he | 3 | - * -------------------------- + * -------------------------------------- + * | pronoun | id | format | + * -------------------------------------- + * | she | 1 | She | + * | her | 2 | Her | + * | he | 3 | He | + * ------------------------------------- * */ + + public int getPronounId(String pronoun) { + String query = "SELECT id " + + "FROM pronouns " + + "WHERE pronoun = ?;"; + + try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + pStatement.setString(1, pronoun); + ResultSet resultSet = pStatement.executeQuery(); + + while(resultSet.next()) + { + return resultSet.getInt("id"); + } + } catch (SQLException e) + { + logger.err(e.getMessage()); + } + + return -1; + } + + + }