|
43 | 43 |
|
44 | 44 | public class UpdateNetworksTask {
|
45 | 45 |
|
46 |
| - private static final Map<String, Pair<String, CompletableFuture<Void>>> awaitingMoves = new ConcurrentHashMap<>(); |
| 46 | + private final Skoice plugin; |
| 47 | + private final Map<String, Pair<String, CompletableFuture<Void>>> awaitingMoves = new ConcurrentHashMap<>(); |
47 | 48 |
|
48 | 49 | private final ReentrantLock lock = new ReentrantLock();
|
49 |
| - |
50 |
| - private final Skoice plugin; |
| 50 | + private int taskId; |
51 | 51 |
|
52 | 52 | public UpdateNetworksTask(Skoice plugin) {
|
53 | 53 | this.plugin = plugin;
|
54 | 54 | }
|
55 | 55 |
|
56 |
| - public static Map<String, Pair<String, CompletableFuture<Void>>> getAwaitingMoves() { |
57 |
| - try { |
58 |
| - if (Bukkit.isPrimaryThread() && Bukkit.getPluginManager().isPluginEnabled("Skoice")) { |
59 |
| - new IllegalStateException("This method should not be called from the main thread.").printStackTrace(); |
60 |
| - } |
61 |
| - } catch (NullPointerException ignored) { |
| 56 | + public void start() { |
| 57 | + this.taskId = this.plugin.getServer().getScheduler().runTaskTimerAsynchronously( |
| 58 | + this.plugin, |
| 59 | + this::run, |
| 60 | + 0, |
| 61 | + 10 |
| 62 | + ).getTaskId(); |
| 63 | + } |
| 64 | + |
| 65 | + public void interrupt() { |
| 66 | + this.plugin.getServer().getScheduler().cancelTask(this.taskId); |
| 67 | + |
| 68 | + for (Pair<String, CompletableFuture<Void>> value : this.awaitingMoves.values()) { |
| 69 | + value.getRight().cancel(true); |
62 | 70 | }
|
63 |
| - return UpdateNetworksTask.awaitingMoves; |
64 | 71 | }
|
65 | 72 |
|
66 |
| - public void run() { |
| 73 | + private void run() { |
67 | 74 | try {
|
68 | 75 | if (Bukkit.isPrimaryThread()) {
|
69 | 76 | new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
|
@@ -111,16 +118,16 @@ public void run() {
|
111 | 118 | shouldBeInChannel = mainVoiceChannel;
|
112 | 119 | }
|
113 | 120 |
|
114 |
| - Pair<String, CompletableFuture<Void>> awaitingMove = UpdateNetworksTask.awaitingMoves.get(member.getId()); |
| 121 | + Pair<String, CompletableFuture<Void>> awaitingMove = this.awaitingMoves.get(member.getId()); |
115 | 122 | if (awaitingMove == null
|
116 | 123 | || !awaitingMove.getLeft().equals(shouldBeInChannel.getId())
|
117 | 124 | && awaitingMove.getRight().cancel(false)) {
|
118 | 125 | GuildVoiceState voiceState = member.getVoiceState();
|
119 | 126 | if (voiceState != null && voiceState.getChannel() != shouldBeInChannel) {
|
120 |
| - UpdateNetworksTask.awaitingMoves.put(member.getId(), Pair.of( |
| 127 | + this.awaitingMoves.put(member.getId(), Pair.of( |
121 | 128 | shouldBeInChannel.getId(),
|
122 | 129 | this.plugin.getBot().getGuild().moveVoiceMember(member, shouldBeInChannel)
|
123 |
| - .submit().whenCompleteAsync((v, t) -> UpdateNetworksTask.awaitingMoves.remove(member.getId())) |
| 130 | + .submit().whenCompleteAsync((v, t) -> this.awaitingMoves.remove(member.getId())) |
124 | 131 | ));
|
125 | 132 | }
|
126 | 133 | }
|
@@ -211,7 +218,7 @@ private void manageMoves() {
|
211 | 218 | }
|
212 | 219 | LinkedPlayer.getOnlineLinkedPlayers().forEach(p -> {
|
213 | 220 | if (!p.isInMainVoiceChannel() && !p.isInAnyProximityChannel()) {
|
214 |
| - Pair<String, CompletableFuture<Void>> pair = UpdateNetworksTask.awaitingMoves.get(p.getDiscordId()); |
| 221 | + Pair<String, CompletableFuture<Void>> pair = this.awaitingMoves.get(p.getDiscordId()); |
215 | 222 | if (pair != null) {
|
216 | 223 | pair.getRight().cancel(false);
|
217 | 224 | }
|
|
0 commit comments