diff --git a/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java b/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java index c4e9f20d..d9e36612 100644 --- a/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java +++ b/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java @@ -245,6 +245,23 @@ public final void leave(ArenaPlayer player, ArenaLeaveEvent.Cause cause) { player.remove(); } + /** + * Changes the role of the player to the specified role. + * + * @param player the player to change the role of + * @param role the new role of the player + */ + public final void changeRole(ArenaPlayer player, PlayerRole role) { + if (role == player.getRole()) { + return; + } + + this.playersByRole.get(player.getRole()).remove(player); + this.playersByRole.computeIfAbsent(role, e -> new HashSet<>()).add(player); + + player.setRole(role); + } + /** * Gets all the {@link ArenaPlayer players} in the competition. * diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java index 3ebc129b..46dee5ee 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java @@ -21,7 +21,7 @@ public ChangeRoleAction(Map params) { public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { PlayerRole role = PlayerRole.valueOf(this.get(ROLE_KEY).toUpperCase(Locale.ROOT)); boolean changedRole = arenaPlayer.getRole() != role; - arenaPlayer.setRole(role); + arenaPlayer.getCompetition().changeRole(arenaPlayer, role); if (changedRole && role == PlayerRole.SPECTATING) { arenaPlayer.getCompetition().getTeamManager().leaveTeam(arenaPlayer);