Search in sources :

Example 1 with ForSaleItem

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();
}
Also used : Arrays(java.util.Arrays) FeatureSortType(com.almuradev.almura.feature.FeatureSortType) UIComponent(net.malisis.core.client.gui.component.UIComponent) PopupNotification(com.almuradev.almura.feature.notification.type.PopupNotification) I18n(net.minecraft.client.resources.I18n) BigDecimal(java.math.BigDecimal) UISaneTooltip(com.almuradev.almura.shared.client.ui.component.UISaneTooltip) MalisisGui(net.malisis.core.client.gui.MalisisGui) BasicTextBox(net.malisis.core.client.gui.component.interaction.BasicTextBox) Side(net.minecraftforge.fml.relauncher.Side) ExchangeModule(com.almuradev.almura.feature.exchange.ExchangeModule) Map(java.util.Map) BasicList(net.malisis.core.client.gui.component.container.BasicList) BasicContainer(net.malisis.core.client.gui.component.container.BasicContainer) Exchange(com.almuradev.almura.feature.exchange.Exchange) FontColors(net.malisis.core.util.FontColors) FeatureConstants(com.almuradev.almura.shared.feature.FeatureConstants) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) UIImage(net.malisis.core.client.gui.component.decoration.UIImage) MathUtil(com.almuradev.almura.shared.util.MathUtil) Direction(com.almuradev.almura.shared.feature.filter.Direction) SortType(com.almuradev.almura.feature.SortType) ListStatusType(com.almuradev.almura.feature.exchange.ListStatusType) BasicLine(net.malisis.core.client.gui.component.decoration.BasicLine) HashMap(java.util.HashMap) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) FeatureSortTypes(com.almuradev.almura.FeatureSortTypes) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) Inject(javax.inject.Inject) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) Anchor(net.malisis.core.client.gui.Anchor) VirtualStack(com.almuradev.almura.shared.item.VirtualStack) BasicScreen(net.malisis.core.client.gui.BasicScreen) UIExpandingLabel(com.almuradev.almura.shared.client.ui.component.UIExpandingLabel) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) UISelect(net.malisis.core.client.gui.component.interaction.UISelect) Nullable(javax.annotation.Nullable) UIButton(net.malisis.core.client.gui.component.interaction.UIButton) Logger(org.slf4j.Logger) ClientExchangeManager(com.almuradev.almura.feature.exchange.client.ClientExchangeManager) UILabel(net.malisis.core.client.gui.component.decoration.UILabel) TextFormatting(net.minecraft.util.text.TextFormatting) ClientNotificationManager(com.almuradev.almura.feature.notification.ClientNotificationManager) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) FontRenderer(net.minecraft.client.gui.FontRenderer) FontOptions(net.malisis.core.renderer.font.FontOptions) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem)

Example 2 with ForSaleItem

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);
}
Also used : Arrays(java.util.Arrays) FeatureSortType(com.almuradev.almura.feature.FeatureSortType) UIComponent(net.malisis.core.client.gui.component.UIComponent) PopupNotification(com.almuradev.almura.feature.notification.type.PopupNotification) I18n(net.minecraft.client.resources.I18n) BigDecimal(java.math.BigDecimal) UISaneTooltip(com.almuradev.almura.shared.client.ui.component.UISaneTooltip) MalisisGui(net.malisis.core.client.gui.MalisisGui) BasicTextBox(net.malisis.core.client.gui.component.interaction.BasicTextBox) Side(net.minecraftforge.fml.relauncher.Side) ExchangeModule(com.almuradev.almura.feature.exchange.ExchangeModule) Map(java.util.Map) BasicList(net.malisis.core.client.gui.component.container.BasicList) BasicContainer(net.malisis.core.client.gui.component.container.BasicContainer) Exchange(com.almuradev.almura.feature.exchange.Exchange) FontColors(net.malisis.core.util.FontColors) FeatureConstants(com.almuradev.almura.shared.feature.FeatureConstants) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) UIImage(net.malisis.core.client.gui.component.decoration.UIImage) MathUtil(com.almuradev.almura.shared.util.MathUtil) Direction(com.almuradev.almura.shared.feature.filter.Direction) SortType(com.almuradev.almura.feature.SortType) ListStatusType(com.almuradev.almura.feature.exchange.ListStatusType) BasicLine(net.malisis.core.client.gui.component.decoration.BasicLine) HashMap(java.util.HashMap) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) FeatureSortTypes(com.almuradev.almura.FeatureSortTypes) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) Inject(javax.inject.Inject) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) Anchor(net.malisis.core.client.gui.Anchor) VirtualStack(com.almuradev.almura.shared.item.VirtualStack) BasicScreen(net.malisis.core.client.gui.BasicScreen) UIExpandingLabel(com.almuradev.almura.shared.client.ui.component.UIExpandingLabel) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) UISelect(net.malisis.core.client.gui.component.interaction.UISelect) Nullable(javax.annotation.Nullable) UIButton(net.malisis.core.client.gui.component.interaction.UIButton) Logger(org.slf4j.Logger) ClientExchangeManager(com.almuradev.almura.feature.exchange.client.ClientExchangeManager) UILabel(net.malisis.core.client.gui.component.decoration.UILabel) TextFormatting(net.minecraft.util.text.TextFormatting) ClientNotificationManager(com.almuradev.almura.feature.notification.ClientNotificationManager) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) FontRenderer(net.minecraft.client.gui.FontRenderer) FontOptions(net.malisis.core.renderer.font.FontOptions) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem)

