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