use of com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem 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.BasicForSaleItem 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);
}
use of com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem 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.BasicForSaleItem 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.BasicForSaleItem in project Almura by AlmuraDev.
the class ClientboundForSaleItemsResponsePacket method readFrom.
@Override
public void readFrom(final ChannelBuf buf) {
this.id = buf.readString();
final int count = buf.readInteger();
if (count > 0) {
this.forSaleItems = new ArrayList<>();
for (int i = 0; i < count; i++) {
final int listItemRecNo = 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 when receiving for sale item! Id [" + location + "]. Skipping...").printStackTrace();
continue;
}
final int quantity = buf.readInteger();
final int metadata = buf.readInteger();
final Instant listItemCreated;
try {
listItemCreated = 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(listItemRecNo, listItemCreated, seller, item, quantity, metadata, index, lastKnownPrice, compound);
if (Sponge.getPlatform().getExecutionType().isClient()) {
basicListItem.setSellerName(sellerName);
}
final int forSaleItemRecNo = buf.readInteger();
// ForSaleItem
final Instant forSaleItemCreated;
try {
forSaleItemCreated = SerializationUtil.bytesToObject(buf.readBytes(buf.readInteger()));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
continue;
}
final BigDecimal price = ByteBufUtil.readBigDecimal((ByteBuf) buf);
final BasicForSaleItem basicForSaleItem = new BasicForSaleItem(basicListItem, forSaleItemRecNo, forSaleItemCreated, price);
this.forSaleItems.add(basicForSaleItem);
}
}
this.preLimitCount = buf.readInteger();
}
Aggregations