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));
}
}
}
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);
}
}
}
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;
}
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);
}
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;
}
Aggregations