Search in sources :

Example 1 with MessageFormatter

use of org.spongepowered.api.event.message.MessageEvent.MessageFormatter in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createMessageChannelEventChat.

public static MessageChannelEvent.Chat createMessageChannelEventChat(ServerChatEvent forgeEvent) {
    final ITextComponent forgeComponent = forgeEvent.getComponent();
    final MessageFormatter formatter = new MessageFormatter();
    MessageChannel channel;
    Text[] chat = SpongeTexts.splitChatMessage((TextComponentTranslation) forgeComponent);
    if (chat[1] == null) {
        // Move content from head part to body part
        chat[1] = chat[0] != null ? chat[0] : SpongeTexts.toText(forgeComponent);
        chat[0] = null;
    }
    if (chat[0] != null) {
        formatter.getHeader().add(new DefaultHeaderApplier(chat[0]));
    }
    formatter.getBody().add(new DefaultBodyApplier(chat[1]));
    Text rawSpongeMessage = Text.of(forgeEvent.getMessage());
    MessageChannel originalChannel = channel = ((Player) forgeEvent.getPlayer()).getMessageChannel();
    Sponge.getCauseStackManager().pushCause(forgeEvent.getPlayer());
    return SpongeEventFactory.createMessageChannelEventChat(Sponge.getCauseStackManager().getCurrentCause(), originalChannel, Optional.ofNullable(channel), formatter, rawSpongeMessage, false);
}
Also used : MessageChannel(org.spongepowered.api.text.channel.MessageChannel) DefaultHeaderApplier(org.spongepowered.api.event.message.MessageEvent.DefaultHeaderApplier) ITextComponent(net.minecraft.util.text.ITextComponent) DefaultBodyApplier(org.spongepowered.api.event.message.MessageEvent.DefaultBodyApplier) Text(org.spongepowered.api.text.Text) MessageFormatter(org.spongepowered.api.event.message.MessageEvent.MessageFormatter)

Example 2 with MessageFormatter

use of org.spongepowered.api.event.message.MessageEvent.MessageFormatter 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)

Aggregations

MessageFormatter (org.spongepowered.api.event.message.MessageEvent.MessageFormatter)2 Text (org.spongepowered.api.text.Text)2 ITextComponent (net.minecraft.util.text.ITextComponent)1 Cause (org.spongepowered.api.event.cause.Cause)1 MessageChannelEvent (org.spongepowered.api.event.message.MessageChannelEvent)1 DefaultBodyApplier (org.spongepowered.api.event.message.MessageEvent.DefaultBodyApplier)1 DefaultHeaderApplier (org.spongepowered.api.event.message.MessageEvent.DefaultHeaderApplier)1 MessageChannel (org.spongepowered.api.text.channel.MessageChannel)1