From 5d5e6a5cfbdfbb172906e8ab85fcc55e4e50680e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 22:30:26 +0200 Subject: [PATCH] Implement full PAPI support --- .../nounspicker/objects/PAPIManager.java | 17 ++++++++- .../nounspicker/utils/DatabaseManager.java | 38 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/main/java/wtf/beatrice/nounspicker/objects/PAPIManager.java b/src/main/java/wtf/beatrice/nounspicker/objects/PAPIManager.java index db7ff1a..cf5cce1 100644 --- a/src/main/java/wtf/beatrice/nounspicker/objects/PAPIManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/objects/PAPIManager.java @@ -4,6 +4,7 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import wtf.beatrice.nounspicker.NounsPicker; +import wtf.beatrice.nounspicker.utils.Cache; public class PAPIManager extends PlaceholderExpansion { @@ -47,12 +48,24 @@ public class PAPIManager extends PlaceholderExpansion return ""; } + int pronounId; + + // todo: this sends query every time we get a papi call, which is horrible for performance. + // todo: we should cache player pronouns too. switch (identifier.toLowerCase()) { case "main_pronoun": - return "1st"; + pronounId = Cache.dbManager.getPlayerPronounId(player.getName(), 0); + return Cache.dbManager.getPronounFormat(pronounId); case "secondary_pronoun": - return "2nd"; + pronounId = Cache.dbManager.getPlayerPronounId(player.getName(), 1); + return Cache.dbManager.getPronounFormat(pronounId); + case "full_tag": + pronounId = Cache.dbManager.getPlayerPronounId(player.getName(), 0); + String mainPronounFormat = Cache.dbManager.getPronounFormat(pronounId); + pronounId = Cache.dbManager.getPlayerPronounId(player.getName(), 1); + String secondaryPronounFormat = Cache.dbManager.getPronounFormat(pronounId); + return "[" + mainPronounFormat + "/" + secondaryPronounFormat + "]"; } return null; diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 3e4d9ac..6901d0a 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -224,7 +224,7 @@ public class DatabaseManager public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) { // if the player is not in the table, add them - if(!hasPronouns(playerName)) + if(!isInTable(playerName)) { String query = "INSERT INTO players (player) VALUES (?);"; @@ -275,7 +275,7 @@ public class DatabaseManager return null; } - public boolean hasPronouns(String playerName) { + public boolean isInTable(String playerName) { String query = "SELECT player FROM players " + "WHERE player = ? " + "LIMIT 1;"; @@ -332,4 +332,38 @@ public class DatabaseManager return allPronouns; } + public int getPlayerPronounId(String playerName, int priority) { + + if(!isInTable(playerName)) return -1; + + String query = "SELECT ? FROM players " + + "WHERE player = ? " + + "LIMIT 1"; + + + // instead of making two separate methods for main and secondary pronouns (copying a lot of code), + // we just set up a priority value that decides whether to pick the main or secondary pronoun. + // this also helps in case we want to allow for more than two pronouns in the future. + + String priorityStr = ""; + if(priority == 0) priorityStr = "main_pronoun_id"; + else if(priority == 1) priorityStr = "secondary_pronoun_id"; + + try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + + pStatement.setString(1, priorityStr); + pStatement.setString(2, playerName); + + ResultSet rSet = pStatement.executeQuery(); + if(rSet.isClosed()) return -1; + + while(rSet.next()) return rSet.getInt(priorityStr); + + } catch (SQLException e) { + e.printStackTrace(); + } + + return -1; + } + }