Search in sources :

Example 1 with ListItem

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

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

the class ClientboundForSaleItemsResponsePacket method writeTo.

@Override
public void writeTo(final ChannelBuf buf) {
    checkNotNull(this.id);
    buf.writeString(this.id);
    buf.writeInteger(this.forSaleItems == null ? 0 : this.forSaleItems.size());
    if (this.forSaleItems != null) {
        for (final ForSaleItem forSaleItem : this.forSaleItems) {
            final ListItem listItem = forSaleItem.getListItem();
            final ResourceLocation location = listItem.getItem().getRegistryName();
            if (location == null) {
                new IOException("Malformed location when sending for sale item! Id [" + listItem.getItem().getRegistryName() + "].").printStackTrace();
                continue;
            }
            final byte[] listItemCreatedData;
            try {
                listItemCreatedData = SerializationUtil.objectToBytes(listItem.getCreated());
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
            final NBTTagCompound compound = listItem.getCompound();
            byte[] compoundData = null;
            if (compound != null) {
                try {
                    compoundData = SerializationUtil.toBytes(compound);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            final byte[] forSaleItemCreatedData;
            try {
                forSaleItemCreatedData = SerializationUtil.objectToBytes(forSaleItem.getCreated());
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
            // ListItem
            buf.writeInteger(listItem.getRecord());
            buf.writeString(location.getNamespace());
            buf.writeString(location.getPath());
            buf.writeInteger(listItem.getQuantity());
            buf.writeInteger(listItem.getMetadata());
            buf.writeInteger(listItemCreatedData.length);
            buf.writeBytes(listItemCreatedData);
            buf.writeUniqueId(listItem.getSeller());
            final String sellerName = listItem.getSellerName().orElse(null);
            buf.writeBoolean(sellerName != null);
            if (sellerName != null) {
                buf.writeString(sellerName);
            }
            buf.writeInteger(listItem.getIndex());
            buf.writeBoolean(listItem.getLastKnownPrice().isPresent());
            if (listItem.getLastKnownPrice().isPresent()) {
                ByteBufUtil.writeBigDecimal((ByteBuf) buf, listItem.getLastKnownPrice().get());
            }
            if (compoundData == null) {
                buf.writeInteger(0);
            } else {
                buf.writeInteger(compoundData.length);
                buf.writeBytes(compoundData);
            }
            // ForSaleItem
            buf.writeInteger(forSaleItem.getRecord());
            buf.writeInteger(forSaleItemCreatedData.length);
            buf.writeBytes(forSaleItemCreatedData);
            ByteBufUtil.writeBigDecimal((ByteBuf) buf, forSaleItem.getPrice());
        }
    }
    buf.writeInteger(this.preLimitCount);
}
Also used : ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) IOException(java.io.IOException)

Example 3 with ListItem

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

the class ServerExchangeManager method handleAdjustPriceForSaleItem.

public void handleAdjustPriceForSaleItem(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 adjust a price for a list item for exchange '{}' but the server has no knowledge of it. " + "Syncing exchange registry...", player.getName(), id);
        this.syncExchangeRegistryTo(player);
        return;
    }
    final List<ListItem> listItems = axs.getListItemsFor(player.getUniqueId()).orElse(null);
    if (listItems == 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 adjust the price for list item '{}' for exchange '{}' but the server has no knowledge " + "of any list items for them. Syncing list items...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(axs.getId(), null));
        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 adjust the price for list item '{}' for exchange '{}' but the server has no knowledge " + "of it. Syncing list items...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(axs.getId(), listItems));
        return;
    }
    final ForSaleItem forSaleItem = found.getForSaleItem().orElse(null);
    if (forSaleItem == 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 adjust the price for list item '{}' for exchange '{}' but the server has no knowledge " + "of it being for sale. Syncing list items...", player.getName(), listItemRecNo, id);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(axs.getId(), listItems));
        this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(axs.getId(), axs.getForSaleItemsFor(player.getUniqueId()).orElse(null), null));
        return;
    }
    this.scheduler.createTaskBuilder().async().execute(() -> {
        try (final DSLContext context = this.databaseManager.createContext(true)) {
            final int result = ExchangeQueries.createUpdateForSaleItemPrice(forSaleItem.getRecord(), price).build(context).execute();
            if (result == 0) {
                this.logger.error("Player '{}' attempted to adjust the price for list item '{}' for exchange '{}' to the database but it " + "failed. Discarding changes...", player.getName(), listItemRecNo, id);
                return;
            }
            ExchangeQueries.createUpdateListItemLastKnownPrice(found.getRecord(), forSaleItem.getPrice()).build(context).execute();
            this.scheduler.createTaskBuilder().execute(() -> {
                found.setLastKnownPrice(forSaleItem.getPrice());
                ((BasicForSaleItem) forSaleItem).setPrice(price);
                this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(axs.getId(), axs.getForSaleItemsFor(player.getUniqueId()).orElse(null), Lists.newArrayList(found)));
                this.network.sendToAll(new ClientboundForSaleFilterRequestPacket(axs.getId()));
            }).submit(this.container);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }).submit(this.container);
}
Also used : BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) SQLException(java.sql.SQLException) 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) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) DSLContext(org.jooq.DSLContext) 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 4 with ListItem

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

the class ClientboundListItemsResponsePacket method writeTo.

