allow DiscordMessageEvent calls from any context

This commit is contained in:
Josh Roy 2023-04-02 20:58:56 -04:00
parent 2ba1eb0cf9
commit 7279be7850
No known key found for this signature in database
GPG Key ID: 86A69D08540BC29A
3 changed files with 6 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package net.essentialsx.api.v2.events.discord;
import net.essentialsx.api.v2.services.discord.MessageType;
import org.bukkit.Bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
@ -10,6 +11,8 @@ import java.util.UUID;
/**
* Fired before a message is about to be sent to a Discord channel.
* <p>
* Note: This event has no guarantee of the thread it is fired on, please use {@link #isAsynchronous()}} to see if this event is off the main Bukkit thread.
*/
public class DiscordMessageEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
@ -40,6 +43,7 @@ public class DiscordMessageEvent extends Event implements Cancellable {
* @param uuid The UUID of the player which caused this event or null if this wasn't a player triggered event.
*/
public DiscordMessageEvent(final MessageType type, final String message, final boolean allowGroupMentions, final String avatarUrl, final String name, final UUID uuid) {
super(!Bukkit.isPrimaryThread());
this.type = type;
this.message = message;
this.allowGroupMentions = allowGroupMentions;

View File

@ -258,11 +258,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
logger.warning("Sending message to channel \"" + type.getKey() + "\" which is an unregistered type! If you are a plugin author, you should be registering your MessageType before using them.");
}
final DiscordMessageEvent event = new DiscordMessageEvent(type, FormatUtil.stripFormat(message), allowGroupMentions);
if (Bukkit.getServer().isPrimaryThread()) {
Bukkit.getPluginManager().callEvent(event);
} else {
Bukkit.getScheduler().runTask(plugin, () -> Bukkit.getPluginManager().callEvent(event));
}
}
@Override

View File

@ -214,6 +214,6 @@ public final class DiscordUtil {
return;
}
jda.getPlugin().getEss().scheduleGlobalDelayedTask(() -> Bukkit.getPluginManager().callEvent(event));
Bukkit.getPluginManager().callEvent(event);
}
}