diff --git a/build.gradle b/build.gradle index 99c8887..340961d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'net.simplyvanilla' -version = '0.2.1' +version = '0.3.0' java { toolchain.languageVersion.set(JavaLanguageVersion.of(17)) diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 41d9927..d64cd49 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fae0804..a80b22c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 1b6c787..1aa94a4 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +80,11 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,22 +131,29 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,11 +198,15 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ @@ -205,6 +214,12 @@ set -- \ org.gradle.wrapper.GradleWrapperMain \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index ac1b06f..7101f8e 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -14,7 +14,7 @@ @rem limitations under the License. @rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +25,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,13 +41,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -56,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/src/main/java/net/simplyvanilla/simplynicks/SimplyNicks.java b/src/main/java/net/simplyvanilla/simplynicks/SimplyNicks.java index 35c0e74..b924c6e 100644 --- a/src/main/java/net/simplyvanilla/simplynicks/SimplyNicks.java +++ b/src/main/java/net/simplyvanilla/simplynicks/SimplyNicks.java @@ -101,7 +101,9 @@ public void onEnable() { TeamMySQL.PlayerTeam playerTeam = teamCache.getTeam(player.getUniqueId()); Component teamPrefix = playerTeam == null ? Component.text("") - : Component.text(playerTeam.getName()).color(NamedTextColor.NAMES.valueOr(playerTeam.getName(), NamedTextColor.WHITE)); + : Component.text(playerTeam.getName()) + .color(NamedTextColor.NAMES.valueOr(playerTeam.getColor(), NamedTextColor.WHITE)) + .append(Component.text(" ")); return Tag.selfClosingInserting(teamPrefix); }) .build() @@ -169,4 +171,3 @@ public static boolean isFolia() { } } } - diff --git a/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java b/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java index 1b9388f..32a6490 100644 --- a/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java +++ b/src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java @@ -3,9 +3,7 @@ import net.simplyvanilla.simplynicks.SimplyNicks; import net.simplyvanilla.simplynicks.database.TeamMySQL; import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; +import org.bukkit.command.*; import org.jetbrains.annotations.NotNull; import java.util.Locale; @@ -20,8 +18,12 @@ public TeamCommandExecutor(SimplyNicks plugin) { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (!sender.hasPermission("simplynicks.team")) { + public boolean onCommand( + @NotNull CommandSender sender, + @NotNull Command command, + @NotNull String label, + @NotNull String[] args) { + if (!(sender instanceof ConsoleCommandSender) && !(sender instanceof RemoteConsoleCommandSender)) { return false; } @@ -88,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; } @@ -120,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; } @@ -141,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; } @@ -157,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; } @@ -172,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 82087b1..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 { @@ -30,32 +33,32 @@ public synchronized void connect() { this.plugin.getConfig().getString("database.username"), this.plugin.getConfig().getString("database.password")); - PreparedStatement teamTableCheckQuery = this.connection.prepareStatement( + this.statement = this.connection.createStatement(); + + String teamTableCheckQuery = String.format( """ - CREATE TABLE IF NOT EXISTS ? ( + CREATE TABLE IF NOT EXISTS `%s` ( `id` INT NOT NULL AUTO_INCREMENT, - `name` varchar(256) NOT NULL UNIQUE, + `name` varchar(256) NOT NULL, `color` varchar(256) NOT NULL, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE INDEX `name` (`name`) ); - """ + """, this.teamTableName ); - teamTableCheckQuery.setString(1, this.teamTableName); - teamTableCheckQuery.executeUpdate(); + this.statement.executeUpdate(teamTableCheckQuery); - PreparedStatement playerTeamTableCheckQuery = this.connection.prepareStatement( + String playerTeamTableCheckQuery = String.format( """ - CREATE TABLE IF NOT EXISTS ? ( + CREATE TABLE IF NOT EXISTS `%s` ( `id` BINARY(16) NOT NULL, `team_id` INT NOT NULL, PRIMARY KEY (`id`), - FOREIGN KEY (`team_id`) REFERENCES ?(`id`) ON DELETE CASCADE + FOREIGN KEY (`team_id`) REFERENCES `%s`(`id`) ON DELETE CASCADE ); - """ + """, this.playerTeamTableName, this.teamTableName ); - playerTeamTableCheckQuery.setString(1, this.playerTeamTableName); - playerTeamTableCheckQuery.setString(2, this.teamTableName); - playerTeamTableCheckQuery.executeUpdate(); + this.statement.executeUpdate(playerTeamTableCheckQuery); this.plugin.getLogger().log(Level.INFO, "Connected to the MySQL server!"); } catch (Exception ex) { @@ -81,138 +84,103 @@ public synchronized void close() { } } - public boolean createTeam(String name) { + public int getTeamId(String name) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - INSERT INTO ? (name, color) VALUES (?, ?); - """ + String.format("SELECT `id` FROM `%s` WHERE `name` = ?", this.teamTableName) )) { - preparedStatement.setString(1, this.teamTableName); - preparedStatement.setString(2, name); - preparedStatement.setString(3, ""); - preparedStatement.executeUpdate(); - return true; + 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 createTeam...", ex); - return false; + this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeamId...", ex); } + return -1; } - public boolean modifyTeam(String name, String modifyType, String value) { + public boolean createTeam(String name) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - UPDATE ? SET ? = ? WHERE name = ?; - """ + String.format("INSERT INTO `%s` (`name`, `color`) VALUES (?, ?)", this.teamTableName) )) { - preparedStatement.setString(1, this.teamTableName); - Runnable afterRun; - if (modifyType.equalsIgnoreCase("name")) { - preparedStatement.setString(2, "name"); - afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setName(value)); - } else if (modifyType.equalsIgnoreCase("color")) { - preparedStatement.setString(2, "color"); - afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setColor(value)); - } else { - return false; - } - preparedStatement.setString(3, value); - preparedStatement.setString(4, name); + preparedStatement.setString(1, name); + preparedStatement.setString(2, ""); preparedStatement.executeUpdate(); - afterRun.run(); return true; } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to modifyTeam...", ex); + this.plugin.getLogger().log(Level.SEVERE, "Unable to createTeam...", ex); return false; } } - public boolean joinTeam(UUID uuid, String name) { - int teamId; - try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - SELECT id FROM ? WHERE name = ?; - """ - )) { - preparedStatement.setString(1, this.teamTableName); - preparedStatement.setString(2, name); - var resultSet = preparedStatement.executeQuery(); - if (resultSet.next()) { - teamId = resultSet.getInt("id"); - } else { - return false; - } - } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to joinTeam...", ex); - return false; - } - + public boolean deleteTeam(int teamId) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - INSERT INTO ? (id, team_id) VALUES (UUID_TO_BIN(?), ?); - """ + String.format("DELETE FROM `%s` WHERE id = ?", this.teamTableName) )) { - preparedStatement.setString(1, this.playerTeamTableName); - preparedStatement.setString(2, uuid.toString()); - preparedStatement.setInt(3, teamId); + preparedStatement.setInt(1, teamId); preparedStatement.executeUpdate(); - this.plugin.getTeamCache().addTeam(uuid, getTeam(uuid)); + this.plugin.getTeamCache().removeTeamByTeamId(teamId); return true; } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to joinTeam...", ex); + this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex); return false; } } - public boolean leaveTeam(UUID uuid, String name) { - int teamId; + public boolean modifyTeam(int teamId, String modifyType, String value) { + if (!modifyType.equalsIgnoreCase("name") && + !modifyType.equalsIgnoreCase("color")) { + return false; + } + try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - SELECT id FROM ? WHERE name = ?; - """ + String.format("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", this.teamTableName, modifyType.toLowerCase()) )) { - preparedStatement.setString(1, this.teamTableName); - preparedStatement.setString(2, name); - var resultSet = preparedStatement.executeQuery(); - if (resultSet.next()) { - teamId = resultSet.getInt("id"); + Runnable afterRun; + if (modifyType.equalsIgnoreCase("name")) { + afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setName(value)); + } else if (modifyType.equalsIgnoreCase("color")) { + afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setColor(value)); } else { return false; } + preparedStatement.setString(1, value); + preparedStatement.setInt(2, teamId); + preparedStatement.executeUpdate(); + afterRun.run(); + return true; } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to joinTeam...", ex); + this.plugin.getLogger().log(Level.SEVERE, "Unable to modifyTeam...", ex); return false; } + } + public boolean joinTeam(UUID uuid, int teamId) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - DELETE FROM ? WHERE id = UUID_TO_BIN(?) AND team_id = ?; - """ + String.format("INSERT INTO `%s` (`id`, `team_id`) VALUES (UUID_TO_BIN(?), ?)", this.playerTeamTableName) )) { - preparedStatement.setString(1, this.playerTeamTableName); - preparedStatement.setString(2, uuid.toString()); - preparedStatement.setInt(3, teamId); + preparedStatement.setString(1, uuid.toString()); + preparedStatement.setInt(2, teamId); preparedStatement.executeUpdate(); - this.plugin.getTeamCache().removeTeam(uuid); + this.plugin.getTeamCache().addTeam(uuid, getTeam(uuid)); return true; } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to leaveTeam...", ex); + this.plugin.getLogger().log(Level.SEVERE, "Unable to joinTeam...", ex); return false; } } - public boolean deleteTeam(String name) { + public boolean leaveTeam(UUID uuid, int teamId) { try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - DELETE FROM ? WHERE name = ?; - """ + String.format("DELETE FROM `%s` WHERE `id` = UUID_TO_BIN(?) AND `team_id` = ?", this.playerTeamTableName) )) { - preparedStatement.setString(1, this.teamTableName); - preparedStatement.setString(2, name); + preparedStatement.setString(1, uuid.toString()); + preparedStatement.setInt(2, teamId); preparedStatement.executeUpdate(); - this.plugin.getTeamCache().removeTeamByName(name); + this.plugin.getTeamCache().removeTeam(uuid); return true; } catch (Exception ex) { - this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex); + this.plugin.getLogger().log(Level.SEVERE, "Unable to leaveTeam...", ex); return false; } } @@ -221,20 +189,20 @@ public Map getAllTeams() { Map teams = new HashMap<>(); try (PreparedStatement preparedStatement = this.connection.prepareStatement( - """ - SELECT BIN_TO_UUID(pt.id) AS player_id, t.name AS team_name, t.color AS team_color - FROM `?` AS pt - JOIN `?` AS t ON pt.team_id = t.id; + String.format( """ + 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) )) { - preparedStatement.setString(1, this.playerTeamTableName); - preparedStatement.setString(2, 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); @@ -245,21 +213,20 @@ 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( - """ - SELECT t.name AS team_name, t.color AS team_color - FROM `?` AS pt - JOIN `?` AS t ON pt.team_id = t.id - WHERE pt.id = UUID_TO_BIN(?); - """ + String.format(""" + 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(?); + """, this.playerTeamTableName, this.teamTableName) )) { - preparedStatement.setString(1, this.playerTeamTableName); - preparedStatement.setString(2, this.teamTableName); - preparedStatement.setString(3, uuid.toString()); + 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); @@ -270,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; } @@ -302,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; + } } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e704aa8..b564a89 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -51,5 +51,5 @@ permissions: default: op simplynicks.team: - description: allows access to the team command - default: false + description: whether the player can access the team commands + default: op