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;
}
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();
}
Aggregations