Search in sources :

Example 1 with ServerChatEvent

use of net.minecraftforge.event.ServerChatEvent in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createServerChatEvent.

// Server events
private static ServerChatEvent createServerChatEvent(Event event) {
    MessageChannelEvent.Chat spongeEvent = (MessageChannelEvent.Chat) event;
    Optional<Player> player = spongeEvent.getCause().first(Player.class);
    if (!player.isPresent()) {
        return null;
    }
    Text spongeText = spongeEvent.getOriginalMessage();
    ITextComponent component = SpongeTexts.toComponent(spongeText);
    if (!(component instanceof TextComponentTranslation)) {
        component = new TextComponentTranslation("%s", component);
    }
    // Using toPlain here is fine, since the raw message from the client
    // can't have formatting.
    ServerChatEvent forgeEvent = new ServerChatEvent((EntityPlayerMP) player.get(), spongeEvent.getOriginalMessage().toPlain(), (TextComponentTranslation) component);
    ((IMixinInitCause) forgeEvent).initCause(spongeEvent.getCause());
    return forgeEvent;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) ITextComponent(net.minecraft.util.text.ITextComponent) IMixinInitCause(org.spongepowered.common.interfaces.IMixinInitCause) Text(org.spongepowered.api.text.Text) ServerChatEvent(net.minecraftforge.event.ServerChatEvent)

Example 2 with ServerChatEvent

use of net.minecraftforge.event.ServerChatEvent in project SpongeForge by SpongePowered.

the class MixinNetHandlerPlayServer method injectChatEvent.

@Inject(method = "processChatMessage", at = @At(value = "INVOKE", target = "net.minecraftforge.common.ForgeHooks.onServerChatEvent" + "(Lnet/minecraft/network/NetHandlerPlayServer;Ljava/lang/String;Lnet/minecraft/util/text/ITextComponent;)" + "Lnet/minecraft/util/text/ITextComponent;", remap = false), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
public void injectChatEvent(CPacketChatMessage packetIn, CallbackInfo ci, String s, ITextComponent component) {
    final ServerChatEvent event = new ServerChatEvent(this.player, s, component);
    MessageChannelEvent.Chat spongeEvent = (MessageChannelEvent.Chat) ((IMixinEventBus) MinecraftForge.EVENT_BUS).postForgeAndCreateSpongeEvent(event);
    if (!spongeEvent.isCancelled()) {
        Text message = spongeEvent.getMessage();
        if (!spongeEvent.isMessageCancelled()) {
            spongeEvent.getChannel().ifPresent(channel -> channel.send(this.player, message, ChatTypes.CHAT));
        }
        // Chat spam suppression from MC
        this.chatSpamThresholdCount += 20;
        if (this.chatSpamThresholdCount > 200 && !SpongeImpl.getServer().getPlayerList().canSendCommands(this.player.getGameProfile())) {
            this.disconnect(new TextComponentTranslation("disconnect.spam"));
        }
    }
    ci.cancel();
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) Text(org.spongepowered.api.text.Text) ServerChatEvent(net.minecraftforge.event.ServerChatEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)2 ServerChatEvent (net.minecraftforge.event.ServerChatEvent)2 MessageChannelEvent (org.spongepowered.api.event.message.MessageChannelEvent)2 Text (org.spongepowered.api.text.Text)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 Player (org.spongepowered.api.entity.living.player.Player)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1 IMixinInitCause (org.spongepowered.common.interfaces.IMixinInitCause)1