From 56c8e2ee1d2ebc044205ea190e326e46e704ef7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 21:22:27 +0200 Subject: [PATCH] Finish implementing pronoun setting command --- .../subcommands/NounsSetSubCommand.java | 13 +++++- .../nounspicker/utils/DatabaseManager.java | 41 ++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsSetSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsSetSubCommand.java index deb0dc0..f24f440 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsSetSubCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsSetSubCommand.java @@ -31,8 +31,17 @@ public class NounsSetSubCommand return true; } - // todo: add to sqlite - return true; + if(Cache.dbManager.setPlayerPronouns(sender.getName(), mainPronoun, secondaryPronoun)) { + + mainPronoun = Cache.dbManager.getPronounFormat(Cache.dbManager.getPronounId(mainPronoun)); + secondaryPronoun = Cache.dbManager.getPronounFormat(Cache.dbManager.getPronounId(secondaryPronoun)); + + sender.sendMessage("Your pronouns have correctly been set to " + mainPronoun + "/" + secondaryPronoun + "!"); + return true; + } else { + sender.sendMessage("Error updating pronouns! Please contact an administrator."); + return true; + } } } diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index bc77fa1..c0c0fad 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -111,7 +111,8 @@ public class DatabaseManager public int getPronounId(String pronoun) { String query = "SELECT id " + "FROM pronouns " + - "WHERE pronoun = ?;"; + "WHERE pronoun = ? " + + "LIMIT 1;"; try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) { pStatement.setString(1, pronoun); @@ -206,10 +207,46 @@ public class DatabaseManager } } - // accounce success + // announce success return true; } + public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) { + + int mainPronounId = getPronounId(mainPronoun); + int secondaryPronounId = getPronounId(secondaryPronoun); + + String query = "UPDATE players " + + "SET (main_pronoun_id, secondary_pronoun_id) = (?,?) " + + "WHERE player = ?;"; + + try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + pStatement.setInt(1, mainPronounId); + pStatement.setInt(2, secondaryPronounId); + pStatement.setString(3, playerName); + return pStatement.execute(); + } catch (SQLException e) { + logger.err(e.getMessage()); + } + + return false; + } + + public String getPronounFormat(int pronounId) { + String query = "SELECT format FROM pronouns" + + "WHERE id = " + pronounId + " " + + "LIMIT 1;"; + + try (Statement stmt = dbConnection.createStatement()) + { + ResultSet rSet = stmt.executeQuery(query); + return rSet.getString("id"); + } catch (SQLException e) { + logger.err(e.getMessage()); + } + + return null; + } }