Skip to content

Commit c232a79

Browse files
committed
Syncronize onlinePlayerList access and catch any runtime-exceptions in the player-update logic. Fixes: #777
1 parent 776d969 commit c232a79

4 files changed

Lines changed: 59 additions & 39 deletions

File tree

implementations/fabric/src/main/java/de/bluecolored/bluemap/fabric/FabricMod.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ public void onInitialize() {
126126
});
127127

128128
ServerTickEvents.END_SERVER_TICK.register((MinecraftServer server) -> {
129-
if (server == this.serverInstance) this.updateSomePlayers();
129+
try {
130+
if (server == this.serverInstance) this.updateSomePlayers();
131+
} catch (RuntimeException e) {
132+
Logger.global.logError("Exception trying to update players", e);
133+
}
130134
});
131135

132136
this.eventForwarder.init();
@@ -228,18 +232,20 @@ public Collection<Player> getOnlinePlayers() {
228232
* Only call this method on the server-thread.
229233
*/
230234
private void updateSomePlayers() {
231-
int onlinePlayerCount = onlinePlayerList.size();
232-
if (onlinePlayerCount == 0) return;
235+
synchronized (onlinePlayerList) {
236+
int onlinePlayerCount = onlinePlayerList.size();
237+
if (onlinePlayerCount == 0) return;
233238

234-
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
235-
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
239+
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
240+
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
236241

237-
for (int i = 0; i < playersToBeUpdated; i++) {
238-
playerUpdateIndex++;
239-
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
242+
for (int i = 0; i < playersToBeUpdated; i++) {
243+
playerUpdateIndex++;
244+
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
240245

241-
if (playerUpdateIndex < onlinePlayerCount) {
242-
onlinePlayerList.get(playerUpdateIndex).update();
246+
if (playerUpdateIndex < onlinePlayerCount) {
247+
onlinePlayerList.get(playerUpdateIndex).update();
248+
}
243249
}
244250
}
245251
}

implementations/forge/src/main/java/de/bluecolored/bluemap/forge/ForgeMod.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ public void onServerStopping(ServerStoppingEvent event) {
145145

146146
@SubscribeEvent
147147
public void onTick(ServerTickEvent.Post evt) {
148-
updateSomePlayers();
148+
try {
149+
updateSomePlayers();
150+
} catch (RuntimeException e) {
151+
Logger.global.logError("Exception trying to update players", e);
152+
}
149153
}
150154

151155
@Override
@@ -246,18 +250,20 @@ public Collection<Player> getOnlinePlayers() {
246250
* Only call this method on the server-thread.
247251
*/
248252
private void updateSomePlayers() {
249-
int onlinePlayerCount = onlinePlayerList.size();
250-
if (onlinePlayerCount == 0) return;
253+
synchronized (onlinePlayerList) {
254+
int onlinePlayerCount = onlinePlayerList.size();
255+
if (onlinePlayerCount == 0) return;
251256

252-
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
253-
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
257+
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
258+
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
254259

255-
for (int i = 0; i < playersToBeUpdated; i++) {
256-
playerUpdateIndex++;
257-
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
260+
for (int i = 0; i < playersToBeUpdated; i++) {
261+
playerUpdateIndex++;
262+
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
258263

259-
if (playerUpdateIndex < onlinePlayerCount) {
260-
onlinePlayerList.get(playerUpdateIndex).update();
264+
if (playerUpdateIndex < onlinePlayerCount) {
265+
onlinePlayerList.get(playerUpdateIndex).update();
266+
}
261267
}
262268
}
263269
}

implementations/neoforge/src/main/java/de/bluecolored/bluemap/forge/ForgeMod.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ public void onServerStopping(ServerStoppingEvent event) {
133133

134134
@SubscribeEvent
135135
public void onTick(ServerTickEvent.Post evt) {
136-
updateSomePlayers();
136+
try {
137+
updateSomePlayers();
138+
} catch (RuntimeException e) {
139+
Logger.global.logError("Exception trying to update players", e);
140+
}
137141
}
138142

139143
@Override
@@ -231,18 +235,20 @@ public Collection<Player> getOnlinePlayers() {
231235
* Only call this method on the server-thread.
232236
*/
233237
private void updateSomePlayers() {
234-
int onlinePlayerCount = onlinePlayerList.size();
235-
if (onlinePlayerCount == 0) return;
238+
synchronized (onlinePlayerList) {
239+
int onlinePlayerCount = onlinePlayerList.size();
240+
if (onlinePlayerCount == 0) return;
236241

237-
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
238-
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
242+
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
243+
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
239244

240-
for (int i = 0; i < playersToBeUpdated; i++) {
241-
playerUpdateIndex++;
242-
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
245+
for (int i = 0; i < playersToBeUpdated; i++) {
246+
playerUpdateIndex++;
247+
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
243248

244-
if (playerUpdateIndex < onlinePlayerCount) {
245-
onlinePlayerList.get(playerUpdateIndex).update();
249+
if (playerUpdateIndex < onlinePlayerCount) {
250+
onlinePlayerList.get(playerUpdateIndex).update();
251+
}
246252
}
247253
}
248254
}

implementations/spigot/src/main/java/de/bluecolored/bluemap/bukkit/BukkitPlugin.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,20 @@ public Collection<Player> getOnlinePlayers() {
267267
* Only call this method on the server-thread.
268268
*/
269269
private void updateSomePlayers() {
270-
int onlinePlayerCount = onlinePlayerList.size();
271-
if (onlinePlayerCount == 0) return;
270+
synchronized (onlinePlayerList) {
271+
int onlinePlayerCount = onlinePlayerList.size();
272+
if (onlinePlayerCount == 0) return;
272273

273-
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
274-
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
274+
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
275+
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
275276

276-
for (int i = 0; i < playersToBeUpdated; i++) {
277-
playerUpdateIndex++;
278-
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
277+
for (int i = 0; i < playersToBeUpdated; i++) {
278+
playerUpdateIndex++;
279+
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
279280

280-
if (playerUpdateIndex < onlinePlayerCount) {
281-
onlinePlayerList.get(playerUpdateIndex).update();
281+
if (playerUpdateIndex < onlinePlayerCount) {
282+
onlinePlayerList.get(playerUpdateIndex).update();
283+
}
282284
}
283285
}
284286
}

0 commit comments

Comments
 (0)