Skip to content

Commit

Permalink
Pull all Bookmarked Items: use groups
Browse files Browse the repository at this point in the history
  • Loading branch information
slprime committed Jan 13, 2024
1 parent 0a24334 commit c5c798c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
63 changes: 58 additions & 5 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,15 @@ public void draw(int mousex, int mousey) {
}

if (NEIClientUtils.shiftKey() && !LayoutManager.bookmarkPanel.inEditingState()) {
final ItemPanelSlot focused = getSlotMouseOver(mousex, mousey);
this.focusedGroupId = focused != null ? this.metadata.get(focused.slotIndex).groupId : -1;
final int rowId = getHoveredRowIndex(true);

if (rowId != -1) {
this.focusedGroupId = getRowGroupId(rowId);
} else {
final ItemPanelSlot focused = getSlotMouseOver(mousex, mousey);
this.focusedGroupId = focused != null ? this.metadata.get(focused.slotIndex).groupId : -1;
}

} else {
this.focusedGroupId = -1;
}
Expand Down Expand Up @@ -1138,7 +1145,7 @@ public boolean onButtonPress(boolean rightclick) {
if (rightclick) {
return false;
}
return pullBookmarkItems();
return pullBookmarkItems(-1, false);
}

@Override
Expand Down Expand Up @@ -1278,6 +1285,12 @@ public BookmarkRecipeId getBookmarkRecipeId(ItemStack stackA) {
return null;
}

protected int getHoveredGroupId() {
final BookmarkGrid BGrid = (BookmarkGrid) grid;
final int overRowIndex = BGrid.getHoveredRowIndex(true);
return overRowIndex >= 0 ? BGrid.getRowGroupId(overRowIndex) : -1;
}

protected String getNamespaceLabelText(boolean shortFormat) {
String activePage = String.valueOf(activeNamespaceIndex + 1);

Expand Down Expand Up @@ -1941,6 +1954,14 @@ public List<String> handleTooltip(int mx, int my, List<String> tooltip) {
@Override
public boolean handleKeyPress(int keyID, char keyChar) {

if (NEIClientConfig.isKeyHashDown("gui.bookmark_pull_items")) {
return this.pullBookmarkItems(this.getHoveredGroupId(), false);
}

if (NEIClientConfig.isKeyHashDown("gui.bookmark_pull_items_ingredients")) {
return this.pullBookmarkItems(this.getHoveredGroupId(), true);
}

if (NEIClientConfig.isKeyHashDown("gui.bookmark_recipe")) {
final BookmarkGrid BGrid = (BookmarkGrid) grid;
final int overRowIndex = BGrid.getHoveredRowIndex(true);
Expand Down Expand Up @@ -2078,15 +2099,47 @@ private ItemStack shiftStackSize(BookmarkGrid BGrid, int slotIndex, int shift, i
return null;
}

public boolean pullBookmarkItems() {
public boolean pullBookmarkItems(int groupId, boolean onlyIngredients) {
IBookmarkContainerHandler containerHandler = BookmarkContainerInfo
.getBookmarkContainerHandler(getGuiContainer());

if (containerHandler == null) {
return false;
}

containerHandler.pullBookmarkItemsFromContainer(getGuiContainer(), ((BookmarkGrid) grid).realItems);
final BookmarkGrid BGrid = (BookmarkGrid) grid;
final ArrayList<ItemStack> items = new ArrayList<>();
final ItemStackMap<Integer> uniqueItems = new ItemStackMap<>();
final BookmarkGroup group = groupId >= 0 ? BGrid.groups.get(groupId) : null;
ItemStackMetadata meta;

System.out.println("onlyIngredients: " + onlyIngredients);

for (int idx = 0; idx < BGrid.realItems.size(); idx++) {
meta = BGrid.metadata.get(idx);

if (groupId == -1 || groupId == meta.groupId) {
final ItemStack stack = BGrid.getItem(idx);

if (!onlyIngredients || meta.ingredient && (group == null || group.crafting == null
|| group.crafting.inputs.containsKey(BGrid.realItems.get(idx)))) {
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(stack);
uniqueItems.put(stack, uniqueItems.getOrDefault(stack, 0) + nbTag.getInteger("Count"));
}

}
}

for (ItemStackMap.Entry<Integer> entry : uniqueItems.entries()) {
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(entry.key);
items.add(StackInfo.loadFromNBT(nbTag, entry.value));
}

if (items.isEmpty()) {
return false;
}

containerHandler.pullBookmarkItemsFromContainer(getGuiContainer(), items);
return true;
}
}
3 changes: 1 addition & 2 deletions src/main/java/codechicken/nei/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ public static void loadHiddenHandlers() {

try (FileReader reader = new FileReader(file)) {
NEIClientConfig.logger.info("Loading hidden handlers from file {}", file);
NEIClientConfig.hiddenHandlerRegex = IOUtils.readLines(reader).stream()
.filter((line) -> !line.startsWith("#")).map(Pattern::compile)
NEIClientConfig.hiddenHandlers = IOUtils.readLines(reader).stream().filter((line) -> !line.startsWith("#"))
.collect(Collectors.toCollection(HashSet::new));
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to load hidden handlers from file {}", file, e);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/codechicken/nei/ItemStackMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public T get(ItemStack key) {
return map == null ? null : map.get(key);
}

public T getOrDefault(ItemStack key, T defaultValue) {
T e = get(key);
return e == null ? defaultValue : e;
}

public void put(ItemStack key, T value) {
if (key == null || key.getItem() == null) return;

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/codechicken/nei/LayoutManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,6 @@ public boolean lastKeyTyped(GuiContainer gui, char keyChar, int keyID) {
return true;
}

if (NEIClientConfig.isKeyHashDown("gui.bookmark_pull_items")) {
return bookmarkPanel.pullBookmarkItems();
}

if (isEnabled() && !isHidden()) {
for (Widget widget : controlWidgets) if (inputFocused == null) widget.lastKeyTyped(keyID, keyChar);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ public class NEIClientConfig {
// We use regex here so that we can apply the height hack to entire mods with one entry.
public static HashSet<Pattern> heightHackHandlerRegex = new HashSet<>();

// Set of regexes matching handler Name of handlers that need hide.
// We use regex here so that we can hide to entire mods with one entry.
public static HashSet<Pattern> hiddenHandlerRegex = new HashSet<>();
// Set of handler Name or Id of handlers that need hide.
public static HashSet<String> hiddenHandlers = new HashSet<>();

// Map of handler ID to sort order.
// Handlers will be sorted in ascending order, so smaller numbers show up earlier.
Expand Down Expand Up @@ -362,6 +361,7 @@ private static void setDefaultKeyBindings() {
"gui.bookmark_recipe_count",
Keyboard.KEY_A + NEIClientUtils.SHIFT_HASH + NEIClientUtils.CTRL_HASH);
API.addHashBind("gui.bookmark_pull_items", Keyboard.KEY_V);
API.addHashBind("gui.bookmark_pull_items_ingredients", Keyboard.KEY_V + NEIClientUtils.SHIFT_HASH);
API.addHashBind("gui.overlay", Keyboard.KEY_S);
API.addHashBind("gui.overlay_use", Keyboard.KEY_S + NEIClientUtils.SHIFT_HASH);
API.addHashBind("gui.overlay_hide", Keyboard.KEY_S + NEIClientUtils.CTRL_HASH);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/codechicken/nei/recipe/RecipeHandlerQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import net.minecraft.client.Minecraft;
Expand All @@ -13,6 +12,8 @@
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;

import com.google.common.base.Objects;

import codechicken.core.TaskProfiler;
import codechicken.nei.ItemList;
import codechicken.nei.NEIClientConfig;
Expand Down Expand Up @@ -90,8 +91,8 @@ private ArrayList<T> getHandlersWithRecipes() throws InterruptedException, Execu
}

private boolean isHidden(T handler) {
return NEIClientConfig.hiddenHandlerRegex.stream()
.map(pattern -> pattern.matcher(RecipeCatalysts.getRecipeID(handler))).anyMatch(Matcher::matches);
final String handlerId = Objects.firstNonNull(handler.getOverlayIdentifier(), handler.getHandlerId());
return NEIClientConfig.hiddenHandlers.stream().anyMatch(h -> h.equals(handlerId));
}

private void printLog(Throwable t) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/nei/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ nei.options.keys.gui.bookmark=Add Bookmark
nei.options.keys.gui.bookmark_recipe=Add Bookmark with Recipe
nei.options.keys.gui.bookmark_count=Add Bookmark with Count
nei.options.keys.gui.bookmark_pull_items=Pull all Bookmarked Items
nei.options.keys.gui.bookmark_pull_items_ingredients=Pull all Bookmarked Ingredients
nei.options.keys.gui.bookmark_recipe_count=Add Bookmark with Recipe & Count
nei.options.keys.gui.hide_bookmarks=Toggle Bookmark Panel Visibility
nei.options.keys.gui.overlay=Overlay Recipe
Expand Down

0 comments on commit c5c798c

Please sign in to comment.