Search in sources :

Example 1 with AxsForSaleItemRecord

use of com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord in project Almura by AlmuraDev.

the class ServerExchangeManager method handleListForSaleItem.

public void handleListForSaleItem(final Player player, final String id, final int listItemRecNo, final BigDecimal price) {
    checkNotNull(player);
    checkNotNull(id);
    checkState(listItemRecNo >= 0);
    checkNotNull(price);
    checkState(price.doubleValue() >= 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 mark list item '{}' for sale for exchange '{}' but the server has no knowledge of that " + "exchange. Syncing exchange registry...", player.getName(), listItemRecNo, id);
        this.syncExchangeRegistryTo(player);
        return;
    }
    final List<ListItem> listItems = axs.getListItemsFor(player.getUniqueId()).orElse(null);
    if (listItems == null || listItems.isEmpty()) {
        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 mark list item '{}' for sale for exchange '{}' but the server has no record of any list " + "items for that player. Syncing list items...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(id, listItems));
        return;
    }
    final ListItem found = listItems.stream().filter(item -> item.getRecord() == listItemRecNo).findAny().orElse(null);
    if (found == 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 mark list item '{}' for sale for exchange '{}' but the server has no record of the listing. " + "Syncing list items...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(id, listItems));
        return;
    }
    final List<ForSaleItem> forSaleItems = axs.getForSaleItemsFor(player.getUniqueId()).orElse(null);
    if (found.getForSaleItem().isPresent()) {
        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 mark list item '{}' for sale for exchange '{}' but the " + "server already has a listing for that item. Syncing list items sale status...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(id, forSaleItems, null));
        return;
    }
    if (forSaleItems != null && forSaleItems.size() + 1 > this.getListingsLimit(player)) {
        this.notificationManager.sendWindowMessage(player, Text.of("Exchange"), Text.of("You have reached your listing limit."));
        return;
    }
    this.scheduler.createTaskBuilder().async().execute(() -> {
        try (final DSLContext context = this.databaseManager.createContext(true)) {
            final Instant created = Instant.now();
            final AxsForSaleItemRecord record = ExchangeQueries.createInsertForSaleItem(created, found.getRecord(), price).build(context).fetchOne();
            if (record == null) {
                this.logger.error("Player '{}' attempted to mark list item '{}' for sale for exchange '{}' to the database but it failed. " + "Discarding changes...", player.getName(), listItemRecNo, id);
                return;
            }
            this.scheduler.createTaskBuilder().execute(() -> {
                final BasicForSaleItem basicForSaleItem = new BasicForSaleItem((BasicListItem) found, record.getRecNo(), created, record.getPrice());
                List<ForSaleItem> forSaleItemsRef = forSaleItems;
                if (forSaleItemsRef == null) {
                    forSaleItemsRef = new ArrayList<>();
                    axs.putForSaleItemsFor(player.getUniqueId(), forSaleItemsRef);
                }
                forSaleItemsRef.add(basicForSaleItem);
                this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(axs.getId(), forSaleItems, null));
                this.network.sendToAll(new ClientboundForSaleFilterRequestPacket(axs.getId()));
            }).submit(this.container);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }).submit(this.container);
}
Also used : AxsForSaleItemRecord(com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord) SQLException(java.sql.SQLException) Instant(java.time.Instant) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket) DSLContext(org.jooq.DSLContext) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ClientboundForSaleFilterRequestPacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) 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

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 ForSaleItem (com.almuradev.almura.feature.exchange.listing.ForSaleItem)1 ListItem (com.almuradev.almura.feature.exchange.listing.ListItem)1 ClientboundForSaleFilterRequestPacket (com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket)1 ClientboundListItemsResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket)1 ClientboundListItemsSaleStatusPacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket)1 AxsForSaleItem (com.almuradev.generated.axs.tables.AxsForSaleItem)1 AxsListItem (com.almuradev.generated.axs.tables.AxsListItem)1 AxsForSaleItemRecord (com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord)1 SQLException (java.sql.SQLException)1 Instant (java.time.Instant)1 DSLContext (org.jooq.DSLContext)1