Skip to content

Commit 36ce616

Browse files
committed
Add ThreadUtil for main thread checks
1 parent c016f35 commit 36ce616

12 files changed

+87
-162
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class Skoice extends JavaPlugin {
4545

4646
private static final String OUTDATED_MINECRAFT_SERVER_ERROR_MESSAGE = "Skoice only supports Minecraft 1.8 or later. Please update your Minecraft server to use the proximity voice chat.";
4747
private static SkoiceAPI api;
48+
private static AnalyticManager analyticManager;
4849
private MinecraftLang lang;
4950
private ConfigYamlFile configYamlFile;
5051
private LinksYamlFile linksYamlFile;
@@ -55,12 +56,15 @@ public class Skoice extends JavaPlugin {
5556
private UpdateNetworksTask updateNetworksTask;
5657
private BukkitAudiences adventure;
5758
private HookManager hookManager;
58-
private AnalyticManager analyticManager;
5959

6060
public static SkoiceAPI api() {
6161
return Skoice.api;
6262
}
6363

64+
public static AnalyticManager analyticManager() {
65+
return Skoice.analyticManager;
66+
}
67+
6468
@Override
6569
public void onEnable() {
6670
if (!this.isMinecraftServerCompatible()) {
@@ -82,8 +86,8 @@ public void onEnable() {
8286
this.tempYamlFile.load();
8387
this.loginNotificationYamlFile = new LoginNotificationYamlFile(this);
8488
this.loginNotificationYamlFile.load();
85-
this.analyticManager = new AnalyticManager(this);
86-
this.analyticManager.initialize();
89+
Skoice.analyticManager = new AnalyticManager(this);
90+
Skoice.analyticManager.initialize();
8791
Skoice.api = new SkoiceAPI(this);
8892
this.getServer().getPluginManager().registerEvents(Skoice.api, this);
8993
this.listenerManager = new ListenerManager(this);
@@ -170,8 +174,4 @@ public UpdateNetworksTask getUpdateNetworksTask() {
170174
public HookManager getHookManager() {
171175
return this.hookManager;
172176
}
173-
174-
public AnalyticManager getAnalyticManager() {
175-
return this.analyticManager;
176-
}
177177
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private synchronized void update(String version, String expectedHash) {
155155
Files.delete(tempUpdateFile.toPath());
156156
} catch (IOException ignored) {
157157
}
158-
this.plugin.getAnalyticManager().getBugsnag().notify(exception, Severity.WARNING);
158+
Skoice.analyticManager().getBugsnag().notify(exception, Severity.WARNING);
159159
} finally {
160160
if (connection != null) {
161161
connection.disconnect();
@@ -198,7 +198,7 @@ private boolean verifyFileIntegrity(File file, String expectedHash) throws NoSuc
198198
for (byte b : fileHashBytes) {
199199
fileHash.append(String.format("%02x", b));
200200
}
201-
this.plugin.getAnalyticManager().getBugsnag().notify(new IOException("File integrity check failed: expected " + expectedHash + ", got " + fileHash), Severity.WARNING);
201+
Skoice.analyticManager().getBugsnag().notify(new IOException("File integrity check failed: expected " + expectedHash + ", got " + fileHash), Severity.WARNING);
202202
return fileHash.toString().equals(expectedHash);
203203
}
204204
}

src/main/java/net/clementraynaud/skoice/commands/LinkCommand.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
import net.clementraynaud.skoice.Skoice;
2323
import net.clementraynaud.skoice.menus.EmbeddedMenu;
24+
import net.clementraynaud.skoice.util.ThreadUtil;
2425
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
25-
import org.bukkit.Bukkit;
2626

2727
import java.util.Map;
2828
import java.util.Random;
@@ -38,23 +38,13 @@ public LinkCommand(Skoice plugin, CommandExecutor executor, SlashCommandInteract
3838
}
3939

4040
public static Map<String, String> getDiscordIdCode() {
41-
try {
42-
if (Bukkit.isPrimaryThread()) {
43-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
44-
}
45-
} catch (NullPointerException ignored) {
46-
}
41+
ThreadUtil.ensureNotMainThread();
4742
return LinkCommand.discordIdCode;
4843
}
4944

5045
@Override
5146
public void run() {
52-
try {
53-
if (Bukkit.isPrimaryThread()) {
54-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
55-
}
56-
} catch (NullPointerException ignored) {
57-
}
47+
ThreadUtil.ensureNotMainThread();
5848
if (super.plugin.getLinksYamlFile().getLinks().containsValue(super.executor.getUser().getId())) {
5949
new EmbeddedMenu(this.plugin.getBot()).setContent("account-already-linked",
6050
super.plugin.getBot().getCommands().getAsMention(CommandInfo.UNLINK.toString()))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private void setDefaultFailure() {
8282
return;
8383
}
8484

85-
this.plugin.getAnalyticManager().getBugsnag().notify(throwable, Severity.ERROR);
85+
Skoice.analyticManager().getBugsnag().notify(throwable, Severity.ERROR);
8686
defaultFailure.accept(throwable);
8787
});
8888
}

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

+4-19
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import net.clementraynaud.skoice.Skoice;
2323
import net.clementraynaud.skoice.storage.config.ConfigField;
2424
import net.clementraynaud.skoice.util.DistanceUtil;
25+
import net.clementraynaud.skoice.util.ThreadUtil;
2526
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
2627
import net.kyori.adventure.text.Component;
27-
import org.bukkit.Bukkit;
2828
import org.bukkit.ChatColor;
2929
import org.bukkit.GameMode;
3030
import org.bukkit.entity.Player;
@@ -53,22 +53,12 @@ public LinkedPlayer(Skoice plugin, Player player, String discordId) {
5353
}
5454

5555
public static Set<LinkedPlayer> getOnlineLinkedPlayers() {
56-
try {
57-
if (Bukkit.isPrimaryThread()) {
58-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
59-
}
60-
} catch (NullPointerException ignored) {
61-
}
56+
ThreadUtil.ensureNotMainThread();
6257
return LinkedPlayer.onlineLinkedPlayers;
6358
}
6459

6560
public static LinkedPlayer fromMemberId(String memberId) {
66-
try {
67-
if (Bukkit.isPrimaryThread()) {
68-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
69-
}
70-
} catch (NullPointerException ignored) {
71-
}
61+
ThreadUtil.ensureNotMainThread();
7262
return LinkedPlayer.onlineLinkedPlayers.stream()
7363
.filter(p -> p.getDiscordId().equals(memberId))
7464
.findFirst().orElse(null);
@@ -97,12 +87,7 @@ public void sendDisconnectingAlert() {
9787
}
9888

9989
public Set<LinkedPlayer> getPlayersWithinRange() {
100-
try {
101-
if (Bukkit.isPrimaryThread()) {
102-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
103-
}
104-
} catch (NullPointerException ignored) {
105-
}
90+
ThreadUtil.ensureNotMainThread();
10691
return LinkedPlayer.onlineLinkedPlayers.stream()
10792
.filter(p -> p.isInMainVoiceChannel() || p.isInAnyProximityChannel())
10893
.filter(p -> !p.equals(this))

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

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
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;
4645
import net.dv8tion.jda.api.entities.User;
4746
import org.bukkit.event.HandlerList;
4847

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

+8-43
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package net.clementraynaud.skoice.system;
2121

22-
import org.bukkit.Bukkit;
22+
import net.clementraynaud.skoice.util.ThreadUtil;
2323

2424
import java.util.Set;
2525
import java.util.concurrent.ConcurrentHashMap;
@@ -33,56 +33,31 @@ private Networks() {
3333
}
3434

3535
public static Set<Network> getAll() {
36-
try {
37-
if (Bukkit.isPrimaryThread() && Bukkit.getPluginManager().isPluginEnabled("Skoice")) {
38-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
39-
}
40-
} catch (NullPointerException ignored) {
41-
}
36+
ThreadUtil.ensureNotMainThread(true);
4237
return Networks.networkSet;
4338
}
4439

4540
public static Set<Network> getInitialized() {
46-
try {
47-
if (Bukkit.isPrimaryThread()) {
48-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
49-
}
50-
} catch (NullPointerException ignored) {
51-
}
41+
ThreadUtil.ensureNotMainThread();
5242
return Networks.networkSet.stream()
5343
.filter(network -> network.getProximityChannel().isInitialized())
5444
.collect(Collectors.toSet());
5545
}
5646

5747
public static Set<ProximityChannel> getProximityChannels() {
58-
try {
59-
if (Bukkit.isPrimaryThread()) {
60-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
61-
}
62-
} catch (NullPointerException ignored) {
63-
}
48+
ThreadUtil.ensureNotMainThread();
6449
return Networks.networkSet.stream()
6550
.map(Network::getProximityChannel)
6651
.collect(Collectors.toSet());
6752
}
6853

6954
public static void add(Network network) {
70-
try {
71-
if (Bukkit.isPrimaryThread()) {
72-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
73-
}
74-
} catch (NullPointerException ignored) {
75-
}
55+
ThreadUtil.ensureNotMainThread();
7656
Networks.networkSet.add(network);
7757
}
7858

7959
public static void remove(Network network) {
80-
try {
81-
if (Bukkit.isPrimaryThread()) {
82-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
83-
}
84-
} catch (NullPointerException ignored) {
85-
}
60+
ThreadUtil.ensureNotMainThread();
8661
Networks.networkSet.remove(network);
8762
}
8863

@@ -95,24 +70,14 @@ public static void merge(Network network1, Network network2) {
9570
}
9671

9772
public static void clean() {
98-
try {
99-
if (Bukkit.isPrimaryThread()) {
100-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
101-
}
102-
} catch (NullPointerException ignored) {
103-
}
73+
ThreadUtil.ensureNotMainThread();
10474
Networks.networkSet.stream()
10575
.filter(Network::isEmpty)
10676
.forEach(Networks::remove);
10777
}
10878

10979
public static void clear() {
110-
try {
111-
if (Bukkit.isPrimaryThread() && Bukkit.getPluginManager().isPluginEnabled("Skoice")) {
112-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
113-
}
114-
} catch (NullPointerException ignored) {
115-
}
80+
ThreadUtil.ensureNotMainThread(true);
11681
Networks.networkSet.clear();
11782
}
11883
}

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

+8-43
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package net.clementraynaud.skoice.system;
2121

22-
import org.bukkit.Bukkit;
22+
import net.clementraynaud.skoice.util.ThreadUtil;
2323

2424
import java.util.Set;
2525
import java.util.concurrent.ConcurrentHashMap;
@@ -34,65 +34,35 @@ private ProximityChannels() {
3434
}
3535

3636
public static Set<ProximityChannel> getAll() {
37-
try {
38-
if (Bukkit.isPrimaryThread()) {
39-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
40-
}
41-
} catch (NullPointerException ignored) {
42-
}
37+
ThreadUtil.ensureNotMainThread();
4338
return ProximityChannels.proximityChannelSet;
4439
}
4540

4641
public static Set<ProximityChannel> getInitialized() {
47-
try {
48-
if (Bukkit.isPrimaryThread() && Bukkit.getPluginManager().isPluginEnabled("Skoice")) {
49-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
50-
}
51-
} catch (NullPointerException ignored) {
52-
}
42+
ThreadUtil.ensureNotMainThread(true);
5343
return ProximityChannels.proximityChannelSet.stream()
5444
.filter(ProximityChannel::isInitialized)
5545
.collect(Collectors.toSet());
5646
}
5747

5848
public static void add(ProximityChannel proximityChannel) {
59-
try {
60-
if (Bukkit.isPrimaryThread()) {
61-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
62-
}
63-
} catch (NullPointerException ignored) {
64-
}
49+
ThreadUtil.ensureNotMainThread();
6550
ProximityChannels.proximityChannelSet.add(proximityChannel);
6651
}
6752

6853
public static void remove(ProximityChannel proximityChannel) {
69-
try {
70-
if (Bukkit.isPrimaryThread()) {
71-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
72-
}
73-
} catch (NullPointerException ignored) {
74-
}
54+
ThreadUtil.ensureNotMainThread();
7555
ProximityChannels.proximityChannelSet.remove(proximityChannel);
7656
}
7757

7858
public static void remove(String channelId) {
79-
try {
80-
if (Bukkit.isPrimaryThread()) {
81-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
82-
}
83-
} catch (NullPointerException ignored) {
84-
}
59+
ThreadUtil.ensureNotMainThread();
8560
ProximityChannels.proximityChannelSet.removeIf(proximityChannel ->
8661
proximityChannel.getChannelId().equals(channelId));
8762
}
8863

8964
public static void clean(int possibleUsers) {
90-
try {
91-
if (Bukkit.isPrimaryThread()) {
92-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
93-
}
94-
} catch (NullPointerException ignored) {
95-
}
65+
ThreadUtil.ensureNotMainThread();
9666
int possibleNetworks = possibleUsers / 2;
9767

9868
ProximityChannels.proximityChannelSet.stream()
@@ -107,12 +77,7 @@ public static void clean(int possibleUsers) {
10777
}
10878

10979
public static void clear() {
110-
try {
111-
if (Bukkit.isPrimaryThread() && Bukkit.getPluginManager().isPluginEnabled("Skoice")) {
112-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
113-
}
114-
} catch (NullPointerException ignored) {
115-
}
80+
ThreadUtil.ensureNotMainThread(true);
11681
ProximityChannels.proximityChannelSet.clear();
11782
}
11883

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import net.clementraynaud.skoice.system.Networks;
2525
import net.clementraynaud.skoice.system.ProximityChannel;
2626
import net.clementraynaud.skoice.system.ProximityChannels;
27+
import net.clementraynaud.skoice.util.ThreadUtil;
2728
import net.dv8tion.jda.api.Permission;
2829
import net.dv8tion.jda.api.entities.Guild;
2930
import net.dv8tion.jda.api.entities.Member;
3031
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
31-
import org.bukkit.Bukkit;
3232

3333
import java.util.List;
3434

@@ -41,12 +41,7 @@ public InterruptSystemTask(Skoice plugin) {
4141
}
4242

4343
public void run() {
44-
try {
45-
if (Bukkit.isPrimaryThread() && this.plugin.isEnabled()) {
46-
new IllegalStateException("This method should not be called from the main thread.").printStackTrace();
47-
}
48-
} catch (NullPointerException ignored) {
49-
}
44+
ThreadUtil.ensureNotMainThread(true);
5045

5146
this.plugin.getUpdateNetworksTask().interrupt();
5247

0 commit comments

Comments
 (0)