Search in sources :

Example 1 with DefaultBodyApplier

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

Aggregations

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