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