Search in sources :

Example 1 with StoreItem

use of com.almuradev.almura.feature.store.listing.StoreItem in project Almura by AlmuraDev.

the class StoreScreen method updateStoreControls.

private void updateStoreControls() {
    final String sideName = this.currentSide.name().toLowerCase();
    final StoreItem selectedItem = this.itemList.getSelectedItem();
    // Common
    this.buttonTransactOne.setText(I18n.format("almura.feature.common.button." + sideName + ".one"));
    this.buttonTransactQuantity.setText(I18n.format("almura.feature.common.button." + sideName + ".quantity"));
    this.buttonTransactAll.setText(I18n.format("almura.feature.common.button." + sideName + ".all"));
    // Nothing selected
    if (selectedItem == null) {
        this.buttonTransactOne.setEnabled(false);
        this.buttonTransactStack.setEnabled(false);
        this.buttonTransactQuantity.setEnabled(false);
        this.buttonTransactAll.setEnabled(false);
        // Generic info
        this.buttonTransactStack.setText(I18n.format("almura.feature.common.button." + sideName + ".stack", 64));
        return;
    }
    // Do we have stock?
    final boolean isAvailable = selectedItem.getQuantity() != 0;
    final int maxStackSize = selectedItem.asRealStack().getMaxStackSize();
    if (currentSide == SideType.SELL) {
        // Determine how much we have in our main inventory
        final int heldQuantity = Minecraft.getMinecraft().player.inventory.mainInventory.stream().filter(i -> ItemStack.areItemsEqual(selectedItem.asRealStack(), i)).mapToInt(ItemStack::getCount).sum();
        // Update all enable states
        // We want at least one
        this.buttonTransactOne.setEnabled(isAvailable && heldQuantity >= 1);
        this.buttonTransactStack.setEnabled(isAvailable && heldQuantity >= maxStackSize);
        this.buttonTransactQuantity.setEnabled(isAvailable && heldQuantity > 1);
        // We want at least one
        this.buttonTransactAll.setEnabled(isAvailable && heldQuantity >= 1);
        // Update stack button
        this.buttonTransactStack.setText(I18n.format("almura.feature.common.button." + sideName + ".stack", maxStackSize));
    } else {
        final boolean isInfinite = selectedItem.getQuantity() == -1;
        this.buttonTransactStack.setText(I18n.format("almura.feature.common.button." + sideName + ".stack", maxStackSize));
        this.buttonTransactOne.setEnabled(isInfinite || isAvailable && selectedItem.getQuantity() > 0);
        this.buttonTransactStack.setEnabled(isInfinite || isAvailable && selectedItem.getQuantity() >= maxStackSize);
        this.buttonTransactQuantity.setEnabled(isInfinite || isAvailable && selectedItem.getQuantity() > 0);
        this.buttonTransactAll.setEnabled(isAvailable && selectedItem.getQuantity() >= 1);
    }
}
Also used : StoreItem(com.almuradev.almura.feature.store.listing.StoreItem)

Example 2 with StoreItem

use of com.almuradev.almura.feature.store.listing.StoreItem in project Almura by AlmuraDev.

the class ClientboundListItemsResponsePacket method writeTo.

@Override
public void writeTo(final ChannelBuf buf) {
    checkNotNull(this.id);
    checkNotNull(this.type);
    buf.writeString(this.id);
    buf.writeString(this.type.name().toUpperCase(Locale.ENGLISH));
    buf.writeInteger(this.storeItems == null ? 0 : this.storeItems.size());
    if (this.storeItems != null) {
        for (final StoreItem item : this.storeItems) {
            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.writeInteger(createdData.length);
            buf.writeBytes(createdData);
            buf.writeString(location.getNamespace());
            buf.writeString(location.getPath());
            buf.writeInteger(item.getQuantity());
            buf.writeInteger(item.getMetadata());
            ByteBufUtil.writeBigDecimal((ByteBuf) buf, item.getPrice());
            buf.writeInteger(item.getIndex());
            if (compoundData == null) {
                buf.writeInteger(0);
            } else {
                buf.writeInteger(compoundData.length);
                buf.writeBytes(compoundData);
            }
        }
    }
}
Also used : AbstractStoreItem(com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException)

