Search in sources :

Example 6 with AsyncPlayerChatEvent

use of org.bukkit.event.player.AsyncPlayerChatEvent 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)

Example 7 with AsyncPlayerChatEvent

use of org.bukkit.event.player.AsyncPlayerChatEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldCancelChatEventForNoRemainingRecipients.

@Test
public void shouldCancelChatEventForNoRemainingRecipients() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_CHAT)).willReturn(false);
    AsyncPlayerChatEvent event = newAsyncChatEvent();
    given(listenerService.shouldCancelEvent(any(Player.class))).willReturn(true);
    given(listenerService.shouldCancelEvent(event.getPlayer())).willReturn(false);
    given(settings.getProperty(RestrictionSettings.HIDE_CHAT)).willReturn(true);
    // when
    listener.onPlayerChat(event);
    // then
    verify(listenerService).shouldCancelEvent(event.getPlayer());
    // message sender + 3 recipients = 4
    verify(listenerService, times(4)).shouldCancelEvent(any(Player.class));
    verify(event).setCancelled(true);
    assertThat(event.getRecipients(), empty());
}
Also used : Player(org.bukkit.entity.Player) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) Test(org.junit.Test)

Example 8 with AsyncPlayerChatEvent

use of org.bukkit.event.player.AsyncPlayerChatEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldSendChatToEveryone.

@Test
public void shouldSendChatToEveryone() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_CHAT)).willReturn(false);
    AsyncPlayerChatEvent event = newAsyncChatEvent();
    given(listenerService.shouldCancelEvent(event.getPlayer())).willReturn(false);
    given(settings.getProperty(RestrictionSettings.HIDE_CHAT)).willReturn(false);
    // when
    listener.onPlayerChat(event);
    // then
    verify(listenerService).shouldCancelEvent(event.getPlayer());
    verifyZeroInteractions(event, messages);
}
Also used : AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) Test(org.junit.Test)

Aggregations

AsyncPlayerChatEvent (org.bukkit.event.player.AsyncPlayerChatEvent)8 Test (org.junit.Test)5 Player (org.bukkit.entity.Player)3 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 FutureTask (java.util.concurrent.FutureTask)1 IEssentials (net.ess3.api.IEssentials)1 EventHandler (org.bukkit.event.EventHandler)1 PlayerChatEvent (org.bukkit.event.player.PlayerChatEvent)1 PluginManager (org.bukkit.plugin.PluginManager)1