Finish implementing pronoun setting command
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Bea 2022-10-24 21:22:27 +02:00
parent 72b49a15ed
commit 56c8e2ee1d
2 changed files with 50 additions and 4 deletions

View File

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

View File

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