use of com.almuradev.almura.feature.exchange.network.ClientboundForSaleItemsResponsePacket in project Almura by AlmuraDev.
the class ServerExchangeManager method handleForSaleFilter.
public void handleForSaleFilter(final Player player, final String id, @Nullable final String filter, @Nullable final String sorter, final int skip, final int limit) {
checkNotNull(player);
checkNotNull(id);
checkState(skip >= 0);
final Exchange axs = this.getExchange(id).orElse(null);
if (axs == null) {
this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("Critical error encountered, check the " + "server console for more details!"));
this.logger.error("Player '{}' attempted to filter for sale items for exchange '{}' but the server has no knowledge of it. Syncing " + "exchange registry...", player.getName(), id);
this.syncExchangeRegistryTo(player);
return;
}
Stream<ForSaleItem> stream = axs.getForSaleItems().values().stream().flatMap(List::stream);
if (filter != null) {
final List<FilterRegistry.FilterElement<ListItem>> elements = FilterRegistry.instance.getFilterElements(filter);
stream = stream.filter(forSaleItem -> elements.stream().allMatch(element -> element.getFilter().test(forSaleItem.getListItem(), element.getValue())));
}
if (sorter != null) {
final List<FilterRegistry.SorterElement<ListItem>> elements = FilterRegistry.instance.getSortingElements(sorter);
final Comparator<ListItem> comparator = FilterRegistry.instance.buildSortingComparator(elements).orElse(null);
if (comparator != null) {
stream = stream.map(ForSaleItem::getListItem).sorted(comparator).map(k -> k.getForSaleItem().orElse(null));
}
}
final List<ForSaleItem> result = stream.collect(Collectors.toList());
final Stream<ForSaleItem> adjustedStream = result.stream().skip(skip);
final List<ForSaleItem> limitedResult;
if (limit > -1) {
limitedResult = adjustedStream.limit(limit).collect(Collectors.toList());
} else {
limitedResult = adjustedStream.collect(Collectors.toList());
}
this.network.sendTo(player, new ClientboundForSaleItemsResponsePacket(axs.getId(), limitedResult, result.size()));
}
Aggregations