Implement full PAPI support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-10-24 22:30:26 +02:00
parent 791f95491d
commit 5d5e6a5cfb
2 changed files with 51 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import wtf.beatrice.nounspicker.NounsPicker; import wtf.beatrice.nounspicker.NounsPicker;
import wtf.beatrice.nounspicker.utils.Cache;
public class PAPIManager extends PlaceholderExpansion public class PAPIManager extends PlaceholderExpansion
{ {
@ -47,12 +48,24 @@ public class PAPIManager extends PlaceholderExpansion
return ""; 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()) { switch (identifier.toLowerCase()) {
case "main_pronoun": case "main_pronoun":
return "1st"; pronounId = Cache.dbManager.getPlayerPronounId(player.getName(), 0);
return Cache.dbManager.getPronounFormat(pronounId);
case "secondary_pronoun": 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; return null;

View File

@ -224,7 +224,7 @@ public class DatabaseManager
public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) { public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) {
// if the player is not in the table, add them // if the player is not in the table, add them
if(!hasPronouns(playerName)) if(!isInTable(playerName))
{ {
String query = "INSERT INTO players (player) VALUES (?);"; String query = "INSERT INTO players (player) VALUES (?);";
@ -275,7 +275,7 @@ public class DatabaseManager
return null; return null;
} }
public boolean hasPronouns(String playerName) { public boolean isInTable(String playerName) {
String query = "SELECT player FROM players " + String query = "SELECT player FROM players " +
"WHERE player = ? " + "WHERE player = ? " +
"LIMIT 1;"; "LIMIT 1;";
@ -332,4 +332,38 @@ public class DatabaseManager
return allPronouns; 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;
}
} }