name: Build

on: [ workflow_dispatch, pull_request, push ]

env:
  JAVA_VERSION: 17

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Setup Java
        run: echo "JAVA_HOME=$JAVA_HOME_${{ env.JAVA_VERSION }}_X64" >> "$GITHUB_ENV"

      - name: Loom Cache
        uses: actions/cache@v4
        with:
          path: "**/.gradle/loom-cache"
          key: "${{ runner.os }}-gradle-${{ hashFiles('**/libs.versions.*', '**/*.gradle*', '**/gradle-wrapper.properties') }}"
          restore-keys: "${{ runner.os }}-gradle-"

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4.2.0
        with:
          gradle-home-cache-cleanup: true
          cache-read-only: ${{ !endsWith(github.ref_name, '/dev') }}

      - name: Validate Gradle Wrapper Integrity
        uses: gradle/wrapper-validation-action@v2

      - name: Build
        # Doesn't actually publish, as no secrets are passed in, just makes sure that publishing works
        # Also generate the mod jars for the test job
        run: ./gradlew remapTestModJar publish --no-daemon

      - name: Capture Build Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: Artifacts
          path: |
            common/build/libs/
            fabric/build/libs/
            forge/build/libs/
            vanillinFabric/build/libs/
            vanillinForge/build/libs/

  test:
    strategy:
      fail-fast: false
      matrix:
        loader: [ forge, fabric ]
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: Artifacts

      - name: Setup Environment Variables
        run: |
          echo "MINECRAFT_VERSION=$(grep '^minecraft_version =' gradle.properties | cut -d'=' -f2 | tr -d ' ')" >> "$GITHUB_ENV"
          echo "FABRIC_API_VERSION=$(grep '^fabric_api_version =' gradle.properties | cut -d'=' -f2 | tr -d ' ' | sed 's/+.*//')" >> "$GITHUB_ENV"

      - name: Move Test Mod and Flywheel into run/mods
        # We don't want to recreate the jar name formatting so glob everything over then remove the sources and javadoc jars
        run: |
          mkdir -p run/mods
          cp ${{ matrix.loader }}/build/libs/*.jar run/mods
          rm -f run/mods/*-sources.jar run/mods/*-javadoc.jar

      # Lock to a specific commit, it would be bad if the tag is re-pushed with unwanted changes
      - name: Run the MC client
        uses: 3arthqu4ke/mc-runtime-test@e72f8fe1134aabf6fc749a2a8c09bb56dd7d283e
        with:
          mc: ${{ env.MINECRAFT_VERSION }}
          modloader: ${{ matrix.loader }}
          regex: .*${{ matrix.loader }}.*
          mc-runtime-test: none
          java: ${{ env.JAVA_VERSION }}
          fabric-api: ${{ matrix.loader == 'fabric' && env.FABRIC_API_VERSION || 'none' }}