Search in sources :

Example 1 with ProxySource

use of org.spongepowered.api.command.source.ProxySource in project LanternServer by LanternPowered.

the class LanternPaginationList method sendTo.

@SuppressWarnings("rawtypes")
@Override
public void sendTo(final MessageReceiver receiver, int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    this.service.registerCommandOnce();
    MessageReceiver realSource = receiver;
    while (realSource instanceof ProxySource) {
        realSource = ((ProxySource) realSource).getOriginalSource();
    }
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    Iterable<Map.Entry<Text, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Text title = this.title.orElse(null);
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    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(realSource, calculator, ImmutableList.copyOf(counts), title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    } else {
        pagination = new IterablePagination(realSource, calculator, counts, title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (CommandException e) {
        receiver.sendMessage(error(e.getText()));
    }
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) CommandException(org.spongepowered.api.command.CommandException) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) Nullable(javax.annotation.Nullable) CommandMessageFormatting.error(org.spongepowered.api.command.CommandMessageFormatting.error) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) Text(org.spongepowered.api.text.Text) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CommandException(org.spongepowered.api.command.CommandException)

Example 2 with ProxySource

use of org.spongepowered.api.command.source.ProxySource in project SpongeCommon by SpongePowered.

the class SpongePaginationList method sendTo.

@Override
public void sendTo(final MessageReceiver receiver, int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    this.service.registerCommandOnce();
    MessageReceiver realSource = receiver;
    while (realSource instanceof ProxySource) {
        realSource = ((ProxySource) realSource).getOriginalSource();
    }
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    Iterable<Map.Entry<Text, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Text title = this.title.orElse(null);
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    // If the MessageReceiver is a Player, then upon death, they will become a different MessageReceiver object.
    // Thus, we use a supplier to supply the player from the server, if required.
    Supplier<Optional<MessageReceiver>> messageReceiverSupplier;
    if (receiver instanceof Player) {
        final UUID playerUuid = ((Player) receiver).getUniqueId();
        messageReceiverSupplier = () -> Sponge.getServer().getPlayer(playerUuid).map(x -> (MessageReceiver) x);
    } else {
        WeakReference<MessageReceiver> srcReference = new WeakReference<>(receiver);
        messageReceiverSupplier = () -> Optional.ofNullable(srcReference.get());
    }
    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(messageReceiverSupplier, calculator, ImmutableList.copyOf(counts), title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    } else {
        pagination = new IterablePagination(messageReceiverSupplier, calculator, counts, title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (CommandException e) {
        receiver.sendMessage(error(e.getText()));
    }
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) 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) CommandException(org.spongepowered.api.command.CommandException) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) 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(javax.annotation.Nullable) CommandMessageFormatting.error(org.spongepowered.api.command.CommandMessageFormatting.error) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) ProxySource(org.spongepowered.api.command.source.ProxySource) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) WeakReference(java.lang.ref.WeakReference) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableList (com.google.common.collect.ImmutableList)2 Maps (com.google.common.collect.Maps)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 Nullable (javax.annotation.Nullable)2 CommandException (org.spongepowered.api.command.CommandException)2 CommandMessageFormatting.error (org.spongepowered.api.command.CommandMessageFormatting.error)2 ProxySource (org.spongepowered.api.command.source.ProxySource)2 PaginationList (org.spongepowered.api.service.pagination.PaginationList)2 Text (org.spongepowered.api.text.Text)2 MessageReceiver (org.spongepowered.api.text.channel.MessageReceiver)2 WeakReference (java.lang.ref.WeakReference)1 UUID (java.util.UUID)1 Supplier (java.util.function.Supplier)1 Sponge (org.spongepowered.api.Sponge)1 Player (org.spongepowered.api.entity.living.player.Player)1