From d66a60aa8d3942c01e3eb5752e5529b4fe613518 Mon Sep 17 00:00:00 2001 From: Huynh Tien Date: Sun, 10 Mar 2024 22:35:17 +0700 Subject: [PATCH] team check to team id (#32) --- .../commands/TeamCommandExecutor.java | 20 +-- .../simplynicks/database/TeamCache.java | 12 +- .../simplynicks/database/TeamMySQL.java | 115 +++++++++--------- 3 files changed, 76 insertions(+), 71 deletions(-) diff --git a/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java b/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java index 56ff70e..32a6490 100644 --- a/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java +++ b/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java @@ -90,12 +90,13 @@ private UUID getPlayerId(String input) { // /team delete private boolean handleDelete(CommandSender sender, String name) { - if (!this.plugin.getTeamCache().isTeamExists(name)) { + int teamId = this.plugin.getTeamDatabase().getTeamId(name); + if (teamId < 0) { this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound"); return false; } - if (!this.plugin.getTeamDatabase().deleteTeam(name)) { + if (!this.plugin.getTeamDatabase().deleteTeam(teamId)) { return false; } @@ -122,7 +123,7 @@ private boolean handleLeave(CommandSender sender, String leaver, String name) { return false; } - if (!this.plugin.getTeamDatabase().leaveTeam(leaverId, name)) { + if (!this.plugin.getTeamDatabase().leaveTeam(leaverId, playerTeam.getTeamId())) { return false; } @@ -143,12 +144,13 @@ private boolean handleJoin(CommandSender sender, String member, String name) { return false; } - if (!this.plugin.getTeamCache().isTeamExists(name)) { + int teamId = this.plugin.getTeamDatabase().getTeamId(name); + if (teamId < 0) { this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound"); return false; } - if (!this.plugin.getTeamDatabase().joinTeam(memberId, name)) { + if (!this.plugin.getTeamDatabase().joinTeam(memberId, teamId)) { return false; } @@ -159,12 +161,13 @@ private boolean handleJoin(CommandSender sender, String member, String name) { // /team modify [value] // modifyType: name, color private boolean handleModify(CommandSender sender, String name, String modifyType, String value) { - if (!this.plugin.getTeamCache().isTeamExists(name)) { + int teamId = this.plugin.getTeamDatabase().getTeamId(name); + if (teamId < 0) { this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound"); return false; } - if (!this.plugin.getTeamDatabase().modifyTeam(name, modifyType, value)) { + if (!this.plugin.getTeamDatabase().modifyTeam(teamId, modifyType, value)) { return false; } @@ -174,7 +177,8 @@ private boolean handleModify(CommandSender sender, String name, String modifyTyp // /team create private boolean handleCreate(CommandSender sender, String teamName) { - if (this.plugin.getTeamCache().isTeamExists(teamName)) { + int teamId = this.plugin.getTeamDatabase().getTeamId(teamName); + if (teamId >= 0) { this.plugin.sendConfigMessage(sender, "messages.error.teamAlreadyExists"); return false; } diff --git a/src/main/java/net/simplyvanilla/simplynicks/database/TeamCache.java b/src/main/java/net/simplyvanilla/simplynicks/database/TeamCache.java index 8027805..387e166 100644 --- a/src/main/java/net/simplyvanilla/simplynicks/database/TeamCache.java +++ b/src/main/java/net/simplyvanilla/simplynicks/database/TeamCache.java @@ -21,10 +21,6 @@ public void initCache() { } } - public boolean isTeamExists(String name) { - return this.teams.values().stream().anyMatch(team -> team.getName().equals(name)); - } - public void addTeam(UUID key, TeamMySQL.PlayerTeam value) { this.teams.put(key, value); } @@ -37,11 +33,11 @@ public void removeTeam(UUID uuid) { this.teams.remove(uuid); } - public void removeTeamByName(String name) { - this.teams.values().removeIf(team -> team.getName().equals(name)); + public void removeTeamByTeamId(int teamId) { + this.teams.values().removeIf(team -> team.getTeamId() == teamId); } - public void updateTeam(String name, Consumer consumer) { - this.teams.values().stream().filter(team -> team.getName().equals(name)).forEach(consumer); + public void updateTeam(int teamId, Consumer consumer) { + this.teams.values().stream().filter(team -> team.getTeamId() == teamId).forEach(consumer); } } diff --git a/src/main/java/net/simplyvanilla/simplynicks/database/TeamMySQL.java b/src/main/java/net/simplyvanilla/simplynicks/database/TeamMySQL.java index 261b9d8..113947b 100644 --- a/src/main/java/net/simplyvanilla/simplynicks/database/TeamMySQL.java +++ b/src/main/java/net/simplyvanilla/simplynicks/database/TeamMySQL.java @@ -6,7 +6,10 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; import java.util.logging.Level; public class TeamMySQL { @@ -81,6 +84,21 @@ public synchronized void close() { } } + public int getTeamId(String name) { + try (PreparedStatement preparedStatement = this.connection.prepareStatement( + String.format("SELECT `id` FROM `%s` WHERE `name` = ?", this.teamTableName) + )) { + preparedStatement.setString(1, name); + var resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + return resultSet.getInt("id"); + } + } catch (Exception ex) { + this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeamId...", ex); + } + return -1; + } + public boolean createTeam(String name) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( String.format("INSERT INTO `%s` (`name`, `color`) VALUES (?, ?)", this.teamTableName) @@ -95,25 +113,39 @@ public boolean createTeam(String name) { } } - public boolean modifyTeam(String name, String modifyType, String value) { + public boolean deleteTeam(int teamId) { + try (PreparedStatement preparedStatement = this.connection.prepareStatement( + String.format("DELETE FROM `%s` WHERE id = ?", this.teamTableName) + )) { + preparedStatement.setInt(1, teamId); + preparedStatement.executeUpdate(); + this.plugin.getTeamCache().removeTeamByTeamId(teamId); + return true; + } catch (Exception ex) { + this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex); + return false; + } + } + + public boolean modifyTeam(int teamId, String modifyType, String value) { if (!modifyType.equalsIgnoreCase("name") && !modifyType.equalsIgnoreCase("color")) { return false; } try (PreparedStatement preparedStatement = this.connection.prepareStatement( - String.format("UPDATE `%s` SET `%s` = ? WHERE `name` = ?", this.teamTableName, modifyType.toLowerCase()) + String.format("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", this.teamTableName, modifyType.toLowerCase()) )) { Runnable afterRun; if (modifyType.equalsIgnoreCase("name")) { - afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setName(value)); + afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setName(value)); } else if (modifyType.equalsIgnoreCase("color")) { - afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setColor(value)); + afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setColor(value)); } else { return false; } preparedStatement.setString(1, value); - preparedStatement.setString(2, name); + preparedStatement.setInt(2, teamId); preparedStatement.executeUpdate(); afterRun.run(); return true; @@ -123,27 +155,7 @@ public boolean modifyTeam(String name, String modifyType, String value) { } } - private int getTeamId(String name) { - try (PreparedStatement preparedStatement = this.connection.prepareStatement( - String.format("SELECT `id` FROM `%s` WHERE `name` = ?", this.teamTableName) - )) { - preparedStatement.setString(1, name); - var resultSet = preparedStatement.executeQuery(); - if (resultSet.next()) { - return resultSet.getInt("id"); - } - } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeamId...", ex); - } - return -1; - } - - public boolean joinTeam(UUID uuid, String name) { - int teamId = getTeamId(name); - if (teamId < 0) { - return false; - } - + public boolean joinTeam(UUID uuid, int teamId) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( String.format("INSERT INTO `%s` (`id`, `team_id`) VALUES (UUID_TO_BIN(?), ?)", this.playerTeamTableName) )) { @@ -158,12 +170,7 @@ public boolean joinTeam(UUID uuid, String name) { } } - public boolean leaveTeam(UUID uuid, String name) { - int teamId = getTeamId(name); - if (teamId < 0) { - return false; - } - + public boolean leaveTeam(UUID uuid, int teamId) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( String.format("DELETE FROM `%s` WHERE `id` = UUID_TO_BIN(?) AND `team_id` = ?", this.playerTeamTableName) )) { @@ -178,37 +185,24 @@ public boolean leaveTeam(UUID uuid, String name) { } } - public boolean deleteTeam(String name) { - try (PreparedStatement preparedStatement = this.connection.prepareStatement( - String.format("DELETE FROM `%s` WHERE name = ?", this.teamTableName) - )) { - preparedStatement.setString(1, name); - preparedStatement.executeUpdate(); - this.plugin.getTeamCache().removeTeamByName(name); - return true; - } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex); - return false; - } - } - public Map getAllTeams() { Map teams = new HashMap<>(); try (PreparedStatement preparedStatement = this.connection.prepareStatement( String.format( """ - SELECT BIN_TO_UUID(pt.id) AS player_id, t.name AS team_name, t.color AS team_color - FROM `%s` AS pt - JOIN `%s` AS t ON pt.team_id = t.id; - """, this.playerTeamTableName, this.teamTableName) + SELECT BIN_TO_UUID(pt.id) AS player_id, t.id AS team_id, t.name AS team_name, t.color AS team_color + FROM `%s` AS pt + JOIN `%s` AS t ON pt.team_id = t.id; + """, this.playerTeamTableName, this.teamTableName) )) { var resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { UUID playerId = UUID.fromString(resultSet.getString("player_id")); + int teamId = resultSet.getInt("team_id"); String teamName = resultSet.getString("team_name"); String teamColor = resultSet.getString("team_color"); - teams.put(playerId, new PlayerTeam(playerId, teamName, teamColor)); + teams.put(playerId, new PlayerTeam(playerId, teamId, teamName, teamColor)); } } catch (Exception ex) { this.plugin.getLogger().log(Level.SEVERE, "Unable to getAllTeams...", ex); @@ -220,7 +214,7 @@ SELECT BIN_TO_UUID(pt.id) AS player_id, t.name AS team_name, t.color AS team_col public PlayerTeam getTeam(UUID uuid) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( String.format(""" - SELECT t.name AS team_name, t.color AS team_color + SELECT t.id AS team_id, t.name AS team_name, t.color AS team_color FROM `%s` AS pt JOIN `%s` AS t ON pt.team_id = t.id WHERE pt.id = UUID_TO_BIN(?); @@ -229,9 +223,10 @@ public PlayerTeam getTeam(UUID uuid) { preparedStatement.setString(1, uuid.toString()); var resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { + int teamId = resultSet.getInt("team_id"); String teamName = resultSet.getString("team_name"); String teamColor = resultSet.getString("team_color"); - return new PlayerTeam(uuid, teamName, teamColor); + return new PlayerTeam(uuid, teamId, teamName, teamColor); } } catch (Exception ex) { this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeam...", ex); @@ -242,11 +237,13 @@ public PlayerTeam getTeam(UUID uuid) { public static final class PlayerTeam { private UUID uuid; + private int teamId; private String name; private String color; - public PlayerTeam(UUID uuid, String name, String color) { + public PlayerTeam(UUID uuid, int teamId, String name, String color) { this.uuid = uuid; + this.teamId = teamId; this.name = name; this.color = color; } @@ -274,5 +271,13 @@ public String getColor() { public void setColor(String color) { this.color = color; } + + public int getTeamId() { + return teamId; + } + + public void setTeamId(int teamId) { + this.teamId = teamId; + } } }