#include <windows.h>
#include <iostream>
#include <string>

HWND FindMinecraft() {
    return FindWindowA("LWJGL", NULL);
}

void SendCmd(HWND hwnd, const std::string& cmd) {
    PostMessageA(hwnd, WM_CHAR, 'T', 0);
    Sleep(80);
    
    for (int i = 0; i < cmd.size(); ++i) {
        PostMessageA(hwnd, WM_CHAR, cmd[i], 0);
        Sleep(20);
    }
    
    PostMessageA(hwnd, WM_KEYDOWN, VK_RETURN, 0);
    PostMessageA(hwnd, WM_KEYUP, VK_RETURN, 0);
    Sleep(100);
}

bool IsF3F4Pressed() {
    return (GetAsyncKeyState(VK_F3) & 0x8000) &&
           (GetAsyncKeyState(VK_F4) & 0x8000);
}

int main() {
    HWND mc = FindMinecraft();
    
    SetConsoleTitleA("MC1.8.8 终极后台控制器");
    system("mode con cols=50 lines=25");
    std::cout << "==========================================" << std::endl;
    std::cout << "       MC 1.8.8 终极指令助手 (完美版)" << std::endl;
    std::cout << "==========================================" << std::endl;
    std::cout << "F3 + F4 = 激活模式切换" << std::endl;
    std::cout << "F9      = 完全退出程序(带提示)" << std::endl;
    std::cout << "------------------------------------------" << std::endl;
    std::cout << "  【模式切换】" << std::endl;
    std::cout << "    0 = 生存模式" << std::endl;
    std::cout << "    1 = 创造模式" << std::endl;
    std::cout << "    2 = 冒险模式" << std::endl;
    std::cout << "    3 = 旁观模式" << std::endl;
    std::cout << "------------------------------------------" << std::endl;
    std::cout << "  【快捷指令】" << std::endl;
    std::cout << "    F5 = 晴天" << std::endl;
    std::cout << "    F6 = 夜晚" << std::endl;
    std::cout << "    F7 = 飞行模式" << std::endl;
    std::cout << "    F8 = 杀死附近怪物" << std::endl;
    std::cout << "------------------------------------------" << std::endl;
    std::cout << "  按任意其他键 = 退出监听" << std::endl;
    std::cout << "==========================================" << std::endl;

    if (!mc) {
        std::cout << "\n[错误] 未找到 Minecraft 1.8.8!" << std::endl;
        system("pause");
        return 0;
    }
    std::cout << "\n[成功] 已连接到游戏!后台运行中..." << std::endl;

    while (true) {
        if (GetAsyncKeyState(VK_F9) & 0x8000) {
            MessageBoxA(NULL, "MC指令控制器已安全退出!", "退出成功", MB_OK | MB_ICONINFORMATION);
            return 0;
        }

        if (IsF3F4Pressed()) {
            std::cout << "\n[已激活] 请按数字键切换模式:" << std::endl;
            Sleep(200);
            
            while (true) {
                if (GetAsyncKeyState(VK_F9) & 0x8000) {
                    MessageBoxA(NULL, "MC指令控制器已安全退出!", "退出成功", MB_OK | MB_ICONINFORMATION);
                    return 0;
                }

                if (GetAsyncKeyState('0') & 0x8000) {
                    std::cout << " → 生存模式" << std::endl;
                    SendCmd(mc, "/gamemode 0");
                    break;
                }
                if (GetAsyncKeyState('1') & 0x8000) {
                    std::cout << " → 创造模式" << std::endl;
                    SendCmd(mc, "/gamemode 1");
                    break;
                }
                if (GetAsyncKeyState('2') & 0x8000) {
                    std::cout << " → 冒险模式" << std::endl;
                    SendCmd(mc, "/gamemode 2");
                    break;
                }
                if (GetAsyncKeyState('3') & 0x8000) {
                    std::cout << " → 旁观模式" << std::endl;
                    SendCmd(mc, "/gamemode 3");
                    break;
                }

                if (GetAsyncKeyState(VK_F5) & 0x8000) {
                    std::cout << " → 已设置:晴天" << std::endl;
                    SendCmd(mc, "/weather clear");
                    break;
                }
                if (GetAsyncKeyState(VK_F6) & 0x8000) {
                    std::cout << " → 已设置:夜晚" << std::endl;
                    SendCmd(mc, "/time set night");
                    break;
                }
                if (GetAsyncKeyState(VK_F7) & 0x8000) {
                    std::cout << " → 已开启:飞行模式" << std::endl;
                    SendCmd(mc, "/fly");
                    break;
                }
                if (GetAsyncKeyState(VK_F8) & 0x8000) {
                    std::cout << " → 已杀死:附近所有怪物" << std::endl;
                    SendCmd(mc, "/kill @e[type=!player]");
                    break;
                }

                for (int i = 8; i < 256; ++i) {
                    if (i != '0' && i != '1' && i != '2' && i != '3' && i != VK_F5 && i != VK_F6 && i != VK_F7 && i != VK_F8 && (GetAsyncKeyState(i) & 0x8000)) {
                        std::cout << " → 退出监听" << std::endl;
                        goto endLoop;
                    }
                }
                Sleep(50);
            }
            endLoop:
            std::cout << "[返回] 等待 F3+F4 重新激活..." << std::endl;
            Sleep(300);
        }
        Sleep(30);
    }
    return 0;
}