|
| 1 | +package net.blay09.mods.clienttweaks.tweak; |
| 2 | + |
| 3 | +import com.mojang.blaze3d.platform.InputConstants; |
| 4 | +import net.blay09.mods.balm.Balm; |
| 5 | +import net.blay09.mods.balm.client.platform.event.callback.ScreenCallback; |
| 6 | +import net.blay09.mods.balm.mixin.AbstractContainerScreenAccessor; |
| 7 | +import net.blay09.mods.clienttweaks.ClientTweaksConfig; |
| 8 | +import net.blay09.mods.clienttweaks.ClientTweaksConfigData; |
| 9 | +import net.blay09.mods.clienttweaks.mixin.AbstractRecipeBookScreenAccessor; |
| 10 | +import net.blay09.mods.clienttweaks.mixin.GhostSlotAccessor; |
| 11 | +import net.blay09.mods.clienttweaks.mixin.GhostSlotsAccessor; |
| 12 | +import net.blay09.mods.clienttweaks.mixin.RecipeBookComponentAccessor; |
| 13 | +import net.minecraft.client.Minecraft; |
| 14 | +import net.minecraft.client.gui.screens.Screen; |
| 15 | +import net.minecraft.client.gui.screens.inventory.AbstractRecipeBookScreen; |
| 16 | +import net.minecraft.client.gui.screens.recipebook.RecipeCollection; |
| 17 | +import net.minecraft.client.input.KeyEvent; |
| 18 | +import net.minecraft.client.input.MouseButtonEvent; |
| 19 | +import net.minecraft.world.item.ItemStack; |
| 20 | +import net.minecraft.world.item.crafting.display.RecipeDisplayId; |
| 21 | +import net.minecraft.world.item.crafting.display.SlotDisplayContext; |
| 22 | +import org.jspecify.annotations.Nullable; |
| 23 | + |
| 24 | +import java.util.ArrayDeque; |
| 25 | +import java.util.Deque; |
| 26 | + |
| 27 | +public class NavigateToGhostIngredients extends AbstractClientTweak { |
| 28 | + |
| 29 | + private final Deque<HistoryEntry> history = new ArrayDeque<>(); |
| 30 | + @Nullable |
| 31 | + private HistoryEntry currentEntry; |
| 32 | + |
| 33 | + public NavigateToGhostIngredients() { |
| 34 | + super("click_ghost_ingredients_in_recipe_book"); |
| 35 | + |
| 36 | + ScreenCallback.Opening.EVENT.register(this::onScreenOpening); |
| 37 | + ScreenCallback.MousePress.Before.EVENT.register(this::onMousePress); |
| 38 | + ScreenCallback.KeyPress.Before.EVENT.register(this::onKeyPress); |
| 39 | + } |
| 40 | + |
| 41 | + private Screen onScreenOpening(Screen screen) { |
| 42 | + history.clear(); |
| 43 | + return screen; |
| 44 | + } |
| 45 | + |
| 46 | + private boolean onMousePress(Screen screen, MouseButtonEvent event) { |
| 47 | + final var level = Minecraft.getInstance().level; |
| 48 | + if (!isEnabled() || event.button() != 0 || !(screen instanceof AbstractRecipeBookScreen) || level == null) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + final var recipeBookComponent = ((AbstractRecipeBookScreenAccessor) screen).getRecipeBookComponent(); |
| 53 | + if (!recipeBookComponent.isVisible()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + final var ghostSlots = (GhostSlotsAccessor) ((RecipeBookComponentAccessor) recipeBookComponent).getGhostSlots(); |
| 58 | + final var hoveredSlot = ((AbstractContainerScreenAccessor) screen).getHoveredSlot(); |
| 59 | + final var ghostSlot = (GhostSlotAccessor) ghostSlots.getIngredients().get(hoveredSlot); |
| 60 | + if (ghostSlot == null || ghostSlot.getIsResultSlot()) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + final var currentIndex = ghostSlots.getSlotSelectTime().currentIndex(); |
| 64 | + final var ghostItem = ghostSlot.callGetItem(currentIndex); |
| 65 | + |
| 66 | + var recipeBook = ((RecipeBookComponentAccessor) recipeBookComponent).getBook(); |
| 67 | + var context = SlotDisplayContext.fromLevel(level); |
| 68 | + |
| 69 | + for (final var collection : recipeBook.getCollections()) { |
| 70 | + for (final var entry : collection.getRecipes()) { |
| 71 | + for (final var result : entry.resultItems(context)) { |
| 72 | + if (ItemStack.isSameItemSameComponents(result, ghostItem)) { |
| 73 | + if (currentEntry == null) { |
| 74 | + final var recipeBookPage = ((RecipeBookComponentAccessor) recipeBookComponent).getRecipeBookPage(); |
| 75 | + final var initialCollection = recipeBookPage.getLastClickedRecipeCollection(); |
| 76 | + final var initialRecipeId = recipeBookPage.getLastClickedRecipe(); |
| 77 | + currentEntry = new HistoryEntry(initialCollection, initialRecipeId); |
| 78 | + } |
| 79 | + history.add(currentEntry); |
| 80 | + currentEntry = new HistoryEntry(collection, entry.id()); |
| 81 | + ((RecipeBookComponentAccessor) recipeBookComponent).callTryPlaceRecipe(collection, entry.id(), event.hasShiftDown()); |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean onKeyPress(Screen screen, KeyEvent event) { |
| 92 | + if (!isEnabled() || event.key() != InputConstants.KEY_BACKSPACE || !(screen instanceof AbstractRecipeBookScreen)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + final var recipeBookComponent = ((AbstractRecipeBookScreenAccessor) screen).getRecipeBookComponent(); |
| 97 | + if (!recipeBookComponent.isVisible()) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + final var searchBox = ((RecipeBookComponentAccessor) recipeBookComponent).getSearchBox(); |
| 102 | + if (searchBox != null && searchBox.isFocused()) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (history.isEmpty()) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + currentEntry = history.pop(); |
| 111 | + ((RecipeBookComponentAccessor) recipeBookComponent).callTryPlaceRecipe(currentEntry.collection(), currentEntry.recipeDisplayId(), false); |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean isEnabled() { |
| 117 | + return ClientTweaksConfig.getActive().tweaks.navigateToGhostIngredients; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void setEnabled(boolean enabled) { |
| 122 | + Balm.config().updateLocalConfig(ClientTweaksConfigData.class, it -> it.tweaks.navigateToGhostIngredients = enabled); |
| 123 | + } |
| 124 | + |
| 125 | + private record HistoryEntry(RecipeCollection collection, RecipeDisplayId recipeDisplayId) { |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments