Search in sources :

Example 1 with PlayerChatEvent

use of org.bukkit.event.player.PlayerChatEvent in project Denizen-For-Bukkit by DenizenScript.

the class ChatTrigger method asyncChatTrigger.

@EventHandler
public void asyncChatTrigger(final AsyncPlayerChatEvent event) {
    if (HyperDebug) {
        dB.log("Chat trigger seen, cancelled: " + event.isCancelled() + ", chatasync: " + Settings.chatAsynchronous());
    }
    if (event.isCancelled()) {
        return;
    }
    // Return if "Use asynchronous event" is false in config file
    if (!Settings.chatAsynchronous()) {
        return;
    }
    if (!event.isAsynchronous()) {
        syncChatTrigger(new PlayerChatEvent(event.getPlayer(), event.getMessage(), event.getFormat(), event.getRecipients()));
        return;
    }
    FutureTask<ChatContext> futureTask = new FutureTask<ChatContext>(new Callable<ChatContext>() {

        @Override
        public ChatContext call() {
            return process(event.getPlayer(), event.getMessage());
        }
    });
    Bukkit.getScheduler().runTask(DenizenAPI.getCurrentInstance(), futureTask);
    try {
        ChatContext context = futureTask.get();
        if (context.wasTriggered()) {
            event.setCancelled(true);
        }
        if (context.hasChanges()) {
            event.setMessage(context.getChanges());
        }
    } catch (Exception e) {
        dB.echoError(e);
    }
}
Also used : AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) PlayerChatEvent(org.bukkit.event.player.PlayerChatEvent) FutureTask(java.util.concurrent.FutureTask) EventHandler(org.bukkit.event.EventHandler)

Aggregations

FutureTask (java.util.concurrent.FutureTask)1 EventHandler (org.bukkit.event.EventHandler)1 AsyncPlayerChatEvent (org.bukkit.event.player.AsyncPlayerChatEvent)1 PlayerChatEvent (org.bukkit.event.player.PlayerChatEvent)1