Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,35 @@ private void openLootPage(Player player, SpawnerData spawner, int page, boolean
return;
}

// Initialize sort preference on first open
Material currentSort = spawner.getPreferredSortItem();
if (currentSort == null && spawner.getLootConfig() != null && spawner.getLootConfig().getAllItems() != null) {
var lootItems = spawner.getLootConfig().getAllItems();
if (!lootItems.isEmpty()) {
var sortedLoot = lootItems.stream()
.map(LootItem::getMaterial)
.distinct()
.sorted(Comparator.comparing(Material::name))
.toList();

if (!sortedLoot.isEmpty()) {
Material firstItem = sortedLoot.getFirst();
spawner.setPreferredSortItem(firstItem);
currentSort = firstItem;

if (!spawner.isInteracted()) {
spawner.markInteracted();
}
spawnerManager.queueSpawnerForSaving(spawner.getSpawnerId());
}
}
}

// Apply sort to virtual inventory if a sort preference exists
if (currentSort != null) {
spawner.getVirtualInventory().sortItems(currentSort);
}

Inventory pageInventory = lootManager.createInventory(spawner, title, page, totalPages);

openStorageInventories.put(playerId, pageInventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ private ItemStack createSortButton(SpawnerData spawner, Material material) {
// Get current sort item
Material currentSort = spawner.getPreferredSortItem();

// Get format strings from configuration
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");

// Get available items from spawner drops
StringBuilder availableItems = new StringBuilder();
if (spawner.getLootConfig() != null && spawner.getLootConfig().getAllItems() != null) {
Expand All @@ -358,14 +363,17 @@ private ItemStack createSortButton(SpawnerData spawner, Material material) {
for (var lootItem : sortedLoot) {
if (!first) availableItems.append("\n");
String itemName = languageManager.getVanillaItemName(lootItem.getMaterial());
String color = currentSort == lootItem.getMaterial() ? "&#00F986" : "&#f8f8ff";
availableItems.append(color).append("• ").append(itemName);
String format = currentSort == lootItem.getMaterial() ? selectedItemFormat : unselectedItemFormat;

// Replace %item_name% placeholder in format string
String formattedItem = format.replace("%item_name%", itemName);
availableItems.append(formattedItem);
first = false;
}
}

if (availableItems.isEmpty()) {
availableItems.append("&#bdc3c7• None");
availableItems.append(noneText);
}

placeholders.put("available_items", availableItems.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class VirtualInventory {
private boolean metricsCacheDirty;
// Cache sorted entries to avoid resorting when display isn't changing
private List<Map.Entry<ItemSignature, Long>> sortedEntriesCache;
private org.bukkit.Material preferredSortMaterial;

// Add an LRU cache for expensive item operations
private static final int ITEM_CACHE_SIZE = 128;
Expand All @@ -39,6 +40,7 @@ public VirtualInventory(int maxSlots) {
this.usedSlotsCache = 0;
this.totalItemsCache = 0;
this.sortedEntriesCache = null;
this.preferredSortMaterial = null;
}

public static class ItemSignature {
Expand Down Expand Up @@ -219,8 +221,22 @@ public Map<Integer, ItemStack> getDisplayInventory() {
// Get and sort the items - only use cached sort result if available
if (sortedEntriesCache == null) {
sortedEntriesCache = new ArrayList<>(consolidatedItems.entrySet());
// Use optimized comparator based on cached material name
sortedEntriesCache.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));
// Apply preferred sort if set, otherwise sort alphabetically
if (preferredSortMaterial != null) {
sortedEntriesCache.sort((e1, e2) -> {
boolean e1Preferred = e1.getKey().getTemplate().getType() == preferredSortMaterial;
boolean e2Preferred = e2.getKey().getTemplate().getType() == preferredSortMaterial;

if (e1Preferred && !e2Preferred) return -1;
if (!e1Preferred && e2Preferred) return 1;

// Both preferred or both not preferred, sort by material name
return e1.getKey().getMaterialName().compareTo(e2.getKey().getMaterialName());
});
} else {
// Use optimized comparator based on cached material name
sortedEntriesCache.sort(Comparator.comparing(e -> e.getKey().getMaterialName()));
}
}

// Process items directly to the display inventory
Expand Down Expand Up @@ -311,6 +327,9 @@ public boolean isDirty() {
* @param preferredMaterial The material to sort first, or null for no preference
*/
public void sortItems(org.bukkit.Material preferredMaterial) {
// Store the preferred material for future cache rebuilds
this.preferredSortMaterial = preferredMaterial;

// Clear the sorted cache to force re-sorting with new preference
this.sortedEntriesCache = null;

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/resources/language/en_US/gui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ sort_items_button:
- '%available_items%'
- ''
- '&#00F986⊳ &#f8f8ffᴄʟɪᴄᴋ ᴛᴏ ꜱᴏʀᴛ'
selected_item: '&#00F986• %item_name%'
unselected_item: '&#f8f8ff• %item_name%'
none_text: '&#bdc3c7• None'

drop_page_button:
name: '&#607D8Bᴅʀᴏᴘ ᴀʟʟ ɪᴛᴇᴍꜱ'
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/resources/language/vi_VN/gui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ sort_items_button:
- '%available_items%'
- ''
- '&#ffd700⊳ &#f8f8ffɴʜấᴘ để ᴄʜuyểɴ đổɪ ᴄáᴄʜ sắᴘ xếᴘ'
selected_item: '&#ffd700• %item_name%'
unselected_item: '&#f8f8ff• %item_name%'
none_text: '&#bdc3c7• Không có'

drop_page_button:
name: '&#7b68eeᴛʜả đồ'
Expand Down