@Override
public void writeTo(final ChannelBuf buf) {
    checkNotNull(this.id);
    buf.writeString(this.id);
    buf.writeInteger(this.listItems == null ? 0 : this.listItems.size());
    if (this.listItems != null) {
        for (final ListItem item : this.listItems) {
            final ResourceLocation location = item.getItem().getRegistryName();
            if (location == null) {
                new IOException("Malformed resource location for Item '" + item + "' when sending list item!").printStackTrace();
                continue;
            }
            final byte[] createdData;
            try {
                createdData = SerializationUtil.objectToBytes(item.getCreated());
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
            final NBTTagCompound compound = item.getCompound();
            byte[] compoundData = null;
            if (compound != null) {
                try {
                    compoundData = SerializationUtil.toBytes(compound);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            buf.writeInteger(item.getRecord());
            buf.writeString(location.getNamespace());
            buf.writeString(location.getPath());
            buf.writeInteger(item.getQuantity());
            buf.writeInteger(item.getMetadata());
            buf.writeInteger(createdData.length);
            buf.writeBytes(createdData);
            buf.writeUniqueId(item.getSeller());
            final String sellerName = item.getSellerName().orElse(null);
            buf.writeBoolean(sellerName != null);
            if (sellerName != null) {
                buf.writeString(sellerName);
            }
            buf.writeInteger(item.getIndex());
            buf.writeBoolean(item.getLastKnownPrice().isPresent());
            if (item.getLastKnownPrice().isPresent()) {
                ByteBufUtil.writeBigDecimal((ByteBuf) buf, item.getLastKnownPrice().get());
            }
            if (compoundData == null) {
                buf.writeInteger(0);
            } else {
                buf.writeInteger(compoundData.length);
                buf.writeBytes(compoundData);
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) IOException(java.io.IOException)

Example 5 with ListItem

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

the class ServerExchangeManager method openExchangeSpecific.

void openExchangeSpecific(final Player player, final Exchange axs) {
    checkNotNull(player);
    checkNotNull(axs);
    if (!player.hasPermission(axs.getPermission())) {
        this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Exchange"), Text.of("You do not have permission " + "to open this exchange!"), 5);
        return;
    }
    this.network.sendTo(player, new ClientboundExchangeGuiResponsePacket(ExchangeGuiType.SPECIFIC, axs.getId(), this.getListingsLimit(player)));
    if (!axs.isLoaded()) {
        this.playerSpecificInitiatorIds.add(player.getUniqueId());
        this.databaseManager.getQueue().queue(DatabaseQueue.ActionType.FETCH_IGNORE_DUPLICATES, axs.getId(), () -> {
            this.loadListItems(axs);
            this.loadForSaleItems(axs);
            this.scheduler.createTaskBuilder().execute(() -> {
                axs.setLoaded(true);
                final Iterator<UUID> iter = this.playerSpecificInitiatorIds.iterator();
                while (iter.hasNext()) {
                    final UUID uniqueId = iter.next();
                    iter.remove();
                    final Player p = Sponge.getServer().getPlayer(uniqueId).orElse(null);
                    if (p != null && p.isOnline() && !p.isRemoved()) {
                        final List<ListItem> listItems = axs.getListItemsFor(p.getUniqueId()).orElse(null);
                        this.network.sendTo(p, new ClientboundListItemsResponsePacket(axs.getId(), listItems));
                        if (listItems != null && !listItems.isEmpty()) {
                            final List<ForSaleItem> forSaleItems = axs.getForSaleItemsFor(p.getUniqueId()).orElse(null);
                            if (forSaleItems != null && !forSaleItems.isEmpty()) {
                                this.network.sendTo(p, new ClientboundListItemsSaleStatusPacket(axs.getId(), forSaleItems, null));
                            }
                        }
                        this.network.sendTo(p, new ClientboundForSaleFilterRequestPacket(axs.getId()));
                    }
                }
            }).submit(this.container);
        });
    } else {
        final List<ListItem> listItems = axs.getListItemsFor(player.getUniqueId()).orElse(null);
        this.network.sendTo(player, new ClientboundListItemsResponsePacket(axs.getId(), listItems));
        if (listItems != null && !listItems.isEmpty()) {
            final List<ForSaleItem> forSaleItems = axs.getForSaleItemsFor(player.getUniqueId()).orElse(null);
            if (forSaleItems != null && !forSaleItems.isEmpty()) {
                this.network.sendTo(player, new ClientboundListItemsSaleStatusPacket(axs.getId(), forSaleItems, null));
            }
        }
        this.network.sendTo(player, new ClientboundForSaleFilterRequestPacket(axs.getId()));
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ClientboundExchangeGuiResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundExchangeGuiResponsePacket) Iterator(java.util.Iterator) ClientboundForSaleFilterRequestPacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) 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) UUID(java.util.UUID)

Aggregations

ListItem (com.almuradev.almura.feature.exchange.listing.ListItem)17 BasicListItem (com.almuradev.almura.feature.exchange.basic.listing.BasicListItem)14 ForSaleItem (com.almuradev.almura.feature.exchange.listing.ForSaleItem)14 BasicForSaleItem (com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem)12 AxsListItem (com.almuradev.generated.axs.tables.AxsListItem)11 ClientboundListItemsSaleStatusPacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket)10 AxsForSaleItem (com.almuradev.generated.axs.tables.AxsForSaleItem)10 ClientboundForSaleFilterRequestPacket (com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket)9 ClientboundListItemsResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket)9 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 DSLContext (org.jooq.DSLContext)8 BasicExchange (com.almuradev.almura.feature.exchange.basic.BasicExchange)7 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7 List (java.util.List)7 UUID (java.util.UUID)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)6 ClientboundExchangeGuiResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundExchangeGuiResponsePacket)5 ClientboundTransactionCompletePacket (com.almuradev.almura.feature.exchange.network.ClientboundTransactionCompletePacket)5