Example 3 with ForSaleItem

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();
}
Also used : UILabel(net.malisis.core.client.gui.component.decoration.UILabel) BasicLine(net.malisis.core.client.gui.component.decoration.BasicLine) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) UIButton(net.malisis.core.client.gui.component.interaction.UIButton) BasicContainer(net.malisis.core.client.gui.component.container.BasicContainer) BasicTextBox(net.malisis.core.client.gui.component.interaction.BasicTextBox) PopupNotification(com.almuradev.almura.feature.notification.type.PopupNotification)

Example 4 with ForSaleItem

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);
}
Also used : ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) IOException(java.io.IOException)

Example 5 with ForSaleItem

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);
}
Also used : BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) SQLException(java.sql.SQLException) BasicForSaleItem(com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem) AxsForSaleItem(com.almuradev.generated.axs.tables.AxsForSaleItem) ForSaleItem(com.almuradev.almura.feature.exchange.listing.ForSaleItem) ClientboundForSaleFilterRequestPacket(com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket) ClientboundListItemsResponsePacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket) ClientboundListItemsSaleStatusPacket(com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket) DSLContext(org.jooq.DSLContext) AxsListItem(com.almuradev.generated.axs.tables.AxsListItem) BasicListItem(com.almuradev.almura.feature.exchange.basic.listing.BasicListItem) ListItem(com.almuradev.almura.feature.exchange.listing.ListItem)

Aggregations

ForSaleItem (com.almuradev.almura.feature.exchange.listing.ForSaleItem)16 ListItem (com.almuradev.almura.feature.exchange.listing.ListItem)15 BasicForSaleItem (com.almuradev.almura.feature.exchange.basic.listing.BasicForSaleItem)11 BasicListItem (com.almuradev.almura.feature.exchange.basic.listing.BasicListItem)11 ClientboundForSaleFilterRequestPacket (com.almuradev.almura.feature.exchange.network.ClientboundForSaleFilterRequestPacket)8 ClientboundListItemsResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsResponsePacket)8 ClientboundListItemsSaleStatusPacket (com.almuradev.almura.feature.exchange.network.ClientboundListItemsSaleStatusPacket)8 AxsForSaleItem (com.almuradev.generated.axs.tables.AxsForSaleItem)8 AxsListItem (com.almuradev.generated.axs.tables.AxsListItem)8 BigDecimal (java.math.BigDecimal)8 List (java.util.List)7 BasicExchange (com.almuradev.almura.feature.exchange.basic.BasicExchange)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 UUID (java.util.UUID)6 DSLContext (org.jooq.DSLContext)6 ClientboundExchangeGuiResponsePacket (com.almuradev.almura.feature.exchange.network.ClientboundExchangeGuiResponsePacket)5 ClientboundTransactionCompletePacket (com.almuradev.almura.feature.exchange.network.ClientboundTransactionCompletePacket)5