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