Search in sources :

Example 1 with BasicBuyingItem

use of com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem in project Almura by AlmuraDev.

the class ServerStoreManager method parseBuyingItemsFor.

private List<BuyingItem> parseBuyingItemsFor(final Result<Record> result) {
    final List<BuyingItem> items = new ArrayList<>();
    for (final Record record : result) {
        final Integer recNo = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.REC_NO);
        final String domain = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.DOMAIN);
        final String path = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.PATH);
        final ResourceLocation location = new ResourceLocation(domain, path);
        final Item item = ForgeRegistries.ITEMS.getValue(location);
        if (item == null) {
            this.logger.error("Unknown buying item for domain '{}' and path '{}' found at record number '{}'. Skipping... (Did you remove a " + "mod?)", domain, path, recNo);
            continue;
        }
        final Timestamp created = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.CREATED);
        final Integer quantity = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.QUANTITY);
        final Integer metadata = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.METADATA);
        final BigDecimal price = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.PRICE);
        final Integer index = record.getValue(StoreBuyingItem.STORE_BUYING_ITEM.INDEX);
        final byte[] compoundData = record.getValue(StoreBuyingItemData.STORE_BUYING_ITEM_DATA.DATA);
        NBTTagCompound compound = null;
        if (compoundData != null) {
            try {
                compound = SerializationUtil.compoundFromBytes(compoundData);
            } catch (final IOException e) {
                this.logger.error("Malformed buying item data found at record number '{}'. Skipping...", recNo);
                continue;
            }
        }
        final BasicBuyingItem basicBuyingItem = new BasicBuyingItem(recNo, created.toInstant(), item, quantity, metadata, price, index, compound);
        items.add(basicBuyingItem);
    }
    return items;
}
Also used : BasicBuyingItem(com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Item(net.minecraft.item.Item) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) StoreBuyingItem(com.almuradev.generated.store.tables.StoreBuyingItem) BasicBuyingItem(com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem) BasicSellingItem(com.almuradev.almura.feature.store.basic.listing.BasicSellingItem) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) StoreSellingItem(com.almuradev.generated.store.tables.StoreSellingItem) ResourceLocation(net.minecraft.util.ResourceLocation) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) StoreBuyingItem(com.almuradev.generated.store.tables.StoreBuyingItem) BasicBuyingItem(com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem) StoreSellingItemDataRecord(com.almuradev.generated.store.tables.records.StoreSellingItemDataRecord) StoreSellingItemRecord(com.almuradev.generated.store.tables.records.StoreSellingItemRecord) StoreBuyingItemRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemRecord) Record(org.jooq.Record) StoreBuyingItemDataRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemDataRecord)

Example 2 with BasicBuyingItem

use of com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem in project Almura by AlmuraDev.

the class ServerStoreManager method handleListBuyingItems.

