Show the button on hovered inventory/craft slots and craft-book results. Click calls Create Goal. Command add stays as a debug path.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user