Example 3 with StoreItem

use of com.almuradev.almura.feature.store.listing.StoreItem in project Almura by AlmuraDev.

the class ClientStoreManager method filterLocalItems.

@SuppressWarnings("unchecked")
public <T extends StoreItem> List<T> filterLocalItems(final String id, @Nullable final String filter, @Nullable final String sort, final SideType targetSide) {
    checkNotNull(id);
    checkNotNull(targetSide);
    final Store axs = this.getStore(id);
    checkNotNull(axs);
    Stream<T> stream = targetSide == SideType.BUY ? (Stream<T>) axs.getBuyingItems().stream() : (Stream<T>) axs.getSellingItems().stream();
    if (filter != null) {
        final List<FilterRegistry.FilterElement<StoreItem>> elements = FilterRegistry.instance.getFilterElements(filter);
        stream = stream.filter(storeItem -> elements.stream().allMatch(element -> element.getFilter().test(storeItem, element.getValue())));
    }
    if (sort != null) {
        final List<FilterRegistry.SorterElement<StoreItem>> elements = FilterRegistry.instance.getSortingElements(sort);
        final Comparator<StoreItem> comparator = FilterRegistry.instance.buildSortingComparator(elements).orElse(null);
        if (comparator != null) {
            stream = stream.sorted(comparator);
        }
    }
    return stream.collect(Collectors.toList());
}
Also used : FMLNetworkEvent(net.minecraftforge.fml.common.network.FMLNetworkEvent) Almura(com.almuradev.almura.Almura) FilterRegistry(com.almuradev.almura.shared.feature.filter.FilterRegistry) Inject(com.google.inject.Inject) ServerboundListItemsRequestPacket(com.almuradev.almura.feature.store.network.ServerboundListItemsRequestPacket) Singleton(javax.inject.Singleton) StoreModifyType(com.almuradev.almura.feature.store.StoreModifyType) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) Minecraft(net.minecraft.client.Minecraft) ServerboundDelistItemsPacket(com.almuradev.almura.feature.store.network.ServerboundDelistItemsPacket) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) StoreItemSegmentType(com.almuradev.almura.feature.store.StoreItemSegmentType) Nullable(javax.annotation.Nullable) VanillaStack(com.almuradev.almura.shared.item.VanillaStack) ServerboundItemTransactionPacket(com.almuradev.almura.feature.store.network.ServerboundItemTransactionPacket) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) FeatureConstants(com.almuradev.almura.shared.feature.FeatureConstants) Store(com.almuradev.almura.feature.store.Store) StoreScreen(com.almuradev.almura.feature.store.client.gui.StoreScreen) Set(java.util.Set) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) SideType(com.almuradev.almura.feature.store.SideType) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) StoreManagementScreen(com.almuradev.almura.feature.store.client.gui.StoreManagementScreen) ChannelBinding(org.spongepowered.api.network.ChannelBinding) GuiScreen(net.minecraft.client.gui.GuiScreen) List(java.util.List) Stream(java.util.stream.Stream) Witness(com.almuradev.core.event.Witness) ServerboundModifyStorePacket(com.almuradev.almura.feature.store.network.ServerboundModifyStorePacket) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) ChannelId(org.spongepowered.api.network.ChannelId) Comparator(java.util.Comparator) ServerboundModifyItemsPacket(com.almuradev.almura.feature.store.network.ServerboundModifyItemsPacket) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) Store(com.almuradev.almura.feature.store.Store)

Example 4 with StoreItem

use of com.almuradev.almura.feature.store.listing.StoreItem in project Almura by AlmuraDev.

the class StoreScreen method construct.

