Search in sources :

Example 1 with ClientboundForSaleItemsResponsePacket

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()));
}
Also used : AxsListItemRecord(com.almuradev.generated.axs.tables.records.AxsListItemRecord) AxsListItemData(com.almuradev.generated.axs.tables.AxsListItemData) IItemHandler(net.minecraftforge.items.IItemHandler) FilterRegistry(com.almuradev.almura.shared.feature.filter.FilterRegistry) Results(org.jooq.Results) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) Item(net.minecraft.item.Item) Axs(com.almuradev.generated.axs.tables.Axs) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) DatabaseManager(com.almuradev.almura.shared.database.DatabaseManager) BigDecimal(java.math.BigDecimal) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) Map(java.util.Map) AxsListItem(com.almuradev.generated.axs.tables.AxsListItem) DSLContext(org.jooq.DSLContext) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ClientboundExchangeGuiResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundExchangeGuiResponsePacket) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) EconomyService(org.spongepowered.api.service.economy.EconomyService) VanillaStack(com.almuradev.almura.shared.item.VanillaStack) ClientboundForSaleFilterRequestPacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket) Timestamp(java.sql.Timestamp) FeatureConstants(com.almuradev.almura.shared.feature.FeatureConstants) Sponge(org.spongepowered.api.Sponge) DatabaseQueue(com.almuradev.almura.shared.database.DatabaseQueue) ServiceManager(org.spongepowered.api.service.ServiceManager) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) UUID(java.util.UUID) Result(org.jooq.Result) Instant(java.time.Instant) ClientboundExchangeRegistryPacket(com.almuradev.almura.feature.exchange.network.ClientboundExchangeRegistryPacket) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) ChannelBinding(org.spongepowered.api.network.ChannelBinding) List(java.util.List) Stream(java.util.stream.Stream) ExchangeQueries(com.almuradev.almura.feature.exchange.database.ExchangeQueries) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ChannelId(org.spongepowered.api.network.ChannelId) IngameFeature(com.almuradev.almura.shared.feature.IngameFeature) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Almura(com.almuradev.almura.Almura) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) Getter(org.spongepowered.api.event.filter.Getter) GameStartingServerEvent(org.spongepowered.api.event.game.state.GameStartingServerEvent) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) ArrayList(java.util.ArrayList) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket) Inject(javax.inject.Inject) AxsListItemDataRecord(com.almuradev.generated.axs.tables.records.AxsListItemDataRecord) ItemStack(net.minecraft.item.ItemStack) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Text(org.spongepowered.api.text.Text) AxsForSaleItemRecord(com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord) GameState(org.spongepowered.api.GameState) CauseStackManager(org.spongepowered.api.event.CauseStackManager) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) ClientboundForSaleItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleItemsResponsePacket) Record(org.jooq.Record) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ClientboundTransactionCompletePacket(com.almuradev.almura.feature.exchange.network.ClientboundTransactionCompletePacket) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Scheduler(org.spongepowered.api.scheduler.Scheduler) ServerNotificationManager(com.almuradev.almura.feature.notification.ServerNotificationManager) EnumFacing(net.minecraft.util.EnumFacing) IOException(java.io.IOException) SerializationUtil(com.almuradev.almura.shared.util.SerializationUtil) Witness(com.almuradev.core.event.Witness) ForgeRegistries(net.minecraftforge.fml.common.registry.ForgeRegistries) ResourceLocation(net.minecraft.util.ResourceLocation) Listener(org.spongepowered.api.event.Listener) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Comparator(java.util.Comparator) ClientboundForSaleItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleItemsResponsePacket) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) List(java.util.List) ArrayList(java.util.ArrayList) AxsListItem(com.almuradev.generated.axs.tables.AxsListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem)

Aggregations

Almura (com.almuradev.almura.Almura)1 BasicExchange (com.almuradev.almura.feature.exchange.basic.BasicExchange)1 BasicForSaleItem (com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem)1 BasicListItem (com.almuradev.almura.feature.exchange.basic.listing.BasicListItem)1 ExchangeQueries (com.almuradev.almura.feature.exchange.database.ExchangeQueries)1 ForSaleItem (com.almuradev.almura.feature.exchange.listing.ForSaleItem)1 ListItem (com.almuradev.almura.feature.exchange.listing.ListItem)1 ClientboundExchangeGuiResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundExchangeGuiResponsePacket)1 ClientboundExchangeRegistryPacket (com.almuradev.almura.feature.exchange.network.ClientboundExchangeRegistryPacket)1 ClientboundForSaleFilterRequestPacket (com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket)1 ClientboundForSaleItemsResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundForSaleItemsResponsePacket)1 ClientboundListItemsResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket)1 ClientboundListItemsSaleStatusPacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket)1 ClientboundTransactionCompletePacket (com.almuradev.almura.feature.exchange.network.ClientboundTransactionCompletePacket)1 ServerNotificationManager (com.almuradev.almura.feature.notification.ServerNotificationManager)1 DatabaseManager (com.almuradev.almura.shared.database.DatabaseManager)1 DatabaseQueue (com.almuradev.almura.shared.database.DatabaseQueue)1 FeatureConstants (com.almuradev.almura.shared.feature.FeatureConstants)1 IngameFeature (com.almuradev.almura.shared.feature.IngameFeature)1 FilterRegistry (com.almuradev.almura.shared.feature.filter.FilterRegistry)1