Search in sources :

Example 11 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class ActivePagination method specificPage.

public void specificPage(final int page) throws CommandException {
    final Audience src = this.src.get().orElseThrow(() -> new CommandException(Component.text("Source for pagination " + this.getId() + " is no longer active!")));
    this.currentPage = page;
    final List<Component> toSend = new ArrayList<>();
    final Component title = this.title;
    if (title != null) {
        toSend.add(title);
    }
    if (this.header != null) {
        toSend.add(this.header);
    }
    for (final Component line : this.getLines(page)) {
        toSend.add(line);
    }
    final Component footer = this.calculateFooter(page);
    toSend.add(this.calc.center(footer, this.padding));
    if (this.footer != null) {
        toSend.add(this.footer);
    }
    for (final Component line : toSend) {
        src.sendMessage(Identity.nil(), line);
    }
}
Also used : Audience(net.kyori.adventure.audience.Audience) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.exception.CommandException) TextComponent(net.kyori.adventure.text.TextComponent) Component(net.kyori.adventure.text.Component)

Example 12 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class SpongePaginationList method sendTo.

@Override
public void sendTo(final Audience receiver, final int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    final Iterable<Map.Entry<Component, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        final int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Component title = this.title;
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    // If the Audience is a Player, then upon death, they will become a different Audience object.
    // Thus, we use a supplier to supply the player from the server, if required.
    final Supplier<Optional<? extends Audience>> audienceSupplier;
    if (receiver instanceof Player) {
        final UUID playerUuid = ((Player) receiver).uniqueId();
        audienceSupplier = () -> Sponge.server().player(playerUuid);
    } else {
        final WeakReference<Audience> srcReference = new WeakReference<>(receiver);
        audienceSupplier = () -> Optional.ofNullable(srcReference.get());
    }
    final ActivePagination pagination;
    if (this.contents instanceof List) {
        // If it started out as a list, it's probably reasonable to copy it to another list
        pagination = new ListPagination(audienceSupplier, calculator, ImmutableList.copyOf(counts), title, this.header, this.footer, this.paginationSpacer);
    } else {
        pagination = new IterablePagination(audienceSupplier, calculator, counts, title, this.header, this.footer, this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (final CommandException e) {
        final Component text = e.componentMessage();
        if (text != null) {
            receiver.sendMessage(Identity.nil(), text.color(NamedTextColor.RED));
        }
    }
}
Also used : Identity(net.kyori.adventure.identity.Identity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Audience(net.kyori.adventure.audience.Audience) Component(net.kyori.adventure.text.Component) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) Player(org.spongepowered.api.entity.living.player.Player) WeakReference(java.lang.ref.WeakReference) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CommandException(org.spongepowered.api.command.exception.CommandException) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) Audience(net.kyori.adventure.audience.Audience) CommandException(org.spongepowered.api.command.exception.CommandException) WeakReference(java.lang.ref.WeakReference) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Component(net.kyori.adventure.text.Component) UUID(java.util.UUID)

Example 13 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class PlayerAdvancementsMixin method impl$callGrantEventIfSuccessful.

@Inject(method = "award", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerAdvancements;ensureVisibility(Lnet/minecraft/advancements/Advancement;)V"))
private void impl$callGrantEventIfSuccessful(final Advancement advancement, final String string, final CallbackInfoReturnable<Boolean> ci) {
    if (!this.impl$wasSuccess) {
        return;
    }
    final Instant instant = Instant.now();
    final Audience channel;
    if (this.impl$message != null) {
        channel = Sponge.server().broadcastAudience();
    } else {
        channel = Audience.empty();
    }
    final AdvancementEvent.Grant event = SpongeEventFactory.createAdvancementEventGrant(Sponge.server().causeStackManager().currentCause(), channel, Optional.of(channel), this.impl$message == null ? Component.empty() : this.impl$message, this.impl$message == null ? Component.empty() : this.impl$message, (org.spongepowered.api.advancement.Advancement) advancement, (ServerPlayer) this.player, instant, false);
    SpongeCommon.post(event);
    if (!event.isMessageCancelled()) {
        event.audience().ifPresent(eventChannel -> eventChannel.sendMessage(Identity.nil(), event.message()));
    }
    this.impl$message = null;
    this.impl$wasSuccess = false;
}
Also used : Audience(net.kyori.adventure.audience.Audience) AdvancementEvent(org.spongepowered.api.event.advancement.AdvancementEvent) Instant(java.time.Instant) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 14 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class PlayerListMixin method impl$onInitPlayer_join.

@Inject(method = "placeNewPlayer", at = @At(value = "RETURN"))
private void impl$onInitPlayer_join(final Connection networkManager, final net.minecraft.server.level.ServerPlayer mcPlayer, final CallbackInfo ci) {
    final ServerPlayer player = (ServerPlayer) mcPlayer;
    final ServerSideConnection connection = player.connection();
    final Cause cause = Cause.of(EventContext.empty(), connection, player);
    final Audience audience = Audiences.onlinePlayers();
    final Component joinComponent = SpongeAdventure.asAdventure(((ServerPlayerBridge) mcPlayer).bridge$getConnectionMessageToSend());
    final ServerSideConnectionEvent.Join event = SpongeEventFactory.createServerSideConnectionEventJoin(cause, audience, Optional.of(audience), joinComponent, joinComponent, connection, player, false);
    SpongeCommon.post(event);
    if (!event.isMessageCancelled()) {
        event.audience().ifPresent(audience1 -> audience1.sendMessage(Identity.nil(), event.message()));
    }
    ((ServerPlayerBridge) mcPlayer).bridge$setConnectionMessageToSend(null);
    final PhaseContext<?> context = PhaseTracker.SERVER.getPhaseContext();
    PhaseTracker.SERVER.pushCause(event);
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    transactor.logPlayerInventoryChange(mcPlayer, PlayerInventoryTransaction.EventCreator.STANDARD);
    try (EffectTransactor ignored = BroadcastInventoryChangesEffect.transact(transactor)) {
        // in case plugins modified it
        mcPlayer.inventoryMenu.broadcastChanges();
    }
}
Also used : TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) Audience(net.kyori.adventure.audience.Audience) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Cause(org.spongepowered.api.event.Cause) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) MutableComponent(net.minecraft.network.chat.MutableComponent) Component(net.kyori.adventure.text.Component) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 15 with Audience

use of net.kyori.adventure.audience.Audience 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

Audience (net.kyori.adventure.audience.Audience)19 Component (net.kyori.adventure.text.Component)9 CommandException (org.spongepowered.api.command.exception.CommandException)6 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)5 Optional (java.util.Optional)4 Player (org.spongepowered.api.entity.living.player.Player)4 Collection (java.util.Collection)3 List (java.util.List)3 UUID (java.util.UUID)3 Identity (net.kyori.adventure.identity.Identity)3 TextComponent (net.kyori.adventure.text.TextComponent)3 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)3 Inject (com.google.inject.Inject)2 Instant (java.time.Instant)2 Collections (java.util.Collections)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)2 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)2 QueryResult (me.botsko.prism.actionlibs.QueryResult)2