|
5 | 5 | import net.kyori.adventure.text.Component; |
6 | 6 | import net.kyori.adventure.text.format.NamedTextColor; |
7 | 7 | import org.bukkit.entity.Player; |
| 8 | +import org.bukkit.event.EventHandler; |
| 9 | +import org.bukkit.event.EventPriority; |
| 10 | +import org.bukkit.event.Listener; |
| 11 | +import org.bukkit.event.player.PlayerQuitEvent; |
8 | 12 | import org.bukkit.plugin.messaging.Messenger; |
9 | 13 | import org.bukkit.plugin.messaging.PluginMessageListener; |
10 | 14 | import org.jetbrains.annotations.NotNull; |
|
13 | 17 | import java.io.*; |
14 | 18 | import java.nio.charset.StandardCharsets; |
15 | 19 | import java.util.*; |
16 | | -import java.util.concurrent.Executors; |
17 | | -import java.util.concurrent.ScheduledExecutorService; |
18 | | -import java.util.concurrent.TimeUnit; |
19 | 20 |
|
20 | | -public final class PlayerMessaging implements PluginMessageListener { |
| 21 | +public final class PlayerMessaging implements PluginMessageListener, Listener { |
21 | 22 | public static final String REGISTER = "extras:register"; |
22 | 23 | public static final String UNREGISTER = "extras:unregister"; |
23 | 24 | public static final String MESSAGE = "extras:message"; |
24 | 25 |
|
25 | 26 | private static final Component ERROR = |
26 | 27 | Component.text("Could not send plugin channel message.", NamedTextColor.RED); |
27 | | - private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = |
28 | | - Executors.newSingleThreadScheduledExecutor(); |
29 | 28 | private static final byte END_CHAR_MASK = (byte) 0x80; |
30 | 29 |
|
31 | 30 | private final Main plugin; |
32 | 31 |
|
33 | 32 | public PlayerMessaging(final Main plugin) { |
34 | 33 | this.plugin = plugin; |
35 | | - |
36 | | - SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> { |
37 | | - synchronized (this.listening) { |
38 | | - final Iterator<Map.Entry<String, Set<Player>>> iterator = |
39 | | - this.listening.entrySet().iterator(); |
40 | | - |
41 | | - while (iterator.hasNext()) { |
42 | | - final Map.Entry<String, Set<Player>> entry = iterator.next(); |
43 | | - |
44 | | - final Set<Player> players = entry.getValue(); |
45 | | - synchronized (players) { |
46 | | - // try and avoid issues with other plugins causing player obj leaks |
47 | | - int onlineCount = 0; |
48 | | - |
49 | | - for (final Player player: players) { |
50 | | - if (!player.isOnline()) continue; |
51 | | - onlineCount++; |
52 | | - } |
53 | | - |
54 | | - if (onlineCount != 0) continue; |
55 | | - iterator.remove(); |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - }, 1, 1, TimeUnit.MINUTES); |
60 | 34 | } |
61 | 35 |
|
62 | 36 | private final Map<String, Set<Player>> listening = Collections.synchronizedMap(new HashMap<>()); |
@@ -190,4 +164,24 @@ public void onPluginMessageReceived(final @NotNull String channelName, |
190 | 164 | player.sendMessage(ERROR); |
191 | 165 | } |
192 | 166 | } |
| 167 | + |
| 168 | + @EventHandler(priority = EventPriority.MONITOR) |
| 169 | + public void onPlayerQuit(PlayerQuitEvent event) { |
| 170 | + final Player removedPlayer = event.getPlayer(); |
| 171 | + |
| 172 | + synchronized (this.listening) { |
| 173 | + final Iterator<Map.Entry<String, Set<Player>>> listeningIterator = |
| 174 | + this.listening.entrySet().iterator(); |
| 175 | + |
| 176 | + while (listeningIterator.hasNext()) { |
| 177 | + final Map.Entry<String, Set<Player>> entry = listeningIterator.next(); |
| 178 | + final Set<Player> players = entry.getValue(); |
| 179 | + synchronized (players) { |
| 180 | + players.remove(removedPlayer); |
| 181 | + |
| 182 | + if (players.isEmpty()) listeningIterator.remove(); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
193 | 187 | } |
0 commit comments