From 0fdc77157f52c4f6b4839352f4f1ae94cb79a7dc Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 10 May 2022 10:01:48 -0400 Subject: [PATCH] rank-place placeholder, fix null pointer in sql --- .../hideAndSeek/database/PlayerInfoTable.java | 27 ++++++++++++++----- .../hideAndSeek/util/PAPIExpansion.java | 12 +++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java index 6948134..b4f1602 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java @@ -186,6 +186,23 @@ public class PlayerInfoTable { return null; } + @Nullable + public Integer getRanking(String order, UUID uuid){ + String sql = "SELECT count(*) AS total FROM hs_data WHERE "+order+" < (SELECT "+order+" FROM hs_data WHERE uuid = ?);"; + try(Connection connection = Database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){ + statement.setBytes(1, encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if(rs.next()){ + return rs.getInt("total")+1; + } + rs.close(); + } catch (SQLException e){ + Main.plugin.getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + public void addWins(List uuids, List winners, Map kills, Map deaths, WinType type){ for(UUID uuid : uuids){ String sql = "INSERT OR REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; @@ -196,10 +213,10 @@ public class PlayerInfoTable { statement.setInt(3, info.seeker_wins + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0)); statement.setInt(4, info.hider_games + (Board.isHider(uuid) ? 1 : 0)); statement.setInt(5, info.seeker_games + (Board.isSeeker(uuid) ? 1 : 0)); - statement.setInt(6, info.hider_kills + (Board.isHider(uuid) ? kills.get(uuid.toString()) : 0)); - statement.setInt(7, info.seeker_kills + (Board.isSeeker(uuid) ? kills.get(uuid.toString()) : 0)); - statement.setInt(8, info.hider_deaths + (Board.isHider(uuid) ? deaths.get(uuid.toString()) : 0)); - statement.setInt(9, info.seeker_deaths + (Board.isSeeker(uuid) ? deaths.get(uuid.toString()) : 0)); + statement.setInt(6, info.hider_kills + (Board.isHider(uuid) ? kills.getOrDefault(uuid.toString(), 0) : 0)); + statement.setInt(7, info.seeker_kills + (Board.isSeeker(uuid) ? kills.getOrDefault(uuid.toString(), 0) : 0)); + statement.setInt(8, info.hider_deaths + (Board.isHider(uuid) ? deaths.getOrDefault(uuid.toString(), 0) : 0)); + statement.setInt(9, info.seeker_deaths + (Board.isSeeker(uuid) ? deaths.getOrDefault(uuid.toString(), 0) : 0)); statement.execute(); } catch (SQLException e){ Main.plugin.getLogger().severe("SQL Error: " + e.getMessage()); @@ -211,6 +228,4 @@ public class PlayerInfoTable { } } - - } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java index 311fcee..d467b62 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java @@ -57,6 +57,18 @@ public class PAPIExpansion extends PlaceholderExpansion { } else { return UUIDFetcher.getPlayer(info.uuid).getName(); } + } else if(args[0].equals("rank-place") && args.length == 2){ + if(getRanking(args[1]) == null) { return placeholderError; } + Integer count = Database.playerInfo.getRanking(getRanking(args[1]), player.getUniqueId()); + if(count == null) { return placeholderNoData; } + return count.toString(); + } else if(args[0].equals("rank-place") && args.length == 3){ + UUID uuid; + try { uuid = UUIDFetcher.getUUID(args[2]); } catch (Exception e) { return placeholderError; } + if(getRanking(args[1]) == null) { return placeholderError; } + Integer count = Database.playerInfo.getRanking(getRanking(args[1]), uuid); + if(count == null) { return placeholderNoData; } + return count.toString(); } return null; }