Search in sources :

Example 1 with MessageChannelEvent

use of org.spongepowered.api.event.message.MessageChannelEvent in project SpongeCommon by SpongePowered.

the class ChatUtil method sendMessage.

public static void sendMessage(ITextComponent component, MessageChannel channel, CommandSource source, boolean isChat) {
    Text raw = SpongeTexts.toText(component);
    MessageFormatter formatter = new MessageEvent.MessageFormatter(raw);
    final boolean isMainThread = Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
    Cause cause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), source);
    MessageChannelEvent event;
    if (isChat) {
        event = SpongeEventFactory.createMessageChannelEventChat(cause, channel, Optional.of(channel), formatter, raw, false);
    } else {
        event = SpongeEventFactory.createMessageChannelEvent(cause, channel, Optional.of(channel), formatter, false);
    }
    if (!SpongeImpl.postEvent(event) && !event.isMessageCancelled() && event.getChannel().isPresent()) {
        event.getChannel().get().send(source, event.getMessage(), isChat ? ChatTypes.CHAT : ChatTypes.SYSTEM);
    }
}
Also used : MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) Cause(org.spongepowered.api.event.cause.Cause) Text(org.spongepowered.api.text.Text) MessageFormatter(org.spongepowered.api.event.message.MessageEvent.MessageFormatter)

Example 2 with MessageChannelEvent

use of org.spongepowered.api.event.message.MessageChannelEvent in project UltimateChat by FabioZumbi12.

the class UCChannel method sendMessage.

/**
 * Send a message from a channel as player.
 * @param src {@code Player}
 * @param message {@code Text} - Message to send.
 * @param direct {@code boolean} - Send message direct to players on channel.
 */
public void sendMessage(Player src, Text message, boolean direct) {
    if (direct) {
        for (Player p : Sponge.getServer().getOnlinePlayers()) {
            UCChannel chp = UChat.get().getPlayerChannel(p);
            if (UChat.get().getPerms().channelReadPerm(p, this) && !this.isIgnoring(p.getName()) && (!this.neeFocus() || chp.equals(this))) {
                UChat.get().getLogger().timings(timingType.START, "UCChannel#sendMessage()|Direct Message");
                p.sendMessage(message);
            }
        }
        src.sendMessage(message);
    } else {
        MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(UChat.get().getVHelper().getCause(src), src.getMessageChannel(), Optional.of(src.getMessageChannel()), new MessageEvent.MessageFormatter(Text.builder("<" + src.getName() + "> ").onShiftClick(TextActions.insertText(src.getName())).onClick(TextActions.suggestCommand("/msg " + src.getName())).build(), message), message, false);
        UChat.get().getLogger().timings(timingType.START, "UCChannel#sendMessage()|Fire MessageChannelEvent");
        if (!Sponge.getEventManager().post(event)) {
            UChat.get().tempChannels.put(src.getName(), this.getAlias());
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) MessageEvent(org.spongepowered.api.event.message.MessageEvent)

Aggregations

MessageChannelEvent (org.spongepowered.api.event.message.MessageChannelEvent)2 Player (org.spongepowered.api.entity.living.player.Player)1 Cause (org.spongepowered.api.event.cause.Cause)1 MessageEvent (org.spongepowered.api.event.message.MessageEvent)1 MessageFormatter (org.spongepowered.api.event.message.MessageEvent.MessageFormatter)1 Text (org.spongepowered.api.text.Text)1