diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index df2c815..31ba17d 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -12,6 +12,7 @@ + diff --git a/ButtonCore/src/main/java/buttondevteam/lib/ThorpeUtils.java b/ButtonCore/src/main/java/buttondevteam/lib/ThorpeUtils.java index d69983e..bf19323 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/ThorpeUtils.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/ThorpeUtils.java @@ -1,7 +1,13 @@ package buttondevteam.lib; +import buttondevteam.core.MainPlugin; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; + +import java.util.function.Supplier; public final class ThorpeUtils { private ThorpeUtils() {} @@ -51,4 +57,33 @@ public final class ThorpeUtils { return number.doubleValue(); return number; } + + /** + * Calls the event always asynchronously. The return value is always false if async. + * + * @param event The event to call + * @return The event cancelled state or false if async. + */ + public static boolean callEventAsync(T event) { + Supplier task = () -> { + Bukkit.getPluginManager().callEvent(event); + return event.isCancelled(); + }; + return doItAsync(task, false); + } + + /** + * Does something always asynchronously. It will execute in the same thread if it's not the server thread. + * + * @param what What to do + * @param def Default if async + * @return The event cancelled state or false if async. + */ + public static T doItAsync(Supplier what, T def) { + if (Bukkit.isPrimaryThread()) + Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, what::get); + else + return what.get(); + return def; + } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 2ba2190..92dece9 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -4,10 +4,7 @@ import buttondevteam.core.CommandCaller; import buttondevteam.core.MainPlugin; import buttondevteam.core.component.channel.Channel; import buttondevteam.core.component.channel.Channel.RecipientTestResult; -import buttondevteam.lib.TBMCChatEvent; -import buttondevteam.lib.TBMCChatPreprocessEvent; -import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.TBMCSystemChatEvent; +import buttondevteam.lib.*; import buttondevteam.lib.architecture.Component; import lombok.val; import org.bukkit.Bukkit; @@ -290,11 +287,7 @@ public class TBMCChatAPI { Bukkit.getPluginManager().callEvent(event); return event.isCancelled(); }; - if (Bukkit.isPrimaryThread()) - Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, task::get); - else - return task.get(); - return false; //Not cancelled if async + return ThorpeUtils.doItAsync(task, false); //Not cancelled if async } /** @@ -314,8 +307,7 @@ public class TBMCChatAPI { if (!Arrays.asList(exceptions).contains("Minecraft")) Bukkit.getConsoleSender().sendMessage("[" + channel.DisplayName().get() + "] " + message); TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions, target); - Bukkit.getPluginManager().callEvent(event); - return event.isCancelled(); + return ThorpeUtils.callEventAsync(event); } private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {