Search in sources :

Example 1 with BasicListItem

use of com.almuradev.almura.feature.exchange.basic.listing.BasicListItem in project Almura by AlmuraDev.

the class ClientExchangeManager method handleListItemsSaleStatus.

public void handleListItemsSaleStatus(final String id, @Nullable final List<ClientboundListItemsSaleStatusPacket.ListedItemUpdate> itemCandidates, @Nullable final List<ClientboundListItemsSaleStatusPacket.LastKnownPriceUpdate> lastKnowPriceItemCandidates) {
    checkNotNull(id);
    final Exchange axs = this.getExchange(id);
    if (axs == null) {
        return;
    }
    final List<ListItem> listItems = axs.getListItemsFor(Minecraft.getMinecraft().player.getUniqueID()).orElse(null);
    if (listItems == null || listItems.isEmpty()) {
        return;
    }
    // Null out all for sale items, our candidates will have what we currently have
    listItems.forEach(item -> item.setForSaleItem(null));
    if (itemCandidates != null) {
        for (final ClientboundListItemsSaleStatusPacket.ListedItemUpdate itemCandidate : itemCandidates) {
            listItems.stream().filter(item -> item.getRecord() == itemCandidate.listItemRecNo).findAny().ifPresent(listItem -> listItem.setForSaleItem(new BasicForSaleItem((BasicListItem) listItem, itemCandidate.forSaleItemRecNo, itemCandidate.created, itemCandidate.price)));
        }
    }
    if (lastKnowPriceItemCandidates != null) {
        for (final ClientboundListItemsSaleStatusPacket.LastKnownPriceUpdate lastKnownPriceItemCandidate : lastKnowPriceItemCandidates) {
            listItems.stream().filter(item -> item.getRecord() == lastKnownPriceItemCandidate.listItemRecNo).findAny().ifPresent(listItem -> listItem.setLastKnownPrice(lastKnownPriceItemCandidate.lastKnownPrice));
        }
    }
}
Also used : Exchange(com.almuradev.almura.feature.exchange.Exchange) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem)

Example 2 with BasicListItem

use of com.almuradev.almura.feature.exchange.basic.listing.BasicListItem in project Almura by AlmuraDev.

the class ClientboundListItemsResponsePacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    this.id = buf.readString();
    final int count = buf.readInteger();
    if (count > 0) {
        this.listItems = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            final int record = buf.readInteger();
            final ResourceLocation location = new ResourceLocation(buf.readString(), buf.readString());
            final Item item = ForgeRegistries.ITEMS.getValue(location);
            if (item == null) {
                new IOException("Unknown item id '" + location.toString() + "' when receiving list item! . Skipping...").printStackTrace();
                continue;
            }
            final int quantity = buf.readInteger();
            final int metadata = buf.readInteger();
            final Instant created;
            try {
                created = SerializationUtil.bytesToObject(buf.readBytes(buf.readInteger()));
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                continue;
            }
            final UUID seller = buf.readUniqueId();
            final String sellerName = buf.readBoolean() ? buf.readString() : null;
            final int index = buf.readInteger();
            BigDecimal lastKnownPrice = null;
            if (buf.readBoolean()) {
                lastKnownPrice = ByteBufUtil.readBigDecimal((ByteBuf) buf);
            }
            final int compoundDataLength = buf.readInteger();
            NBTTagCompound compound = null;
            if (compoundDataLength > 0) {
                try {
                    compound = SerializationUtil.compoundFromBytes(buf.readBytes(compoundDataLength));
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            final BasicListItem basicListItem = new BasicListItem(record, created, seller, item, quantity, metadata, index, lastKnownPrice, compound);
            if (Sponge.getPlatform().getExecutionType().isClient()) {
                basicListItem.setSellerName(sellerName);
            }
            this.listItems.add(basicListItem);
        }
    }
}
Also used : Instant(java.time.Instant) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ByteBuf(io.netty.buffer.ByteBuf) BigDecimal(java.math.BigDecimal) Item(net.minecraft.item.Item) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ResourceLocation(net.minecraft.util.ResourceLocation) UUID(java.util.UUID)

Example 3 with BasicListItem

use of com.almuradev.almura.feature.exchange.basic.listing.BasicListItem in project Almura by AlmuraDev.

the class ServerExchangeManager method parseForSaleItemsFrom.

