|
| 1 | +package com.neofastftl.infinitypattern.client; |
| 2 | + |
| 3 | +import com.neofastftl.infinitypattern.InfinityPattern; |
| 4 | +import net.minecraft.client.Minecraft; |
| 5 | +import net.minecraft.client.gui.screens.Screen; |
| 6 | +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; |
| 7 | +import net.minecraft.resources.ResourceLocation; |
| 8 | +import net.minecraft.world.inventory.Slot; |
| 9 | +import net.minecraft.world.item.ItemStack; |
| 10 | +import net.neoforged.api.distmarker.Dist; |
| 11 | +import net.neoforged.bus.api.SubscribeEvent; |
| 12 | +import net.neoforged.fml.common.EventBusSubscriber; |
| 13 | +import net.neoforged.neoforge.client.event.ScreenEvent; |
| 14 | +import org.lwjgl.glfw.GLFW; |
| 15 | + |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +@EventBusSubscriber(modid = InfinityPattern.MODID, value = Dist.CLIENT, bus = EventBusSubscriber.Bus.GAME) |
| 19 | +public class GuideHandler { |
| 20 | + |
| 21 | + private static final ResourceLocation GUIDE_PAGE = ResourceLocation.fromNamespaceAndPath(InfinityPattern.MODID, "ifpat_intro/ifpat-index.md"); |
| 22 | + |
| 23 | + @SubscribeEvent |
| 24 | + public static void onKeyPressed(ScreenEvent.KeyPressed.Post event) { |
| 25 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Key pressed: {}, G is {}", event.getKeyCode(), GLFW.GLFW_KEY_G); |
| 26 | + if (event.getKeyCode() == GLFW.GLFW_KEY_G) { |
| 27 | + Screen screen = event.getScreen(); |
| 28 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Screen: {}", screen.getClass().getName()); |
| 29 | + if (screen instanceof AbstractContainerScreen<?> containerScreen) { |
| 30 | + Slot slot = containerScreen.getSlotUnderMouse(); |
| 31 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Slot under mouse: {}", slot); |
| 32 | + if (slot != null && slot.hasItem()) { |
| 33 | + ItemStack stack = slot.getItem(); |
| 34 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Item under mouse: {}", stack.getItem()); |
| 35 | + if (stack.is(InfinityPattern.ITEM_INFINITE_EMPTY_PATTERN.get())) { |
| 36 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Matches Infinite Pattern! Opening guide..."); |
| 37 | + openGuide(); |
| 38 | + event.setCanceled(true); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void openGuide() { |
| 46 | + try { |
| 47 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Attempting to open guide page: {}", GUIDE_PAGE); |
| 48 | + // In AE2 1.21.1, the guide is opened via appeng.client.guide.GuideHandler.openEntry |
| 49 | + Class<?> guideHandlerClass = Class.forName("appeng.client.guide.GuideHandler"); |
| 50 | + var openEntryMethod = guideHandlerClass.getMethod("openEntry", ResourceLocation.class); |
| 51 | + openEntryMethod.invoke(null, GUIDE_PAGE); |
| 52 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Guide openEntry called successfully."); |
| 53 | + } catch (Exception e) { |
| 54 | + InfinityPattern.LOGGER.error("[DEBUG_LOG] Failed to open guide via GuideHandler: {}", e.getMessage()); |
| 55 | + try { |
| 56 | + // Fallback for older AE2 versions or different class structure |
| 57 | + Class<?> guideScreenClass = Class.forName("appeng.client.guide.GuideScreen"); |
| 58 | + var showMethod = guideScreenClass.getMethod("show", ResourceLocation.class); |
| 59 | + showMethod.invoke(null, GUIDE_PAGE); |
| 60 | + InfinityPattern.LOGGER.info("[DEBUG_LOG] Guide GuideScreen.show called successfully."); |
| 61 | + } catch (Exception e2) { |
| 62 | + InfinityPattern.LOGGER.error("[DEBUG_LOG] Failed to open guide via GuideScreen fallback: {}", e2.getMessage()); |
| 63 | + Minecraft.getInstance().player.displayClientMessage( |
| 64 | + net.minecraft.network.chat.Component.literal("Could not open AE2 guide: " + e.getMessage()), |
| 65 | + false |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments