use of com.almuradev.almura.feature.exchange.listing.ForSaleItem in project Almura by AlmuraDev.
the class ExchangeScreen method refreshForSaleItemResults.
public void refreshForSaleItemResults(@Nullable final List<ForSaleItem> forSaleItems, final int preLimitCount) {
if (this.forSaleList == null) {
return;
}
final ForSaleItem currentItem = this.forSaleList.getSelectedItem() == null ? null : this.forSaleList.getSelectedItem().copy();
this.forSaleList.clearItems();
if (forSaleItems != null) {
this.forSaleList.addItems(forSaleItems);
}
this.pages = (Math.max(preLimitCount - 1, 1) / 10) + 1;
// If we can go back to the same page, try it. Otherwise default back to the first.
this.currentPage = this.currentPage <= this.pages ? this.currentPage : 1;
// Attempt to re-select the same item
if (currentItem != null) {
this.forSaleList.setSelectedItem(this.forSaleList.getItems().stream().filter(i -> i.getRecord() == currentItem.getRecord()).findFirst().orElse(null));
}
this.updateControls();
}
use of com.almuradev.almura.feature.exchange.listing.ForSaleItem in project Almura by AlmuraDev.
the class ExchangeScreen method updateControls.
private void updateControls() {
// Results list
final ForSaleItem currentForSaleItem = this.forSaleList.getSelectedItem();
final boolean isResultSelected = currentForSaleItem != null;
this.buttonBuyOne.setEnabled(isResultSelected);
this.buttonBuyQuantity.setEnabled(isResultSelected && currentForSaleItem.getQuantity() > 1);
final int maxStackSize = currentForSaleItem != null ? currentForSaleItem.asRealStack().getMaxStackSize() : 0;
this.buttonBuyStack.setEnabled(isResultSelected && currentForSaleItem.getQuantity() > 1 && currentForSaleItem.getQuantity() >= maxStackSize);
this.buttonBuyStack.setText(I18n.format("almura.feature.common.button.buy.stack", maxStackSize));
this.buttonBuyAll.setEnabled(isResultSelected && currentForSaleItem.getQuantity() > 1);
// Selling list
final boolean isSellingItemSelected = this.listItemList.getSelectedItem() != null;
final boolean isListed = isSellingItemSelected && this.listItemList.getSelectedItem().getForSaleItem().isPresent();
this.buttonList.setEnabled(isSellingItemSelected);
this.buttonList.setText(I18n.format("almura.feature.common.button." + (isListed ? "unlist" : "list")));
// Update page buttons/labels
this.buttonFirstPage.setEnabled(this.currentPage != 1);
this.buttonPreviousPage.setEnabled(this.currentPage != 1);
this.buttonNextPage.setEnabled(this.currentPage != this.pages);
this.buttonLastPage.setEnabled(this.currentPage != this.pages);
this.labelSearchPage.setText(TextFormatting.WHITE + String.valueOf(this.currentPage) + "/" + this.pages);
// Update all items
this.listItemList.getComponents().stream().filter(c -> c instanceof ListItemComponent).map(c -> (ListItemComponent) c).forEach(ListItemComponent::update);
// Update the limit label
this.labelLimit.setText(TextFormatting.WHITE + String.valueOf(this.listItemList.getItems().size()) + "/" + this.limit);
// Hide the list and show the label if there are no items to present
final boolean isEmpty = this.forSaleList.getItems().isEmpty();
this.forSaleList.setVisible(!isEmpty);
this.noResultsLabel.setVisible(isEmpty);
}
use of com.almuradev.almura.feature.exchange.listing.ForSaleItem in project Almura by AlmuraDev.
the class ExchangeScreen method construct.
@Override
public void construct() {
guiscreenBackground = false;
// Detect if screen area is large enough to display.
if (minScreenWidth > this.resolution.getScaledWidth() || minScreenHeight > this.resolution.getScaledHeight()) {
notificationManager.handlePopup(new PopupNotification("Exchange Error", "Screen area of: " + minScreenHeight + " x " + minScreenWidth + " required.", 5));
this.close();
return;
}
// Main Panel
final BasicForm form = new BasicForm(this, minScreenWidth, minScreenHeight, this.getExchange().getName());
// Search section
final BasicContainer<?> searchContainer = new BasicContainer<>(this, 295, 322);
searchContainer.setPosition(0, 0, Anchor.LEFT | Anchor.TOP);
searchContainer.setColor(0);
searchContainer.setBorder(FontColors.WHITE, 1, 185);
searchContainer.setBackgroundAlpha(215);
searchContainer.setPadding(3, 3);
final UILabel itemSearchLabel = new UILabel(this, I18n.format("almura.feature.exchange.text.item_name") + ":");
itemSearchLabel.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).scale(1.1F).build());
itemSearchLabel.setPosition(0, 4, Anchor.LEFT | Anchor.TOP);
this.itemDisplayNameSearchBox = new BasicTextBox(this, "");
this.itemDisplayNameSearchBox.setAcceptsReturn(false);
this.itemDisplayNameSearchBox.setAcceptsTab(false);
this.itemDisplayNameSearchBox.setTabIndex(0);
this.itemDisplayNameSearchBox.setOnEnter((tb) -> this.search());
this.itemDisplayNameSearchBox.setSize(145, 0);
this.itemDisplayNameSearchBox.setPosition(itemSearchLabel.getWidth() + innerPadding, 2, Anchor.LEFT | Anchor.TOP);
this.itemDisplayNameSearchBox.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).shadow(false).build());
final UILabel sellerSearchLabel = new UILabel(this, I18n.format("almura.feature.exchange.text.seller") + ":");
sellerSearchLabel.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).scale(1.1F).build());
sellerSearchLabel.setPosition(itemDisplayNameSearchBox.getX() - sellerSearchLabel.getWidth() - 1, getPaddedY(itemSearchLabel, 7), Anchor.LEFT | Anchor.TOP);
this.sellerSearchTextBox = new BasicTextBox(this, "");
this.sellerSearchTextBox.setAcceptsReturn(false);
this.sellerSearchTextBox.setAcceptsTab(false);
this.sellerSearchTextBox.setTabIndex(1);
this.sellerSearchTextBox.setOnEnter(tb -> this.search());
this.sellerSearchTextBox.setSize(145, 0);
this.sellerSearchTextBox.setPosition(this.itemDisplayNameSearchBox.getX(), getPaddedY(this.itemDisplayNameSearchBox, 4), Anchor.LEFT | Anchor.TOP);
this.sellerSearchTextBox.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).shadow(false).build());
// Sort combobox
// Sort combobox
this.comboBoxSortType = new UISelect<>(this, 78);
this.comboBoxSortType.setOptions(Arrays.asList(FeatureSortTypes.CREATED_ASC, FeatureSortTypes.CREATED_DESC, FeatureSortTypes.PRICE_ASC, FeatureSortTypes.PRICE_DESC, FeatureSortTypes.ITEM_DISPLAY_NAME_ASC, FeatureSortTypes.ITEM_DISPLAY_NAME_DESC, ExchangeSortTypes.SELLER_NAME_ASC, ExchangeSortTypes.SELLER_NAME_DESC));
// Because that's reasonable.
this.comboBoxSortType.setLabelFunction(type -> type == null ? "" : type.getName());
this.comboBoxSortType.select(FeatureSortTypes.CREATED_ASC);
this.comboBoxSortType.setPosition(-(innerPadding + 1), 2, Anchor.RIGHT | Anchor.TOP);
// Search button
final UIButton buttonSearch = new UIButtonBuilder(this).width(80).anchor(Anchor.RIGHT | Anchor.TOP).position(-innerPadding, this.sellerSearchTextBox.getY() - 1).text(I18n.format("almura.feature.common.button.search")).onClick(this::search).build("button.search");
// Separator
final BasicContainer<?> forSaleTopLine = new BasicContainer<>(this, searchContainer.getRawWidth() - 2, 1);
forSaleTopLine.setColor(FontColors.WHITE);
forSaleTopLine.setBackgroundAlpha(185);
forSaleTopLine.setPosition(0, BasicScreen.getPaddedY(this.sellerSearchTextBox, 4), Anchor.CENTER | Anchor.TOP);
this.forSaleList = new BasicList<>(this, UIComponent.INHERITED, 254);
this.forSaleList.setPosition(0, BasicScreen.getPaddedY(forSaleTopLine, 2));
this.forSaleList.setItemComponentFactory(ForSaleItemComponent::new);
this.forSaleList.setItemComponentSpacing(1);
this.forSaleList.setSelectConsumer((i) -> this.updateControls());
// No results label
this.noResultsLabel = new UILabel(this, "No results found.");
this.noResultsLabel.setPosition(0, this.forSaleList.getY() + 8, Anchor.TOP | Anchor.CENTER);
this.noResultsLabel.setFontOptions(FontColors.GRAY_FO);
this.noResultsLabel.setVisible(false);
// Bottom Page Control - first button
this.buttonFirstPage = new UIButtonBuilder(this).size(20).anchor(Anchor.LEFT | Anchor.BOTTOM).position(0, 0).text("|<").enabled(false).onClick(() -> setPage(1)).build("button.first");
// Bottom Page Control - previous button
this.buttonPreviousPage = new UIButtonBuilder(this).size(20).anchor(Anchor.LEFT | Anchor.BOTTOM).position(getPaddedX(this.buttonFirstPage, innerPadding), 0).text("<").enabled(false).onClick(() -> setPage(--this.currentPage)).build("button.previous");
// Search pages label
this.labelSearchPage = new UILabel(this, "");
this.labelSearchPage.setPosition(0, -4, Anchor.CENTER | Anchor.BOTTOM);
// Bottom Page Control - last button
this.buttonLastPage = new UIButtonBuilder(this).size(20).anchor(Anchor.RIGHT | Anchor.BOTTOM).position(0, 0).text(">|").enabled(false).onClick(() -> setPage(this.pages)).build("button.last");
// Bottom Page Control - next button
this.buttonNextPage = new UIButtonBuilder(this).size(20).anchor(Anchor.RIGHT | Anchor.BOTTOM).position(getPaddedX(this.buttonLastPage, innerPadding, Anchor.RIGHT), 0).text(">").enabled(false).onClick(() -> setPage(++this.currentPage)).build("button.next");
// Separator
final BasicLine forSaleBottomLine = new BasicLine(this, searchContainer.getRawWidth() - 2);
forSaleBottomLine.setPosition(0, BasicScreen.getPaddedY(this.buttonFirstPage, 2, Anchor.BOTTOM), Anchor.CENTER | Anchor.BOTTOM);
// Add Elements of Search Area
searchContainer.add(itemSearchLabel, this.itemDisplayNameSearchBox, sellerSearchLabel, this.sellerSearchTextBox, buttonSearch, this.comboBoxSortType, this.buttonFirstPage, this.buttonPreviousPage, this.buttonNextPage, this.buttonLastPage, this.forSaleList, this.noResultsLabel, this.labelSearchPage, forSaleTopLine, forSaleBottomLine);
// Buy container
final BasicContainer<?> buyContainer = new BasicContainer(this, 295, 21);
buyContainer.setPosition(0, getPaddedY(searchContainer, innerPadding), Anchor.LEFT | Anchor.TOP);
buyContainer.setColor(0);
buyContainer.setBorder(FontColors.WHITE, 1, 185);
buyContainer.setBackgroundAlpha(215);
buyContainer.setPadding(3, 0);
this.buttonBuyOne = new UIButtonBuilder(this).width(60).anchor(Anchor.LEFT | Anchor.MIDDLE).position(0, 0).text(I18n.format("almura.feature.common.button.buy.one")).enabled(false).onClick(() -> this.buy(1)).build("button.buy.single");
this.buttonBuyStack = new UIButtonBuilder(this).width(60).anchor(Anchor.LEFT | Anchor.MIDDLE).position(BasicScreen.getPaddedX(this.buttonBuyOne, 10), 0).text(I18n.format("almura.feature.common.button.buy.stack", 0)).enabled(false).onClick(() -> {
final ForSaleItem forSaleItem = this.forSaleList.getSelectedItem();
if (forSaleItem != null) {
this.buy(forSaleItem.asRealStack().getMaxStackSize());
}
}).build("button.buy.stack");
this.buttonBuyAll = new UIButtonBuilder(this).width(50).anchor(Anchor.RIGHT | Anchor.MIDDLE).position(0, 0).text(I18n.format("almura.feature.common.button.buy.all")).enabled(false).onClick(() -> {
final ForSaleItem forSaleItem = this.forSaleList.getSelectedItem();
if (forSaleItem != null) {
this.buy(forSaleItem.getQuantity());
}
}).build("button.buy.all");
this.buttonBuyQuantity = new UIButtonBuilder(this).width(60).anchor(Anchor.RIGHT | Anchor.MIDDLE).position(BasicScreen.getPaddedX(this.buttonBuyAll, 10, Anchor.RIGHT), 0).text(I18n.format("almura.feature.common.button.buy.quantity")).enabled(false).onClick(() -> {
final ForSaleItem forSaleItem = this.forSaleList.getSelectedItem();
if (forSaleItem != null) {
new ExchangeBuyQuantityScreen(this, this.axs, forSaleItem).display();
}
}).build("button.buy.quantity");
buyContainer.add(this.buttonBuyOne, this.buttonBuyStack, this.buttonBuyQuantity, this.buttonBuyAll);
// Listable Items section
final BasicContainer<?> listableItemsContainer = new BasicContainer(this, 295, 345);
listableItemsContainer.setPosition(0, 0, Anchor.RIGHT | Anchor.TOP);
listableItemsContainer.setBorder(FontColors.WHITE, 1, 185);
listableItemsContainer.setColor(0);
listableItemsContainer.setBackgroundAlpha(215);
listableItemsContainer.setPadding(3, 3);
final UILabel listableItemsLabel = new UILabel(this, I18n.format("almura.feature.exchange.title.listable_items"));
listableItemsLabel.setPosition(0, 2, Anchor.CENTER | Anchor.TOP);
listableItemsLabel.setFontOptions(FontColors.WHITE_FO);
final BasicLine listTopLine = new BasicLine(this, listableItemsContainer.getRawWidth() - 2);
listTopLine.setPosition(0, BasicScreen.getPaddedY(listableItemsLabel, 3), Anchor.TOP | Anchor.CENTER);
this.listItemList = new BasicList<>(this, UIComponent.INHERITED, 302);
this.listItemList.setPosition(0, BasicScreen.getPaddedY(listTopLine, 2));
this.listItemList.setItemComponentFactory(ListItemComponent::new);
this.listItemList.setItemComponentSpacing(1);
this.listItemList.setSelectConsumer((i) -> this.updateControls());
this.buttonList = new UIButtonBuilder(this).width(40).anchor(Anchor.LEFT | Anchor.BOTTOM).position(0, 0).text(I18n.format("almura.feature.common.button.list")).enabled(false).onClick(this::list).build("button.list");
this.labelLimit = new UILabel(this, "");
this.labelLimit.setPosition(0, -2, Anchor.CENTER | Anchor.BOTTOM);
final UIButton buttonOffer = new UIButtonBuilder(this).width(30).anchor(Anchor.RIGHT | Anchor.BOTTOM).position(0, 0).text(TextFormatting.DARK_GREEN + "+" + TextFormatting.GRAY + "/" + TextFormatting.RED + "-").enabled(true).onClick(() -> exchangeManager.requestExchangeSpecificOfferGui(this.axs.getId())).build("button.offer");
// Separator
final BasicLine listBottomLine = new BasicLine(this, searchContainer.getRawWidth() - 2);
listBottomLine.setPosition(0, BasicScreen.getPaddedY(this.buttonList, 2, Anchor.BOTTOM), Anchor.CENTER | Anchor.BOTTOM);
listableItemsContainer.add(listableItemsLabel, listTopLine, this.listItemList, this.buttonList, this.labelLimit, buttonOffer, listBottomLine);
form.add(searchContainer, buyContainer, listableItemsContainer);
addToScreen(form);
this.itemDisplayNameSearchBox.focus();
}
use of com.almuradev.almura.feature.exchange.listing.ForSaleItem 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.ForSaleItem 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);
}
Aggregations