Fixed schrestart message async event stuff

This commit is contained in:
Norbi Peti 2019-08-11 20:02:30 +02:00
parent fa6c453a8d
commit 14bcebabcd
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 39 additions and 11 deletions

View file

@ -12,6 +12,7 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" /> <orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" /> <orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" /> <orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" /> <orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -1,7 +1,13 @@
package buttondevteam.lib; package buttondevteam.lib;
import buttondevteam.core.MainPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import java.util.function.Supplier;
public final class ThorpeUtils { public final class ThorpeUtils {
private ThorpeUtils() {} private ThorpeUtils() {}
@ -51,4 +57,33 @@ public final class ThorpeUtils {
return number.doubleValue(); return number.doubleValue();
return number; 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 <T extends Event & Cancellable> boolean callEventAsync(T event) {
Supplier<Boolean> 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> T doItAsync(Supplier<T> what, T def) {
if (Bukkit.isPrimaryThread())
Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, what::get);
else
return what.get();
return def;
}
} }

View file

@ -4,10 +4,7 @@ import buttondevteam.core.CommandCaller;
import buttondevteam.core.MainPlugin; import buttondevteam.core.MainPlugin;
import buttondevteam.core.component.channel.Channel; import buttondevteam.core.component.channel.Channel;
import buttondevteam.core.component.channel.Channel.RecipientTestResult; import buttondevteam.core.component.channel.Channel.RecipientTestResult;
import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.*;
import buttondevteam.lib.TBMCChatPreprocessEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -290,11 +287,7 @@ public class TBMCChatAPI {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
}; };
if (Bukkit.isPrimaryThread()) return ThorpeUtils.doItAsync(task, false); //Not cancelled if async
Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, task::get);
else
return task.get();
return false; //Not cancelled if async
} }
/** /**
@ -314,8 +307,7 @@ public class TBMCChatAPI {
if (!Arrays.asList(exceptions).contains("Minecraft")) if (!Arrays.asList(exceptions).contains("Minecraft"))
Bukkit.getConsoleSender().sendMessage("[" + channel.DisplayName().get() + "] " + message); Bukkit.getConsoleSender().sendMessage("[" + channel.DisplayName().get() + "] " + message);
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions, target); TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions, target);
Bukkit.getPluginManager().callEvent(event); return ThorpeUtils.callEventAsync(event);
return event.isCancelled();
} }
private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) { private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {