Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add control to force potentially existing bookmark to top of list #3808

Open
wants to merge 2 commits into
base: 1.21.x
Choose a base branch
from
Open
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 @@ -23,6 +23,7 @@ public interface IInternalKeyMappings extends IJeiKeyMappings {
IJeiKeyMapping getCloseRecipeGui();

IJeiKeyMapping getBookmark();
IJeiKeyMapping getForceBookmark();
IJeiKeyMapping getToggleBookmarkOverlay();

@Override
Expand Down
1 change: 1 addition & 0 deletions Common/src/main/resources/assets/jei/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 23 additions & 15 deletions Gui/src/main/java/mezz/jei/gui/bookmarks/BookmarkList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -90,21 +90,26 @@ public boolean contains(IBookmark value) {
return this.bookmarksSet.contains(value);
}

public <T> boolean onElementBookmarked(IElement<T> element) {
return element.getBookmark()
.map(this::remove)
.orElseGet(() -> {
ITypedIngredient<T> ingredient = element.getTypedIngredient();
IBookmark bookmark = IngredientBookmark.create(ingredient, ingredientManager);
return add(bookmark);
});
public <T> boolean onElementBookmarked(IElement<T> element, boolean shouldForceAdd) {
boolean didExist = element.getBookmark()
.map(this::remove)
.orElse(false);

if(shouldForceAdd || !didExist) {
ITypedIngredient<T> 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);
add(bookmark, false);
}

public boolean remove(IBookmark ingredient) {
Expand All @@ -131,9 +136,12 @@ public void setFromConfigFile(List<IBookmark> 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);
Expand Down
12 changes: 12 additions & 0 deletions Gui/src/main/java/mezz/jei/gui/config/InternalKeyMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,6 +129,12 @@ public InternalKeyMappings(Consumer<KeyMapping> 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)
Expand Down Expand Up @@ -369,6 +376,11 @@ public IJeiKeyMapping getBookmark() {
return bookmark;
}

@Override
public IJeiKeyMapping getForceBookmark() {
return forceBookmark;
}

@Override
public IJeiKeyMapping getToggleBookmarkOverlay() {
return toggleBookmarkOverlay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ public BookmarkInputHandler(CombinedRecipeFocusSource focusSource, BookmarkList

@Override
public Optional<IUserInputHandler> 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<IUserInputHandler> handleBookmark(UserInput input, IInternalKeyMappings keyBindings) {
private Optional<IUserInputHandler> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down