Skip to content

Fix storage sort persistence and add configurable format for sort button#28

Merged
ptthanh02 merged 3 commits intomainfrom
copilot/fix-0b2b17e0-05b9-47c3-a2f1-8e0afbdc5e40
Oct 1, 2025
Merged

Fix storage sort persistence and add configurable format for sort button#28
ptthanh02 merged 3 commits intomainfrom
copilot/fix-0b2b17e0-05b9-47c3-a2f1-8e0afbdc5e40

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 1, 2025

Problem

The storage sort functionality had several UX issues:

  1. Sort lost on reopen: When closing and reopening the spawner storage, the selected sort option was reset to default alphabetical order, even though the preference was saved in SpawnerData
  2. Sort reset on actions: Any action in the storage (taking items, adding items, etc.) would cause the sort to revert to default display
  3. No default selection: When first opening storage, no sort option was selected, making the lore display less user-friendly
  4. Hardcoded formatting: Sort button item formatting was hardcoded instead of being configurable like the filter/sort buttons in the list GUI

Root Cause

The issue was that VirtualInventory.getDisplayInventory() would rebuild the display cache without considering the preferred sort material stored in SpawnerData. When the cache was invalidated (by actions or reopening), it defaulted to alphabetical sorting.

Solution

1. Store sort preference in VirtualInventory

Added a preferredSortMaterial field to VirtualInventory that persists the sort preference at runtime:

private org.bukkit.Material preferredSortMaterial;

When sortItems() is called, this preference is stored and automatically applied whenever the display cache needs to be rebuilt.

2. Initialize default sort on first open

Modified SpawnerStorageAction.openLootPage() to:

  • Check if preferredSortItem is null when opening storage
  • If null and loot items exist, automatically select the first item (alphabetically sorted) as default
  • Save the preference to SpawnerData
  • Apply the sort to VirtualInventory on every open

This ensures users always see a consistent sort from the first time they open storage.

3. Make item format fully configurable

Replaced hardcoded formatting in SpawnerStorageUI.createSortButton() with configuration-based format strings:

String selectedItemFormat = languageManager.getGuiItemName("sort_items_button.selected_item");
String unselectedItemFormat = languageManager.getGuiItemName("sort_items_button.unselected_item");
String noneText = languageManager.getGuiItemName("sort_items_button.none_text");

Added these configuration keys to gui.yml (en_US and vi_VN):

sort_items_button:
  selected_item: '&#00F986• %item_name%'      # Format for currently selected sort item
  unselected_item: '&#f8f8ff• %item_name%'    # Format for unselected items
  none_text: '&#bdc3c7• None'                 # Text when no items available

The %item_name% placeholder gets replaced with the actual item name, allowing complete customization of colors, bullet styles, prefixes, suffixes, and overall layout.

Result

  • ✅ Sort preference now persists across storage reopens
  • ✅ Sort is maintained after any action (take items, add items, etc.)
  • ✅ First loot item is automatically selected as default on first open
  • ✅ Sort button item format is now fully configurable via gui.yml, consistent with filter/sort buttons in list GUI
  • ✅ Better UX with always-visible sort selection
  • ✅ Users can customize not just colors but entire format including bullet style, text, and layout

Technical Details

The fix works by storing the sort preference in two places:

  • SpawnerData: Persisted to disk for long-term storage
  • VirtualInventory: Used at runtime when rebuilding the display cache

When VirtualInventory.getDisplayInventory() rebuilds the cache (which happens after any inventory modification), it now checks preferredSortMaterial and applies the appropriate sorting logic automatically.

Backward Compatibility

All changes are backward compatible:

  • Existing spawners without a sort preference will automatically get the first item as default
  • No database migration required
  • Default format values match the previous hardcoded format
Original prompt

at the function handleSortItemsClick(), when reopen the storage the sort display is gone, this also apply to when user use the action in the SpawnerStorage the short will return to the default display, you need to make it consistent maybe modify the VirtualInventory. And at the start when first open SpawnerStorage no sort option item was selected, can you add default item selected is the first item? so the lore display can be more UX/UI friendly. And also can SpawnerStorageUI createSortButton() can you make it that it can be config able color for these line?
String color = currentSort == lootItem.getMaterial() ? "&#00F986" : "&#f8f8ff";
availableItems.append("&#bdc3c7• None");

so it not hard coded there is example for this in filter and sort button in the list gui command

dont write javadocs


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add preferredSortMaterial field to VirtualInventory to persist sort preference
- Update getDisplayInventory() to apply preferred sort when rebuilding cache
- Initialize default sort (first item) when opening storage for the first time
- Apply sort to VirtualInventory when storage is opened with existing preference
- Make sort button colors configurable via gui.yml (selected_color, unselected_color, none_color)
- Update language files (en_US and vi_VN) with color configuration

Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
Copilot AI changed the title at the function handleSortItemsClick(), when reopen the storage the sort display is gone, this also apply to when user use the action in the SpawnerStorage the short will return to the default display, you need to make it consistent maybe modify the V... Fix storage sort persistence and add configurable colors for sort button Oct 1, 2025
Copilot AI requested a review from ptthanh02 October 1, 2025 17:11
@ptthanh02
Copy link
Copy Markdown
Contributor

@copilot
ok but I don't want to change only the color but the entire text itselft for example:

sort_items_button:
selected_item: '• &#00F986%item_name%' # Currently selected sort item
unselected_item: '• &#f8f8ff%item_name%' # Unselected items
none_color: '&#bdc3c7• None # "None" text when no items

Changed from color-only configuration to full format strings with placeholders:
- selected_item: Full format string with %item_name% placeholder for selected items
- unselected_item: Full format string with %item_name% placeholder for unselected items
- none_text: Full format string for when no items are available

This allows users to customize not just colors but also the bullet style, text, and layout of sort items in the button lore.

Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 1, 2025

@copilot
ok but I don't want to change only the color but the entire text itselft for example:

...

Done! Changed the configuration from just colors to full format strings with placeholders. You can now customize the entire format:

sort_items_button:
  selected_item: '&#00F986• %item_name%'
  unselected_item: '&#f8f8ff• %item_name%'
  none_text: '&#bdc3c7• None'

The %item_name% placeholder gets replaced with the actual item name. You can change the bullet style, add prefixes/suffixes, or completely customize the format. Commit: ddfaec5

Copilot AI changed the title Fix storage sort persistence and add configurable colors for sort button Fix storage sort persistence and add configurable format for sort button Oct 1, 2025
@ptthanh02 ptthanh02 marked this pull request as ready for review October 1, 2025 17:23
@ptthanh02 ptthanh02 merged commit 3fac905 into main Oct 1, 2025
2 checks passed
@ptthanh02 ptthanh02 deleted the copilot/fix-0b2b17e0-05b9-47c3-a2f1-8e0afbdc5e40 branch October 1, 2025 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants