Search in sources :

Example 1 with SpongeAdventure

use of org.spongepowered.common.adventure.SpongeAdventure in project SpongeCommon by SpongePowered.

the class SpongeArgumentCommandNode method suggestUsingModifier.

private CompletableFuture<Suggestions> suggestUsingModifier(final CommandContext<?> context, final SuggestionsBuilder suggestionsBuilder, final CompletableFuture<Suggestions> suggestions) {
    if (this.modifier != null) {
        return suggestions.thenApply(x -> {
            final List<CommandCompletion> originalSuggestions = x.getList().stream().map(SpongeCommandCompletion::from).collect(Collectors.toList());
            final List<CommandCompletion> modifiedSuggestions = this.modifier.modifyCompletion((org.spongepowered.api.command.parameter.CommandContext) context, suggestionsBuilder.getRemaining(), new ArrayList<>(originalSuggestions));
            if (originalSuggestions.equals(modifiedSuggestions)) {
                return x;
            }
            final SuggestionsBuilder newBuilder = suggestionsBuilder.restart();
            for (final CommandCompletion suggestion : modifiedSuggestions) {
                newBuilder.suggest(suggestion.completion(), suggestion.tooltip().map(SpongeAdventure::asVanilla).orElse(null));
            }
            return newBuilder.build();
        });
    }
    return suggestionsBuilder.buildFuture();
}
Also used : SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) SpongeCommandCompletion(org.spongepowered.common.command.SpongeCommandCompletion) CommandCompletion(org.spongepowered.api.command.CommandCompletion) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder)

Example 2 with SpongeAdventure

use of org.spongepowered.common.adventure.SpongeAdventure 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 3 with SpongeAdventure

use of org.spongepowered.common.adventure.SpongeAdventure in project SpongeCommon by SpongePowered.

the class SpongeCommandCauseFactory method create.

@Override
@NonNull
public CommandCause create() {
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        final Cause cause = frame.currentCause();
        final CommandSource iCommandSource = cause.first(CommandSource.class).orElseGet(() -> SpongeCommon.game().systemSubject());
        final CommandSourceStack commandSource;
        if (iCommandSource instanceof CommandSourceProviderBridge) {
            // We know about this one so we can create it using the factory method on the source.
            commandSource = ((CommandSourceProviderBridge) iCommandSource).bridge$getCommandSource(cause);
        } else {
            // try to create a command cause from the given ICommandSource, but as Mojang did not see fit to
            // put any identifying characteristics on the object, we have to go it alone...
            final EventContext context = cause.context();
            @Nullable final Locatable locatable = iCommandSource instanceof Locatable ? (Locatable) iCommandSource : null;
            final Component displayName;
            if (iCommandSource instanceof Entity) {
                displayName = ((Entity) iCommandSource).get(Keys.DISPLAY_NAME).map(SpongeAdventure::asVanilla).orElseGet(() -> new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName()));
            } else {
                displayName = new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName());
            }
            final String name = displayName.getString();
            commandSource = new CommandSourceStack(iCommandSource, context.get(EventContextKeys.LOCATION).map(x -> VecHelper.toVanillaVector3d(x.position())).orElseGet(() -> locatable == null ? Vec3.ZERO : VecHelper.toVanillaVector3d(locatable.location().position())), context.get(EventContextKeys.ROTATION).map(rot -> new Vec2((float) rot.x(), (float) rot.y())).orElse(Vec2.ZERO), context.get(EventContextKeys.LOCATION).map(x -> (ServerLevel) x.world()).orElseGet(() -> locatable == null ? SpongeCommon.server().getLevel(Level.OVERWORLD) : (ServerLevel) locatable.serverLocation().world()), 4, name, displayName, SpongeCommon.server(), iCommandSource instanceof Entity ? (net.minecraft.world.entity.Entity) iCommandSource : null);
        }
        // We don't want the command source to have altered the cause here (unless there is the special case of the
        // server), so we reset it back to what it was (in the ctor of CommandSource, it will add the current source
        // to the cause - that's for if the source is created elsewhere, not here)
        ((CommandSourceStackBridge) commandSource).bridge$setCause(frame.currentCause());
        return (CommandCause) commandSource;
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) NonNull(org.checkerframework.checker.nullness.qual.NonNull) EventContextKeys(org.spongepowered.api.event.EventContextKeys) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) CommandSourceProviderBridge(org.spongepowered.common.bridge.commands.CommandSourceProviderBridge) ServerLevel(net.minecraft.server.level.ServerLevel) EventContext(org.spongepowered.api.event.EventContext) Locatable(org.spongepowered.api.world.Locatable) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Nameable(org.spongepowered.api.util.Nameable) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Component(net.minecraft.network.chat.Component) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Entity(org.spongepowered.api.entity.Entity) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) TextComponent(net.minecraft.network.chat.TextComponent) Keys(org.spongepowered.api.data.Keys) Vec2(net.minecraft.world.phys.Vec2) Vec3(net.minecraft.world.phys.Vec3) VecHelper(org.spongepowered.common.util.VecHelper) CommandSource(net.minecraft.commands.CommandSource) Level(net.minecraft.world.level.Level) Entity(org.spongepowered.api.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) CommandSource(net.minecraft.commands.CommandSource) CommandCause(org.spongepowered.api.command.CommandCause) CommandSourceStack(net.minecraft.commands.CommandSourceStack) EventContext(org.spongepowered.api.event.EventContext) Nameable(org.spongepowered.api.util.Nameable) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Vec2(net.minecraft.world.phys.Vec2) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) CommandSourceProviderBridge(org.spongepowered.common.bridge.commands.CommandSourceProviderBridge) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Locatable(org.spongepowered.api.world.Locatable) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 4 with SpongeAdventure

use of org.spongepowered.common.adventure.SpongeAdventure 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

SpongeAdventure (org.spongepowered.common.adventure.SpongeAdventure)4 NonNull (org.checkerframework.checker.nullness.qual.NonNull)3 SpongeCommon (org.spongepowered.common.SpongeCommon)3 Optional (java.util.Optional)2 Audience (net.kyori.adventure.audience.Audience)2 Component (net.kyori.adventure.text.Component)2 Keys (org.spongepowered.api.data.Keys)2 Cause (org.spongepowered.api.event.Cause)2 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)2 SuggestionsBuilder (com.mojang.brigadier.suggestion.SuggestionsBuilder)1 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