From d4e5d4e49c793a62ea26c3aea4443434ba6364a4 Mon Sep 17 00:00:00 2001 From: Goldenlion5648 Date: Sat, 12 Oct 2024 10:17:57 -0400 Subject: [PATCH 1/2] add control to force potentially existing bookmark to top of list --- .../jei/common/input/IInternalKeyMappings.java | 1 + .../main/resources/assets/jei/lang/en_us.json | 1 + .../mezz/jei/gui/bookmarks/BookmarkList.java | 17 ++++++++++------- .../jei/gui/config/InternalKeyMappings.java | 12 ++++++++++++ .../input/handlers/BookmarkInputHandler.java | 11 +++++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Common/src/main/java/mezz/jei/common/input/IInternalKeyMappings.java b/Common/src/main/java/mezz/jei/common/input/IInternalKeyMappings.java index aa3c55778..303312a0d 100644 --- a/Common/src/main/java/mezz/jei/common/input/IInternalKeyMappings.java +++ b/Common/src/main/java/mezz/jei/common/input/IInternalKeyMappings.java @@ -23,6 +23,7 @@ public interface IInternalKeyMappings extends IJeiKeyMappings { IJeiKeyMapping getCloseRecipeGui(); IJeiKeyMapping getBookmark(); + IJeiKeyMapping getForceBookmark(); IJeiKeyMapping getToggleBookmarkOverlay(); @Override diff --git a/Common/src/main/resources/assets/jei/lang/en_us.json b/Common/src/main/resources/assets/jei/lang/en_us.json index a67ae678f..b603243ff 100644 --- a/Common/src/main/resources/assets/jei/lang/en_us.json +++ b/Common/src/main/resources/assets/jei/lang/en_us.json @@ -84,6 +84,7 @@ "jei.key.category.mouse.hover": "JEI (Hovering With Mouse)", "key.jei.bookmark": "Add/Remove Bookmark", + "key.jei.forceBookmark": "Replace Bookmark", "key.jei.showRecipe": "Show Recipe", "key.jei.showRecipe2": "Show Recipe", "key.jei.showUses": "Show Uses", diff --git a/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java b/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java index f5a63f945..dbd3e5dea 100644 --- a/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java +++ b/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java @@ -54,8 +54,8 @@ public BookmarkList( this.codecHelper = codecHelper; } - public boolean add(IBookmark value) { - if (!addToListWithoutNotifying(value, clientConfig.isAddingBookmarksToFrontEnabled())) { + public boolean add(IBookmark value, boolean shouldForce) { + if (!addToListWithoutNotifying(value, clientConfig.isAddingBookmarksToFrontEnabled(), shouldForce)) { return false; } notifyListenersOfChange(); @@ -90,13 +90,13 @@ public boolean contains(IBookmark value) { return this.bookmarksSet.contains(value); } - public boolean onElementBookmarked(IElement element) { + public boolean onElementBookmarked(IElement element, boolean shouldForce) { return element.getBookmark() .map(this::remove) .orElseGet(() -> { ITypedIngredient ingredient = element.getTypedIngredient(); IBookmark bookmark = IngredientBookmark.create(ingredient, ingredientManager); - return add(bookmark); + return add(bookmark, shouldForce); }); } @@ -104,7 +104,7 @@ public void toggleBookmark(IBookmark bookmark) { if (remove(bookmark)) { return; } - add(bookmark); + add(bookmark, false); } public boolean remove(IBookmark ingredient) { @@ -131,9 +131,12 @@ public void setFromConfigFile(List bookmarks) { notifyListenersOfChange(); } - private boolean addToListWithoutNotifying(IBookmark value, boolean addToFront) { + private boolean addToListWithoutNotifying(IBookmark value, boolean addToFront, boolean shouldForce) { if (contains(value)) { - return false; + if(!shouldForce) { + return false; + } + remove(value); } if (addToFront) { bookmarksList.addFirst(value); diff --git a/Gui/src/main/java/mezz/jei/gui/config/InternalKeyMappings.java b/Gui/src/main/java/mezz/jei/gui/config/InternalKeyMappings.java index 712baefd9..3d6e59907 100644 --- a/Gui/src/main/java/mezz/jei/gui/config/InternalKeyMappings.java +++ b/Gui/src/main/java/mezz/jei/gui/config/InternalKeyMappings.java @@ -32,6 +32,7 @@ public final class InternalKeyMappings implements IInternalKeyMappings { private final IJeiKeyMapping nextPage; private final IJeiKeyMapping bookmark; + private final IJeiKeyMapping forceBookmark; private final IJeiKeyMapping toggleBookmarkOverlay; private final IJeiKeyMapping transferRecipeBookmark; private final IJeiKeyMapping maxTransferRecipeBookmark; @@ -128,6 +129,12 @@ public InternalKeyMappings(Consumer registerMethod) { .setContext(JeiKeyConflictContext.JEI_GUI_HOVER) .buildKeyboardKey(GLFW.GLFW_KEY_A) .register(registerMethod); + // Mouse Hover + forceBookmark = mouseHover.createMapping("key.jei.forceBookmark") + .setContext(JeiKeyConflictContext.JEI_GUI_HOVER) + .setModifier(JeiKeyModifier.SHIFT) + .buildKeyboardKey(GLFW.GLFW_KEY_A) + .register(registerMethod); showRecipe1 = mouseHover.createMapping("key.jei.showRecipe") .setContext(JeiKeyConflictContext.JEI_GUI_HOVER) @@ -369,6 +376,11 @@ public IJeiKeyMapping getBookmark() { return bookmark; } + @Override + public IJeiKeyMapping getForceBookmark() { + return forceBookmark; + } + @Override public IJeiKeyMapping getToggleBookmarkOverlay() { return toggleBookmarkOverlay; diff --git a/Gui/src/main/java/mezz/jei/gui/input/handlers/BookmarkInputHandler.java b/Gui/src/main/java/mezz/jei/gui/input/handlers/BookmarkInputHandler.java index 4ece53bdb..d70dcfc1d 100644 --- a/Gui/src/main/java/mezz/jei/gui/input/handlers/BookmarkInputHandler.java +++ b/Gui/src/main/java/mezz/jei/gui/input/handlers/BookmarkInputHandler.java @@ -20,18 +20,21 @@ public BookmarkInputHandler(CombinedRecipeFocusSource focusSource, BookmarkList @Override public Optional handleUserInput(Screen screen, UserInput input, IInternalKeyMappings keyBindings) { - if (input.is(keyBindings.getBookmark())) { - return handleBookmark(input, keyBindings); + if (input.is(keyBindings.getForceBookmark())) { + return handleBookmark(input, keyBindings, true); + } + else if (input.is(keyBindings.getBookmark())) { + return handleBookmark(input, keyBindings, false); } return Optional.empty(); } - private Optional handleBookmark(UserInput input, IInternalKeyMappings keyBindings) { + private Optional handleBookmark(UserInput input, IInternalKeyMappings keyBindings, boolean shouldForce) { return focusSource.getIngredientUnderMouse(input, keyBindings) .findFirst() .flatMap(clicked -> { if (input.isSimulate() || - bookmarkList.onElementBookmarked(clicked.getElement()) + bookmarkList.onElementBookmarked(clicked.getElement(), shouldForce) ) { IUserInputHandler handler = new SameElementInputHandler(this, clicked::isMouseOver); return Optional.of(handler); From 2d1f4f3ba4028acd6dc8390c3e0bda176b9fb369 Mon Sep 17 00:00:00 2001 From: Goldenlion5648 Date: Sat, 12 Oct 2024 13:49:14 -0400 Subject: [PATCH 2/2] make new keybind work when hovering items on bookmarked side --- .../mezz/jei/gui/bookmarks/BookmarkList.java | 25 +++++++++++-------- .../jei/gui/recipes/RecipeBookmarkButton.java | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java b/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java index dbd3e5dea..5a7f02499 100644 --- a/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java +++ b/Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java @@ -90,19 +90,24 @@ public boolean contains(IBookmark value) { return this.bookmarksSet.contains(value); } - public boolean onElementBookmarked(IElement element, boolean shouldForce) { - return element.getBookmark() - .map(this::remove) - .orElseGet(() -> { - ITypedIngredient ingredient = element.getTypedIngredient(); - IBookmark bookmark = IngredientBookmark.create(ingredient, ingredientManager); - return add(bookmark, shouldForce); - }); + public boolean onElementBookmarked(IElement element, boolean shouldForceAdd) { + boolean didExist = element.getBookmark() + .map(this::remove) + .orElse(false); + + if(shouldForceAdd || !didExist) { + ITypedIngredient ingredient = element.getTypedIngredient(); + IBookmark bookmark = IngredientBookmark.create(ingredient, ingredientManager); + return add(bookmark, shouldForceAdd); + } + return true; } - public void toggleBookmark(IBookmark bookmark) { + public void toggleBookmark(IBookmark bookmark, boolean shouldAddBack) { if (remove(bookmark)) { - return; + if(!shouldAddBack) { + return; + } } add(bookmark, false); } diff --git a/Gui/src/main/java/mezz/jei/gui/recipes/RecipeBookmarkButton.java b/Gui/src/main/java/mezz/jei/gui/recipes/RecipeBookmarkButton.java index a66da24c4..23bdd0967 100644 --- a/Gui/src/main/java/mezz/jei/gui/recipes/RecipeBookmarkButton.java +++ b/Gui/src/main/java/mezz/jei/gui/recipes/RecipeBookmarkButton.java @@ -87,7 +87,7 @@ protected boolean isIconToggledOn() { protected boolean onMouseClicked(UserInput input) { if (recipeBookmark != null) { if (!input.isSimulate()) { - bookmarks.toggleBookmark(recipeBookmark); + bookmarks.toggleBookmark(recipeBookmark, false); } return true; }