@Override
public void construct() {
    guiscreenBackground = false;
    final BasicForm form = new BasicForm(this, 257, 300, this.store.getName());
    form.setBackgroundAlpha(255);
    final BasicContainer<?> storeContainer = new BasicContainer<>(this, 251, UIComponent.INHERITED);
    storeContainer.setBorder(FontColors.WHITE, 1, 185);
    storeContainer.setPadding(3);
    storeContainer.setColor(0);
    // Buy tab
    final int leftOffset = storeContainer.getLeftPadding() - storeContainer.getLeftBorderSize();
    this.buyTabContainer = new BasicContainer<>(this, this.tabWidth, this.tabHeight);
    this.buyTabContainer.setBorders(FontColors.WHITE, 185, 1, 1, 1, 0);
    this.buyTabContainer.setPosition(2, 2);
    this.buyTabContainer.setColor(defaultTabColor);
    this.buyTabContainer.attachData(SideType.BUY);
    // Buy tab label
    this.buyTabLabel = new UILabel(this, I18n.format("almura.feature.common.button.buy"));
    this.buyTabLabel.setPosition(0, 1, Anchor.MIDDLE | Anchor.CENTER);
    // Always use the same data as their parent
    this.buyTabLabel.attachData(this.buyTabContainer.getData());
    this.buyTabContainer.add(this.buyTabLabel);
    // Sell tab
    this.sellTabContainer = new BasicContainer<>(this, this.tabWidth, this.tabHeight);
    this.sellTabContainer.setBorders(FontColors.WHITE, 185, 1, 1, 1, 0);
    this.sellTabContainer.setColor(defaultTabColor);
    this.sellTabContainer.setPosition(-2, 2, Anchor.TOP | Anchor.RIGHT);
    this.sellTabContainer.attachData(SideType.SELL);
    // Sell tab label
    this.sellTabLabel = new UILabel(this, I18n.format("almura.feature.common.button.sell"));
    this.sellTabLabel.setPosition(0, 1, Anchor.MIDDLE | Anchor.CENTER);
    // Always use the same data as their parent
    this.sellTabLabel.attachData(this.sellTabContainer.getData());
    this.sellTabContainer.add(this.sellTabLabel);
    // Doesn't exist.
    this.thisDoesNotExistLine = new BasicLine(this, this.tabWidth - 2);
    this.thisDoesNotExistLine.setBackgroundAlpha(255);
    this.thisDoesNotExistLine.setColor(selectedTabColor);
    // Bottom tab line
    final BasicLine tabContainerLineBottom = new BasicLine(this, storeContainer.getWidth() - (storeContainer.getLeftBorderSize() + storeContainer.getRightBorderSize()), 1);
    tabContainerLineBottom.setPosition(-leftOffset, BasicScreen.getPaddedY(this.buyTabContainer, 0));
    // Search Y
    final int searchY = BasicScreen.getPaddedY(tabContainerLineBottom, 2);
    // Item Display Name Search Label
    final UILabel itemSearchLabel = new UILabel(this, I18n.format("almura.feature.exchange.text.item_name") + ":");
    itemSearchLabel.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).build());
    itemSearchLabel.setPosition(1, searchY + 3, Anchor.LEFT | Anchor.TOP);
    // Item Display Name Search Text Box
    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(60, 0);
    this.itemDisplayNameSearchBox.setPosition(BasicScreen.getPaddedX(itemSearchLabel, innerPadding), searchY + 1, Anchor.LEFT | Anchor.TOP);
    this.itemDisplayNameSearchBox.setFontOptions(FontOptions.builder().from(FontColors.WHITE_FO).shadow(false).build());
    // Sort combobox
    this.comboBoxSortType = new UISelect<>(this, 78);
    this.comboBoxSortType.setPosition(BasicScreen.getPaddedX(this.itemDisplayNameSearchBox, innerPadding), searchY + 1);
    this.comboBoxSortType.setOptions(Arrays.asList(FeatureSortTypes.ITEM_DISPLAY_NAME_ASC, FeatureSortTypes.ITEM_DISPLAY_NAME_DESC, FeatureSortTypes.PRICE_ASC, FeatureSortTypes.PRICE_DESC));
    this.comboBoxSortType.setOptionsWidth(UISelect.LONGEST_OPTION);
    // Because that's reasonable.
    this.comboBoxSortType.setLabelFunction(type -> type == null ? "" : type.getName());
    this.comboBoxSortType.select(FeatureSortTypes.ITEM_DISPLAY_NAME_ASC);
    // Search button
    final UIButton buttonSearch = new UIButtonBuilder(this).width(50).anchor(Anchor.RIGHT | Anchor.TOP).position(0, searchY).text(I18n.format("almura.feature.common.button.search")).onClick(this::search).build("button.search");
    // List
    this.itemList = new BasicList<>(this, UIComponent.INHERITED, BasicScreen.getPaddedHeight(form) - this.buyTabContainer.getHeight() - tabContainerLineBottom.getHeight() - buttonSearch.getHeight() - 30);
    this.itemList.setPosition(0, BasicScreen.getPaddedY(buttonSearch, 2));
    this.itemList.setSelectConsumer(i -> this.updateStoreControls());
    this.itemList.setItemComponentSpacing(1);
    this.itemList.setBorder(FontColors.WHITE, 1, 185);
    this.itemList.setPadding(2);
    this.itemList.setItemComponentFactory(StoreItemComponent::new);
    this.buttonTransactOne = new UIButtonBuilder(this).width(60).anchor(Anchor.BOTTOM | Anchor.LEFT).enabled(false).onClick(() -> this.transact(1)).build("button.transact.single");
    this.buttonTransactStack = new UIButtonBuilder(this).width(60).x(BasicScreen.getPaddedX(this.buttonTransactOne, 2)).anchor(Anchor.BOTTOM | Anchor.LEFT).enabled(false).onClick(() -> {
        if (this.itemList.getSelectedItem() == null) {
            return;
        }
        final int value = this.itemList.getSelectedItem().asRealStack().getMaxStackSize();
        this.transact(value);
    }).build("button.transact.stack");
    this.buttonTransactAll = new UIButtonBuilder(this).width(60).anchor(Anchor.BOTTOM | Anchor.RIGHT).enabled(false).onClick(() -> {
        final StoreItem selectedItem = this.itemList.getSelectedItem();
        if (selectedItem == null) {
            return;
        }
        // Determine the value to transact
        final int value;
        if (this.currentSide == SideType.BUY) {
            // If we are buying then base the value on the quantity remaining for the item to purchase
            value = this.itemList.getSelectedItem().getQuantity();
        } else {
            // If we're selling then base it on how many we have in our main inventory
            value = Minecraft.getMinecraft().player.inventory.mainInventory.stream().filter(i -> ItemStack.areItemsEqual(i, selectedItem.asRealStack())).mapToInt(ItemStack::getCount).sum();
        }
        this.transact(value);
    }).build("button.transact.all");
    this.buttonTransactQuantity = new UIButtonBuilder(this).width(60).x(BasicScreen.getPaddedX(this.buttonTransactAll, 2, Anchor.RIGHT)).anchor(Anchor.BOTTOM | Anchor.RIGHT).enabled(false).onClick(() -> {
        final StoreItem selectedItem = this.itemList.getSelectedItem();
        if (selectedItem == null) {
            return;
        }
        new StoreTransactQuantityScreen(this, this.store, selectedItem, this.currentSide).display();
    }).build("button.transact.quantity");
    storeContainer.add(this.buyTabContainer, this.sellTabContainer, tabContainerLineBottom, this.thisDoesNotExistLine, itemSearchLabel, this.itemDisplayNameSearchBox, this.comboBoxSortType, buttonSearch, this.itemList, this.buttonTransactOne, this.buttonTransactStack, this.buttonTransactQuantity, this.buttonTransactAll);
    form.add(storeContainer);
    // Logic for admin pane
    if (this.isAdmin) {
        // Adjust form width
        form.setWidth(form.getWidth() + 224);
        // Create admin pane
        final BasicContainer<?> adminContainer = new BasicContainer(this, 220, UIComponent.INHERITED);
        adminContainer.setPosition(BasicScreen.getPaddedX(storeContainer, 4), 0);
        adminContainer.setBorder(FontColors.WHITE, 1, 185);
        adminContainer.setPadding(3);
        adminContainer.setColor(0);
        // Buy tab label
        this.typeLabel = new UILabel(this, TextFormatting.WHITE + "Type: ");
        this.typeLabel.setPosition(0, 2, Anchor.TOP | Anchor.LEFT);
        // Item location selection
        this.locationSelect = new UISelect<>(this, UIComponent.INHERITED);
        this.locationSelect.setOptions(itemFinderRelationshipMap.keySet());
        this.locationSelect.setLabelFunction(o -> I18n.format(o.translationKey));
        this.locationSelect.setPosition(30, 0);
        this.locationSelect.setSize(184, 12);
        this.locationSelect.maxDisplayedOptions(24);
        this.locationSelect.register(this);
        // Search text box
        this.adminSearchTextBox = new BasicTextBox(this, "");
        this.adminSearchTextBox.setSize(UIComponent.INHERITED, this.locationSelect.getHeight());
        this.adminSearchTextBox.setPosition(0, BasicScreen.getPaddedY(this.locationSelect, 2));
        this.adminSearchTextBox.register(this);
        // Item list
        this.adminItemList = new BasicList<>(this, UIComponent.INHERITED, BasicScreen.getPaddedHeight(form) - this.locationSelect.getHeight() - this.adminSearchTextBox.getHeight() - 28);
        this.adminItemList.setItemComponentFactory(AdminItemComponent::new);
        this.adminItemList.setItemComponentSpacing(1);
        this.adminItemList.setPosition(0, BasicScreen.getPaddedY(this.adminSearchTextBox, 2));
        this.adminItemList.setPadding(2);
        this.adminItemList.setBorder(FontColors.WHITE, 1, 185);
        this.adminItemList.setSelectConsumer(i -> this.updateAdminControls());
        this.buttonAdminList = new UIButtonBuilder(this).width(50).anchor(Anchor.BOTTOM | Anchor.LEFT).enabled(false).onClick(this::listOrModify).text(I18n.format("almura.feature.common.button.list")).build("button.list");
        this.buttonAdminUnlist = new UIButtonBuilder(this).width(50).anchor(Anchor.BOTTOM | Anchor.LEFT).position(BasicScreen.getPaddedX(this.buttonAdminList, 2), 0).enabled(false).onClick(this::unlist).text(I18n.format("almura.feature.common.button.unlist")).build("button.unlist");
        this.adminListTotalLabel = new UILabel(this, TextFormatting.WHITE + I18n.format("almura.feature.common.text.total") + ": ");
        this.adminListTotalLabel.setPosition(0, -2, Anchor.BOTTOM | Anchor.RIGHT);
        adminContainer.add(this.typeLabel, this.locationSelect, this.adminSearchTextBox, this.adminItemList, this.buttonAdminList, this.buttonAdminUnlist, this.adminListTotalLabel);
        form.add(adminContainer);
        // Select the first item
        this.locationSelect.selectFirst();
    }
    addToScreen(form);
}
Also used : UILabel(net.malisis.core.client.gui.component.decoration.UILabel) Arrays(java.util.Arrays) UIComponent(net.malisis.core.client.gui.component.UIComponent) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) 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) Map(java.util.Map) BasicList(net.malisis.core.client.gui.component.container.BasicList) BasicContainer(net.malisis.core.client.gui.component.container.BasicContainer) NonNullList(net.minecraft.util.NonNullList) FontColors(net.malisis.core.util.FontColors) FeatureConstants(com.almuradev.almura.shared.feature.FeatureConstants) StoreModule(com.almuradev.almura.feature.store.StoreModule) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) ClientStoreManager(com.almuradev.almura.feature.store.client.ClientStoreManager) UITooltip(net.malisis.core.client.gui.component.decoration.UITooltip) Collectors(java.util.stream.Collectors) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) UIImage(net.malisis.core.client.gui.component.decoration.UIImage) Optional(java.util.Optional) SortType(com.almuradev.almura.feature.SortType) BasicLine(net.malisis.core.client.gui.component.decoration.BasicLine) HashMap(java.util.HashMap) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) FeatureSortTypes(com.almuradev.almura.FeatureSortTypes) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) Anchor(net.malisis.core.client.gui.Anchor) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) Subscribe(com.google.common.eventbus.Subscribe) BasicScreen(net.malisis.core.client.gui.BasicScreen) UIExpandingLabel(com.almuradev.almura.shared.client.ui.component.UIExpandingLabel) CreativeTabs(net.minecraft.creativetab.CreativeTabs) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) UISelect(net.malisis.core.client.gui.component.interaction.UISelect) ComponentEvent(net.malisis.core.client.gui.event.ComponentEvent) Nullable(javax.annotation.Nullable) UIButton(net.malisis.core.client.gui.component.interaction.UIButton) UITextField(net.malisis.core.client.gui.component.interaction.UITextField) UILabel(net.malisis.core.client.gui.component.decoration.UILabel) TextFormatting(net.minecraft.util.text.TextFormatting) Store(com.almuradev.almura.feature.store.Store) SideType(com.almuradev.almura.feature.store.SideType) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) FontRenderer(net.minecraft.client.gui.FontRenderer) ForgeRegistries(net.minecraftforge.fml.common.registry.ForgeRegistries) Comparator(java.util.Comparator) FontOptions(net.malisis.core.renderer.font.FontOptions) BasicLine(net.malisis.core.client.gui.component.decoration.BasicLine) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) UIButton(net.malisis.core.client.gui.component.interaction.UIButton) BasicContainer(net.malisis.core.client.gui.component.container.BasicContainer) ItemStack(net.minecraft.item.ItemStack) BasicTextBox(net.malisis.core.client.gui.component.interaction.BasicTextBox)

