Conditions | 8 |
Total Lines | 68 |
Code Lines | 32 |
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:
1 | package de.pewpewproject.lasertag.lasertaggame.state.management.server.implementation; |
||
76 | private void initSpawnpointCache(ServerWorld world) { |
||
77 | |||
78 | try { |
||
79 | this.spawnpointCache.clear(); |
||
80 | |||
81 | // Initialize team lists |
||
82 | for (var team : teamsConfigState.getTeams()) { |
||
83 | |||
84 | // Skip spectators |
||
85 | if (team.equals(TeamsConfigState.SPECTATORS)) { |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | spawnpointCache.put(team, new ArrayList<>()); |
||
90 | } |
||
91 | |||
92 | // Start time measurement |
||
93 | var startTime = System.nanoTime(); |
||
94 | |||
95 | // Iterate over blocks and find spawnpoints |
||
96 | world.fastSearchBlock((block, pos) -> { |
||
97 | for (var teamDto : teamsConfigState.getTeams()) { |
||
98 | |||
99 | // Skip spectators |
||
100 | if (teamDto.equals(TeamsConfigState.SPECTATORS)) { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | if (teamDto.spawnpointBlock().equals(block)) { |
||
105 | var team = spawnpointCache.get(teamDto); |
||
106 | synchronized (this) { |
||
107 | team.add(pos); |
||
108 | } |
||
109 | break; |
||
110 | } |
||
111 | } |
||
112 | }, (currChunk, maxChunk) -> { |
||
113 | // Only send a progress update every second chunk to not ddos our players |
||
114 | if (currChunk % 2 == 0) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | // Create packet buffer |
||
119 | var buf = new PacketByteBuf(Unpooled.buffer()); |
||
120 | |||
121 | // Write progress to buffer |
||
122 | buf.writeDouble((double) currChunk / (double) maxChunk); |
||
123 | |||
124 | ServerEventSending.sendToEveryone(world.getServer(), NetworkingConstants.PROGRESS, buf); |
||
125 | }); |
||
126 | |||
127 | // Stop time measurement |
||
128 | var stopTime = System.nanoTime(); |
||
129 | var duration = (stopTime - startTime) / 1000000000.0; |
||
130 | LasertagMod.LOGGER.info("Spawnpoint search took " + duration + "s."); |
||
131 | } catch (Exception ex) { |
||
132 | |||
133 | LasertagMod.LOGGER.error("Unexpected error while scanning for spawnpoints: ", ex); |
||
134 | |||
135 | } finally { |
||
136 | |||
137 | // Create packet buffer |
||
138 | var buf = new PacketByteBuf(Unpooled.buffer()); |
||
139 | |||
140 | // Write progress to buffer |
||
141 | buf.writeDouble(-1.0F); |
||
142 | |||
143 | ServerEventSending.sendToEveryone(world.getServer(), NetworkingConstants.PROGRESS, buf); |
||
144 | } |
||
147 |