@echo off
set JAVA_PATH=java
set AUTO_RESTART=false
set FORGE_VERSION=43.3.5
set INSTALLER="%~dp0forge-1.19.2-%FORGE_VERSION%-installer.jar"
set FORGE_URL="https://maven.minecraftforge.net/net/minecraftforge/forge/1.19.2-%FORGE_VERSION%/forge-1.19.2-%FORGE_VERSION%-installer.jar"

:JAVA
if not defined ATM9_JAVA (
    set ATM9_JAVA=java
)

"%JAVA_PATH%" -version 1>nul 2>nul || (
   echo Minecraft 1.19.2 requires Java 17 - Java not found
   pause
   exit /b 1
)

:FORGE
setlocal
cd /D "%~dp0"
if not exist "libraries" (
    echo Forge not installed, installing now.
    if not exist %INSTALLER% (
        echo No Forge installer found, downloading from %FORGE_URL%
        bitsadmin.exe /rawreturn /nowrap /transfer forgeinstaller /download /priority FOREGROUND %FORGE_URL% %INSTALLER%
    )
    
    echo Running Forge installer.
    "%JAVA_PATH%" -jar %INSTALLER% -installServer
)

if not exist "server.properties" (
    (
        echo motd=\u00A7cZombie \u00A7fInvade \u00A7c100 \u00A7fDays
    )> "server.properties"
)

if not exist "eula.txt" (
    (
        echo eula=true
    )> "eula.txt"
)

for /f tokens^=2-5^ delims^=.-_^" %%j in ('"%JAVA_PATH%" -fullversion 2^>^&1') do set "jver=%%j"
if not %jver% geq 17  (
    echo Minecraft 1.19.2 requires Java 17 - found Java %jver%
    pause
    exit /b 1
) 

:START
"%JAVA_PATH%" @user_jvm_args.txt @libraries/net/minecraftforge/forge/1.19.2-%FORGE_VERSION%/win_args.txt nogui

if "%AUTO_RESTART%" == "false" ( 
    goto:EOF 
)

echo Restarting automatically in 10 seconds (press Ctrl + C to cancel)
timeout /t 10 /nobreak > NUL
goto:START
