diff --git a/src/main/java/fr/communaywen/core/teams/Team.java b/src/main/java/fr/communaywen/core/teams/Team.java index 6aa819df..945a5182 100644 --- a/src/main/java/fr/communaywen/core/teams/Team.java +++ b/src/main/java/fr/communaywen/core/teams/Team.java @@ -88,6 +88,7 @@ public boolean addPlayer(UUID player) { public MethodState removePlayer(UUID player) throws SQLException { if (players.size() - 1 == 0) { players.remove(player); + AywenCraftPlugin.getInstance().getTeamManager().removePlayer(player); if (!AywenCraftPlugin.getInstance().getTeamManager().deleteTeam(this)) { players.add(player); return MethodState.INVALID; diff --git a/src/main/java/fr/communaywen/core/teams/TeamManager.java b/src/main/java/fr/communaywen/core/teams/TeamManager.java index dca5a88c..1349841f 100644 --- a/src/main/java/fr/communaywen/core/teams/TeamManager.java +++ b/src/main/java/fr/communaywen/core/teams/TeamManager.java @@ -126,6 +126,29 @@ public Team acceptInvite(UUID player) throws SQLException { return team; } + public Team removePlayer(UUID player) throws SQLException { + Team team = isInTeam(player); + if (team != null) { + team.removePlayer(player); + try (Connection connection = databaseConnection.getConnection(); + PreparedStatement updatePlayers = connection.prepareStatement("UPDATE teams SET players = ? WHERE name = ?")) { + + String playerListString = team.getPlayers().stream() + .map(UUID::toString) + .collect(Collectors.joining(",")); + + updatePlayers.setString(1, playerListString); + updatePlayers.setString(2, team.getName()); + updatePlayers.executeUpdate(); + + if (team.getPlayers().size() == 0) { + deleteTeam(team); + } + } + } + return team; + } + public Team getTeamByName(String name) throws SQLException { try (Connection connection = databaseConnection.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT owner, players FROM teams WHERE name = ?")) {