Skip to content

Commit 1061b61

Browse files
authored
GH-1258 Fix Quit/Death message performance by building Notice asynchronous. (#1258)
* Optimize join(first join)/quit message broadcasts to prevent main thread blocking by useless creating ton's of Notices * Refactor imports and formatting in `BukkitViewerProvider` and `PlayerQuitMessageController` for consistent code style. * Refactor `PlayerJoinMessageController` for cleaner conditional handling in join message logic. * Compile death message async. * Fix formatting.
1 parent 0be5d59 commit 1061b61

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

eternalcore-core/src/main/java/com/eternalcode/core/feature/deathmessage/handler/EnvironmentDeathMessageHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private void handleEntityKill(Player victim, Entity killer) {
7575
.placeholder("{MOB}", mobName)
7676
.placeholder("{MOB_TYPE}", mobType.name())
7777
.onlinePlayers()
78-
.send();
78+
.sendAsync();
7979
}
8080

8181
private void handleEnvironmentDeath(String victimName, EntityDamageEvent.DamageCause cause) {
@@ -92,7 +92,7 @@ private void handleEnvironmentDeath(String victimName, EntityDamageEvent.DamageC
9292
.placeholder("{PLAYER}", victimName)
9393
.placeholder("{CAUSE}", this.formatCauseName(cause))
9494
.onlinePlayers()
95-
.send();
95+
.sendAsync();
9696
}
9797

9898
private void handleUnknownDeath(String victimName) {
@@ -101,7 +101,7 @@ private void handleUnknownDeath(String victimName) {
101101
.playerDiedByUnknownCause()))
102102
.placeholder("{PLAYER}", victimName)
103103
.onlinePlayers()
104-
.send();
104+
.sendAsync();
105105
}
106106

107107
private String getMobName(Entity entity) {

eternalcore-core/src/main/java/com/eternalcode/core/feature/deathmessage/handler/PlayerKillMessageHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public void handle(DeathContext context) {
4040
String weaponName = this.getWeaponName(weapon);
4141

4242
this.noticeService.create()
43-
.noticeOptional(translation -> RandomElementUtil.randomElement(translation.deathMessage().playerKilledByOtherPlayer()))
43+
.noticeOptional(translation -> RandomElementUtil.randomElement(translation.deathMessage()
44+
.playerKilledByOtherPlayer()))
4445
.placeholder("{PLAYER}", victimName)
4546
.placeholder("{KILLER}", killerName)
4647
.placeholder("{WEAPON}", weaponName)
4748
.onlinePlayers()
48-
.send();
49+
.sendAsync();
4950
}
5051

5152
private String getWeaponName(ItemStack weapon) {

eternalcore-core/src/main/java/com/eternalcode/core/feature/joinmessage/PlayerJoinMessageController.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eternalcode.core.feature.joinmessage;
22

3-
43
import com.eternalcode.commons.RandomElementUtil;
54
import com.eternalcode.core.feature.vanish.VanishService;
65
import com.eternalcode.core.injector.annotations.Inject;
@@ -34,18 +33,16 @@ void onPlayerJoin(PlayerJoinEvent event) {
3433
return;
3534
}
3635

37-
if (!player.hasPlayedBefore()) {
38-
this.noticeService.create()
39-
.noticeOptional(translation -> RandomElementUtil.randomElement(translation.join().playerJoinedServerFirstTime()))
40-
.placeholder("{PLAYER}", player.getName())
41-
.onlinePlayers()
42-
.send();
43-
}
44-
4536
event.setJoinMessage(EMPTY_MESSAGE);
4637

38+
boolean firstTime = !player.hasPlayedBefore();
39+
4740
this.noticeService.create()
48-
.noticeOptional(translation -> RandomElementUtil.randomElement(translation.join().playerJoinedServer()))
41+
.noticeOptional(translation -> RandomElementUtil.randomElement(
42+
firstTime
43+
? translation.join().playerJoinedServerFirstTime()
44+
: translation.join().playerJoinedServer()
45+
))
4946
.placeholder("{PLAYER}", player.getName())
5047
.onlinePlayers()
5148
.sendAsync();

eternalcore-core/src/main/java/com/eternalcode/core/feature/quitmessage/PlayerQuitMessageController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eternalcode.core.feature.quitmessage;
22

3-
43
import com.eternalcode.commons.RandomElementUtil;
54
import com.eternalcode.core.feature.vanish.VanishService;
65
import com.eternalcode.core.injector.annotations.Inject;
@@ -40,6 +39,6 @@ void onPlayerQuit(PlayerQuitEvent event) {
4039
.noticeOptional(translation -> RandomElementUtil.randomElement(translation.quit().playerLeftServer()))
4140
.placeholder("{PLAYER}", player.getName())
4241
.onlinePlayers()
43-
.send();
42+
.sendAsync();
4443
}
4544
}

eternalcore-core/src/main/java/com/eternalcode/core/viewer/BukkitViewerProvider.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
import com.eternalcode.core.user.User;
66
import com.eternalcode.core.user.UserManager;
77
import com.eternalcode.multification.viewer.ViewerProvider;
8-
import java.util.List;
8+
import java.util.Collection;
9+
import java.util.HashSet;
10+
import java.util.Optional;
11+
import java.util.Set;
12+
import java.util.UUID;
913
import org.bukkit.Server;
1014
import org.bukkit.command.BlockCommandSender;
1115
import org.bukkit.command.CommandSender;
1216
import org.bukkit.command.ConsoleCommandSender;
1317
import org.bukkit.command.RemoteConsoleCommandSender;
1418
import org.bukkit.entity.Player;
1519

16-
import java.util.Collection;
17-
import java.util.HashSet;
18-
import java.util.Optional;
19-
import java.util.Set;
20-
import java.util.UUID;
21-
2220
@Service
2321
public class BukkitViewerProvider implements ViewerProvider<Viewer>, ViewerService {
2422

@@ -42,10 +40,10 @@ public Collection<Viewer> all() {
4240

4341
@Override
4442
public Collection<Viewer> onlinePlayers() {
43+
Collection<? extends Player> players = this.server.getOnlinePlayers();
44+
Set<Viewer> audiences = new HashSet<>(players.size());
4545

46-
Set<Viewer> audiences = new HashSet<>();
47-
48-
for (Player player : this.server.getOnlinePlayers()) {
46+
for (Player player : players) {
4947
audiences.add(this.player(player.getUniqueId()));
5048
}
5149

@@ -54,9 +52,10 @@ public Collection<Viewer> onlinePlayers() {
5452

5553
@Override
5654
public Collection<Viewer> onlinePlayers(String permission) {
57-
Set<Viewer> audiences = new HashSet<>();
55+
Collection<? extends Player> players = this.server.getOnlinePlayers();
56+
Set<Viewer> audiences = new HashSet<>(players.size());
5857

59-
for (Player player : this.server.getOnlinePlayers()) {
58+
for (Player player : players) {
6059
if (player.hasPermission(permission)) {
6160
audiences.add(this.player(player.getUniqueId()));
6261
}
@@ -98,11 +97,11 @@ public Viewer any(Object any) {
9897
return BukkitViewerImpl.player(player.getUniqueId());
9998
}
10099

101-
if (any instanceof ConsoleCommandSender || any instanceof RemoteConsoleCommandSender || any instanceof BlockCommandSender) {
100+
if (any instanceof ConsoleCommandSender || any instanceof RemoteConsoleCommandSender
101+
|| any instanceof BlockCommandSender) {
102102
return BukkitViewerImpl.console();
103103
}
104104

105105
throw new IllegalArgumentException("Unsupported sender type: " + any.getClass().getName());
106106
}
107-
108107
}

0 commit comments

Comments
 (0)