Search in sources :

Example 1 with PlayerChatFormatter

use of org.spongepowered.api.entity.living.player.PlayerChatFormatter in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin_Vanilla method vanilla$onProcessChatMessage.

@Inject(method = "handleChat(Ljava/lang/String;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;Ljava/util/UUID;)V"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
private void vanilla$onProcessChatMessage(String var1, CallbackInfo ci, net.minecraft.network.chat.Component component) {
    ChatFormatter.formatChatComponent((net.minecraft.network.chat.TranslatableComponent) component);
    final ServerPlayer player = (ServerPlayer) this.player;
    final PlayerChatFormatter chatFormatter = player.chatFormatter();
    final TextComponent rawMessage = Component.text(var1);
    try (CauseStackManager.StackFrame frame = PhaseTracker.SERVER.pushCauseFrame()) {
        frame.pushCause(this.player);
        final Audience audience = (Audience) this.server;
        final PlayerChatEvent event = SpongeEventFactory.createPlayerChatEvent(frame.currentCause(), audience, Optional.of(audience), chatFormatter, Optional.of(chatFormatter), rawMessage, rawMessage);
        if (SpongeCommon.post(event)) {
            ci.cancel();
        } else {
            event.chatFormatter().ifPresent(formatter -> event.audience().map(SpongeAdventure::unpackAudiences).ifPresent(targets -> {
                for (Audience target : targets) {
                    formatter.format(player, target, event.message(), event.originalMessage()).ifPresent(formattedMessage -> target.sendMessage(player, formattedMessage));
                }
            }));
        }
    }
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) Inventory(org.spongepowered.api.item.inventory.Inventory) NonNull(org.checkerframework.checker.nullness.qual.NonNull) RecipeBookMenu(net.minecraft.world.inventory.RecipeBookMenu) Inject(org.spongepowered.asm.mixin.injection.Inject) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) PlayerList(net.minecraft.server.players.PlayerList) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) MinecraftServer(net.minecraft.server.MinecraftServer) Mixin(org.spongepowered.asm.mixin.Mixin) Component(net.kyori.adventure.text.Component) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) CauseStackManager(org.spongepowered.api.event.CauseStackManager) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) ServerGamePacketListener(net.minecraft.network.protocol.game.ServerGamePacketListener) LocalCapture(org.spongepowered.asm.mixin.injection.callback.LocalCapture) At(org.spongepowered.asm.mixin.injection.At) TextComponent(net.kyori.adventure.text.TextComponent) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) EngineConnection(org.spongepowered.api.network.EngineConnection) Sponge(org.spongepowered.api.Sponge) PlayerChatEvent(org.spongepowered.api.event.message.PlayerChatEvent) SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) Group(org.spongepowered.asm.mixin.injection.Group) ChatType(net.minecraft.network.chat.ChatType) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) UUID(java.util.UUID) Final(org.spongepowered.asm.mixin.Final) QueryTypes(org.spongepowered.api.item.inventory.query.QueryTypes) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) PlayerChatFormatter(org.spongepowered.api.entity.living.player.PlayerChatFormatter) Audience(net.kyori.adventure.audience.Audience) Recipe(net.minecraft.world.item.crafting.Recipe) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) ServerboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor) ChatFormatter(org.spongepowered.vanilla.chat.ChatFormatter) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) PlayerChatEvent(org.spongepowered.api.event.message.PlayerChatEvent) Audience(net.kyori.adventure.audience.Audience) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PlayerChatFormatter(org.spongepowered.api.entity.living.player.PlayerChatFormatter) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with PlayerChatFormatter

use of org.spongepowered.api.entity.living.player.PlayerChatFormatter in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin_API method simulateChat.

