From c336477c82b0a9181f6449e88bf34ca4e940f096 Mon Sep 17 00:00:00 2001 From: Stephen Fasciani Date: Tue, 8 Sep 2026 04:52:06 -0400 Subject: [PATCH] Add hover capture: + Add to Crafting Goals. Show the button on hovered inventory/craft slots and craft-book results. Click calls Create Goal. Command add stays as a debug path. --- docs/03-Architecture/▶️ Create Goal.md | 4 +- docs/03-Architecture/📦 Packages.md | 2 +- .../satistaskory/SatistaskoryClient.java | 19 ++- .../infrastructure/client/CaptureHook.java | 149 ++++++++++++++++++ 4 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/sufficient/satistaskory/infrastructure/client/CaptureHook.java diff --git a/docs/03-Architecture/▶️ Create Goal.md b/docs/03-Architecture/▶️ Create Goal.md index 18829e3..fdf9474 100644 --- a/docs/03-Architecture/▶️ Create Goal.md +++ b/docs/03-Architecture/▶️ Create Goal.md @@ -60,7 +60,9 @@ No confirm on Clear. ## First wiring -The three capture hosts are the experience target. The first adapter that can call `CreateGoal(ItemId)` is enough to prove the use case. Overlay on the **right** of the play screen. +Capture: hover an item in inventory, the crafting bench craft pane, or the craft book, then `+ Add to Crafting Goals`. That calls this use case. + +`/satistaskory add` remains a debug caller. Overlay on the **right** of the play screen. Do not start EMI. Do not start a planner menu. diff --git a/docs/03-Architecture/📦 Packages.md b/docs/03-Architecture/📦 Packages.md index 54833b5..e804f3e 100644 --- a/docs/03-Architecture/📦 Packages.md +++ b/docs/03-Architecture/📦 Packages.md @@ -37,7 +37,7 @@ com.sufficient.satistaskory PlayerInventoryAdapter # player inventory → InventoryState client/ PlayOverlay # Screen 2 - CaptureHook # Screen 1 — one host is enough to start + CaptureHook # Screen 1 — hover add on inventory, craft pane, craft book ``` Dependencies point inward: `infrastructure` and `application` may import `domain`. `domain` imports nothing from NeoForge, EMI, JEI, REI, or `infrastructure`. diff --git a/src/main/java/com/sufficient/satistaskory/SatistaskoryClient.java b/src/main/java/com/sufficient/satistaskory/SatistaskoryClient.java index 8620966..29c0809 100644 --- a/src/main/java/com/sufficient/satistaskory/SatistaskoryClient.java +++ b/src/main/java/com/sufficient/satistaskory/SatistaskoryClient.java @@ -2,6 +2,7 @@ package com.sufficient.satistaskory; import org.lwjgl.glfw.GLFW; +import com.sufficient.satistaskory.infrastructure.client.CaptureHook; import com.sufficient.satistaskory.infrastructure.client.ClientCommands; import com.sufficient.satistaskory.infrastructure.client.PlannerSession; import com.sufficient.satistaskory.infrastructure.client.PlayOverlay; @@ -17,15 +18,14 @@ import net.neoforged.fml.common.Mod; import net.neoforged.neoforge.client.event.InputEvent; import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent; +import net.neoforged.neoforge.client.event.ScreenEvent; import net.neoforged.neoforge.client.gui.VanillaGuiLayers; -import net.neoforged.neoforge.common.NeoForge; @Mod(value = Satistaskory.MODID, dist = Dist.CLIENT) public class SatistaskoryClient { public SatistaskoryClient(IEventBus modEventBus, ModContainer container) { PlannerSession.bootstrap(); modEventBus.addListener(SatistaskoryClient::registerGuiLayers); - NeoForge.EVENT_BUS.register(GameEvents.class); } private static void registerGuiLayers(RegisterGuiLayersEvent event) { @@ -61,5 +61,20 @@ public class SatistaskoryClient { event.setCanceled(true); } } + + @SubscribeEvent + public static void onScreenRender(ScreenEvent.Render.Post event) { + CaptureHook.render(event.getScreen(), event.getGuiGraphics(), event.getMouseX(), event.getMouseY()); + } + + @SubscribeEvent + public static void onScreenClick(ScreenEvent.MouseButtonPressed.Pre event) { + if (event.getButton() != GLFW.GLFW_MOUSE_BUTTON_LEFT) { + return; + } + if (CaptureHook.click(event.getMouseX(), event.getMouseY())) { + event.setCanceled(true); + } + } } } diff --git a/src/main/java/com/sufficient/satistaskory/infrastructure/client/CaptureHook.java b/src/main/java/com/sufficient/satistaskory/infrastructure/client/CaptureHook.java new file mode 100644 index 0000000..84d42d0 --- /dev/null +++ b/src/main/java/com/sufficient/satistaskory/infrastructure/client/CaptureHook.java @@ -0,0 +1,149 @@ +package com.sufficient.satistaskory.infrastructure.client; + +import java.lang.reflect.Field; + +import com.sufficient.satistaskory.domain.ItemId; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; +import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent; +import net.minecraft.client.gui.screens.recipebook.RecipeButton; +import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener; +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; + +/** + * Screen 1: a button on host views we do not own. + * Inventory slots, crafting grid, and the craft book. + */ +public final class CaptureHook { + private static final String LABEL = "+ Add to Crafting Goals"; + private static final int PAD_X = 6; + private static final int PAD_Y = 4; + + private static final Field RECIPE_PAGE; + private static final Field HOVERED_BUTTON; + + static { + Field page = null; + Field hovered = null; + try { + page = RecipeBookComponent.class.getDeclaredField("recipeBookPage"); + page.setAccessible(true); + Class pageClass = page.getType(); + hovered = pageClass.getDeclaredField("hoveredButton"); + hovered.setAccessible(true); + } catch (ReflectiveOperationException ignored) { + // Slot hover still works if craft-book fields move. + } + RECIPE_PAGE = page; + HOVERED_BUTTON = hovered; + } + + private static int buttonX; + private static int buttonY; + private static int buttonW; + private static int buttonH; + private static ItemId hoveredItem; + + private CaptureHook() {} + + public static void render(Screen screen, GuiGraphics graphics, int mouseX, int mouseY) { + hoveredItem = null; + Minecraft minecraft = Minecraft.getInstance(); + if (minecraft.player == null) { + return; + } + + ItemStack stack = hoveredStack(screen); + if (stack.isEmpty()) { + return; + } + + ResourceLocation key = BuiltInRegistries.ITEM.getKey(stack.getItem()); + hoveredItem = ItemId.parse(key.toString()); + + Font font = minecraft.font; + buttonW = font.width(LABEL) + PAD_X * 2; + buttonH = font.lineHeight + PAD_Y * 2; + buttonX = mouseX + 12; + buttonY = mouseY + 20; + if (buttonX + buttonW > screen.width - 4) { + buttonX = screen.width - 4 - buttonW; + } + if (buttonY + buttonH > screen.height - 4) { + buttonY = mouseY - buttonH - 8; + } + if (buttonX < 4) { + buttonX = 4; + } + if (buttonY < 4) { + buttonY = 4; + } + + graphics.fill(buttonX, buttonY, buttonX + buttonW, buttonY + buttonH, 0xE0101010); + graphics.fill(buttonX + 1, buttonY + 1, buttonX + buttonW - 1, buttonY + buttonH - 1, 0xE0282828); + graphics.drawString(font, LABEL, buttonX + PAD_X, buttonY + PAD_Y, 0xFFFFFF, false); + } + + public static boolean click(double mouseX, double mouseY) { + if (hoveredItem == null || !contains(mouseX, mouseY)) { + return false; + } + PlannerSession.get().create(hoveredItem); + Minecraft minecraft = Minecraft.getInstance(); + minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + minecraft.player.displayClientMessage(Component.literal("Added " + hoveredItem), false); + return true; + } + + private static boolean contains(double mouseX, double mouseY) { + return hoveredItem != null + && mouseX >= buttonX + && mouseX < buttonX + buttonW + && mouseY >= buttonY + && mouseY < buttonY + buttonH; + } + + private static ItemStack hoveredStack(Screen screen) { + if (screen instanceof AbstractContainerScreen container) { + Slot slot = container.getSlotUnderMouse(); + if (slot != null && slot.hasItem()) { + return slot.getItem(); + } + } + return hoveredRecipeResult(screen); + } + + private static ItemStack hoveredRecipeResult(Screen screen) { + if (!(screen instanceof RecipeUpdateListener listener) || RECIPE_PAGE == null || HOVERED_BUTTON == null) { + return ItemStack.EMPTY; + } + RecipeBookComponent book = listener.getRecipeBookComponent(); + if (!book.isVisible()) { + return ItemStack.EMPTY; + } + Minecraft minecraft = Minecraft.getInstance(); + if (minecraft.level == null) { + return ItemStack.EMPTY; + } + try { + Object page = RECIPE_PAGE.get(book); + Object hovered = HOVERED_BUTTON.get(page); + if (hovered instanceof RecipeButton button && button.visible && button.isHoveredOrFocused()) { + return button.getRecipe().value().getResultItem(minecraft.level.registryAccess()); + } + } catch (ReflectiveOperationException ignored) { + return ItemStack.EMPTY; + } + return ItemStack.EMPTY; + } +}