public void handleListBuyingItems(final Player player, final String id, final List<ServerboundListItemsRequestPacket.ListCandidate> candidates) {
    checkNotNull(player);
    checkNotNull(id);
    if (!player.hasPermission(Almura.ID + ".store.admin")) {
        this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to list items!"), 5);
        return;
    }
    final Store store = this.getStore(id).orElse(null);
    if (store == null) {
        this.logger.error("Player '{}' attempted to list buying items for store '{}' but the server has no knowledge of it. Syncing " + "store registry...", player.getName(), id);
        this.syncStoreRegistryTo(player);
        return;
    }
    this.scheduler.createTaskBuilder().async().execute(() -> {
        try (final DSLContext context = this.databaseManager.createContext(true)) {
            final Map<StoreBuyingItemRecord, VanillaStack> inserted = new HashMap<>();
            for (final ServerboundListItemsRequestPacket.ListCandidate candidate : candidates) {
                final VanillaStack stack = candidate.stack;
                final int index = candidate.index;
                final BigDecimal price = candidate.price;
                final StoreBuyingItemRecord itemRecord = StoreQueries.createInsertBuyingItem(store.getId(), Instant.now(), stack.getItem(), stack.getQuantity(), stack.getMetadata(), index, price).build(context).fetchOne();
                if (itemRecord == null) {
                    this.notificationManager.sendWindowMessage(player, Text.of("Store"), Text.of("Critical error encountered, check the server console for more details!"));
                    this.logger.error("Player '{}' submitted a new buying item for store '{}' to the database but it failed. " + "Discarding changes and printing stack...", player.getName(), id);
                    this.printStacksToConsole(Lists.newArrayList(stack));
                    continue;
                }
                final NBTTagCompound compound = stack.getCompound();
                if (compound != null) {
                    StoreBuyingItemDataRecord dataRecord = null;
                    try {
                        dataRecord = StoreQueries.createInsertBuyingItemData(itemRecord.getRecNo(), compound).build(context).fetchOne();
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                    if (dataRecord == null) {
                        this.notificationManager.sendWindowMessage(player, Text.of("Store"), Text.of("Critical error encountered, check the server console for more details!"));
                        this.logger.error("Player '{}' submitted data for buying item record '{}' for store '{}' but it failed. " + "Discarding changes...", player.getName(), itemRecord.getRecNo(), id);
                        StoreQueries.createDeleteBuyingItem(itemRecord.getRecNo()).build(context).execute();
                    }
                }
                inserted.put(itemRecord, stack);
            }
            this.scheduler.createTaskBuilder().execute(() -> {
                for (final Map.Entry<StoreBuyingItemRecord, VanillaStack> entry : inserted.entrySet()) {
                    final StoreBuyingItemRecord record = entry.getKey();
                    final VanillaStack stack = entry.getValue();
                    final BasicBuyingItem item = new BasicBuyingItem(record.getRecNo(), record.getCreated().toInstant(), stack.getItem(), stack.getQuantity(), stack.getMetadata(), record.getPrice(), record.getIndex(), stack.getCompound());
                    store.getBuyingItems().add(item);
                }
                this.network.sendToAll(new ClientboundListItemsResponsePacket(store.getId(), StoreItemSegmentType.BUYING, store.getBuyingItems()));
            }).submit(this.container);
        } catch (final SQLException e) {
            e.printStackTrace();
        }
    }).submit(this.container);
}
Also used : SQLException(java.sql.SQLException) BasicBuyingItem(com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BasicStore(com.almuradev.almura.feature.store.basic.BasicStore) DSLContext(org.jooq.DSLContext) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket) IOException(java.io.IOException) StoreBuyingItemDataRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemDataRecord) VanillaStack(com.almuradev.almura.shared.item.VanillaStack) BigDecimal(java.math.BigDecimal) StoreBuyingItemRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemRecord) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

BasicBuyingItem (com.almuradev.almura.feature.store.basic.listing.BasicBuyingItem)2 StoreBuyingItemDataRecord (com.almuradev.generated.store.tables.records.StoreBuyingItemDataRecord)2 StoreBuyingItemRecord (com.almuradev.generated.store.tables.records.StoreBuyingItemRecord)2 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 BasicStore (com.almuradev.almura.feature.store.basic.BasicStore)1 BasicSellingItem (com.almuradev.almura.feature.store.basic.listing.BasicSellingItem)1 BuyingItem (com.almuradev.almura.feature.store.listing.BuyingItem)1 SellingItem (com.almuradev.almura.feature.store.listing.SellingItem)1 StoreItem (com.almuradev.almura.feature.store.listing.StoreItem)1 ClientboundListItemsResponsePacket (com.almuradev.almura.feature.store.network.ClientboundListItemsResponsePacket)1 VanillaStack (com.almuradev.almura.shared.item.VanillaStack)1 StoreBuyingItem (com.almuradev.generated.store.tables.StoreBuyingItem)1 StoreSellingItem (com.almuradev.generated.store.tables.StoreSellingItem)1 StoreSellingItemDataRecord (com.almuradev.generated.store.tables.records.StoreSellingItemDataRecord)1 StoreSellingItemRecord (com.almuradev.generated.store.tables.records.StoreSellingItemRecord)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1