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