private List<ForSaleItem> parseForSaleItemsFrom(final List<ListItem> listItems, final Result<Record> result) {
    final List<ForSaleItem> forSaleItems = new ArrayList<>();
    result.forEach(record -> {
        final Integer recNo = record.getValue(AxsForSaleItem.AXS_FOR_SALE_ITEM.REC_NO);
        final Integer itemRecNo = record.getValue(AxsForSaleItem.AXS_FOR_SALE_ITEM.LIST_ITEM);
        final ListItem found = listItems.stream().filter(item -> item.getRecord() == itemRecNo).findAny().orElse(null);
        if (found == null) {
            this.logger.error("A for sale listing at record number '{}' is being loaded but the listing no longer exists. Somehow an entity has" + " tampered with the structure of the database. Report to an AlmuraDev developer ASAP.", recNo);
        } else {
            final Timestamp created = record.getValue(AxsForSaleItem.AXS_FOR_SALE_ITEM.CREATED);
            final BigDecimal price = record.getValue(AxsForSaleItem.AXS_FOR_SALE_ITEM.PRICE);
            final BasicForSaleItem basicForSaleItem = new BasicForSaleItem((BasicListItem) found, recNo, created.toInstant(), price);
            forSaleItems.add(basicForSaleItem);
        }
    });
    return forSaleItems;
}
Also used : 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) 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) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 4 with BasicListItem

use of com.almuradev.almura.feature.exchange.basic.listing.BasicListItem 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)

Example 5 with BasicListItem

use of com.almuradev.almura.feature.exchange.basic.listing.BasicListItem in project Almura by AlmuraDev.

the class ServerExchangeManager method parseListItemsFrom.

private List<ListItem> parseListItemsFrom(final Result<Record> result) {
    final List<ListItem> items = new ArrayList<>();
    for (final Record record : result) {
        final Integer recNo = record.getValue(AxsListItem.AXS_LIST_ITEM.REC_NO);
        final String domain = record.getValue(AxsListItem.AXS_LIST_ITEM.DOMAIN);
        final String path = record.getValue(AxsListItem.AXS_LIST_ITEM.PATH);
        final ResourceLocation location = new ResourceLocation(domain, path);
        final Item item = ForgeRegistries.ITEMS.getValue(location);
        if (item == null) {
            this.logger.error("Unknown item for domain '{}' and path '{}' found at record number '{}'. Skipping... (Did you remove a mod?)", domain, path, recNo);
            continue;
        }
        final Timestamp created = record.getValue(AxsListItem.AXS_LIST_ITEM.CREATED);
        final UUID seller = SerializationUtil.uniqueIdFromBytes(record.getValue(AxsListItem.AXS_LIST_ITEM.SELLER));
        final Integer quantity = record.getValue(AxsListItem.AXS_LIST_ITEM.QUANTITY);
        final Integer metadata = record.getValue(AxsListItem.AXS_LIST_ITEM.METADATA);
        final Integer index = record.getValue(AxsListItem.AXS_LIST_ITEM.INDEX);
        final BigDecimal lastKnownPrice = record.getValue(AxsListItem.AXS_LIST_ITEM.LAST_KNOWN_PRICE);
        final byte[] compoundData = record.getValue(AxsListItemData.AXS_LIST_ITEM_DATA.DATA);
        NBTTagCompound compound = null;
        if (compoundData != null) {
            try {
                compound = SerializationUtil.compoundFromBytes(compoundData);
            } catch (IOException e) {
                this.logger.error("Malformed item data found at record number '{}'. Skipping...", recNo);
                continue;
            }
        }
        final BasicListItem basicListItem = new BasicListItem(recNo, created.toInstant(), seller, item, quantity, metadata, index, lastKnownPrice, compound);
        basicListItem.syncSellerNameToUniqueId();
        items.add(basicListItem);
    }
    return items;
}
Also used : ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Item(net.minecraft.item.Item) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) AxsListItem(com.almuradev.generated.axs.tables.AxsListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) ResourceLocation(net.minecraft.util.ResourceLocation) AxsListItemRecord(com.almuradev.generated.axs.tables.records.AxsListItemRecord) AxsListItemDataRecord(com.almuradev.generated.axs.tables.records.AxsListItemDataRecord) AxsForSaleItemRecord(com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord) Record(org.jooq.Record) AxsListItem(com.almuradev.generated.axs.tables.AxsListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) UUID(java.util.UUID)

Aggregations

BasicListItem (com.almuradev.almura.feature.exchange.basic.listing.BasicListItem)6 ListItem (com.almuradev.almura.feature.exchange.listing.ListItem)6 BasicForSaleItem (com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem)5 ForSaleItem (com.almuradev.almura.feature.exchange.listing.ForSaleItem)4 BigDecimal (java.math.BigDecimal)4 AxsForSaleItem (com.almuradev.generated.axs.tables.AxsForSaleItem)3 AxsListItem (com.almuradev.generated.axs.tables.AxsListItem)3 IOException (java.io.IOException)3 Instant (java.time.Instant)3 UUID (java.util.UUID)3 Item (net.minecraft.item.Item)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 ClientboundListItemsSaleStatusPacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket)2 AxsForSaleItemRecord (com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord)2 ByteBuf (io.netty.buffer.ByteBuf)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 Exchange (com.almuradev.almura.feature.exchange.Exchange)1 BasicExchange (com.almuradev.almura.feature.exchange.basic.BasicExchange)1