| Conditions | 10 | 
| Total Lines | 76 | 
| Code Lines | 41 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like de.pewpewproject.lasertag.lasertaggame.state.management.server.implementation.MusicalChairsManager.handlePhaseChange() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package de.pewpewproject.lasertag.lasertaggame.state.management.server.implementation; | ||
| 117 |     private void handlePhaseChange() { | ||
| 118 | |||
| 119 | // Reset the already hit by state of the lasertargets lasertargets | ||
| 120 | lasertargetsManager.resetAlreadyHitBy(); | ||
| 121 | |||
| 122 | // If the scores should be reset at the end of the phase | ||
| 123 |         if (settingsManager.<Boolean>get(SettingDescription.RESET_SCORES_AT_PHASE_END)) { | ||
| 124 | |||
| 125 | scoreManager.resetScores(); | ||
| 126 | } | ||
| 127 | |||
| 128 | // Get the non-eliminated teams | ||
| 129 | var nonEliminatedTeams = teamsConfigState.getTeams().stream() | ||
| 130 | // Filter out empty teams | ||
| 131 | .filter(team -> !teamsManager.getPlayersOfTeam(team).isEmpty()) | ||
| 132 | // Filter out eliminated teams | ||
| 133 | .filter(eliminationManager::isTeamNotEliminated) | ||
| 134 | // Filter out the spectators | ||
| 135 | .filter(team -> !team.equals(TeamsConfigState.SPECTATORS)) | ||
| 136 | .toList(); | ||
| 137 | |||
| 138 |         var sb = new StringBuilder("[MusicalChairsManager] Remaining teams: ["); | ||
| 139 |         nonEliminatedTeams.forEach(t -> sb.append(t.name()).append(", ")); | ||
| 140 |         sb.append("]"); | ||
| 141 | LasertagMod.LOGGER.info(sb.toString()); | ||
| 142 | |||
| 143 | // Get the teams with the fewest amount of points | ||
| 144 | var toBeEliminatedTeams = nonEliminatedTeams.stream() | ||
| 145 | // Map to (Score, Team) tuple | ||
| 146 | .map(team -> new Tuple<>(teamsManager.getPlayersOfTeam(team).stream() | ||
| 147 | .map(scoreManager::getScore).mapToLong(a -> a) | ||
| 148 | .sum(), team)) | ||
| 149 | // Collect the teams with the least amount of points | ||
| 150 | .collect(ArrayDeque::new, | ||
| 151 |                         (Queue<Tuple<Long, TeamDto>> queue, Tuple<Long, TeamDto> tuple) -> { | ||
| 152 |                             if (!queue.isEmpty() && queue.peek().x().compareTo(tuple.x()) > 0) { | ||
| 153 | queue.clear(); | ||
| 154 | } | ||
| 155 |                             if (queue.isEmpty() || queue.peek().x().equals(tuple.x())) { | ||
| 156 | queue.offer(tuple); | ||
| 157 | } | ||
| 158 | }, | ||
| 159 |                         (left, right) -> { | ||
| 160 |                             if (left.peek().x().compareTo(right.peek().x()) > 0) { | ||
| 161 | left.clear(); | ||
| 162 | } | ||
| 163 | if (left.isEmpty() || left.peek().x().equals(right.peek().x())) | ||
| 164 |                             { | ||
| 165 | left.addAll(right); | ||
| 166 | } | ||
| 167 | }) | ||
| 168 | // Map back to stream of teams | ||
| 169 | .stream() | ||
| 170 | .map(Tuple::y) | ||
| 171 | .toList(); | ||
| 172 | |||
| 173 | // If all teams are about to be eliminated | ||
| 174 |         if (nonEliminatedTeams.size() == toBeEliminatedTeams.size()) { | ||
| 175 | |||
| 176 |             LasertagMod.LOGGER.info("[MusicalChairsManager] Tie between all teams present. No team gets eliminated"); | ||
| 177 |             LasertagMod.LOGGER.info("Number of remaining teams: " + nonEliminatedTeams.size()); | ||
| 178 |             LasertagMod.LOGGER.info("Number of to be eliminated teams: " + toBeEliminatedTeams.size()); | ||
| 179 | |||
| 180 | // Eliminate no team - tiebreaker | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | // Eliminate all the teams that need to be eliminated | ||
| 185 |         toBeEliminatedTeams.forEach(team -> { | ||
| 186 | |||
| 187 |             LasertagMod.LOGGER.info("[MusicalChairsManager] Eliminating team '" + team.name() + "'."); | ||
| 188 | eliminationManager.eliminateTeam(team); | ||
| 189 | }); | ||
| 190 | |||
| 191 | // Check if game ended | ||
| 192 | gameModeManager.getGameMode().checkGameOver(world.getServer()); | ||
| 193 | } | ||
| 194 | } |