Skip to content

Commit be344a8

Browse files
committed
Optimize task management
1 parent 195af05 commit be344a8

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/main/java/net/clementraynaud/skoice/Skoice.java

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.clementraynaud.skoice.storage.config.OutdatedConfig;
3535
import net.clementraynaud.skoice.system.ListenerManager;
3636
import net.clementraynaud.skoice.tasks.InterruptSystemTask;
37+
import net.clementraynaud.skoice.tasks.UpdateNetworksTask;
3738
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
3839
import org.bukkit.GameMode;
3940
import org.bukkit.plugin.java.JavaPlugin;
@@ -51,6 +52,7 @@ public class Skoice extends JavaPlugin {
5152
private LoginNotificationYamlFile loginNotificationYamlFile;
5253
private ListenerManager listenerManager;
5354
private Bot bot;
55+
private UpdateNetworksTask updateNetworksTask;
5456
private BukkitAudiences adventure;
5557
private HookManager hookManager;
5658
private AnalyticManager analyticManager;
@@ -86,6 +88,7 @@ public void onEnable() {
8688
this.listenerManager.registerPermanentMinecraftListeners();
8789
this.bot = new Bot(this);
8890
this.bot.connect();
91+
this.updateNetworksTask = new UpdateNetworksTask(this);
8992
this.adventure = BukkitAudiences.create(this);
9093
new SkoiceCommand(this).init();
9194
this.hookManager = new HookManager(this);
@@ -160,6 +163,10 @@ public Bot getBot() {
160163
return this.bot;
161164
}
162165

166+
public UpdateNetworksTask getUpdateNetworksTask() {
167+
return this.updateNetworksTask;
168+
}
169+
163170
public HookManager getHookManager() {
164171
return this.hookManager;
165172
}

src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java

-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import net.clementraynaud.skoice.Skoice;
2424
import net.clementraynaud.skoice.bot.BotStatus;
2525
import net.clementraynaud.skoice.storage.config.ConfigField;
26-
import net.clementraynaud.skoice.tasks.UpdateNetworksTask;
2726
import net.dv8tion.jda.api.Permission;
2827
import net.dv8tion.jda.api.entities.Guild;
2928
import net.dv8tion.jda.api.events.session.ReadyEvent;
@@ -110,16 +109,6 @@ private void handlePublicBot(Player tokenManager) {
110109
}
111110

112111
private void setup(Player tokenManager) {
113-
this.plugin.getServer().getScheduler().runTaskLater(this.plugin, () ->
114-
this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(
115-
this.plugin,
116-
new UpdateNetworksTask(this.plugin)::run,
117-
0,
118-
10
119-
),
120-
0
121-
);
122-
123112
this.plugin.getConfigYamlFile().removeInvalidVoiceChannelId();
124113

125114
this.plugin.getBot().setDefaultAvatar();

src/main/java/net/clementraynaud/skoice/system/ListenerManager.java

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import net.clementraynaud.skoice.menus.ConfigurationMenus;
4343
import net.clementraynaud.skoice.menus.EmbeddedMenu;
4444
import net.clementraynaud.skoice.tasks.InterruptSystemTask;
45+
import net.clementraynaud.skoice.tasks.UpdateNetworksTask;
4546
import net.dv8tion.jda.api.entities.User;
4647
import org.bukkit.event.HandlerList;
4748

@@ -75,6 +76,7 @@ public void update(User user) {
7576
this.registerBotListeners();
7677
this.plugin.getLinksYamlFile().refreshOnlineLinkedPlayers();
7778
this.plugin.getBot().getVoiceChannel().notifyUnlinkedUsers();
79+
this.plugin.getUpdateNetworksTask().start();
7880
this.plugin.getLogger().info(this.plugin.getLang().getMessage("logger.info.configuration-complete"));
7981
if (user != null) {
8082
new EmbeddedMenu(this.plugin.getBot()).setContent("configuration-complete",

src/main/java/net/clementraynaud/skoice/tasks/InterruptSystemTask.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828
import net.dv8tion.jda.api.entities.Guild;
2929
import net.dv8tion.jda.api.entities.Member;
3030
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
31-
import net.dv8tion.jda.internal.utils.tuple.Pair;
3231
import org.bukkit.Bukkit;
3332

34-
import java.util.concurrent.CompletableFuture;
35-
3633
public class InterruptSystemTask {
3734

3835
private final Skoice plugin;
@@ -48,9 +45,8 @@ public void run() {
4845
}
4946
} catch (NullPointerException ignored) {
5047
}
51-
for (Pair<String, CompletableFuture<Void>> value : UpdateNetworksTask.getAwaitingMoves().values()) {
52-
value.getRight().cancel(true);
53-
}
48+
49+
this.plugin.getUpdateNetworksTask().interrupt();
5450

5551
Guild guild = this.plugin.getBot().getGuild();
5652

src/main/java/net/clementraynaud/skoice/tasks/UpdateNetworksTask.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,34 @@
4343

4444
public class UpdateNetworksTask {
4545

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<>();
4748

4849
private final ReentrantLock lock = new ReentrantLock();
49-
50-
private final Skoice plugin;
50+
private int taskId;
5151

5252
public UpdateNetworksTask(Skoice plugin) {
5353
this.plugin = plugin;
5454
}
5555

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);
6270
}
63-
return UpdateNetworksTask.awaitingMoves;
6471
}
6572

66-
public void run() {
73+
private void run() {
6774
try {
6875
if (Bukkit.isPrimaryThread()) {
6976
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
@@ -111,16 +118,16 @@ public void run() {
111118
shouldBeInChannel = mainVoiceChannel;
112119
}
113120

114-
Pair<String, CompletableFuture<Void>> awaitingMove = UpdateNetworksTask.awaitingMoves.get(member.getId());
121+
Pair<String, CompletableFuture<Void>> awaitingMove = this.awaitingMoves.get(member.getId());
115122
if (awaitingMove == null
116123
|| !awaitingMove.getLeft().equals(shouldBeInChannel.getId())
117124
&& awaitingMove.getRight().cancel(false)) {
118125
GuildVoiceState voiceState = member.getVoiceState();
119126
if (voiceState != null && voiceState.getChannel() != shouldBeInChannel) {
120-
UpdateNetworksTask.awaitingMoves.put(member.getId(), Pair.of(
127+
this.awaitingMoves.put(member.getId(), Pair.of(
121128
shouldBeInChannel.getId(),
122129
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()))
124131
));
125132
}
126133
}
@@ -211,7 +218,7 @@ private void manageMoves() {
211218
}
212219
LinkedPlayer.getOnlineLinkedPlayers().forEach(p -> {
213220
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());
215222
if (pair != null) {
216223
pair.getRight().cancel(false);
217224
}

0 commit comments

Comments
 (0)