Skip to content

Commit

Permalink
feat: Backwards container searching and resume search after match (#2483
Browse files Browse the repository at this point in the history
)

* feat: Backwards container searching

* feat: Continue searching after match when pressing enter

* fix: Correct default direction backwards logic

* feat: Add loop option for personal storage containers

* fix: Set matchedItems to false before checking for matches

* feat: Remove looping, shift+enter to start search from page 1
  • Loading branch information
ShadowCat117 committed May 12, 2024
1 parent cc4d773 commit fbe04ae
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.wynntils.core.persisted.config.Config;
import com.wynntils.core.persisted.config.ConfigCategory;
import com.wynntils.core.text.StyledText;
import com.wynntils.mc.event.ContainerClickEvent;
import com.wynntils.mc.event.ContainerCloseEvent;
import com.wynntils.mc.event.ContainerSetContentEvent;
import com.wynntils.mc.event.ContainerSetSlotEvent;
Expand All @@ -34,6 +35,7 @@
import com.wynntils.models.containers.containers.personal.BookshelfContainer;
import com.wynntils.models.containers.containers.personal.CharacterBankContainer;
import com.wynntils.models.containers.containers.personal.MiscBucketContainer;
import com.wynntils.models.containers.containers.personal.PersonalStorageContainer;
import com.wynntils.models.containers.type.SearchableContainerProperty;
import com.wynntils.models.items.WynnItem;
import com.wynntils.models.items.WynnItemData;
Expand All @@ -43,13 +45,15 @@
import com.wynntils.services.itemfilter.type.ItemSearchQuery;
import com.wynntils.utils.colors.CommonColors;
import com.wynntils.utils.colors.CustomColor;
import com.wynntils.utils.mc.KeyboardUtils;
import com.wynntils.utils.mc.McUtils;
import com.wynntils.utils.render.RenderUtils;
import com.wynntils.utils.wynn.ContainerUtils;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.world.Container;
Expand Down Expand Up @@ -131,6 +135,8 @@ public class ContainerSearchFeature extends Feature {
private SearchWidget lastSearchWidget;
private SearchableContainerProperty currentContainer;
private boolean autoSearching = false;
private boolean matchedItems = false;
private int direction = 0;
private ItemSearchQuery lastSearchQuery;

@SubscribeEvent
Expand All @@ -145,6 +151,8 @@ public void onScreenInit(ScreenInitEvent event) {
currentContainer = getCurrentSearchableContainer();
if (currentContainer == null) return;

matchedItems = false;

addWidgets(((AbstractContainerScreen<ChestMenu>) screen), renderX, renderY);
}

Expand All @@ -165,7 +173,9 @@ public void onContainerSetContent(ContainerSetContentEvent.Post event) {
if (currentContainer == null) return;
forceUpdateSearch();

if (autoSearching && McUtils.mc().screen instanceof AbstractContainerScreen<?> abstractContainerScreen) {
if (!matchedItems
&& autoSearching
&& McUtils.mc().screen instanceof AbstractContainerScreen<?> abstractContainerScreen) {
tryAutoSearch(abstractContainerScreen);
}
}
Expand All @@ -176,35 +186,65 @@ public void onContainerSetSlot(ContainerSetSlotEvent.Pre event) {
forceUpdateSearch();
}

@SubscribeEvent
public void onSlotClicked(ContainerClickEvent e) {
autoSearching = false;
}

@SubscribeEvent
public void onContainerClose(ContainerCloseEvent.Post event) {
lastSearchWidget = null;
lastSearchQuery = null;
currentContainer = null;
autoSearching = false;
matchedItems = false;
direction = 0;
guildBankLastSearch = 0;
}

@SubscribeEvent
public void onInventoryKeyPress(InventoryKeyPressEvent event) {
if (event.getKeyCode() != GLFW.GLFW_KEY_ENTER) return;
if (lastSearchWidget == null
|| currentContainer == null
|| currentContainer.getNextItemSlot() == -1
|| !(McUtils.mc().screen instanceof AbstractContainerScreen<?> abstractContainerScreen)
|| !(abstractContainerScreen.getMenu() instanceof ChestMenu chestMenu)) return;
// Don't want to be able to search whilst the edit widget is open
if (event.getKeyCode() == GLFW.GLFW_KEY_ENTER && !Models.Bank.isEditingName()) {
if (lastSearchWidget == null
|| lastSearchWidget.getTextBoxInput().isEmpty()
|| currentContainer == null
|| !(McUtils.mc().screen instanceof AbstractContainerScreen<?> abstractContainerScreen)
|| !(abstractContainerScreen.getMenu() instanceof ChestMenu chestMenu)) return;

// Default to forwards
direction = 1;

StyledText nextItemName = StyledText.fromComponent(
chestMenu.getItems().get(currentContainer.getNextItemSlot()).getHoverName());

// If next page item isn't found, go backwards
if (!nextItemName.matches(currentContainer.getNextItemPattern())) {
direction = -1;
}

ScreenExtension screen = (ScreenExtension) abstractContainerScreen;
if (screen.getFocusedTextInput() != lastSearchWidget) return;
// Set direction based on hovered slot
if (abstractContainerScreen.hoveredSlot != null) {
if (abstractContainerScreen.hoveredSlot.index == currentContainer.getNextItemSlot()) {
direction = 1;
} else if (abstractContainerScreen.hoveredSlot.index == currentContainer.getPreviousItemSlot()) {
direction = -1;
}
}

autoSearching = true;
if (currentContainer.supportsAdvancedSearch()) {
matchItemsAdvanced(lastSearchQuery, chestMenu);
} else {
matchItemsBasic(lastSearchWidget.getTextBoxInput(), chestMenu);
}
autoSearching = true;

tryAutoSearch(abstractContainerScreen);
if (KeyboardUtils.isShiftDown() && currentContainer instanceof PersonalStorageContainer) {
ContainerUtils.clickOnSlot(
Models.Bank.QUICK_JUMP_FIRST_PAGE_SLOT,
abstractContainerScreen.getMenu().containerId,
GLFW.GLFW_MOUSE_BUTTON_LEFT,
abstractContainerScreen.getMenu().getItems());
return;
}

tryAutoSearch(abstractContainerScreen);
}
}

private void tryAutoSearch(AbstractContainerScreen<?> abstractContainerScreen) {
Expand All @@ -219,19 +259,21 @@ private void tryAutoSearch(AbstractContainerScreen<?> abstractContainerScreen) {
guildBankLastSearch = System.currentTimeMillis();
}

StyledText name = StyledText.fromComponent(abstractContainerScreen
.getMenu()
.getItems()
.get(currentContainer.getNextItemSlot())
.getHoverName());
int slot = direction == 1 ? currentContainer.getNextItemSlot() : currentContainer.getPreviousItemSlot();

StyledText name = StyledText.fromComponent(
abstractContainerScreen.getMenu().getItems().get(slot).getHoverName());

Pattern itemPattern =
direction == 1 ? currentContainer.getNextItemPattern() : currentContainer.getPreviousItemPattern();

if (!name.matches(currentContainer.getNextItemPattern())) {
if (!name.matches(itemPattern)) {
autoSearching = false;
return;
}

ContainerUtils.clickOnSlot(
currentContainer.getNextItemSlot(),
slot,
abstractContainerScreen.getMenu().containerId,
GLFW.GLFW_MOUSE_BUTTON_LEFT,
abstractContainerScreen.getMenu().getItems());
Expand Down Expand Up @@ -301,6 +343,8 @@ private void addWidgets(AbstractContainerScreen<ChestMenu> screen, int renderX,
}

private void matchItemsAdvanced(ItemSearchQuery searchQuery, ChestMenu chestMenu) {
matchedItems = false;

if (searchQuery == null) return;

Container container = chestMenu.getContainer();
Expand All @@ -316,14 +360,16 @@ private void matchItemsAdvanced(ItemSearchQuery searchQuery, ChestMenu chestMenu

wynnItemOpt.get().getData().store(WynnItemData.SEARCHED_KEY, filtered);
if (filtered) {
autoSearching = false;
matchedItems = true;
}
}
}

private void matchItemsBasic(String searchStr, ChestMenu chestMenu) {
String search = searchStr.toLowerCase(Locale.ROOT);

matchedItems = false;

Container container = chestMenu.getContainer();
for (int i = 0; i < container.getContainerSize(); i++) {
if (!currentContainer.getBounds().getSlots().contains(i)) continue;
Expand All @@ -340,7 +386,7 @@ private void matchItemsBasic(String searchStr, ChestMenu chestMenu) {

wynnItemOpt.get().getData().store(WynnItemData.SEARCHED_KEY, filtered);
if (filtered) {
autoSearching = false;
matchedItems = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class BankModel extends Model {
new Storage<>(new TreeMap<>());

public static final int LAST_BANK_PAGE_SLOT = 8;
public static final int QUICK_JUMP_FIRST_PAGE_SLOT = 7;

// Test in BankModel_PERSONAL_STORAGE_PATTERN
private static final Pattern PERSONAL_STORAGE_PATTERN =
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/assets/wynntils/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
"feature.wynntils.containerScroll.invertScroll.description": "Should the scrolling direction be inverted?",
"feature.wynntils.containerScroll.invertScroll.name": "Invert scroll",
"feature.wynntils.containerScroll.name": "Container Scroll",
"feature.wynntils.containerSearch.description": "Adds the ability to search containers.",
"feature.wynntils.containerSearch.description": "Adds the ability to search containers. Hover the previous item slot to search backwards. Shift+Enter to jump to page 1 and start search from there for supported containers",
"feature.wynntils.containerSearch.filterInBank.description": "Should there be a search widget in the bank?",
"feature.wynntils.containerSearch.filterInBank.name": "Search Bar in Bank",
"feature.wynntils.containerSearch.filterInBlockBank.description": "Should there be a search widget in the block bank in your housing plot?",
Expand Down

0 comments on commit fbe04ae

Please sign in to comment.