Example 5 with StoreItem

use of com.almuradev.almura.feature.store.listing.StoreItem in project Almura by AlmuraDev.

the class ClientboundListItemsResponsePacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    this.id = buf.readString();
    this.type = StoreItemSegmentType.valueOf(buf.readString());
    final int count = buf.readInteger();
    if (count > 0) {
        if (type == StoreItemSegmentType.SELLING) {
            this.storeItems = new ArrayList<SellingItem>();
        } else {
            this.storeItems = new ArrayList<BuyingItem>();
        }
        for (int i = 0; i < count; i++) {
            final int record = buf.readInteger();
            final Instant created;
            try {
                created = SerializationUtil.bytesToObject(buf.readBytes(buf.readInteger()));
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                continue;
            }
            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 item! . Skipping...").printStackTrace();
                continue;
            }
            final int quantity = buf.readInteger();
            final int metadata = buf.readInteger();
            final BigDecimal price = ByteBufUtil.readBigDecimal((ByteBuf) buf);
            final int index = buf.readInteger();
            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 Constructor<? extends AbstractStoreItem> constructor;
            try {
                constructor = this.type.getItemClass().getConstructor(Integer.TYPE, Instant.class, Item.class, Integer.TYPE, Integer.TYPE, BigDecimal.class, Integer.TYPE, NBTTagCompound.class);
                final AbstractStoreItem storeItem = constructor.newInstance(record, created, item, quantity, metadata, price, index, compound);
                if (this.type == StoreItemSegmentType.SELLING) {
                    ((List<SellingItem>) this.storeItems).add((SellingItem) storeItem);
                } else {
                    ((List<BuyingItem>) this.storeItems).add((BuyingItem) storeItem);
                }
            } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : AbstractStoreItem(com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem) Instant(java.time.Instant) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) BasicSellingItem(com.almuradev.almura.feature.store.basic.listing.BasicSellingItem) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) BigDecimal(java.math.BigDecimal) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractStoreItem(com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem) Item(net.minecraft.item.Item) BasicSellingItem(com.almuradev.almura.feature.store.basic.listing.BasicSellingItem) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) ResourceLocation(net.minecraft.util.ResourceLocation) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

StoreItem (com.almuradev.almura.feature.store.listing.StoreItem)5 BuyingItem (com.almuradev.almura.feature.store.listing.BuyingItem)3 SellingItem (com.almuradev.almura.feature.store.listing.SellingItem)3 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SideType (com.almuradev.almura.feature.store.SideType)2 Store (com.almuradev.almura.feature.store.Store)2 AbstractStoreItem (com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem)2 FeatureConstants (com.almuradev.almura.shared.feature.FeatureConstants)2 Comparator (java.util.Comparator)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 Minecraft (net.minecraft.client.Minecraft)2 Almura (com.almuradev.almura.Almura)1 FeatureSortTypes (com.almuradev.almura.FeatureSortTypes)1 SortType (com.almuradev.almura.feature.SortType)1 StoreItemSegmentType (com.almuradev.almura.feature.store.StoreItemSegmentType)1 StoreModifyType (com.almuradev.almura.feature.store.StoreModifyType)1 StoreModule (com.almuradev.almura.feature.store.StoreModule)1