@Override
public PlayerChatEvent simulateChat(final Component message, final Cause cause) {
    Objects.requireNonNull(message, "message");
    Objects.requireNonNull(cause, "cause");
    final PlayerChatFormatter originalRouter = this.chatFormatter();
    final Audience audience = (Audience) this.server;
    final PlayerChatEvent event = SpongeEventFactory.createPlayerChatEvent(cause, audience, Optional.of(audience), originalRouter, Optional.of(originalRouter), message, message);
    if (!SpongeCommon.post(event)) {
        event.chatFormatter().ifPresent(formatter -> event.audience().map(SpongeAdventure::unpackAudiences).ifPresent(targets -> {
            for (final Audience target : targets) {
                formatter.format(this, target, event.message(), event.originalMessage()).ifPresent(formattedMessage -> target.sendMessage(this, formattedMessage));
            }
        }));
    }
    return event;
}
Also used : BookUtil(org.spongepowered.common.util.BookUtil) SpongeServer(org.spongepowered.common.SpongeServer) PlayerAdvancements(net.minecraft.server.PlayerAdvancements) MusicDisc(org.spongepowered.api.effect.sound.music.MusicDisc) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) ClientboundCustomSoundPacket(net.minecraft.network.protocol.game.ClientboundCustomSoundPacket) Registry(net.minecraft.core.Registry) SpongeGameProfile(org.spongepowered.common.profile.SpongeGameProfile) CooldownTracker(org.spongepowered.api.entity.living.player.CooldownTracker) MinecraftServer(net.minecraft.server.MinecraftServer) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) Mixin(org.spongepowered.asm.mixin.Mixin) Locale(java.util.Locale) Duration(java.time.Duration) GameProfile(org.spongepowered.api.profile.GameProfile) ServerScoreboardBridge(org.spongepowered.common.bridge.server.ServerScoreboardBridge) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) SoundStop(net.kyori.adventure.sound.SoundStop) AdvancementProgress(org.spongepowered.api.advancement.AdvancementProgress) ServerBossEvent(net.minecraft.server.level.ServerBossEvent) TitlePart(net.kyori.adventure.title.TitlePart) ClientboundStopSoundPacket(net.minecraft.network.protocol.game.ClientboundStopSoundPacket) User(org.spongepowered.api.entity.living.player.User) NetworkUtil(org.spongepowered.common.util.NetworkUtil) Collection(java.util.Collection) ClientboundSetTitlesPacket(net.minecraft.network.protocol.game.ClientboundSetTitlesPacket) PlayerChatEvent(org.spongepowered.api.event.message.PlayerChatEvent) WorldBorderAccessor(org.spongepowered.common.accessor.world.level.border.WorldBorderAccessor) SpongeResourcePack(org.spongepowered.common.resourcepack.SpongeResourcePack) Set(java.util.Set) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) PermissionChecker(net.kyori.adventure.permission.PermissionChecker) Final(org.spongepowered.asm.mixin.Final) Instant(java.time.Instant) TabList(org.spongepowered.api.entity.living.player.tab.TabList) BlockState(org.spongepowered.api.block.BlockState) PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) Objects(java.util.Objects) Keys(org.spongepowered.api.data.Keys) List(java.util.List) Sound(net.kyori.adventure.sound.Sound) PlayerChatFormatter(org.spongepowered.api.entity.living.player.PlayerChatFormatter) BlockPos(net.minecraft.core.BlockPos) SoundEvent(net.minecraft.sounds.SoundEvent) SpongeUserView(org.spongepowered.common.entity.player.SpongeUserView) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) Packet(net.minecraft.network.protocol.Packet) NotNull(org.jetbrains.annotations.NotNull) ClientboundChatPacket(net.minecraft.network.protocol.game.ClientboundChatPacket) ClientboundSetBorderPacket(net.minecraft.network.protocol.game.ClientboundSetBorderPacket) ChangeWorldBorderEvent(org.spongepowered.api.event.world.ChangeWorldBorderEvent) Pointers(net.kyori.adventure.pointer.Pointers) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ServerWorld(org.spongepowered.api.world.server.ServerWorld) WorldType(org.spongepowered.api.world.WorldType) ServerPlayerConnection(org.spongepowered.api.network.ServerPlayerConnection) Book(net.kyori.adventure.inventory.Book) WorldBorder(org.spongepowered.api.world.border.WorldBorder) Overwrite(org.spongepowered.asm.mixin.Overwrite) Value(org.spongepowered.api.data.value.Value) ClientboundResourcePackPacket(net.minecraft.network.protocol.game.ClientboundResourcePackPacket) WorldBorderBridge(org.spongepowered.common.bridge.world.level.border.WorldBorderBridge) ClientboundBlockUpdatePacket(net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket) Title(net.kyori.adventure.title.Title) Component(net.kyori.adventure.text.Component) ClientboundSoundPacket(net.minecraft.network.protocol.game.ClientboundSoundPacket) Nullable(javax.annotation.Nullable) SpongeTabList(org.spongepowered.common.entity.player.tab.SpongeTabList) ClientboundSoundEntityPacket(net.minecraft.network.protocol.game.ClientboundSoundEntityPacket) Identity(net.kyori.adventure.identity.Identity) BossBar(net.kyori.adventure.bossbar.BossBar) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Cause(org.spongepowered.api.event.Cause) PlayerMixin_API(org.spongepowered.common.mixin.api.minecraft.world.entity.player.PlayerMixin_API) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) Entity(net.minecraft.world.entity.Entity) ChronoUnit(java.time.temporal.ChronoUnit) Vector3d(org.spongepowered.math.vector.Vector3d) Audience(net.kyori.adventure.audience.Audience) Advancement(org.spongepowered.api.advancement.Advancement) MessageType(net.kyori.adventure.audience.MessageType) SpongeParticleHelper(org.spongepowered.common.effect.particle.SpongeParticleHelper) Collections(java.util.Collections) AdvancementTree(org.spongepowered.api.advancement.AdvancementTree) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) SpongeMusicDisc(org.spongepowered.common.effect.record.SpongeMusicDisc) Vector3i(org.spongepowered.math.vector.Vector3i) PlayerChatEvent(org.spongepowered.api.event.message.PlayerChatEvent) Audience(net.kyori.adventure.audience.Audience) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PlayerChatFormatter(org.spongepowered.api.entity.living.player.PlayerChatFormatter)

Aggregations

Optional (java.util.Optional)2 Audience (net.kyori.adventure.audience.Audience)2 Component (net.kyori.adventure.text.Component)2 MinecraftServer (net.minecraft.server.MinecraftServer)2 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 MessageType (net.kyori.adventure.audience.MessageType)1 BossBar (net.kyori.adventure.bossbar.BossBar)1 Identity (net.kyori.adventure.identity.Identity)1 Book (net.kyori.adventure.inventory.Book)1