diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..3e0f77cc --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux shell scripts should use lf +gradlew text eol=lf +*.sh text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c93d9ee6..fecaf346 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,6 +18,8 @@ jobs: steps: - uses: actions/checkout@v3 + with: + ref: HoldYourWaffle-uberbranch - name: Set up JDK 8 uses: actions/setup-java@v3 @@ -26,13 +28,15 @@ jobs: distribution: 'temurin' - name: Build Core - run: ./gradlew shadowJar + run: | + cd core + ./gradlew shadowJar - name: Upload Core - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: advancedbackups-corelib.jar - path: build/libs/advancedbackups-corelib.jar + path: core/build/libs/advancedbackups-corelib.jar forge-1-7-10: @@ -43,12 +47,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.7.10 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 8 uses: actions/setup-java@v3 @@ -57,15 +62,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.7.10 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.7.10-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.7.10-${{ inputs.version }}.jar + path: forge/1.7.10/build/libs/AdvancedBackups-forge-1.7.10-${{ inputs.version }}.jar forge-1-12-2: @@ -76,12 +83,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.12 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 8 uses: actions/setup-java@v3 @@ -90,15 +98,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.12 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.12-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.12-${{ inputs.version }}.jar + path: forge/1.12/build/libs/AdvancedBackups-forge-1.12-${{ inputs.version }}.jar forge-1-16: @@ -109,12 +119,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.16 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 8 uses: actions/setup-java@v3 @@ -123,15 +134,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.16 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.16-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.16-${{ inputs.version }}.jar + path: forge/1.16/build/libs/AdvancedBackups-forge-1.16-${{ inputs.version }}.jar forge-1-18: @@ -142,12 +155,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.18 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -156,15 +170,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.18 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.18-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.18-${{ inputs.version }}.jar + path: forge/1.18/build/libs/AdvancedBackups-forge-1.18-${{ inputs.version }}.jar fabric-1-18: @@ -175,12 +191,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.18 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -189,15 +206,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.18 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.18-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.18-${{ inputs.version }}.jar + path: fabric/1.18/build/libs/AdvancedBackups-fabric-1.18-${{ inputs.version }}.jar forge-1-19-2: @@ -208,12 +227,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.19.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -222,14 +242,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.19.2 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.19.2-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.19.2-${{ inputs.version }}.jar + path: forge/1.19.2/build/libs/AdvancedBackups-forge-1.19.2-${{ inputs.version }}.jar forge-1-19-3: @@ -240,12 +262,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.19.3 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -254,14 +277,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.19.3 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.19.3-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.19.3-${{ inputs.version }}.jar + path: forge/1.19.3/build/libs/AdvancedBackups-forge-1.19.3-${{ inputs.version }}.jar fabric-1-19-2: @@ -272,12 +297,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.19.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -286,14 +312,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.19.2 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.19.2-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.19.2-${{ inputs.version }}.jar + path: fabric/1.19.2/build/libs/AdvancedBackups-fabric-1.19.2-${{ inputs.version }}.jar fabric-1-19-3: @@ -304,12 +332,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.19.3 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -318,14 +347,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.19.3 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.19.3-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.19.3-${{ inputs.version }}.jar + path: fabric/1.19.3/build/libs/AdvancedBackups-fabric-1.19.3-${{ inputs.version }}.jar forge-1-20: @@ -336,12 +367,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.20 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -350,14 +382,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.20 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.20-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.20-${{ inputs.version }}.jar + path: forge/1.20/build/libs/AdvancedBackups-forge-1.20-${{ inputs.version }}.jar forge-1-20-2: @@ -368,12 +402,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.20.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -382,14 +417,16 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.20.2 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.20.2-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.20.2-${{ inputs.version }}.jar + path: forge/1.20.2/build/libs/AdvancedBackups-forge-1.20.2-${{ inputs.version }}.jar forge-1-20-4: @@ -400,12 +437,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.20.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -415,14 +453,16 @@ jobs: - name: Build Mod #Here, we have a little override for the minecraft version. Stops us needing a branch whilst still producing a separate jar. - run: ./gradlew -Pversion=${{ inputs.version }} build -PminecraftVersion=1.20.4 -Pminecraft_version=1.20.4 -Pforge_version=49.1.4 -Pmapping_version=1.20.4 build + run: | + cd forge/1.20.2 + ./gradlew -Pversion=${{ inputs.version }} build -PminecraftVersion=1.20.4 -Pminecraft_version=1.20.4 -Pforge_version=49.1.4 -Pmapping_version=1.20.4 build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.20.4-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.20.4-${{ inputs.version }}.jar + path: forge/1.20.2/build/libs/AdvancedBackups-forge-1.20.4-${{ inputs.version }}.jar forge-1-20-6: @@ -433,12 +473,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: forge-1.20.6 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -447,15 +488,16 @@ jobs: distribution: 'temurin' - name: Build Mod - #Here, we have a little override for the minecraft version. Stops us needing a branch whilst still producing a separate jar. - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd forge/1.20.6 + ./gradlew -Pversion=${{ inputs.version }} build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-forge-1.20.6-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-forge-1.20.6-${{ inputs.version }}.jar + path: forge/1.20.6/build/libs/AdvancedBackups-forge-1.20.6-${{ inputs.version }}.jar fabric-1-20: @@ -466,12 +508,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.20 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -480,15 +523,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.20 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.20-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.20-${{ inputs.version }}.jar + path: fabric/1.20/build/libs/AdvancedBackups-fabric-1.20-${{ inputs.version }}.jar fabric-1-20-2: @@ -499,12 +544,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.20.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -513,15 +559,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.20.2 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.20.2-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.20.2-${{ inputs.version }}.jar + path: fabric/1.20.2/build/libs/AdvancedBackups-fabric-1.20.2-${{ inputs.version }}.jar fabric-1-20-4: @@ -532,12 +580,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.20.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -547,14 +596,16 @@ jobs: - name: Build Mod #Here, we have a little override for the minecraft version. Stops us needing a branch whilst still producing a separate jar. - run: ./gradlew -Pversion=${{ inputs.version }} -PminecraftVersion=1.20.4 -Pminecraft_version=1.20.4 -Pyarn_mappings=1.20.4+build.3 -Ploader_version=0.16.2 -Pfabric_version-0.97.1+1.20.4 build + run: | + cd fabric/1.20.2 + ./gradlew -Pversion=${{ inputs.version }} -PminecraftVersion=1.20.4 -Pminecraft_version=1.20.4 -Pyarn_mappings=1.20.4+build.3 -Ploader_version=0.16.2 -Pfabric_version-0.97.1+1.20.4 build - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.20.4-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.20.4-${{ inputs.version }}.jar + path: fabric/1.20.2/build/libs/AdvancedBackups-fabric-1.20.4-${{ inputs.version }}.jar fabric-1-20-6: @@ -565,12 +616,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.20.6 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -579,15 +631,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.20.6 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.20.6-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.20.6-${{ inputs.version }}.jar + path: fabric/1.20.6/build/libs/AdvancedBackups-fabric-1.20.6-${{ inputs.version }}.jar fabric-1-21: @@ -598,12 +652,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: fabric-1.21 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -612,15 +667,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd fabric/1.21 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-fabric-1.21-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-fabric-1.21-${{ inputs.version }}.jar + path: fabric/1.21/build/libs/AdvancedBackups-fabric-1.21-${{ inputs.version }}.jar neoforge-1-20-2: @@ -631,12 +688,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: neoforge-1.20.2 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -645,15 +703,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd neoforge/1.20.2 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.2-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-neoforge-1.20.2-${{ inputs.version }}.jar + path: neoforge/1.20.2/build/libs/AdvancedBackups-neoforge-1.20.2-${{ inputs.version }}.jar neoforge-1-20-4: @@ -664,12 +724,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: neoforge-1.20.4 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -678,15 +739,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd neoforge/1.20.4 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.4-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-neoforge-1.20.4-${{ inputs.version }}.jar + path: neoforge/1.20.4/build/libs/AdvancedBackups-neoforge-1.20.4-${{ inputs.version }}.jar @@ -698,12 +761,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: neoforge-1.20.6 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -712,15 +776,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd neoforge/1.20.6 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.6-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-neoforge-1.20.6-${{ inputs.version }}.jar + path: neoforge/1.20.6/build/libs/AdvancedBackups-neoforge-1.20.6-${{ inputs.version }}.jar @@ -732,12 +798,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: neoforge-1.21 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -746,15 +813,17 @@ jobs: distribution: 'temurin' - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd neoforge/1.21 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-neoforge-1.21-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-neoforge-1.21-${{ inputs.version }}.jar + path: neoforge/1.21/build/libs/AdvancedBackups-neoforge-1.21-${{ inputs.version }}.jar @@ -766,12 +835,13 @@ jobs: - uses: actions/checkout@v3 with: - ref: spigot-1.21 + ref: HoldYourWaffle-uberbranch - name: Download corelib - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: advancedbackups-corelib.jar + path: core/build/libs/ - name: Set up JDK 21 uses: actions/setup-java@v3 @@ -785,15 +855,17 @@ jobs: java -jar BuildTools.jar --nogui --rev 1.21.1 --remapped - name: Build Mod - run: ./gradlew -Pversion=${{ inputs.version }} build + run: | + cd spigot/1.21 + ./gradlew -Pversion=${{ inputs.version }} build #run: ls -la advancedbackups-corelib.jar - name: Upload Mod - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AdvancedBackups-spigot-1.21-${{ inputs.version }}.jar - path: build/libs/AdvancedBackups-spigot-1.21-${{ inputs.version }}.jar + path: spigot/1.21/build/libs/AdvancedBackups-spigot-1.21-${{ inputs.version }}.jar bundle-archives: runs-on: ubuntu-latest @@ -806,139 +878,139 @@ jobs: - name: Download Forge-1-7-10 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.7.10-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-12 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.12-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-16 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.16-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-18 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.18-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-18 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.18-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-19-2 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.19.2-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-19-3 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.19.3-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-19-2 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.19.2-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-19-3 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.19.3-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-20 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.20-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-20-2 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.20.2-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-20 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.20-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-20-4 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.20.4-${{ inputs.version }}.jar path: archives/ - name: Download Forge-1-20-6 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-forge-1.20.6-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-20-2 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.20.2-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-20-4 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.20.4-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-20-6 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.20.6-${{ inputs.version }}.jar path: archives/ - name: Download Fabric-1-21 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-fabric-1.21-${{ inputs.version }}.jar path: archives/ - name: Download Neoforge-1-20-2 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.2-${{ inputs.version }}.jar path: archives/ - name: Download Neoforge-1-20-4 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.4-${{ inputs.version }}.jar path: archives/ - name: Download Neoforge-1-20-6 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-neoforge-1.20.6-${{ inputs.version }}.jar path: archives/ - name: Download Neoforge-1-21 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-neoforge-1.21-${{ inputs.version }}.jar path: archives/ - name: Download Spigot-1-21 - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: AdvancedBackups-spigot-1.21-${{ inputs.version }}.jar path: archives/ @@ -958,7 +1030,7 @@ jobs: - name: Upload Archives - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 if: ${{ steps.bundle.conclusion != 'failure' }} with: name: AdvancedBackups-ALL_JARS-${{ inputs.version }}.zip diff --git a/.gitignore b/.gitignore index 148abb73..20e4f289 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ bin .metadata .classpath .project +eclipse # idea out @@ -13,19 +14,13 @@ out *.iml .idea +# vscode +.vscode + # gradle build .gradle -gradlew.bat + # other -eclipse run - -# Files from Forge MDK forge*changelog.txt - - - -#custom for core lib -.gitattributes -.vscode \ No newline at end of file diff --git a/build-all.sh b/build-all.sh new file mode 100644 index 00000000..75ce3c24 --- /dev/null +++ b/build-all.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +root=$(pwd) + +# This will fail silently (but deadly) if there's ever a modloader that's lexicographically before "core" +find . -maxdepth 3 -type f -name 'build.gradle' | sed -r 's|/[^/]+$||' | sort \ +| while read -r folder; do + echo -e "\n\e[1;104m Building $folder... \e[0m" + cd "$folder" || exit 1 + + # Older Gradle versions are naughty and consume stdin for some reason + ./gradlew -Pversion=$1 build < /dev/null + + cd "$root" || exit 1 +done diff --git a/build.gradle b/core/build.gradle similarity index 97% rename from build.gradle rename to core/build.gradle index 9a901a85..4b935076 100644 --- a/build.gradle +++ b/core/build.gradle @@ -1,103 +1,103 @@ -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar -buildscript { - repositories { - // These repositories are only for Gradle plugins, put any other repositories in the repository block further below - mavenCentral() - gradlePluginPortal() - } - dependencies { - } -} -// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. -plugins { - id 'eclipse' - id 'maven-publish' - id 'com.gradleup.shadow' version '8.3.0' - id 'java' -} - -if(file('customTasks.gradle').exists()){ - apply from: 'customTasks.gradle' //customTasks.gradle won't exist in git -} - -version = '' -group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html -archivesBaseName = "advancedbackups-corelib" - -// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. -java.toolchain.languageVersion = JavaLanguageVersion.of(8) - -println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" - -// Include resources generated by data generators. -sourceSets.main.resources { srcDir 'src/generated/resources' } - - -repositories { - // These repositories are only for Gradle plugins, put any other repositories in the repository block further below - mavenCentral() -} - -tasks.named('shadowJar', ShadowJar) { - enableRelocation true - archiveClassifier.set('') -} - -dependencies { - // Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed - // that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied. - // The userdev artifact is a special name and will get all sorts of transformations applied to it. - //extraLibs 'com.sun.jna.platform:5.13.0' - compileOnly 'com.google.code.gson:gson:2.10.1' - implementation 'org.fusesource.jansi:jansi:2.4.0' - - // Real mod deobf dependency examples - these get remapped to your current mappings - // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency - // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency - // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency - - // Examples using mod jars from ./libs - // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") - - // For more info... - // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html - // http://www.gradle.org/docs/current/userguide/dependency_management.html -} - -// Example for how to get properties into the manifest for reading at runtime. -jar { - manifest { - attributes([ - "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", - "Specification-Title" : "advancedbackups", - "Specification-Vendor" : "raveninthedark", - "Specification-Version" : "1", // We are version 1 of ourselves - "Implementation-Title" : project.name, - "Implementation-Version" : project.jar.archiveVersion, - "Implementation-Vendor" : "raveninthedark", - "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") - ]) - } -} - -// Example configuration to allow publishing using the maven-publish plugin -// This is the preferred method to reobfuscate your jar file -// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing -// publish.dependsOn('reobfJar') - -publishing { - publications { - mavenJava(MavenPublication) { - artifact jar - } - } - repositories { - maven { - url "file://${project.projectDir}/mcmodsrepo" - } - } -} - -tasks.withType(JavaCompile).configureEach { - options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +buildscript { + repositories { + // These repositories are only for Gradle plugins, put any other repositories in the repository block further below + mavenCentral() + gradlePluginPortal() + } + dependencies { + } +} +// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. +plugins { + id 'eclipse' + id 'maven-publish' + id 'com.gradleup.shadow' version '8.3.0' + id 'java' +} + +if(file('customTasks.gradle').exists()){ + apply from: 'customTasks.gradle' //customTasks.gradle won't exist in git +} + +version = '' +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = "advancedbackups-corelib" + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(8) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + + +repositories { + // These repositories are only for Gradle plugins, put any other repositories in the repository block further below + mavenCentral() +} + +tasks.named('shadowJar', ShadowJar) { + enableRelocation true + archiveClassifier.set('') +} + +dependencies { + // Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed + // that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + //extraLibs 'com.sun.jna.platform:5.13.0' + compileOnly 'com.google.code.gson:gson:2.10.1' + implementation 'org.fusesource.jansi:jansi:2.4.0' + + // Real mod deobf dependency examples - these get remapped to your current mappings + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency + // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency + + // Examples using mod jars from ./libs + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// Example for how to get properties into the manifest for reading at runtime. +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "raveninthedark", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "raveninthedark", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } +} + +// Example configuration to allow publishing using the maven-publish plugin +// This is the preferred method to reobfuscate your jar file +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +// publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation } \ No newline at end of file diff --git a/gradle.properties b/core/gradle.properties similarity index 100% rename from gradle.properties rename to core/gradle.properties diff --git a/core/gradle/wrapper/gradle-wrapper.jar b/core/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/core/gradle/wrapper/gradle-wrapper.jar differ diff --git a/core/gradle/wrapper/gradle-wrapper.properties b/core/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/core/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/core/gradlew b/core/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/core/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/core/gradlew.bat b/core/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/core/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java similarity index 97% rename from src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java index 6aaa907f..39518a62 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java @@ -1,956 +1,956 @@ -package co.uk.mommyheather.advancedbackups.cli; - -import org.fusesource.jansi.AnsiConsole; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.BasicFileAttributes; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.InputMismatchException; -import java.util.Properties; -import java.util.regex.Pattern; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; - -public class AdvancedBackupsCLI { - - private static String backupLocation; - private static File serverDir = new File(new File("").toPath().toAbsolutePath().getParent().toString()); - private static String type; - private static ArrayList fileNames = new ArrayList<>(); - private static File worldFile; - private static String worldPath; - - public static void main(String args[]) throws IllegalBackupException { - if (System.console() != null) { - AnsiConsole.systemInstall(); //this gets ansi escape codes working on windows. this was a FUCKING PAIN IN MY ASS - } - - System.out.print("\033[H\033[2J"); - System.out.flush(); - - - CLIIOHelpers.info("Advanced Backups - Version " + AdvancedBackupsCLI.class.getPackage().getImplementationVersion()); - CLIIOHelpers.info("Note : this cannot restore backups made prior to the 3.0 release."); - CLIIOHelpers.info("Searching for properties...", false); - - - Properties props = new Properties(); - File file = new File(serverDir, "config/AdvancedBackups.properties"); - FileReader reader; - try { - reader = new FileReader(file); - props.load(reader); - - backupLocation = props.getProperty("config.advancedbackups.path"); - type = props.getProperty("config.advancedbackups.type"); - } catch (Exception e) { - CLIIOHelpers.error("ERROR LOADING PROPERTIES!"); - CLIIOHelpers.error(getStackTrace(e)); - CLIIOHelpers.error(""); - CLIIOHelpers.error(""); - CLIIOHelpers.error("Ensure you're running this from within the mods directory, and the config file is in the parent directory!"); - // Fatal, cannot proceed - return; - } - - if (backupLocation == null || type == null) { - CLIIOHelpers.error("ERROR LOADING PROPERTIES!"); - CLIIOHelpers.error("Backup location : " + backupLocation); - CLIIOHelpers.error("Type : " + type); - // Fatal, cannot proceed - return; - } - - CLIIOHelpers.info("Config loaded!"); - - //What's a good way to check between absolute and relative? Not all absolute paths will start with /.. - File backupDir = new File(serverDir, backupLocation.replaceAll(Pattern.quote("." + File.separator), "")); - - if (!backupDir.exists()) { - //Is it absolute? - backupDir = new File(backupLocation.replaceAll(Pattern.quote("." + File.separator), "")); - if (!backupDir.exists()) { - CLIIOHelpers.error("Could not find backup directory!"); - CLIIOHelpers.error(backupDir.getAbsolutePath()); - CLIIOHelpers.error("Have you made any backups before?"); - //Fatal, cannot continue - return; - } - } - - - //check for backups from "other mods" - boolean flag = false; - ArrayList otherBackups = new ArrayList<>(); - for (File b : backupDir.listFiles()) { - if (b.getName().endsWith("zip")) { - flag = true; - otherBackups.add(b); - } - - } - - if (flag) { - String result = CLIIOHelpers.getSelectionFromList( - "Backups from another mod have been found. These can be restored if you want.\nWould you want to work with these backups?", - Arrays.asList(new String[]{"Use backups from AdvancedBackups", "Use backups from other mod"}) - ); - if (result == "Use backups from other mod") { - restoreOtherModZip(backupDir); - return; - } - } - - type = CLIIOHelpers.getBackupType(type); - if ("snapshot (command-made only)".equals(type)) type = "snapshots"; - - /*/ - if (backupLocation.startsWith(Pattern.quote(File.separator)) || backupLocation.indexOf(":") == 1) { - backupDir = new File(backupLocation, File.separator + type + File.separator); - } - else { - backupDir = new File(serverDir, backupLocation.replaceAll(Pattern.quote("." + File.separator), "") + File.separator + type + File.separator); - } -*/ - - - boolean exportMode = false; - int backupDateIndex; - - - CLIIOHelpers.info("Do you want to export a backup, restore the entire world state at this point, or a singular file?"); - - String restore = CLIIOHelpers.getSelectionFromList( - "Enter a number.", - Arrays.asList(new String[]{"Export backup as zip", "Restore single file", "Restore entire world"}) - ); - - if ("Export backup as zip".equals(restore)) { - exportMode = true; - } - - - worldFile = CLIIOHelpers.getWorldFile(serverDir); - worldPath = worldFile.getName().replace(" ", "_"); - - backupDir = new File(backupDir, worldFile.getName() + "/" + type); - - try { - backupDateIndex = getBackupDate(backupDir, exportMode); - } catch (IOException e) { - CLIIOHelpers.error("ERROR VIEWING BACKUPS!"); - e.printStackTrace(); - return; - } - - if (exportMode) { - worldFile = new File(serverDir, "AdvancedBackups.temp"); - worldFile.mkdirs(); - } - - - if (!CLIIOHelpers.confirmWarningMessage()) { - CLIIOHelpers.error("ABORTED - WILL NOT PROCEED."); - return; - } - - CLIIOHelpers.info("Preparing..."); - - - switch (restore) { - case "Restore entire world": { - //No going back now! - CLIIOHelpers.info("Backing up current world state..."); - CLIIOHelpers.info("Backup saved to : " + deleteEntireWorld(worldFile, false)); - switch (type) { - case "snapshots": - case "zips": { - restoreFullZip(backupDateIndex, worldFile); - return; - } - case "differential": { - restoreFullDifferential(backupDateIndex, worldFile); - return; - } - case "incremental": { - restoreFullIncremental(backupDateIndex, worldFile); - return; - } - } - } - case "Restore single file": { - switch (type) { - case "snapshots": - case "zips": { - restorePartialZip(backupDateIndex, worldFile); - return; - } - case "differential": { - restorePartialDifferential(backupDateIndex, worldFile); - return; - } - case "incremental": { - restorePartialIncremental(backupDateIndex, worldFile); - return; - } - } - } - case "Export backup as zip": { - - CLIIOHelpers.info("Restoring to temporary directory..."); - - switch (type) { - case "snapshots": - case "zips": { - restoreFullZip(backupDateIndex, worldFile); - break; - } - case "differential": { - restoreFullDifferential(backupDateIndex, worldFile); - break; - } - case "incremental": { - restoreFullIncremental(backupDateIndex, worldFile); - break; - } - } - - CLIIOHelpers.info("Done. Preparing to write to zip..."); - CLIIOHelpers.info("Export saved to : " + deleteEntireWorld(worldFile, true)); - } - - } - } - - - private static String getStackTrace(final Throwable throwable) { - final StringWriter sw = new StringWriter(); - final PrintWriter pw = new PrintWriter(sw, true); - throwable.printStackTrace(pw); - return sw.getBuffer().toString(); - } - - - private static int getBackupDate(File backupDir, boolean exportMode) throws IOException { - fileNames.clear(); - int inputType; - - CLIIOHelpers.info("Select a backup to restore."); - - String[] fileNameArray = backupDir.list(); - if (fileNameArray == null || fileNameArray.length <= 0) { - throw new IOException(String.format("Selected backup directory %s is empty, or is a file!", backupDir.getAbsolutePath())); - } - ArrayList fileNameList = new ArrayList(Arrays.asList(fileNameArray)); //i need to do this. i hate this. - fileNameList.removeIf(name -> ( - name.endsWith("json") || - name.contains("incomplete") || - name.contains("DS_Store") - )); - - if (fileNameList.isEmpty()) { - throw new IOException(String.format("Selected backup directory %s is empty, or is a file!", backupDir.getAbsolutePath())); - } - - for (String fileName : CLIIOHelpers.sortStringsAlphabeticallyWithDirectoryPriority(fileNameList)) { - if (exportMode) { - if (fileName.endsWith("json")) continue; - if (fileName.contains("incomplete")) continue; - File file = new File(backupDir, fileName); - fileNames.add(file.getAbsolutePath()); - String out = file.getName(); - String[] outs = out.split("\\_"); - if (outs.length >= 2) { - out = ". " + outs[outs.length - 2] + "_" + outs[outs.length - 1]; - } else { - out = ". " + out; - } - CLIIOHelpers.info(fileNames.size() + out); - - } else { - if (fileName.endsWith("json")) continue; - if (fileName.contains("incomplete")) continue; - File file = new File(backupDir, fileName); - fileNames.add(file.getAbsolutePath()); - String out = file.getName(); - out = out.replaceAll(".zip", ""); - //out = out.replaceAll(worldPath + "_", ": "); - out = out.replaceAll("backup_", ": "); - out = out.replaceAll("-partial", "\u001B[33m partial\u001B[0m"); - out = out.replaceAll("-full", "\u001B[32m full\u001B[0m"); - CLIIOHelpers.info(fileNames.size() + out); - - } - } - - try { - String line = CLIIOHelpers.input.nextLine(); - if (line == "") { - CLIIOHelpers.warn("Please enter a number."); - return getBackupDate(backupDir, exportMode); - } - inputType = Integer.parseInt(line); - } catch (InputMismatchException | NumberFormatException e) { - CLIIOHelpers.warn("That was not a number. Please enter a number."); - return getBackupDate(backupDir, exportMode); - } - - if (inputType < 1 || inputType > fileNames.size()) { - CLIIOHelpers.warn("Please enter a number between " + fileNames.size() + "."); - return getBackupDate(backupDir, exportMode); - } - - return inputType - 1; - } - - - private static void restoreFullZip(int index, File worldFile) throws IllegalBackupException { - byte[] buffer = new byte[1024]; - //The most basic of the bunch. - ZipEntry entry; - try { - FileInputStream fileInputStream = new FileInputStream(fileNames.get(index)); - ZipInputStream zip = new ZipInputStream(fileInputStream); - while ((entry = zip.getNextEntry()) != null) { - File outputFile = new File(worldFile, entry.getName()); - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - zip.close(); - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + entry.getName()); - } - - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - - CLIIOHelpers.info("Restoring " + outputFile.getName()); - - FileOutputStream outputStream = new FileOutputStream(outputFile); - int length = 0; - while ((length = zip.read(buffer)) > 0) { - outputStream.write(buffer, 0, length); - } - outputStream.close(); - } - zip.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - private static void restoreFullDifferential(int index, File worldFile) throws IllegalBackupException { - //Do we need to check for past backups? if selected is a full backup, we do not. - File backup = new File(fileNames.get(index)); - if (backup.getName().contains("-full")) { - if (backup.isFile()) { - restoreFullZip(index, worldFile); - return; - } - restoreFolder(index, worldFile); - return; - } - //find last FULL backup - for (int i = index; i >= 0; i--) { - String name = fileNames.get(i); - if (name.contains("-full")) { - CLIIOHelpers.info("Restoring last full backup..."); - File file = new File(name); - if (file.isFile()) { - restoreFullZip(i, worldFile); - } else { - restoreFolder(i, worldFile); - } - break; - } - } - CLIIOHelpers.info("\n\nRestoring selected backup..."); - if (backup.isFile()) { - restoreFullZip(index, worldFile); - } else { - restoreFolder(index, worldFile); - } - } - - private static void restoreFullIncremental(int index, File worldFile) throws IllegalBackupException { - //Do we need to check for past backups? if selected is a full backup, we do not. - File backup = new File(fileNames.get(index)); - if (backup.getName().contains("-full")) { - if (backup.isFile()) { - restoreFullZip(index, worldFile); - return; - } - restoreFolder(index, worldFile); - return; - } - //find last FULL backup - int i = index; - while (i >= 0) { - String name = fileNames.get(i); - if (name.contains("-full")) { - CLIIOHelpers.info("Restoring last full backup..."); - File file = new File(name); - if (file.isFile()) { - restoreFullZip(i, worldFile); - } else { - restoreFolder(i, worldFile); - } - break; - } - i--; - } - //restore backups up until the selected one - while (i < index) { - String name = fileNames.get(i); - CLIIOHelpers.info("Restoring chained backup..."); - File file = new File(name); - if (file.isFile()) { - restoreFullZip(i, worldFile); - } else { - restoreFolder(i, worldFile); - } - i++; - } - - CLIIOHelpers.info("\n\nRestoring selected backup..."); - if (backup.isFile()) { - restoreFullZip(index, worldFile); - } else { - restoreFolder(index, worldFile); - } - } - - private static void restorePartialZip(int index, File worldFile) throws IllegalBackupException { - HashMap filePaths = new HashMap<>(); - HashMap dates = new HashMap<>(); - HashMap entryOwners = new HashMap<>(); - - try { - File backup = new File(fileNames.get(index)); - addBackupNamesToLists(backup, entryOwners, filePaths, dates, "\u001B[32m"); - - for (String string : filePaths.keySet()) { - File outputFile = new File(worldFile, string); - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); - } - } - - ZipEntry select = ((ZipEntry) CLIIOHelpers.getFileToRestore(filePaths, "", worldFile)); - - File outputFile = new File(worldFile, select.toString()); - FileOutputStream outputStream = new FileOutputStream(outputFile); - - CLIIOHelpers.info("Restoring " + select.toString() + "..."); - - byte[] buffer = new byte[1028]; - InputStream inputSteam = entryOwners.get(select.toString()).getInputStream(select); - int length; - while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { - outputStream.write(buffer, 0, length); - } - outputStream.flush(); - outputStream.close(); - - } catch (IOException e) { - // TODO handle IOException - } - } - - private static void restorePartialDifferential(int index, File worldFile) throws IllegalBackupException { - //Do we need to check for past backups? if selected is a full backup, we do not. - HashMap filePaths = new HashMap<>(); - HashMap dates = new HashMap<>(); - HashMap entryOwners = new HashMap<>(); - try { - File backup = new File(fileNames.get(index)); - if (!backup.getName().contains("-full")) { - //find last FULL backup - for (int i = index; i >= 0; i--) { - String name = fileNames.get(i); - if (name.contains("-full")) { - addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); - break; - } - } - } - - File file = new File(fileNames.get(index)); - addBackupNamesToLists(file, entryOwners, filePaths, dates, "\u001B[32m"); - - HashMap properMapping = new HashMap<>(); - for (String date : dates.keySet()) { - properMapping.put( - date + " " + dates.get(date), - filePaths.get(date) - ); - } - - for (String string : filePaths.keySet()) { - File outputFile = new File(worldFile, string); - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); - } - } - - Object select = CLIIOHelpers.getFileToRestore(properMapping, "", worldFile); - if (select instanceof Path) { - Path input = (Path) select; - - - if (select.toString().replace("\\", "/").contains("-full/")) { - select = new File( - select.toString().replace("\\", "/") - .split("-full/")[1] - ).toPath(); - } - if (select.toString().replace("\\", "/").contains("-partial/")) { - select = new File( - select.toString().replace("\\", "/") - .split("-partial/")[1] - ).toPath(); - } - - File outputFile = new File(worldFile, select.toString()); - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); - } - - CLIIOHelpers.info("\n\nRestoring file : " + select); - Files.copy(input, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - } else if (select instanceof ZipEntry) { - ZipEntry entry = (ZipEntry) select; - - File outputFile = new File(worldFile, entry.toString()); - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); - } - - FileOutputStream outputStream = new FileOutputStream(outputFile); - - CLIIOHelpers.info("Restoring " + entry.toString() + "..."); - - byte[] buffer = new byte[1028]; - InputStream inputSteam = entryOwners.get(entry.toString()).getInputStream(entry); - int length; - while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { - outputStream.write(buffer, 0, length); - } - outputStream.flush(); - outputStream.close(); - } - - - } catch (IOException e) { - //TODO : Scream at user - e.printStackTrace(); - } - } - - private static void restorePartialIncremental(int index, File worldFile) throws IllegalBackupException { - //Do we need to check for past backups? if selected is a full backup, we do not. - HashMap filePaths = new HashMap<>(); - HashMap dates = new HashMap<>(); - HashMap entryOwners = new HashMap<>(); - try { - File backup = new File(fileNames.get(index)); - if (!backup.getName().contains("-full")) { - int i; - //find last FULL backup - for (i = index; i >= 0; i--) { - String name = fileNames.get(i); - if (name.contains("-full")) { - addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); - break; - } - } - while (i < index) { - String name = fileNames.get(i); - addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); - i++; - } - - } - - File file = new File(fileNames.get(index)); - addBackupNamesToLists(file, entryOwners, filePaths, dates, "\u001B[32m"); - - for (String string : filePaths.keySet()) { - File outputFile = new File(worldFile, string); - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); - } - } - - HashMap properMapping = new HashMap<>(); - for (String date : dates.keySet()) { - properMapping.put( - date + " " + dates.get(date), - filePaths.get(date) - ); - } - - Object select = CLIIOHelpers.getFileToRestore(properMapping, "", worldFile); - if (select instanceof Path) { - Path input = (Path) select; - - - if (select.toString().replace("\\", "/").contains("-full/")) { - select = new File( - select.toString().replace("\\", "/") - .split("-full/")[1] - ).toPath(); - } - if (select.toString().replace("\\", "/").contains("-partial/")) { - select = new File( - select.toString().replace("\\", "/") - .split("-partial/")[1] - ).toPath(); - } - - File outputFile = new File(worldFile, select.toString()); - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); - } - - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - CLIIOHelpers.info("\n\nRestoring file : " + select); - Files.copy(input, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - } else if (select instanceof ZipEntry) { - ZipEntry entry = (ZipEntry) select; - - File outputFile = new File(worldFile, entry.toString()); - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); - } - - FileOutputStream outputStream = new FileOutputStream(outputFile); - - CLIIOHelpers.info("Restoring " + entry.toString() + "..."); - - byte[] buffer = new byte[1028]; - InputStream inputSteam = entryOwners.get(entry.toString()).getInputStream(entry); - int length; - while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { - outputStream.write(buffer, 0, length); - } - outputStream.flush(); - outputStream.close(); - } - - - } catch (IOException e) { - //TODO : Scream at user - e.printStackTrace(); - } - } - - - private static void restoreFolder(int index, File worldFile) throws IllegalBackupException { - File backup = new File(fileNames.get(index)); - - try { - Files.walkFileTree(backup.toPath(), new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { - File source = backup.toPath().relativize(file).toFile(); - File outputFile = new File(worldFile, source.getPath()); - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + source.getPath()).printStackTrace(); - return FileVisitResult.TERMINATE; - } - - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - - CLIIOHelpers.info("Restoring " + outputFile.getName()); - Files.copy(file, outputFile.toPath()); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - - private static void restoreOtherModZip(File backupDir) throws IllegalBackupException { - worldFile = serverDir; - Path file; - HashMap backups = new HashMap<>(); - HashMap entries = new HashMap<>(); - - for (File b : backupDir.getParentFile().listFiles()) { - if (b.getName().endsWith("zip")) { - backups.put(b.getName(), b); - } - } - - String backupName = CLIIOHelpers.getSelectionFromList("Select a backup to restore from.", new ArrayList(backups.keySet())); - - boolean fullWorld = "Whole world" == CLIIOHelpers.getSelectionFromList( - "Do you want to restore the whole world or a singular file?", - Arrays.asList(new String[]{"Whole world", "Single file"}) - ); - - if (!fullWorld) { - if (!CLIIOHelpers.confirmWarningMessage()) return; - - try { - FileSystem zipFs = FileSystems.newFileSystem(backups.get(backupName).toPath(), AdvancedBackupsCLI.class.getClassLoader()); - Path root = zipFs.getPath(""); - Files.walkFileTree(root, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { - entries.put(file.toString(), file); - return FileVisitResult.CONTINUE; - } - }); - - file = CLIIOHelpers.getFileToRestore(entries, "", worldFile); - CLIIOHelpers.info("Restoring " + file.toString() + "..."); - Path outputFile = new File(worldFile, file.toString()).toPath(); - - if (!outputFile.normalize().startsWith(worldFile.toPath())) { - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + file.toString()); - } - if (!outputFile.getParent().toFile().exists()) { - outputFile.getParent().toFile().mkdirs(); - } - Files.copy(file, outputFile, StandardCopyOption.REPLACE_EXISTING); - CLIIOHelpers.info("Done."); - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; - } - } else { - if (!CLIIOHelpers.confirmWarningMessage()) return; - - Path levelDatPath; - ArrayList levelDatPathWrapper = new ArrayList<>(); - - try { - FileSystem zipFs = FileSystems.newFileSystem(backups.get(backupName).toPath(), AdvancedBackupsCLI.class.getClassLoader()); - Path root = zipFs.getPath(""); - Files.walkFileTree(root, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { - if ("level.dat".equals(file.getFileName().toString())) levelDatPathWrapper.add(file); - return FileVisitResult.CONTINUE; - } - }); - - levelDatPath = levelDatPathWrapper.get(0); - CLIIOHelpers.info("Making backup of existing world..."); - CLIIOHelpers.info("Backup saved to : " + deleteEntireWorld(new File(worldFile, levelDatPath.getParent().toString()), false)); - byte[] buffer = new byte[1024]; - ZipEntry entry; - FileInputStream fileInputStream = new FileInputStream(backups.get(backupName)); - ZipInputStream zip = new ZipInputStream(fileInputStream); - while ((entry = zip.getNextEntry()) != null) { - File outputFile = new File(worldFile, entry.getName()); - - if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { - zip.close(); - CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); - throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + entry.getName()); - } - - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - - CLIIOHelpers.info("Restoring " + outputFile.toString() + "..."); - - FileOutputStream outputStream = new FileOutputStream(outputFile); - int length = 0; - while ((length = zip.read(buffer)) > 0) { - outputStream.write(buffer, 0, length); - } - outputStream.close(); - } - zip.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - CLIIOHelpers.info("Done."); - - } - - - private static String deleteEntireWorld(File worldDir, boolean exportMode) { - String ret = backupExistingWorld(worldDir, exportMode); - try { - Files.walkFileTree(worldDir.toPath(), new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { - file.toFile().delete(); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path file, java.io.IOException arg1) { - if (file.toFile().listFiles().length == 0) { - file.toFile().delete(); - } - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - CLIIOHelpers.warn("Failed to delete file :"); - e.printStackTrace(); - } - return ret; - } - - private static String backupExistingWorld(File worldDir, boolean export) { - File out = new File(worldDir, "../cli" + serialiseBackupName(export ? "export" : "backup") + ".zip"); - try { - FileOutputStream outputStream = new FileOutputStream(out); - ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); - zipOutputStream.setLevel(4); - byte[] bytes = new byte[1024]; - Files.walkFileTree(worldDir.toPath(), new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { - Path targetFile; - try { - targetFile = worldDir.toPath().relativize(file); - if (targetFile.toFile().getName().compareTo("session.lock") == 0) { - return FileVisitResult.CONTINUE; - } - zipOutputStream.putNextEntry(new ZipEntry(targetFile.toString())); - FileInputStream is = new FileInputStream(file.toFile()); - while (true) { - int i = is.read(bytes); - if (i < 0) break; - zipOutputStream.write(bytes, 0, i); - } - is.close(); - //byte[] bytes = Files.readAllBytes(file); - //zipOutputStream.write(bytes, 0, bytes.length); - zipOutputStream.closeEntry(); - - } catch (IOException e) { - // TODO : Scream at user - e.printStackTrace(); - } - - return FileVisitResult.CONTINUE; - } - }); - zipOutputStream.flush(); - zipOutputStream.close(); - - CLIIOHelpers.info("Done."); - } catch (Exception e) { - // TODO Handle Exception - } - - return out.getName(); - } - - - private static void addBackupNamesToLists(File file, HashMap entryOwners, HashMap filePaths, - HashMap dates, String colour) throws IOException { - - if (file.isFile()) { - ZipFile zipFile = new ZipFile(file); - Enumeration entryEnum = zipFile.entries(); - - while (entryEnum.hasMoreElements()) { - ZipEntry entry = entryEnum.nextElement(); - - String backupName = file.toString().replace("\\", "/"); - filePaths.put(entry.toString().replace("\\", "/"), entry); - dates.put( - entry.toString().replace("\\", "/"), - "\u001b[31m" - + backupName - .substring(backupName.toString().lastIndexOf("/") + 1) - //.replace(worldPath + "_", "") - .replace("backup_", "") - .replace("-full.zip", "") - .replace("-partial.zip", "") - + "\u001B[0m" - ); - entryOwners.put(entry.toString(), zipFile); - } - } else { - Files.walkFileTree(file.toPath(), new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) throws IOException { - String backupName = file.toString().replace("\\", "/"); - filePaths.put(file.toPath().relativize(path).toString().replace("\\", "/"), path); - dates.put( - file.toPath().relativize(path).toString().replace("\\", "/"), - "\u001b[31m" - + backupName - .substring(backupName.toString().lastIndexOf("/") + 1) - //.replace(worldPath + "_", "") - .replace("backup_", "") - .replace("-full", "") - .replace("-partial", "") - + "\u001B[0m" - ); - return FileVisitResult.CONTINUE; - } - }); - } - } - - - public static String serialiseBackupName(String in) { - Date date = new Date(); - String pattern = "yyyy-MM-dd_HH-mm-ss"; - return in + "_" + new SimpleDateFormat(pattern).format(date); - } - -} +package co.uk.mommyheather.advancedbackups.cli; + +import org.fusesource.jansi.AnsiConsole; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.InputMismatchException; +import java.util.Properties; +import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +public class AdvancedBackupsCLI { + + private static String backupLocation; + private static File serverDir = new File(new File("").toPath().toAbsolutePath().getParent().toString()); + private static String type; + private static ArrayList fileNames = new ArrayList<>(); + private static File worldFile; + private static String worldPath; + + public static void main(String args[]) throws IllegalBackupException { + if (System.console() != null) { + AnsiConsole.systemInstall(); //this gets ansi escape codes working on windows. this was a FUCKING PAIN IN MY ASS + } + + System.out.print("\033[H\033[2J"); + System.out.flush(); + + + CLIIOHelpers.info("Advanced Backups - Version " + AdvancedBackupsCLI.class.getPackage().getImplementationVersion()); + CLIIOHelpers.info("Note : this cannot restore backups made prior to the 3.0 release."); + CLIIOHelpers.info("Searching for properties...", false); + + + Properties props = new Properties(); + File file = new File(serverDir, "config/AdvancedBackups.properties"); + FileReader reader; + try { + reader = new FileReader(file); + props.load(reader); + + backupLocation = props.getProperty("config.advancedbackups.path"); + type = props.getProperty("config.advancedbackups.type"); + } catch (Exception e) { + CLIIOHelpers.error("ERROR LOADING PROPERTIES!"); + CLIIOHelpers.error(getStackTrace(e)); + CLIIOHelpers.error(""); + CLIIOHelpers.error(""); + CLIIOHelpers.error("Ensure you're running this from within the mods directory, and the config file is in the parent directory!"); + // Fatal, cannot proceed + return; + } + + if (backupLocation == null || type == null) { + CLIIOHelpers.error("ERROR LOADING PROPERTIES!"); + CLIIOHelpers.error("Backup location : " + backupLocation); + CLIIOHelpers.error("Type : " + type); + // Fatal, cannot proceed + return; + } + + CLIIOHelpers.info("Config loaded!"); + + //What's a good way to check between absolute and relative? Not all absolute paths will start with /.. + File backupDir = new File(serverDir, backupLocation.replaceAll(Pattern.quote("." + File.separator), "")); + + if (!backupDir.exists()) { + //Is it absolute? + backupDir = new File(backupLocation.replaceAll(Pattern.quote("." + File.separator), "")); + if (!backupDir.exists()) { + CLIIOHelpers.error("Could not find backup directory!"); + CLIIOHelpers.error(backupDir.getAbsolutePath()); + CLIIOHelpers.error("Have you made any backups before?"); + //Fatal, cannot continue + return; + } + } + + + //check for backups from "other mods" + boolean flag = false; + ArrayList otherBackups = new ArrayList<>(); + for (File b : backupDir.listFiles()) { + if (b.getName().endsWith("zip")) { + flag = true; + otherBackups.add(b); + } + + } + + if (flag) { + String result = CLIIOHelpers.getSelectionFromList( + "Backups from another mod have been found. These can be restored if you want.\nWould you want to work with these backups?", + Arrays.asList(new String[]{"Use backups from AdvancedBackups", "Use backups from other mod"}) + ); + if (result == "Use backups from other mod") { + restoreOtherModZip(backupDir); + return; + } + } + + type = CLIIOHelpers.getBackupType(type); + if ("snapshot (command-made only)".equals(type)) type = "snapshots"; + + /*/ + if (backupLocation.startsWith(Pattern.quote(File.separator)) || backupLocation.indexOf(":") == 1) { + backupDir = new File(backupLocation, File.separator + type + File.separator); + } + else { + backupDir = new File(serverDir, backupLocation.replaceAll(Pattern.quote("." + File.separator), "") + File.separator + type + File.separator); + } +*/ + + + boolean exportMode = false; + int backupDateIndex; + + + CLIIOHelpers.info("Do you want to export a backup, restore the entire world state at this point, or a singular file?"); + + String restore = CLIIOHelpers.getSelectionFromList( + "Enter a number.", + Arrays.asList(new String[]{"Export backup as zip", "Restore single file", "Restore entire world"}) + ); + + if ("Export backup as zip".equals(restore)) { + exportMode = true; + } + + + worldFile = CLIIOHelpers.getWorldFile(serverDir); + worldPath = worldFile.getName().replace(" ", "_"); + + backupDir = new File(backupDir, worldFile.getName() + "/" + type); + + try { + backupDateIndex = getBackupDate(backupDir, exportMode); + } catch (IOException e) { + CLIIOHelpers.error("ERROR VIEWING BACKUPS!"); + e.printStackTrace(); + return; + } + + if (exportMode) { + worldFile = new File(serverDir, "AdvancedBackups.temp"); + worldFile.mkdirs(); + } + + + if (!CLIIOHelpers.confirmWarningMessage()) { + CLIIOHelpers.error("ABORTED - WILL NOT PROCEED."); + return; + } + + CLIIOHelpers.info("Preparing..."); + + + switch (restore) { + case "Restore entire world": { + //No going back now! + CLIIOHelpers.info("Backing up current world state..."); + CLIIOHelpers.info("Backup saved to : " + deleteEntireWorld(worldFile, false)); + switch (type) { + case "snapshots": + case "zips": { + restoreFullZip(backupDateIndex, worldFile); + return; + } + case "differential": { + restoreFullDifferential(backupDateIndex, worldFile); + return; + } + case "incremental": { + restoreFullIncremental(backupDateIndex, worldFile); + return; + } + } + } + case "Restore single file": { + switch (type) { + case "snapshots": + case "zips": { + restorePartialZip(backupDateIndex, worldFile); + return; + } + case "differential": { + restorePartialDifferential(backupDateIndex, worldFile); + return; + } + case "incremental": { + restorePartialIncremental(backupDateIndex, worldFile); + return; + } + } + } + case "Export backup as zip": { + + CLIIOHelpers.info("Restoring to temporary directory..."); + + switch (type) { + case "snapshots": + case "zips": { + restoreFullZip(backupDateIndex, worldFile); + break; + } + case "differential": { + restoreFullDifferential(backupDateIndex, worldFile); + break; + } + case "incremental": { + restoreFullIncremental(backupDateIndex, worldFile); + break; + } + } + + CLIIOHelpers.info("Done. Preparing to write to zip..."); + CLIIOHelpers.info("Export saved to : " + deleteEntireWorld(worldFile, true)); + } + + } + } + + + private static String getStackTrace(final Throwable throwable) { + final StringWriter sw = new StringWriter(); + final PrintWriter pw = new PrintWriter(sw, true); + throwable.printStackTrace(pw); + return sw.getBuffer().toString(); + } + + + private static int getBackupDate(File backupDir, boolean exportMode) throws IOException { + fileNames.clear(); + int inputType; + + CLIIOHelpers.info("Select a backup to restore."); + + String[] fileNameArray = backupDir.list(); + if (fileNameArray == null || fileNameArray.length <= 0) { + throw new IOException(String.format("Selected backup directory %s is empty, or is a file!", backupDir.getAbsolutePath())); + } + ArrayList fileNameList = new ArrayList(Arrays.asList(fileNameArray)); //i need to do this. i hate this. + fileNameList.removeIf(name -> ( + name.endsWith("json") || + name.contains("incomplete") || + name.contains("DS_Store") + )); + + if (fileNameList.isEmpty()) { + throw new IOException(String.format("Selected backup directory %s is empty, or is a file!", backupDir.getAbsolutePath())); + } + + for (String fileName : CLIIOHelpers.sortStringsAlphabeticallyWithDirectoryPriority(fileNameList)) { + if (exportMode) { + if (fileName.endsWith("json")) continue; + if (fileName.contains("incomplete")) continue; + File file = new File(backupDir, fileName); + fileNames.add(file.getAbsolutePath()); + String out = file.getName(); + String[] outs = out.split("\\_"); + if (outs.length >= 2) { + out = ". " + outs[outs.length - 2] + "_" + outs[outs.length - 1]; + } else { + out = ". " + out; + } + CLIIOHelpers.info(fileNames.size() + out); + + } else { + if (fileName.endsWith("json")) continue; + if (fileName.contains("incomplete")) continue; + File file = new File(backupDir, fileName); + fileNames.add(file.getAbsolutePath()); + String out = file.getName(); + out = out.replaceAll(".zip", ""); + //out = out.replaceAll(worldPath + "_", ": "); + out = out.replaceAll("backup_", ": "); + out = out.replaceAll("-partial", "\u001B[33m partial\u001B[0m"); + out = out.replaceAll("-full", "\u001B[32m full\u001B[0m"); + CLIIOHelpers.info(fileNames.size() + out); + + } + } + + try { + String line = CLIIOHelpers.input.nextLine(); + if (line == "") { + CLIIOHelpers.warn("Please enter a number."); + return getBackupDate(backupDir, exportMode); + } + inputType = Integer.parseInt(line); + } catch (InputMismatchException | NumberFormatException e) { + CLIIOHelpers.warn("That was not a number. Please enter a number."); + return getBackupDate(backupDir, exportMode); + } + + if (inputType < 1 || inputType > fileNames.size()) { + CLIIOHelpers.warn("Please enter a number between " + fileNames.size() + "."); + return getBackupDate(backupDir, exportMode); + } + + return inputType - 1; + } + + + private static void restoreFullZip(int index, File worldFile) throws IllegalBackupException { + byte[] buffer = new byte[1024]; + //The most basic of the bunch. + ZipEntry entry; + try { + FileInputStream fileInputStream = new FileInputStream(fileNames.get(index)); + ZipInputStream zip = new ZipInputStream(fileInputStream); + while ((entry = zip.getNextEntry()) != null) { + File outputFile = new File(worldFile, entry.getName()); + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + zip.close(); + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + entry.getName()); + } + + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + + CLIIOHelpers.info("Restoring " + outputFile.getName()); + + FileOutputStream outputStream = new FileOutputStream(outputFile); + int length = 0; + while ((length = zip.read(buffer)) > 0) { + outputStream.write(buffer, 0, length); + } + outputStream.close(); + } + zip.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private static void restoreFullDifferential(int index, File worldFile) throws IllegalBackupException { + //Do we need to check for past backups? if selected is a full backup, we do not. + File backup = new File(fileNames.get(index)); + if (backup.getName().contains("-full")) { + if (backup.isFile()) { + restoreFullZip(index, worldFile); + return; + } + restoreFolder(index, worldFile); + return; + } + //find last FULL backup + for (int i = index; i >= 0; i--) { + String name = fileNames.get(i); + if (name.contains("-full")) { + CLIIOHelpers.info("Restoring last full backup..."); + File file = new File(name); + if (file.isFile()) { + restoreFullZip(i, worldFile); + } else { + restoreFolder(i, worldFile); + } + break; + } + } + CLIIOHelpers.info("\n\nRestoring selected backup..."); + if (backup.isFile()) { + restoreFullZip(index, worldFile); + } else { + restoreFolder(index, worldFile); + } + } + + private static void restoreFullIncremental(int index, File worldFile) throws IllegalBackupException { + //Do we need to check for past backups? if selected is a full backup, we do not. + File backup = new File(fileNames.get(index)); + if (backup.getName().contains("-full")) { + if (backup.isFile()) { + restoreFullZip(index, worldFile); + return; + } + restoreFolder(index, worldFile); + return; + } + //find last FULL backup + int i = index; + while (i >= 0) { + String name = fileNames.get(i); + if (name.contains("-full")) { + CLIIOHelpers.info("Restoring last full backup..."); + File file = new File(name); + if (file.isFile()) { + restoreFullZip(i, worldFile); + } else { + restoreFolder(i, worldFile); + } + break; + } + i--; + } + //restore backups up until the selected one + while (i < index) { + String name = fileNames.get(i); + CLIIOHelpers.info("Restoring chained backup..."); + File file = new File(name); + if (file.isFile()) { + restoreFullZip(i, worldFile); + } else { + restoreFolder(i, worldFile); + } + i++; + } + + CLIIOHelpers.info("\n\nRestoring selected backup..."); + if (backup.isFile()) { + restoreFullZip(index, worldFile); + } else { + restoreFolder(index, worldFile); + } + } + + private static void restorePartialZip(int index, File worldFile) throws IllegalBackupException { + HashMap filePaths = new HashMap<>(); + HashMap dates = new HashMap<>(); + HashMap entryOwners = new HashMap<>(); + + try { + File backup = new File(fileNames.get(index)); + addBackupNamesToLists(backup, entryOwners, filePaths, dates, "\u001B[32m"); + + for (String string : filePaths.keySet()) { + File outputFile = new File(worldFile, string); + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); + } + } + + ZipEntry select = ((ZipEntry) CLIIOHelpers.getFileToRestore(filePaths, "", worldFile)); + + File outputFile = new File(worldFile, select.toString()); + FileOutputStream outputStream = new FileOutputStream(outputFile); + + CLIIOHelpers.info("Restoring " + select.toString() + "..."); + + byte[] buffer = new byte[1028]; + InputStream inputSteam = entryOwners.get(select.toString()).getInputStream(select); + int length; + while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { + outputStream.write(buffer, 0, length); + } + outputStream.flush(); + outputStream.close(); + + } catch (IOException e) { + // TODO handle IOException + } + } + + private static void restorePartialDifferential(int index, File worldFile) throws IllegalBackupException { + //Do we need to check for past backups? if selected is a full backup, we do not. + HashMap filePaths = new HashMap<>(); + HashMap dates = new HashMap<>(); + HashMap entryOwners = new HashMap<>(); + try { + File backup = new File(fileNames.get(index)); + if (!backup.getName().contains("-full")) { + //find last FULL backup + for (int i = index; i >= 0; i--) { + String name = fileNames.get(i); + if (name.contains("-full")) { + addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); + break; + } + } + } + + File file = new File(fileNames.get(index)); + addBackupNamesToLists(file, entryOwners, filePaths, dates, "\u001B[32m"); + + HashMap properMapping = new HashMap<>(); + for (String date : dates.keySet()) { + properMapping.put( + date + " " + dates.get(date), + filePaths.get(date) + ); + } + + for (String string : filePaths.keySet()) { + File outputFile = new File(worldFile, string); + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); + } + } + + Object select = CLIIOHelpers.getFileToRestore(properMapping, "", worldFile); + if (select instanceof Path) { + Path input = (Path) select; + + + if (select.toString().replace("\\", "/").contains("-full/")) { + select = new File( + select.toString().replace("\\", "/") + .split("-full/")[1] + ).toPath(); + } + if (select.toString().replace("\\", "/").contains("-partial/")) { + select = new File( + select.toString().replace("\\", "/") + .split("-partial/")[1] + ).toPath(); + } + + File outputFile = new File(worldFile, select.toString()); + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); + } + + CLIIOHelpers.info("\n\nRestoring file : " + select); + Files.copy(input, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } else if (select instanceof ZipEntry) { + ZipEntry entry = (ZipEntry) select; + + File outputFile = new File(worldFile, entry.toString()); + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); + } + + FileOutputStream outputStream = new FileOutputStream(outputFile); + + CLIIOHelpers.info("Restoring " + entry.toString() + "..."); + + byte[] buffer = new byte[1028]; + InputStream inputSteam = entryOwners.get(entry.toString()).getInputStream(entry); + int length; + while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { + outputStream.write(buffer, 0, length); + } + outputStream.flush(); + outputStream.close(); + } + + + } catch (IOException e) { + //TODO : Scream at user + e.printStackTrace(); + } + } + + private static void restorePartialIncremental(int index, File worldFile) throws IllegalBackupException { + //Do we need to check for past backups? if selected is a full backup, we do not. + HashMap filePaths = new HashMap<>(); + HashMap dates = new HashMap<>(); + HashMap entryOwners = new HashMap<>(); + try { + File backup = new File(fileNames.get(index)); + if (!backup.getName().contains("-full")) { + int i; + //find last FULL backup + for (i = index; i >= 0; i--) { + String name = fileNames.get(i); + if (name.contains("-full")) { + addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); + break; + } + } + while (i < index) { + String name = fileNames.get(i); + addBackupNamesToLists(new File(name), entryOwners, filePaths, dates, "\u001b[31m"); + i++; + } + + } + + File file = new File(fileNames.get(index)); + addBackupNamesToLists(file, entryOwners, filePaths, dates, "\u001B[32m"); + + for (String string : filePaths.keySet()) { + File outputFile = new File(worldFile, string); + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + string); + } + } + + HashMap properMapping = new HashMap<>(); + for (String date : dates.keySet()) { + properMapping.put( + date + " " + dates.get(date), + filePaths.get(date) + ); + } + + Object select = CLIIOHelpers.getFileToRestore(properMapping, "", worldFile); + if (select instanceof Path) { + Path input = (Path) select; + + + if (select.toString().replace("\\", "/").contains("-full/")) { + select = new File( + select.toString().replace("\\", "/") + .split("-full/")[1] + ).toPath(); + } + if (select.toString().replace("\\", "/").contains("-partial/")) { + select = new File( + select.toString().replace("\\", "/") + .split("-partial/")[1] + ).toPath(); + } + + File outputFile = new File(worldFile, select.toString()); + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); + } + + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + CLIIOHelpers.info("\n\nRestoring file : " + select); + Files.copy(input, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } else if (select instanceof ZipEntry) { + ZipEntry entry = (ZipEntry) select; + + File outputFile = new File(worldFile, entry.toString()); + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + select.toString()); + } + + FileOutputStream outputStream = new FileOutputStream(outputFile); + + CLIIOHelpers.info("Restoring " + entry.toString() + "..."); + + byte[] buffer = new byte[1028]; + InputStream inputSteam = entryOwners.get(entry.toString()).getInputStream(entry); + int length; + while ((length = inputSteam.read(buffer, 0, buffer.length)) > 0) { + outputStream.write(buffer, 0, length); + } + outputStream.flush(); + outputStream.close(); + } + + + } catch (IOException e) { + //TODO : Scream at user + e.printStackTrace(); + } + } + + + private static void restoreFolder(int index, File worldFile) throws IllegalBackupException { + File backup = new File(fileNames.get(index)); + + try { + Files.walkFileTree(backup.toPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { + File source = backup.toPath().relativize(file).toFile(); + File outputFile = new File(worldFile, source.getPath()); + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + source.getPath()).printStackTrace(); + return FileVisitResult.TERMINATE; + } + + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + + CLIIOHelpers.info("Restoring " + outputFile.getName()); + Files.copy(file, outputFile.toPath()); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + private static void restoreOtherModZip(File backupDir) throws IllegalBackupException { + worldFile = serverDir; + Path file; + HashMap backups = new HashMap<>(); + HashMap entries = new HashMap<>(); + + for (File b : backupDir.getParentFile().listFiles()) { + if (b.getName().endsWith("zip")) { + backups.put(b.getName(), b); + } + } + + String backupName = CLIIOHelpers.getSelectionFromList("Select a backup to restore from.", new ArrayList(backups.keySet())); + + boolean fullWorld = "Whole world" == CLIIOHelpers.getSelectionFromList( + "Do you want to restore the whole world or a singular file?", + Arrays.asList(new String[]{"Whole world", "Single file"}) + ); + + if (!fullWorld) { + if (!CLIIOHelpers.confirmWarningMessage()) return; + + try { + FileSystem zipFs = FileSystems.newFileSystem(backups.get(backupName).toPath(), AdvancedBackupsCLI.class.getClassLoader()); + Path root = zipFs.getPath(""); + Files.walkFileTree(root, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { + entries.put(file.toString(), file); + return FileVisitResult.CONTINUE; + } + }); + + file = CLIIOHelpers.getFileToRestore(entries, "", worldFile); + CLIIOHelpers.info("Restoring " + file.toString() + "..."); + Path outputFile = new File(worldFile, file.toString()).toPath(); + + if (!outputFile.normalize().startsWith(worldFile.toPath())) { + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + file.toString()); + } + if (!outputFile.getParent().toFile().exists()) { + outputFile.getParent().toFile().mkdirs(); + } + Files.copy(file, outputFile, StandardCopyOption.REPLACE_EXISTING); + CLIIOHelpers.info("Done."); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + } else { + if (!CLIIOHelpers.confirmWarningMessage()) return; + + Path levelDatPath; + ArrayList levelDatPathWrapper = new ArrayList<>(); + + try { + FileSystem zipFs = FileSystems.newFileSystem(backups.get(backupName).toPath(), AdvancedBackupsCLI.class.getClassLoader()); + Path root = zipFs.getPath(""); + Files.walkFileTree(root, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { + if ("level.dat".equals(file.getFileName().toString())) levelDatPathWrapper.add(file); + return FileVisitResult.CONTINUE; + } + }); + + levelDatPath = levelDatPathWrapper.get(0); + CLIIOHelpers.info("Making backup of existing world..."); + CLIIOHelpers.info("Backup saved to : " + deleteEntireWorld(new File(worldFile, levelDatPath.getParent().toString()), false)); + byte[] buffer = new byte[1024]; + ZipEntry entry; + FileInputStream fileInputStream = new FileInputStream(backups.get(backupName)); + ZipInputStream zip = new ZipInputStream(fileInputStream); + while ((entry = zip.getNextEntry()) != null) { + File outputFile = new File(worldFile, entry.getName()); + + if (!outputFile.toPath().normalize().startsWith(worldFile.toPath())) { + zip.close(); + CLIIOHelpers.error("Found a potentially malicious zip file - cowardly exiting, restoration may be incomplete!"); + throw new IllegalBackupException("Zip file is likely malicious! Found an erroneus path: " + entry.getName()); + } + + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + + CLIIOHelpers.info("Restoring " + outputFile.toString() + "..."); + + FileOutputStream outputStream = new FileOutputStream(outputFile); + int length = 0; + while ((length = zip.read(buffer)) > 0) { + outputStream.write(buffer, 0, length); + } + outputStream.close(); + } + zip.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + CLIIOHelpers.info("Done."); + + } + + + private static String deleteEntireWorld(File worldDir, boolean exportMode) { + String ret = backupExistingWorld(worldDir, exportMode); + try { + Files.walkFileTree(worldDir.toPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { + file.toFile().delete(); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path file, java.io.IOException arg1) { + if (file.toFile().listFiles().length == 0) { + file.toFile().delete(); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + CLIIOHelpers.warn("Failed to delete file :"); + e.printStackTrace(); + } + return ret; + } + + private static String backupExistingWorld(File worldDir, boolean export) { + File out = new File(worldDir, "../cli" + serialiseBackupName(export ? "export" : "backup") + ".zip"); + try { + FileOutputStream outputStream = new FileOutputStream(out); + ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); + zipOutputStream.setLevel(4); + byte[] bytes = new byte[1024]; + Files.walkFileTree(worldDir.toPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { + Path targetFile; + try { + targetFile = worldDir.toPath().relativize(file); + if (targetFile.toFile().getName().compareTo("session.lock") == 0) { + return FileVisitResult.CONTINUE; + } + zipOutputStream.putNextEntry(new ZipEntry(targetFile.toString())); + FileInputStream is = new FileInputStream(file.toFile()); + while (true) { + int i = is.read(bytes); + if (i < 0) break; + zipOutputStream.write(bytes, 0, i); + } + is.close(); + //byte[] bytes = Files.readAllBytes(file); + //zipOutputStream.write(bytes, 0, bytes.length); + zipOutputStream.closeEntry(); + + } catch (IOException e) { + // TODO : Scream at user + e.printStackTrace(); + } + + return FileVisitResult.CONTINUE; + } + }); + zipOutputStream.flush(); + zipOutputStream.close(); + + CLIIOHelpers.info("Done."); + } catch (Exception e) { + // TODO Handle Exception + } + + return out.getName(); + } + + + private static void addBackupNamesToLists(File file, HashMap entryOwners, HashMap filePaths, + HashMap dates, String colour) throws IOException { + + if (file.isFile()) { + ZipFile zipFile = new ZipFile(file); + Enumeration entryEnum = zipFile.entries(); + + while (entryEnum.hasMoreElements()) { + ZipEntry entry = entryEnum.nextElement(); + + String backupName = file.toString().replace("\\", "/"); + filePaths.put(entry.toString().replace("\\", "/"), entry); + dates.put( + entry.toString().replace("\\", "/"), + "\u001b[31m" + + backupName + .substring(backupName.toString().lastIndexOf("/") + 1) + //.replace(worldPath + "_", "") + .replace("backup_", "") + .replace("-full.zip", "") + .replace("-partial.zip", "") + + "\u001B[0m" + ); + entryOwners.put(entry.toString(), zipFile); + } + } else { + Files.walkFileTree(file.toPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) throws IOException { + String backupName = file.toString().replace("\\", "/"); + filePaths.put(file.toPath().relativize(path).toString().replace("\\", "/"), path); + dates.put( + file.toPath().relativize(path).toString().replace("\\", "/"), + "\u001b[31m" + + backupName + .substring(backupName.toString().lastIndexOf("/") + 1) + //.replace(worldPath + "_", "") + .replace("backup_", "") + .replace("-full", "") + .replace("-partial", "") + + "\u001B[0m" + ); + return FileVisitResult.CONTINUE; + } + }); + } + } + + + public static String serialiseBackupName(String in) { + Date date = new Date(); + String pattern = "yyyy-MM-dd_HH-mm-ss"; + return in + "_" + new SimpleDateFormat(pattern).format(date); + } + +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/cli/CLIIOHelpers.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/cli/CLIIOHelpers.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/cli/CLIIOHelpers.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/cli/CLIIOHelpers.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java similarity index 94% rename from src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java index 2e42b8b4..f7964b4a 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/cli/IllegalBackupException.java @@ -1,9 +1,9 @@ -package co.uk.mommyheather.advancedbackups.cli; - -public class IllegalBackupException extends Exception { - - public IllegalBackupException(String string) { - super(string); - } - -} +package co.uk.mommyheather.advancedbackups.cli; + +public class IllegalBackupException extends Exception { + + public IllegalBackupException(String string) { + super(string); + } + +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java similarity index 97% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java index 363af620..6e57a4a5 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/ABCore.java @@ -1,123 +1,123 @@ -package co.uk.mommyheather.advancedbackups.core; - -import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; -import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; -import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParseException; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.function.Consumer; - -public class ABCore { - - public static String worldName; - public static Path worldDir; - public static Boolean activity = false; - - public static File modJar; - //version specific stuff will locate the mod's jar - - public static Consumer infoLogger; - public static Consumer warningLogger; - public static Consumer errorLogger; - - public static Runnable disableSaving; - public static Runnable enableSaving; - public static Consumer saveOnce; - - public static Runnable resetActivity; - - public static IClientContactor clientContactor; - - public static String backupPath; - - public static void disableSaving() { - if (ConfigManager.toggleSave.get()) disableSaving.run(); - } - - public static void enableSaving() { - //Technically there's an edgecase here where someone could let saving be disabled, then adjust + reload config to stop it being enabled again... - //but I don't know a good way to counter this right now and there's a failsafe on server boot regardless. - if (ConfigManager.toggleSave.get()) enableSaving.run(); - } - - public static void resetActivity() { - resetActivity.run(); - } - - public static void saveOnce() { - saveOnce.accept(ConfigManager.flush.get()); - } - - public static void setActivity(boolean in) { - if (in != activity) { - activity = in; - GsonBuilder builder = new GsonBuilder(); - Gson gson = builder.setPrettyPrinting().create(); - //i should thread this at some point - File file = new File(ABCore.backupPath); - File backupManifest = new File(file, "manifest.json"); - try { - if (backupManifest.exists()) { - try { - BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); - manifest.general.activity = activity; - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - } catch (JsonParseException e) { - ABCore.errorLogger.accept("Malformed backup manifest! Overwriting, meaning next backup has to be a full backup..."); - ABCore.logStackTrace(e); - - BackupManifest manifest = BackupManifest.defaults(); - manifest.general.activity = activity; - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - } - } else { - BackupManifest manifest = BackupManifest.defaults(); - manifest.general.activity = activity; - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - } - } catch (IOException e) { - ABCore.errorLogger.accept("Error writing player activty to backup manifest!!"); - ABCore.logStackTrace(e); - } - } - } - - - public static String serialiseBackupName(String in) { - Date date = new Date(); - String pattern = "yyyy-MM-dd_HH-mm-ss"; - return in + "_" + new SimpleDateFormat(pattern).format(date); - } - - public static void logStackTrace(Exception e) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - - ABCore.errorLogger.accept(sw.toString()); - pw.close(); - } -} +package co.uk.mommyheather.advancedbackups.core; + +import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.function.Consumer; + +public class ABCore { + + public static String worldName; + public static Path worldDir; + public static Boolean activity = false; + + public static File modJar; + //version specific stuff will locate the mod's jar + + public static Consumer infoLogger; + public static Consumer warningLogger; + public static Consumer errorLogger; + + public static Runnable disableSaving; + public static Runnable enableSaving; + public static Consumer saveOnce; + + public static Runnable resetActivity; + + public static IClientContactor clientContactor; + + public static String backupPath; + + public static void disableSaving() { + if (ConfigManager.toggleSave.get()) disableSaving.run(); + } + + public static void enableSaving() { + //Technically there's an edgecase here where someone could let saving be disabled, then adjust + reload config to stop it being enabled again... + //but I don't know a good way to counter this right now and there's a failsafe on server boot regardless. + if (ConfigManager.toggleSave.get()) enableSaving.run(); + } + + public static void resetActivity() { + resetActivity.run(); + } + + public static void saveOnce() { + saveOnce.accept(ConfigManager.flush.get()); + } + + public static void setActivity(boolean in) { + if (in != activity) { + activity = in; + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.setPrettyPrinting().create(); + //i should thread this at some point + File file = new File(ABCore.backupPath); + File backupManifest = new File(file, "manifest.json"); + try { + if (backupManifest.exists()) { + try { + BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); + manifest.general.activity = activity; + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + } catch (JsonParseException e) { + ABCore.errorLogger.accept("Malformed backup manifest! Overwriting, meaning next backup has to be a full backup..."); + ABCore.logStackTrace(e); + + BackupManifest manifest = BackupManifest.defaults(); + manifest.general.activity = activity; + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + } + } else { + BackupManifest manifest = BackupManifest.defaults(); + manifest.general.activity = activity; + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + } + } catch (IOException e) { + ABCore.errorLogger.accept("Error writing player activty to backup manifest!!"); + ABCore.logStackTrace(e); + } + } + } + + + public static String serialiseBackupName(String in) { + Date date = new Date(); + String pattern = "yyyy-MM-dd_HH-mm-ss"; + return in + "_" + new SimpleDateFormat(pattern).format(date); + } + + public static void logStackTrace(Exception e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + + ABCore.errorLogger.accept(sw.toString()); + pw.close(); + } +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java similarity index 95% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java index e2fd6df1..94a1fd06 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/CoreCommandSystem.java @@ -1,112 +1,112 @@ -package co.uk.mommyheather.advancedbackups.core; - -import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; -import co.uk.mommyheather.advancedbackups.core.backups.ThreadedBackup; -import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; -import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; -import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParseException; - -import java.io.File; -import java.io.FileWriter; -import java.nio.file.Files; -import java.util.function.Consumer; - -public class CoreCommandSystem { - private static GsonBuilder builder = new GsonBuilder(); - private static Gson gson; - - static { - builder.setPrettyPrinting(); - gson = builder.create(); - } - - - //These methods are all called by relevant command classes in version specific code - public static void startBackup(Consumer chat) { - chat.accept("Starting backup..."); - BackupWrapper.checkBackups(); //makes sure the backups folder is present etc - if (ThreadedBackup.running) { - chat.accept("Cannot start a backup whilst a backup is already running!"); - return; - } - BackupWrapper.makeSingleBackup(0, chat, false); - } - - public static void reloadConfig(Consumer chat) { - chat.accept("Reloading config..."); - ConfigManager.loadOrCreateConfig(); - chat.accept("Done!"); - } - - public static void reloadClientConfig(Consumer chat) { - chat.accept("Reloading client config..."); - ClientConfigManager.loadOrCreateConfig(); - chat.accept("Done!"); - } - - public static void snapshot(Consumer chat) { - BackupWrapper.checkBackups(); //makes sure the backups folder is present etc - if (ThreadedBackup.running) { - chat.accept("Cannot start a snapshot whilst a backup is already running!"); - return; - } - BackupWrapper.makeSnapshot(chat); - } - - public static void resetChainLength(Consumer chat) { - chat.accept("Resetting chain length... The next backup will be a complete backup."); - try { - - File file = new File(ABCore.backupPath); - File backupManifest = new File(file, "manifest.json"); - if (backupManifest.exists()) { - try { - BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); - - manifest.incremental.chainLength += (int) ConfigManager.length.get(); - manifest.differential.chainLength += (int) ConfigManager.length.get(); - - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - - } catch (JsonParseException e) { - chat.accept("Malformed backup manifest! Will be completely replaced, with no side effects..."); - chat.accept("Check logs for more info."); - ABCore.logStackTrace(e); - - BackupManifest manifest = BackupManifest.defaults(); - - //don't actually need to set them here - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - - } - } else { - chat.accept("No manifest file exists."); - } - } catch (Exception e) { - chat.accept("Error resetting chain length. - check logs for more info."); - ABCore.logStackTrace(e); - } - } - - public static void cancelBackup(Consumer chat) { - chat.accept("Cancelling ongoing backup if one exists..."); - chat.accept("This may fail or take a while depending on backup stage."); - Thread.getAllStackTraces().keySet().forEach((thread) -> { - if (thread instanceof ThreadedBackup) { - thread.interrupt(); - } - }); - - } -} +package co.uk.mommyheather.advancedbackups.core; + +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.backups.ThreadedBackup; +import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; + +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.function.Consumer; + +public class CoreCommandSystem { + private static GsonBuilder builder = new GsonBuilder(); + private static Gson gson; + + static { + builder.setPrettyPrinting(); + gson = builder.create(); + } + + + //These methods are all called by relevant command classes in version specific code + public static void startBackup(Consumer chat) { + chat.accept("Starting backup..."); + BackupWrapper.checkBackups(); //makes sure the backups folder is present etc + if (ThreadedBackup.running) { + chat.accept("Cannot start a backup whilst a backup is already running!"); + return; + } + BackupWrapper.makeSingleBackup(0, chat, false); + } + + public static void reloadConfig(Consumer chat) { + chat.accept("Reloading config..."); + ConfigManager.loadOrCreateConfig(); + chat.accept("Done!"); + } + + public static void reloadClientConfig(Consumer chat) { + chat.accept("Reloading client config..."); + ClientConfigManager.loadOrCreateConfig(); + chat.accept("Done!"); + } + + public static void snapshot(Consumer chat) { + BackupWrapper.checkBackups(); //makes sure the backups folder is present etc + if (ThreadedBackup.running) { + chat.accept("Cannot start a snapshot whilst a backup is already running!"); + return; + } + BackupWrapper.makeSnapshot(chat); + } + + public static void resetChainLength(Consumer chat) { + chat.accept("Resetting chain length... The next backup will be a complete backup."); + try { + + File file = new File(ABCore.backupPath); + File backupManifest = new File(file, "manifest.json"); + if (backupManifest.exists()) { + try { + BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); + + manifest.incremental.chainLength += (int) ConfigManager.length.get(); + manifest.differential.chainLength += (int) ConfigManager.length.get(); + + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + + } catch (JsonParseException e) { + chat.accept("Malformed backup manifest! Will be completely replaced, with no side effects..."); + chat.accept("Check logs for more info."); + ABCore.logStackTrace(e); + + BackupManifest manifest = BackupManifest.defaults(); + + //don't actually need to set them here + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + + } + } else { + chat.accept("No manifest file exists."); + } + } catch (Exception e) { + chat.accept("Error resetting chain length. - check logs for more info."); + ABCore.logStackTrace(e); + } + } + + public static void cancelBackup(Consumer chat) { + chat.accept("Cancelling ongoing backup if one exists..."); + chat.accept("This may fail or take a while depending on backup stage."); + Thread.getAllStackTraces().keySet().forEach(thread -> { + if (thread instanceof ThreadedBackup) { + thread.interrupt(); + } + }); + + } +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupCheckEnum.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupCheckEnum.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupCheckEnum.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupCheckEnum.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java similarity index 90% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java index a381c9c4..1608f365 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupStatusInstance.java @@ -1,79 +1,79 @@ -package co.uk.mommyheather.advancedbackups.core.backups; - -public class BackupStatusInstance { - - private static BackupStatusInstance instance = null; - - private State state = State.INVALID; - - private int progress = -1; - private int max = -1; - - private long age; - - - public static synchronized void setInstance(BackupStatusInstance instance) { - BackupStatusInstance.instance = instance; - } - - public static synchronized BackupStatusInstance getInstanceCopy() { - return instance == null ? null : copyInstance(instance); - } - - private static synchronized BackupStatusInstance copyInstance(BackupStatusInstance in) { - BackupStatusInstance other = new BackupStatusInstance(); - other.setMax(in.getMax()); - other.setProgress(in.getProgress()); - other.setState(in.getState()); - other.setAge(in.getAge()); - return other; - - } - - public State getState() { - return state; - } - - public void setState(State state) { - this.state = state; - } - - public int getProgress() { - return progress; - } - - public void setProgress(int progress) { - this.progress = progress; - } - - public int getMax() { - return max; - } - - public void setMax(int max) { - this.max = max; - } - - public long getAge() { - return age; - } - - public void setAge(long age) { - this.age = age; - } - - - public static enum State { - - STARTING, - STARTED, - COMPLETE, - FAILED, - CANCELLED, - //This one's used for a default, and should never be encountered! - INVALID; - - } - - -} +package co.uk.mommyheather.advancedbackups.core.backups; + +public class BackupStatusInstance { + + private static BackupStatusInstance instance = null; + + private State state = State.INVALID; + + private int progress = -1; + private int max = -1; + + private long age; + + + public static synchronized void setInstance(BackupStatusInstance instance) { + BackupStatusInstance.instance = instance; + } + + public static synchronized BackupStatusInstance getInstanceCopy() { + return instance == null ? null : copyInstance(instance); + } + + private static synchronized BackupStatusInstance copyInstance(BackupStatusInstance in) { + BackupStatusInstance other = new BackupStatusInstance(); + other.setMax(in.getMax()); + other.setProgress(in.getProgress()); + other.setState(in.getState()); + other.setAge(in.getAge()); + return other; + + } + + public State getState() { + return this.state; + } + + public void setState(State state) { + this.state = state; + } + + public int getProgress() { + return this.progress; + } + + public void setProgress(int progress) { + this.progress = progress; + } + + public int getMax() { + return this.max; + } + + public void setMax(int max) { + this.max = max; + } + + public long getAge() { + return this.age; + } + + public void setAge(long age) { + this.age = age; + } + + + public static enum State { + + STARTING, + STARTED, + COMPLETE, + FAILED, + CANCELLED, + //This one's used for a default, and should never be encountered! + INVALID; + + } + + +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java similarity index 97% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java index 4b1b3013..a914d558 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupTimer.java @@ -130,4 +130,4 @@ private static void checkLogging() { } -} +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java similarity index 97% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java index 3d9b3b67..f811a79f 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/BackupWrapper.java @@ -1,539 +1,539 @@ -package co.uk.mommyheather.advancedbackups.core.backups; - -import co.uk.mommyheather.advancedbackups.core.ABCore; -import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; -import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParseException; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Date; -import java.util.function.Consumer; - -public class BackupWrapper { - - private static GsonBuilder builder = new GsonBuilder(); - private static Gson gson = builder.setPrettyPrinting().create(); - - public static ArrayList configuredPlaytime = new ArrayList<>(); - - - public static void checkStartupBackups() { - //do it here to prevent excess file i/o and reduce work needed for support on a version-by-version basis - prepareBackupDestination(); - ABCore.enableSaving(); - - File file = new File(ABCore.backupPath); - File backupManifest = new File(file, "manifest.json"); - if (backupManifest.exists()) { - try { - try { - BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); - ABCore.activity = manifest.general.activity; - } catch (JsonParseException e) { - ABCore.errorLogger.accept("Malformed backup manifest! Will be completely replaced, and will assume player activity has changed..."); - - BackupManifest manifest = BackupManifest.defaults(); - manifest.general.activity = true; - ABCore.activity = true; - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - - } - } catch (IOException e) { - ABCore.errorLogger.accept("Error reading player actiivty from backup manifest!!"); - ABCore.logStackTrace(e); - } - } - - if (ConfigManager.startup.get()) { - checkAndMakeBackups(Math.max(5000, ConfigManager.delay.get() * 1000), false); - } - - //new BackupTimingThread().start(); - } - - public static void checkShutdownBackups() { - if (ConfigManager.shutdown.get()) { - checkAndMakeBackups(0, true); - } - } - - public static void checkAndMakeBackups(long delay, boolean shutdown) { - BackupCheckEnum e = checkBackups(); - if (e.success()) { - makeSingleBackup(delay, shutdown); - } - } - - public static void checkAndMakeBackups() { - checkAndMakeBackups(0, false); - } - - - public static BackupCheckEnum checkBackups() { - //We shouldn't need this anymore. - //prepareBackupDestination(); - if (!ConfigManager.enabled.get()) return BackupCheckEnum.DISABLED; - if (ConfigManager.activity.get() && !ABCore.activity) return BackupCheckEnum.NOACTIVITY; - if (checkMostRecentBackup()) return BackupCheckEnum.TOORECENT; - - return BackupCheckEnum.SUCCESS; - } - - private static void prepareBackupDestination() { - File file = new File(ABCore.backupPath); - - if (!file.exists()) { - file.mkdirs(); - } - prepareReadMe(file); - prepareRestorationScripts(file); - - File zipFile = new File(file, "/zips/"); - if (!zipFile.exists()) { - zipFile.mkdirs(); - } - File differential = new File(file, "/differential/"); - if (!differential.exists()) { - differential.mkdirs(); - } - File incremental = new File(file, "/incremental/"); - if (!incremental.exists()) { - incremental.mkdirs(); - } - File snapshots = new File(file, "/snapshots/"); - if (!snapshots.exists()) { - snapshots.mkdirs(); - } - - File backupManifest = new File(file, "manifest.json"); - if (!backupManifest.exists()) { - try { - backupManifest.createNewFile(); - BackupManifest manifest = BackupManifest.defaults(); - - //NOW DISABLED - CODE FOR MIGRATING FROM 1.X TO 2.X, BUT IS USELESS IN 3.X - /* - File differentialManifest = new File(file, "/differential/manifest.json"); - if (differentialManifest.exists()) { - try { - DifferentialManifest differentialManifest2 = gson.fromJson(new String(Files.readAllBytes(differentialManifest.toPath())), DifferentialManifest.class); - manifest.differential.setChainLength(differentialManifest2.getChain()); - manifest.differential.setLastBackup(differentialManifest2.getLastFull()); - - differentialManifest.delete(); - - } catch (IOException e) { - - } - } - - File incrementalManifest = new File(file, "/incremental/manifest.json"); - if (incrementalManifest.exists()) { - try { - DifferentialManifest incrementalManifest2 = gson.fromJson(new String(Files.readAllBytes(incrementalManifest.toPath())), DifferentialManifest.class); - manifest.incremental.setChainLength(incrementalManifest2.getChain()); - manifest.incremental.setLastBackup(incrementalManifest2.getLastFull()); - - incrementalManifest.delete(); - - } catch (IOException e) { - - } - }*/ - - - FileWriter writer = new FileWriter(backupManifest); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - } catch (IOException e) { - ABCore.errorLogger.accept("Error initialising backup manifest!!"); - ABCore.logStackTrace(e); - } - } - } - - private static void prepareReadMe(File path) { - File readme = new File(path.getParent(), "README-BEFORE-RESTORING.txt"); - if (!readme.exists()) { - try { - InputStream is = BackupWrapper.class.getClassLoader().getResourceAsStream("advancedbackups-readme.txt"); - readme.createNewFile(); - FileOutputStream outputStream = new FileOutputStream(readme); - byte[] buf = new byte[1028]; - int n; - while ((n = is.read(buf)) > 0) { - outputStream.write(buf, 0, n); - } - outputStream.flush(); - outputStream.close(); - - } catch (IOException e) { - // TODO Auto-generated catch block - ABCore.logStackTrace(e); - } - - } - } - - //generate scripts that run the `java -jar` command. - private static void prepareRestorationScripts(File path) { - try { - String dir = ABCore.modJar.getParent(); - String file = ABCore.modJar.getName(); - - File script = new File(path.getParent(), "restore-script.bat"); - if (script.exists()) script.delete(); - script.createNewFile(); - - FileWriter writer = new FileWriter(script); - writer.append("@echo off \n"); - writer.append(dir.charAt(0) + ":\n"); // command prompt defaults to c:, if this is wrong then we need to change drive. - writer.append("cd \"" + dir + "\"\n"); - writer.append("java -jar \"" + file + "\"\n"); - writer.append("pause\n"); //simply prompts the user to press any key to continue, so they can see the confirmation message or error message - - writer.flush(); - writer.close(); - - script = new File(path.getParent(), "restore-script.sh"); - if (script.exists()) script.delete(); - script.createNewFile(); - - writer = new FileWriter(script); - writer.append("cd \"" + dir + "\"\n"); - writer.append("java -jar \"" + file + "\"\n"); - - writer.flush(); - writer.close(); - } catch (IOException e) { - ABCore.errorLogger.accept("Error writing restoration scripts! Manual running of jar will still work."); - ABCore.logStackTrace(e); - } - - } - - - public static boolean checkMostRecentBackup() { - // Return true if the time difference between the most recent backup and the backup-to-be - // is less than specified in the config. - - if (ConfigManager.minFrequency.get() <= 0) return false; - - Date date = new Date(); - long configVal = (long) (3600000F * ConfigManager.minFrequency.get()); - return (date.getTime() - mostRecentBackupTime()) < configVal; - } - - - public static long mostRecentBackupTime() { - - File directory = new File(ABCore.backupPath); - - switch (ConfigManager.type.get()) { - case "zip": { - directory = new File(directory, "/zips/"); - break; - } - case "differential": { - directory = new File(directory, "/differential/"); - break; - } - case "incremental": { - directory = new File(directory, "/incremental/"); - break; - } - - } - - File[] files = directory.listFiles(); - long lastModifiedTime = Long.MIN_VALUE; - if (files == null || files.length == 0) return 0L; - for (File file : files) { - if (file.lastModified() > lastModifiedTime && !file.getName().contains("manifest")) { - lastModifiedTime = file.lastModified(); - } - } - return lastModifiedTime; - } - - - public static void makeSingleBackup(long delay, boolean shutdown) { - makeSingleBackup(delay, s -> {}, shutdown); - } - - public static void makeSingleBackup(long delay, Consumer output, boolean shutdown) { - try { - if (!shutdown) { - ABCore.disableSaving(); - if (ConfigManager.save.get()) { - ABCore.saveOnce(); - } - } - } catch (Exception e) { - ABCore.errorLogger.accept("Error saving or disabling saving!"); - ABCore.logStackTrace(e); - } - - if (ThreadedBackup.running) { - ABCore.errorLogger.accept("Backup already running!"); - ABCore.logStackTrace(new Exception()); - return; - } - // Make new thread, run backup utility. - ThreadedBackup.running = true; - ThreadedBackup threadedBackup = new ThreadedBackup(delay, output); - if (shutdown) threadedBackup.shutdown(); - - threadedBackup.start(); - // Don't re-enable saving - leave that down to the backup thread. - } - - public static void makeSnapshot(Consumer output) { - ABCore.disableSaving(); - if (ConfigManager.save.get()) { - ABCore.saveOnce(); - } - - if (ThreadedBackup.running) { - ABCore.errorLogger.accept("Backup already running!"); - ABCore.logStackTrace(new Exception()); - return; - } - - ThreadedBackup.running = true; - ThreadedBackup threadedBackup = new ThreadedBackup(0, output); - threadedBackup.snapshot(); - threadedBackup.start(); - - } - - public static void finishBackup(boolean snapshot) { - ABCore.resetActivity(); - - if (snapshot) return; - - File directory = new File(ABCore.backupPath); - switch (ConfigManager.type.get()) { - case "zip": { - directory = new File(directory, "/zips/"); - break; - } - case "differential": { - directory = new File(directory, "/differential/"); - break; - } - case "incremental": { - directory = new File(directory, "/incremental/"); - break; - } - } - - checkSize(directory); - checkCount(directory); - checkDates(directory); - } - - public static File getDependent(File in) { - File file = getFirstBackupAfterDate(in.getParentFile(), in.lastModified()); - if (file.getName().contains("-partial")) return file; - return null; - } - - - public static long calculateDirectorySize(File directory) { - long size = 0; - File[] files = directory.listFiles(); - if (files == null || files.length == 0) return size; - for (File file : files) { - if (file.isFile()) { - size += file.length(); - } else { - size += calculateDirectorySize(file); - } - } - return size; - } - - public static File getFirstBackupAfterDate(File directory, long date) { - File[] files = directory.listFiles(); - File oldestFile = null; - long currentDate = Long.MAX_VALUE; - if (files == null || files.length == 0) return null; - for (File file : files) { - if (file.lastModified() < currentDate && file.lastModified() > date) { - currentDate = file.lastModified(); - oldestFile = file; - } - } - - return oldestFile; - } - - public static int calculateChainCount(File directory) { - int count = 0; - File[] files = directory.listFiles(); - if (files == null || files.length == 0) return 0; - for (File file : files) { - if (file.getName().contains("full")) { - count++; - } - } - return count; - } - - - public static int calculateBackupCount(File directory) { - File[] files = directory.listFiles(); - if (files == null || files.length == 0) return 0; - return files.length; - } - - private static void checkCount(File directory) { - if (ConfigManager.backupsToKeep.get() <= 0) { - return; - } - - long date = 0; - while (true) { - if (calculateBackupCount(directory) <= ConfigManager.backupsToKeep.get()) { - return; - } - - File file = getFirstBackupAfterDate(directory, date); - File dependent = getDependent(file); - if (dependent == null) { - //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. - } else { - //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain - if ("incremental".equals(ConfigManager.type.get())) { - if (!ConfigManager.purgeIncrementals.get()) return; - if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; - - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - while ((dependent = getDependent(dependent)) != null) { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } else { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } - } - } - - private static void checkDates(File directory) { - if (ConfigManager.daysToKeep.get() <= 0) return; - - long date = 0; - long comp = mostRecentBackupTime(); - while (true) { - File file = getFirstBackupAfterDate(directory, date); - date = file.lastModified(); - long days = (comp - date) / 86400000L; - if (days <= ConfigManager.daysToKeep.get()) return; - File dependent = getDependent(file); - if (dependent == null) { - //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. - } else { - date = dependent.lastModified(); - days = (comp - date) / 86400000L; - if (days <= ConfigManager.daysToKeep.get()) return; - //don't purge unless the dependent is also eligible for deletion - - //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain - if ("incremental".equals(ConfigManager.type.get())) { - if (!ConfigManager.purgeIncrementals.get()) return; - if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; - - //now we need to make sure the entire chain meets the deletion date. - //is this a good strategy? - while ((dependent = getDependent(dependent)) != null) { - date = dependent.lastModified(); - days = (comp - date) / 86400000L; - if (days <= ConfigManager.daysToKeep.get()) return; - } - dependent = getDependent(file); - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - while ((dependent = getDependent(dependent)) != null) { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } else { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } - } - } - - private static void checkSize(File directory) { - if (ConfigManager.size.get() <= 0F) return; - if (calculateDirectorySize(directory) < ConfigManager.size.get() * 1000000000L) return; - long date = 0; - while (true) { - if (calculateDirectorySize(directory) < ConfigManager.size.get() * 1000000000L) return; - File file = getFirstBackupAfterDate(directory, date); - File dependent = getDependent(file); - if (dependent == null) { - //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. - } else { - //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain - if ("incremental".equals(ConfigManager.type.get())) { - if (!ConfigManager.purgeIncrementals.get()) return; - if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; - - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - while ((dependent = getDependent(dependent)) != null) { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } else { - if (dependent.isDirectory()) deleteDirectoryContents(dependent); - dependent.delete(); - } - } - } - - } - - private static void deleteDirectoryContents(File directory) { - if (!directory.isDirectory()) return; //safety check - for (File file : directory.listFiles()) { - if (file.isDirectory()) deleteDirectoryContents(file); - file.delete(); - } - } - -} +package co.uk.mommyheather.advancedbackups.core.backups; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Date; +import java.util.function.Consumer; + +public class BackupWrapper { + + private static GsonBuilder builder = new GsonBuilder(); + private static Gson gson = builder.setPrettyPrinting().create(); + + public static ArrayList configuredPlaytime = new ArrayList<>(); + + + public static void checkStartupBackups() { + //do it here to prevent excess file i/o and reduce work needed for support on a version-by-version basis + prepareBackupDestination(); + ABCore.enableSaving(); + + File file = new File(ABCore.backupPath); + File backupManifest = new File(file, "manifest.json"); + if (backupManifest.exists()) { + try { + try { + BackupManifest manifest = gson.fromJson(new String(Files.readAllBytes(backupManifest.toPath())), BackupManifest.class); + ABCore.activity = manifest.general.activity; + } catch (JsonParseException e) { + ABCore.errorLogger.accept("Malformed backup manifest! Will be completely replaced, and will assume player activity has changed..."); + + BackupManifest manifest = BackupManifest.defaults(); + manifest.general.activity = true; + ABCore.activity = true; + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + + } + } catch (IOException e) { + ABCore.errorLogger.accept("Error reading player actiivty from backup manifest!!"); + ABCore.logStackTrace(e); + } + } + + if (ConfigManager.startup.get()) { + checkAndMakeBackups(Math.max(5000, ConfigManager.delay.get() * 1000), false); + } + + //new BackupTimingThread().start(); + } + + public static void checkShutdownBackups() { + if (ConfigManager.shutdown.get()) { + checkAndMakeBackups(0, true); + } + } + + public static void checkAndMakeBackups(long delay, boolean shutdown) { + BackupCheckEnum e = checkBackups(); + if (e.success()) { + makeSingleBackup(delay, shutdown); + } + } + + public static void checkAndMakeBackups() { + checkAndMakeBackups(0, false); + } + + + public static BackupCheckEnum checkBackups() { + //We shouldn't need this anymore. + //prepareBackupDestination(); + if (!ConfigManager.enabled.get()) return BackupCheckEnum.DISABLED; + if (ConfigManager.activity.get() && !ABCore.activity) return BackupCheckEnum.NOACTIVITY; + if (checkMostRecentBackup()) return BackupCheckEnum.TOORECENT; + + return BackupCheckEnum.SUCCESS; + } + + private static void prepareBackupDestination() { + File file = new File(ABCore.backupPath); + + if (!file.exists()) { + file.mkdirs(); + } + prepareReadMe(file); + prepareRestorationScripts(file); + + File zipFile = new File(file, "/zips/"); + if (!zipFile.exists()) { + zipFile.mkdirs(); + } + File differential = new File(file, "/differential/"); + if (!differential.exists()) { + differential.mkdirs(); + } + File incremental = new File(file, "/incremental/"); + if (!incremental.exists()) { + incremental.mkdirs(); + } + File snapshots = new File(file, "/snapshots/"); + if (!snapshots.exists()) { + snapshots.mkdirs(); + } + + File backupManifest = new File(file, "manifest.json"); + if (!backupManifest.exists()) { + try { + backupManifest.createNewFile(); + BackupManifest manifest = BackupManifest.defaults(); + + //NOW DISABLED - CODE FOR MIGRATING FROM 1.X TO 2.X, BUT IS USELESS IN 3.X + /* + File differentialManifest = new File(file, "/differential/manifest.json"); + if (differentialManifest.exists()) { + try { + DifferentialManifest differentialManifest2 = gson.fromJson(new String(Files.readAllBytes(differentialManifest.toPath())), DifferentialManifest.class); + manifest.differential.setChainLength(differentialManifest2.getChain()); + manifest.differential.setLastBackup(differentialManifest2.getLastFull()); + + differentialManifest.delete(); + + } catch (IOException e) { + + } + } + + File incrementalManifest = new File(file, "/incremental/manifest.json"); + if (incrementalManifest.exists()) { + try { + DifferentialManifest incrementalManifest2 = gson.fromJson(new String(Files.readAllBytes(incrementalManifest.toPath())), DifferentialManifest.class); + manifest.incremental.setChainLength(incrementalManifest2.getChain()); + manifest.incremental.setLastBackup(incrementalManifest2.getLastFull()); + + incrementalManifest.delete(); + + } catch (IOException e) { + + } + }*/ + + + FileWriter writer = new FileWriter(backupManifest); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + } catch (IOException e) { + ABCore.errorLogger.accept("Error initialising backup manifest!!"); + ABCore.logStackTrace(e); + } + } + } + + private static void prepareReadMe(File path) { + File readme = new File(path.getParent(), "README-BEFORE-RESTORING.txt"); + if (!readme.exists()) { + try { + InputStream is = BackupWrapper.class.getClassLoader().getResourceAsStream("advancedbackups-readme.txt"); + readme.createNewFile(); + FileOutputStream outputStream = new FileOutputStream(readme); + byte[] buf = new byte[1028]; + int n; + while ((n = is.read(buf)) > 0) { + outputStream.write(buf, 0, n); + } + outputStream.flush(); + outputStream.close(); + + } catch (IOException e) { + // TODO Auto-generated catch block + ABCore.logStackTrace(e); + } + + } + } + + //generate scripts that run the `java -jar` command. + private static void prepareRestorationScripts(File path) { + try { + String dir = ABCore.modJar.getParent(); + String file = ABCore.modJar.getName(); + + File script = new File(path.getParent(), "restore-script.bat"); + if (script.exists()) script.delete(); + script.createNewFile(); + + FileWriter writer = new FileWriter(script); + writer.append("@echo off \n"); + writer.append(dir.charAt(0) + ":\n"); // command prompt defaults to c:, if this is wrong then we need to change drive. + writer.append("cd \"" + dir + "\"\n"); + writer.append("java -jar \"" + file + "\"\n"); + writer.append("pause\n"); //simply prompts the user to press any key to continue, so they can see the confirmation message or error message + + writer.flush(); + writer.close(); + + script = new File(path.getParent(), "restore-script.sh"); + if (script.exists()) script.delete(); + script.createNewFile(); + + writer = new FileWriter(script); + writer.append("cd \"" + dir + "\"\n"); + writer.append("java -jar \"" + file + "\"\n"); + + writer.flush(); + writer.close(); + } catch (IOException e) { + ABCore.errorLogger.accept("Error writing restoration scripts! Manual running of jar will still work."); + ABCore.logStackTrace(e); + } + + } + + + public static boolean checkMostRecentBackup() { + // Return true if the time difference between the most recent backup and the backup-to-be + // is less than specified in the config. + + if (ConfigManager.minFrequency.get() <= 0) return false; + + Date date = new Date(); + long configVal = (long) (3600000F * ConfigManager.minFrequency.get()); + return (date.getTime() - mostRecentBackupTime()) < configVal; + } + + + public static long mostRecentBackupTime() { + + File directory = new File(ABCore.backupPath); + + switch (ConfigManager.type.get()) { + case "zip": { + directory = new File(directory, "/zips/"); + break; + } + case "differential": { + directory = new File(directory, "/differential/"); + break; + } + case "incremental": { + directory = new File(directory, "/incremental/"); + break; + } + + } + + File[] files = directory.listFiles(); + long lastModifiedTime = Long.MIN_VALUE; + if (files == null || files.length == 0) return 0L; + for (File file : files) { + if (file.lastModified() > lastModifiedTime && !file.getName().contains("manifest")) { + lastModifiedTime = file.lastModified(); + } + } + return lastModifiedTime; + } + + + public static void makeSingleBackup(long delay, boolean shutdown) { + makeSingleBackup(delay, s -> {}, shutdown); + } + + public static void makeSingleBackup(long delay, Consumer output, boolean shutdown) { + try { + if (!shutdown) { + ABCore.disableSaving(); + if (ConfigManager.save.get()) { + ABCore.saveOnce(); + } + } + } catch (Exception e) { + ABCore.errorLogger.accept("Error saving or disabling saving!"); + ABCore.logStackTrace(e); + } + + if (ThreadedBackup.running) { + ABCore.errorLogger.accept("Backup already running!"); + ABCore.logStackTrace(new Exception()); + return; + } + // Make new thread, run backup utility. + ThreadedBackup.running = true; + ThreadedBackup threadedBackup = new ThreadedBackup(delay, output); + if (shutdown) threadedBackup.shutdown(); + + threadedBackup.start(); + // Don't re-enable saving - leave that down to the backup thread. + } + + public static void makeSnapshot(Consumer output) { + ABCore.disableSaving(); + if (ConfigManager.save.get()) { + ABCore.saveOnce(); + } + + if (ThreadedBackup.running) { + ABCore.errorLogger.accept("Backup already running!"); + ABCore.logStackTrace(new Exception()); + return; + } + + ThreadedBackup.running = true; + ThreadedBackup threadedBackup = new ThreadedBackup(0, output); + threadedBackup.snapshot(); + threadedBackup.start(); + + } + + public static void finishBackup(boolean snapshot) { + ABCore.resetActivity(); + + if (snapshot) return; + + File directory = new File(ABCore.backupPath); + switch (ConfigManager.type.get()) { + case "zip": { + directory = new File(directory, "/zips/"); + break; + } + case "differential": { + directory = new File(directory, "/differential/"); + break; + } + case "incremental": { + directory = new File(directory, "/incremental/"); + break; + } + } + + checkSize(directory); + checkCount(directory); + checkDates(directory); + } + + public static File getDependent(File in) { + File file = getFirstBackupAfterDate(in.getParentFile(), in.lastModified()); + if (file.getName().contains("-partial")) return file; + return null; + } + + + public static long calculateDirectorySize(File directory) { + long size = 0; + File[] files = directory.listFiles(); + if (files == null || files.length == 0) return size; + for (File file : files) { + if (file.isFile()) { + size += file.length(); + } else { + size += calculateDirectorySize(file); + } + } + return size; + } + + public static File getFirstBackupAfterDate(File directory, long date) { + File[] files = directory.listFiles(); + File oldestFile = null; + long currentDate = Long.MAX_VALUE; + if (files == null || files.length == 0) return null; + for (File file : files) { + if (file.lastModified() < currentDate && file.lastModified() > date) { + currentDate = file.lastModified(); + oldestFile = file; + } + } + + return oldestFile; + } + + public static int calculateChainCount(File directory) { + int count = 0; + File[] files = directory.listFiles(); + if (files == null || files.length == 0) return 0; + for (File file : files) { + if (file.getName().contains("full")) { + count++; + } + } + return count; + } + + + public static int calculateBackupCount(File directory) { + File[] files = directory.listFiles(); + if (files == null || files.length == 0) return 0; + return files.length; + } + + private static void checkCount(File directory) { + if (ConfigManager.backupsToKeep.get() <= 0) { + return; + } + + long date = 0; + while (true) { + if (calculateBackupCount(directory) <= ConfigManager.backupsToKeep.get()) { + return; + } + + File file = getFirstBackupAfterDate(directory, date); + File dependent = getDependent(file); + if (dependent == null) { + //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. + } else { + //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain + if ("incremental".equals(ConfigManager.type.get())) { + if (!ConfigManager.purgeIncrementals.get()) return; + if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; + + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + while ((dependent = getDependent(dependent)) != null) { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } else { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } + } + } + + private static void checkDates(File directory) { + if (ConfigManager.daysToKeep.get() <= 0) return; + + long date = 0; + long comp = mostRecentBackupTime(); + while (true) { + File file = getFirstBackupAfterDate(directory, date); + date = file.lastModified(); + long days = (comp - date) / 86400000L; + if (days <= ConfigManager.daysToKeep.get()) return; + File dependent = getDependent(file); + if (dependent == null) { + //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. + } else { + date = dependent.lastModified(); + days = (comp - date) / 86400000L; + if (days <= ConfigManager.daysToKeep.get()) return; + //don't purge unless the dependent is also eligible for deletion + + //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain + if ("incremental".equals(ConfigManager.type.get())) { + if (!ConfigManager.purgeIncrementals.get()) return; + if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; + + //now we need to make sure the entire chain meets the deletion date. + //is this a good strategy? + while ((dependent = getDependent(dependent)) != null) { + date = dependent.lastModified(); + days = (comp - date) / 86400000L; + if (days <= ConfigManager.daysToKeep.get()) return; + } + dependent = getDependent(file); + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + while ((dependent = getDependent(dependent)) != null) { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } else { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } + } + } + + private static void checkSize(File directory) { + if (ConfigManager.size.get() <= 0F) return; + if (calculateDirectorySize(directory) < ConfigManager.size.get() * 1000000000L) return; + long date = 0; + while (true) { + if (calculateDirectorySize(directory) < ConfigManager.size.get() * 1000000000L) return; + File file = getFirstBackupAfterDate(directory, date); + File dependent = getDependent(file); + if (dependent == null) { + //either a broken differential / incremental chain, a full backup with no dependencies or a zip backup + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + //return; we in theory don't need this - it was a good optimisation, but may do more harm than good now. + } else { + //because we can only purge full incremental chains, we need to make sure we're good to delete the entire chain + if ("incremental".equals(ConfigManager.type.get())) { + if (!ConfigManager.purgeIncrementals.get()) return; + if (calculateChainCount(directory) <= ConfigManager.incrementalChains.get()) return; + + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + while ((dependent = getDependent(dependent)) != null) { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } else { + if (dependent.isDirectory()) deleteDirectoryContents(dependent); + dependent.delete(); + } + } + } + + } + + private static void deleteDirectoryContents(File directory) { + if (!directory.isDirectory()) return; //safety check + for (File file : directory.listFiles()) { + if (file.isDirectory()) deleteDirectoryContents(file); + file.delete(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java similarity index 87% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java index e495349f..5d13de67 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/ThreadedBackup.java @@ -1,532 +1,532 @@ -package co.uk.mommyheather.advancedbackups.core.backups; - -import co.uk.mommyheather.advancedbackups.core.ABCore; -import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; -import co.uk.mommyheather.advancedbackups.core.backups.gson.HashList; -import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParseException; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; -import java.util.function.Consumer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -public class ThreadedBackup extends Thread { - private static GsonBuilder builder = new GsonBuilder(); - private static Gson gson; - private long delay; - private static int count; - private static float partialSize; - private static float completeSize; - public static volatile boolean running = false; - public static volatile boolean wasRunning = false; - private static String backupName; - private Consumer output; - private boolean snapshot = false; - private boolean shutdown = false; - private ArrayList erroringFiles = new ArrayList<>(); - - public static final ArrayList blacklist = new ArrayList<>(); - - static { - builder.setPrettyPrinting(); - gson = builder.create(); - } - - public ThreadedBackup(long delay, Consumer output) { - setName("AB Active Backup Thread"); - this.output = output; - this.delay = delay; - count = 0; - partialSize = 0F; - completeSize = 0F; - } - - @Override - public void run() { - try { - sleep(delay); - } catch (Exception e) { - ABCore.logStackTrace(e); - } - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setState(BackupStatusInstance.State.STARTING); - BackupStatusInstance.setInstance(instance); - } - - try { - makeBackup(); - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setState(BackupStatusInstance.State.COMPLETE); - BackupStatusInstance.setInstance(instance); - } - } catch (InterruptedException e) { - output.accept("Backup cancelled!"); - performDelete(new File(ABCore.backupPath)); - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setState(BackupStatusInstance.State.CANCELLED); - BackupStatusInstance.setInstance(instance); - } - wasRunning = true; - running = false; - return; - } catch (Exception e) { - ABCore.errorLogger.accept("ERROR MAKING BACKUP!"); - ABCore.logStackTrace(e); - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setState(BackupStatusInstance.State.FAILED); - BackupStatusInstance.setInstance(instance); - } - performDelete(new File(ABCore.backupPath)); - wasRunning = true; - running = false; - return; - } - - BackupWrapper.finishBackup(snapshot); - if (erroringFiles.isEmpty()) { - output.accept("Backup complete!"); - } else { - output.accept("Backup completed with errors - the following files could not be backed up."); - output.accept("Check the logs for more information :"); - for (String string : erroringFiles) { - output.accept(string); - //TODO have a toast for backup complete with errors - } - } - wasRunning = true; - running = false; - } - - - public void makeBackup() throws Exception { - - File file = new File(ABCore.backupPath); - backupName = ABCore.serialiseBackupName("incomplete"); - - if (snapshot) { - makeZipBackup(file, true); - output.accept("Snapshot created! This will not be auto-deleted."); - performRename(file); - return; - } - - switch (ConfigManager.type.get()) { - case "zip": { - makeZipBackup(file, false); - break; - } - case "differential": { - makeDifferentialOrIncrementalBackup(file, true); - break; - } - case "incremental": { - makeDifferentialOrIncrementalBackup(file, false); - break; - } - } - - performRename(file); - - } - - - private void makeZipBackup(File file, boolean b) throws InterruptedException, IOException { - try { - - File zip = new File(file.toString() + (snapshot ? "/snapshots/" : "/zips/"), backupName + ".zip"); - ABCore.infoLogger.accept("Preparing " + (snapshot ? "snapshot" : "zip") + " backup with name: " + zip.getName().replace("incomplete", "backup")); - output.accept("Preparing " + (snapshot ? "snapshot" : "zip") + " backup with name: " + zip.getName().replace("incomplete", "backup")); - FileOutputStream outputStream = new FileOutputStream(zip); - ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); - zipOutputStream.setLevel((int) ConfigManager.compression.get()); - - ArrayList paths = new ArrayList<>(); - - Files.walkFileTree(ABCore.worldDir, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { - Path targetFile; - targetFile = ABCore.worldDir.relativize(file); - if (targetFile.toFile().getName().compareTo("session.lock") == 0) { - return FileVisitResult.CONTINUE; - } - if (matchesBlacklist(targetFile)) return FileVisitResult.CONTINUE; - - paths.add(file); - return FileVisitResult.CONTINUE; - } - }); - - Path targetFile; - - int max = paths.size(); - int index = 0; - - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - - for (Path path : paths) { - try { - targetFile = ABCore.worldDir.relativize(path); - zipOutputStream.putNextEntry(new ZipEntry(targetFile.toString())); - byte[] bytes = new byte[(int) ConfigManager.buffer.get()]; - try { - FileInputStream is = new FileInputStream(path.toFile()); - while (true) { - int i = is.read(bytes); - if (i < 0) break; - zipOutputStream.write(bytes, 0, i); - } - is.close(); - } catch (Exception e) { - ABCore.errorLogger.accept("Error backing up file : " + targetFile.toString()); - ABCore.logStackTrace(e); - erroringFiles.add(targetFile.toString()); - } - - zipOutputStream.closeEntry(); - //We need to handle interrupts in various styles in different parts of the process! - if (isInterrupted()) { - zipOutputStream.close(); - //Here, we need to close the outputstream - otherwise we risk a leak! - throw new InterruptedException(); - } - index++; - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - } catch (IOException e) { - ABCore.logStackTrace(e); - ABCore.errorLogger.accept(file.toString()); - throw e; - } - } - - zipOutputStream.flush(); - zipOutputStream.close(); - - } catch (IOException e) { - ABCore.logStackTrace(e); - throw e; - } - } - - - private void makeDifferentialOrIncrementalBackup(File location, boolean differential) throws InterruptedException, IOException { - try { - ABCore.infoLogger.accept("Preparing " + (differential ? "differential" : "incremental") + " backup with name: " + backupName.replace("incomplete", "backup")); - output.accept("Preparing " + (differential ? "differential" : "incremental") + " backup with name: " + backupName.replace("incomplete", "backup")); - long time = 0; - - - File manifestFile = new File(location.toString() + "/manifest.json"); - BackupManifest manifest; - if (manifestFile.exists()) { - try { - manifest = gson.fromJson(new String(Files.readAllBytes(manifestFile.toPath())), BackupManifest.class); - - } catch (JsonParseException e) { - - ABCore.errorLogger.accept("Malformed backup manifest! It will have to be reset..."); - output.accept("Malformed backup manifest! It will have to be reset..."); - ABCore.logStackTrace(e); - - manifest = BackupManifest.defaults(); - } - } else { - manifest = BackupManifest.defaults(); - } - - if (manifest.differential.hashList == null) manifest.differential.hashList = new HashList(); - if (manifest.incremental.hashList == null) manifest.incremental.hashList = new HashList(); - - //mappings here - file path and md5 hash - Map comp = differential ? manifest.differential.getHashList().getHashes() : manifest.incremental.getHashList().getHashes(); - Map newHashes = new HashMap(); - - //long comp = differential ? manifest.differential.getLastBackup() : manifest.incremental.getLastBackup(); - ArrayList toBackup = new ArrayList<>(); - ArrayList completeBackup = new ArrayList<>(); - - int chain = differential ? manifest.differential.chainLength : manifest.incremental.chainLength; - - - boolean completeTemp = chain >= ConfigManager.length.get() ? true : false; - - Files.walkFileTree(ABCore.worldDir, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { - Path targetFile; - targetFile = ABCore.worldDir.relativize(file); - if (targetFile.toFile().getName().compareTo("session.lock") == 0) { - return FileVisitResult.CONTINUE; - } - if (matchesBlacklist(targetFile)) return FileVisitResult.CONTINUE; - count++; - completeSize += attributes.size(); - String hash = getFileHash(file.toAbsolutePath()); - String compHash = comp.getOrDefault(targetFile.toString(), ""); - completeBackup.add(targetFile); - if (completeTemp || !compHash.equals(hash)) { - toBackup.add(targetFile); - partialSize += attributes.size(); - newHashes.put(targetFile.toString(), hash); - } - return FileVisitResult.CONTINUE; - } - }); - boolean complete = completeTemp; - if (toBackup.size() >= count) { - complete = true; - } - if ((partialSize / completeSize) * 100F > ConfigManager.chainsPercent.get()) { - complete = true; - toBackup.clear(); - toBackup.addAll(completeBackup); - } - - if (complete || !differential) { - comp.putAll(newHashes); - } - - backupName += complete ? "-full" : "-partial"; - - - //We need to handle interrupts in various styles in different parts of the process! - if (isInterrupted()) { - //Here however, we have nothing to close! just throw - throw new InterruptedException(); - } - - if (ConfigManager.compressChains.get()) { - File zip = differential ? new File(location.toString() + "/differential/", backupName + ".zip") : new File(location.toString() + "/incremental/", backupName + ".zip"); - FileOutputStream outputStream = new FileOutputStream(zip); - ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); - zipOutputStream.setLevel((int) ConfigManager.compression.get()); - - int max = toBackup.size(); - int index = 0; - - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - for (Path path : toBackup) { - zipOutputStream.putNextEntry(new ZipEntry(path.toString())); - - byte[] bytes = new byte[(int) ConfigManager.buffer.get()]; - try { - FileInputStream is = new FileInputStream(new File(ABCore.worldDir.toString(), path.toString())); - while (true) { - int i = is.read(bytes); - if (i < 0) break; - zipOutputStream.write(bytes, 0, i); - } - is.close(); - } catch (Exception e) { - ABCore.errorLogger.accept("Error backing up file : " + path.toString()); - ABCore.logStackTrace(e); - erroringFiles.add(path.toString()); - } - zipOutputStream.closeEntry(); - index++; - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - - //We need to handle interrupts in various styles in different parts of the process! - if (isInterrupted()) { - zipOutputStream.close(); - //again, we need to close the outputstream - otherwise we risk a leak! - throw new InterruptedException(); - } - } - zipOutputStream.flush(); - zipOutputStream.close(); - - time = zip.lastModified(); - } else { - File dest = differential ? new File(location.toString() + "/differential/", backupName + "/") : new File(location.toString() + "/incremental/", backupName + "/"); - dest.mkdirs(); - int max = toBackup.size(); - int index = 0; - - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - for (Path path : toBackup) { - File out = new File(dest, path.toString()); - if (!out.getParentFile().exists()) { - out.getParentFile().mkdirs(); - } - Files.copy(new File(ABCore.worldDir.toString(), path.toString()).toPath(), out.toPath()); - index++; - if (!shutdown) { - BackupStatusInstance instance = new BackupStatusInstance(); - instance.setAge(System.currentTimeMillis()); - instance.setMax(max); - instance.setProgress(index); - instance.setState(BackupStatusInstance.State.STARTED); - BackupStatusInstance.setInstance(instance); - } - } - time = dest.lastModified(); - } - - //Finally, update + write the manifest - if (complete || toBackup.size() >= count) { - if (differential) { - manifest.differential.setChainLength(0); - manifest.differential.setLastBackup(new Date().getTime()); - } else { - manifest.incremental.setChainLength(0); - manifest.incremental.setLastBackup(new Date().getTime()); - } - } else { - if (differential) { - manifest.differential.chainLength++; - //manifest.differential.setLastBackup(new Date().getTime()); - } else { - manifest.incremental.chainLength++; - manifest.incremental.setLastBackup(new Date().getTime()); - } - } - - FileWriter writer = new FileWriter(manifestFile); - writer.write(gson.toJson(manifest)); - writer.flush(); - writer.close(); - } catch (IOException e) { - ABCore.logStackTrace(e); - throw e; - } - - - } - - public void snapshot() { - snapshot = true; - } - - public void shutdown() { - shutdown = true; - } - - - private String getFileHash(Path path) { - try { - MessageDigest md5 = MessageDigest.getInstance("MD5"); - byte[] data = new byte[(int) ConfigManager.buffer.get()]; - FileInputStream is = new FileInputStream(path.toFile()); - while (true) { - int i = is.read(data); - if (i < 0) break; - md5.update(data, 0, i); - - } - is.close(); - byte[] hash = md5.digest(); - String checksum = new BigInteger(1, hash).toString(16); - return checksum; - } catch (IOException | NoSuchAlgorithmException e) { - ABCore.errorLogger.accept("ERROR CALCULATING HASH FOR FILE! " + path.getFileName()); - ABCore.errorLogger.accept("It will be backed up anyway."); - ABCore.logStackTrace(e); - return Integer.toString(new Random().nextInt()); - } - } - - - public static void performRename(File location) { - //Renames all incomplete backups to no longer have the incomplete marker. This is only done after a successful backup! - for (String string : new String[]{"/zips/", "/snapshots/", "/differential/", "/incremental/"}) { - File file = new File(location, string); - for (String backupName : file.list()) { - if (backupName.contains("incomplete")) { - File file2 = new File(file, backupName); - File file3 = new File(file, backupName.replace("incomplete", "backup")); - file2.renameTo(file3); - } - } - } - } - - private void performDelete(File location) { - //Purges all incomplete backups. This is only done after a cancelled or failed backup! - for (String string : new String[]{"/zips/", "/snapshots/", "/differential/", "/incremental/"}) { - File file = new File(location, string); - for (String backupName : file.list()) { - if (backupName.contains("incomplete")) { - File file2 = new File(file, backupName); - file2.delete(); - } - } - } - } - - private static boolean matchesBlacklist(Path path) { - for (Pattern pattern : blacklist) { - Matcher matcher = pattern.matcher(path.toString().replace("\\", "/")); - if (matcher.matches()) return true; - } - - return false; - } - - -} +package co.uk.mommyheather.advancedbackups.core.backups; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.gson.BackupManifest; +import co.uk.mommyheather.advancedbackups.core.backups.gson.HashList; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.math.BigInteger; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ThreadedBackup extends Thread { + private static GsonBuilder builder = new GsonBuilder(); + private static Gson gson; + private long delay; + private static int count; + private static float partialSize; + private static float completeSize; + public static volatile boolean running = false; + public static volatile boolean wasRunning = false; + private static String backupName; + private Consumer output; + private boolean snapshot = false; + private boolean shutdown = false; + private ArrayList erroringFiles = new ArrayList<>(); + + public static final ArrayList blacklist = new ArrayList<>(); + + static { + builder.setPrettyPrinting(); + gson = builder.create(); + } + + public ThreadedBackup(long delay, Consumer output) { + this.setName("AB Active Backup Thread"); + this.output = output; + this.delay = delay; + count = 0; + partialSize = 0F; + completeSize = 0F; + } + + @Override + public void run() { + try { + sleep(this.delay); + } catch (Exception e) { + ABCore.logStackTrace(e); + } + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setState(BackupStatusInstance.State.STARTING); + BackupStatusInstance.setInstance(instance); + } + + try { + this.makeBackup(); + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setState(BackupStatusInstance.State.COMPLETE); + BackupStatusInstance.setInstance(instance); + } + } catch (InterruptedException e) { + this.output.accept("Backup cancelled!"); + this.performDelete(new File(ABCore.backupPath)); + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setState(BackupStatusInstance.State.CANCELLED); + BackupStatusInstance.setInstance(instance); + } + wasRunning = true; + running = false; + return; + } catch (Exception e) { + ABCore.errorLogger.accept("ERROR MAKING BACKUP!"); + ABCore.logStackTrace(e); + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setState(BackupStatusInstance.State.FAILED); + BackupStatusInstance.setInstance(instance); + } + this.performDelete(new File(ABCore.backupPath)); + wasRunning = true; + running = false; + return; + } + + BackupWrapper.finishBackup(this.snapshot); + if (this.erroringFiles.isEmpty()) { + this.output.accept("Backup complete!"); + } else { + this.output.accept("Backup completed with errors - the following files could not be backed up."); + this.output.accept("Check the logs for more information :"); + for (String string : this.erroringFiles) { + this.output.accept(string); + //TODO have a toast for backup complete with errors + } + } + wasRunning = true; + running = false; + } + + + public void makeBackup() throws Exception { + + File file = new File(ABCore.backupPath); + backupName = ABCore.serialiseBackupName("incomplete"); + + if (this.snapshot) { + this.makeZipBackup(file, true); + this.output.accept("Snapshot created! This will not be auto-deleted."); + performRename(file); + return; + } + + switch (ConfigManager.type.get()) { + case "zip": { + this.makeZipBackup(file, false); + break; + } + case "differential": { + this.makeDifferentialOrIncrementalBackup(file, true); + break; + } + case "incremental": { + this.makeDifferentialOrIncrementalBackup(file, false); + break; + } + } + + performRename(file); + + } + + + private void makeZipBackup(File file, boolean b) throws InterruptedException, IOException { + try { + + File zip = new File(file.toString() + (this.snapshot ? "/snapshots/" : "/zips/"), backupName + ".zip"); + ABCore.infoLogger.accept("Preparing " + (this.snapshot ? "snapshot" : "zip") + " backup with name: " + zip.getName().replace("incomplete", "backup")); + this.output.accept("Preparing " + (this.snapshot ? "snapshot" : "zip") + " backup with name: " + zip.getName().replace("incomplete", "backup")); + FileOutputStream outputStream = new FileOutputStream(zip); + ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); + zipOutputStream.setLevel((int) ConfigManager.compression.get()); + + ArrayList paths = new ArrayList<>(); + + Files.walkFileTree(ABCore.worldDir, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { + Path targetFile; + targetFile = ABCore.worldDir.relativize(file); + if (targetFile.toFile().getName().compareTo("session.lock") == 0) { + return FileVisitResult.CONTINUE; + } + if (matchesBlacklist(targetFile)) return FileVisitResult.CONTINUE; + + paths.add(file); + return FileVisitResult.CONTINUE; + } + }); + + Path targetFile; + + int max = paths.size(); + int index = 0; + + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + + for (Path path : paths) { + try { + targetFile = ABCore.worldDir.relativize(path); + zipOutputStream.putNextEntry(new ZipEntry(targetFile.toString())); + byte[] bytes = new byte[(int) ConfigManager.buffer.get()]; + try { + FileInputStream is = new FileInputStream(path.toFile()); + while (true) { + int i = is.read(bytes); + if (i < 0) break; + zipOutputStream.write(bytes, 0, i); + } + is.close(); + } catch (Exception e) { + ABCore.errorLogger.accept("Error backing up file : " + targetFile.toString()); + ABCore.logStackTrace(e); + this.erroringFiles.add(targetFile.toString()); + } + + zipOutputStream.closeEntry(); + //We need to handle interrupts in various styles in different parts of the process! + if (this.isInterrupted()) { + zipOutputStream.close(); + //Here, we need to close the outputstream - otherwise we risk a leak! + throw new InterruptedException(); + } + index++; + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + } catch (IOException e) { + ABCore.logStackTrace(e); + ABCore.errorLogger.accept(file.toString()); + throw e; + } + } + + zipOutputStream.flush(); + zipOutputStream.close(); + + } catch (IOException e) { + ABCore.logStackTrace(e); + throw e; + } + } + + + private void makeDifferentialOrIncrementalBackup(File location, boolean differential) throws InterruptedException, IOException { + try { + ABCore.infoLogger.accept("Preparing " + (differential ? "differential" : "incremental") + " backup with name: " + backupName.replace("incomplete", "backup")); + this.output.accept("Preparing " + (differential ? "differential" : "incremental") + " backup with name: " + backupName.replace("incomplete", "backup")); + long time = 0; + + + File manifestFile = new File(location.toString() + "/manifest.json"); + BackupManifest manifest; + if (manifestFile.exists()) { + try { + manifest = gson.fromJson(new String(Files.readAllBytes(manifestFile.toPath())), BackupManifest.class); + + } catch (JsonParseException e) { + + ABCore.errorLogger.accept("Malformed backup manifest! It will have to be reset..."); + this.output.accept("Malformed backup manifest! It will have to be reset..."); + ABCore.logStackTrace(e); + + manifest = BackupManifest.defaults(); + } + } else { + manifest = BackupManifest.defaults(); + } + + if (manifest.differential.hashList == null) manifest.differential.hashList = new HashList(); + if (manifest.incremental.hashList == null) manifest.incremental.hashList = new HashList(); + + //mappings here - file path and md5 hash + Map comp = differential ? manifest.differential.getHashList().getHashes() : manifest.incremental.getHashList().getHashes(); + Map newHashes = new HashMap(); + + //long comp = differential ? manifest.differential.getLastBackup() : manifest.incremental.getLastBackup(); + ArrayList toBackup = new ArrayList<>(); + ArrayList completeBackup = new ArrayList<>(); + + int chain = differential ? manifest.differential.chainLength : manifest.incremental.chainLength; + + + boolean completeTemp = chain >= ConfigManager.length.get() ? true : false; + + Files.walkFileTree(ABCore.worldDir, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { + Path targetFile; + targetFile = ABCore.worldDir.relativize(file); + if (targetFile.toFile().getName().compareTo("session.lock") == 0) { + return FileVisitResult.CONTINUE; + } + if (matchesBlacklist(targetFile)) return FileVisitResult.CONTINUE; + count++; + completeSize += attributes.size(); + String hash = ThreadedBackup.this.getFileHash(file.toAbsolutePath()); + String compHash = comp.getOrDefault(targetFile.toString(), ""); + completeBackup.add(targetFile); + if (completeTemp || !compHash.equals(hash)) { + toBackup.add(targetFile); + partialSize += attributes.size(); + newHashes.put(targetFile.toString(), hash); + } + return FileVisitResult.CONTINUE; + } + }); + boolean complete = completeTemp; + if (toBackup.size() >= count) { + complete = true; + } + if ((partialSize / completeSize) * 100F > ConfigManager.chainsPercent.get()) { + complete = true; + toBackup.clear(); + toBackup.addAll(completeBackup); + } + + if (complete || !differential) { + comp.putAll(newHashes); + } + + backupName += complete ? "-full" : "-partial"; + + + //We need to handle interrupts in various styles in different parts of the process! + if (this.isInterrupted()) { + //Here however, we have nothing to close! just throw + throw new InterruptedException(); + } + + if (ConfigManager.compressChains.get()) { + File zip = differential ? new File(location.toString() + "/differential/", backupName + ".zip") : new File(location.toString() + "/incremental/", backupName + ".zip"); + FileOutputStream outputStream = new FileOutputStream(zip); + ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); + zipOutputStream.setLevel((int) ConfigManager.compression.get()); + + int max = toBackup.size(); + int index = 0; + + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + for (Path path : toBackup) { + zipOutputStream.putNextEntry(new ZipEntry(path.toString())); + + byte[] bytes = new byte[(int) ConfigManager.buffer.get()]; + try { + FileInputStream is = new FileInputStream(new File(ABCore.worldDir.toString(), path.toString())); + while (true) { + int i = is.read(bytes); + if (i < 0) break; + zipOutputStream.write(bytes, 0, i); + } + is.close(); + } catch (Exception e) { + ABCore.errorLogger.accept("Error backing up file : " + path.toString()); + ABCore.logStackTrace(e); + this.erroringFiles.add(path.toString()); + } + zipOutputStream.closeEntry(); + index++; + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + + //We need to handle interrupts in various styles in different parts of the process! + if (this.isInterrupted()) { + zipOutputStream.close(); + //again, we need to close the outputstream - otherwise we risk a leak! + throw new InterruptedException(); + } + } + zipOutputStream.flush(); + zipOutputStream.close(); + + time = zip.lastModified(); + } else { + File dest = differential ? new File(location.toString() + "/differential/", backupName + "/") : new File(location.toString() + "/incremental/", backupName + "/"); + dest.mkdirs(); + int max = toBackup.size(); + int index = 0; + + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + for (Path path : toBackup) { + File out = new File(dest, path.toString()); + if (!out.getParentFile().exists()) { + out.getParentFile().mkdirs(); + } + Files.copy(new File(ABCore.worldDir.toString(), path.toString()).toPath(), out.toPath()); + index++; + if (!this.shutdown) { + BackupStatusInstance instance = new BackupStatusInstance(); + instance.setAge(System.currentTimeMillis()); + instance.setMax(max); + instance.setProgress(index); + instance.setState(BackupStatusInstance.State.STARTED); + BackupStatusInstance.setInstance(instance); + } + } + time = dest.lastModified(); + } + + //Finally, update + write the manifest + if (complete || toBackup.size() >= count) { + if (differential) { + manifest.differential.setChainLength(0); + manifest.differential.setLastBackup(new Date().getTime()); + } else { + manifest.incremental.setChainLength(0); + manifest.incremental.setLastBackup(new Date().getTime()); + } + } else { + if (differential) { + manifest.differential.chainLength++; + //manifest.differential.setLastBackup(new Date().getTime()); + } else { + manifest.incremental.chainLength++; + manifest.incremental.setLastBackup(new Date().getTime()); + } + } + + FileWriter writer = new FileWriter(manifestFile); + writer.write(gson.toJson(manifest)); + writer.flush(); + writer.close(); + } catch (IOException e) { + ABCore.logStackTrace(e); + throw e; + } + + + } + + public void snapshot() { + this.snapshot = true; + } + + public void shutdown() { + this.shutdown = true; + } + + + private String getFileHash(Path path) { + try { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] data = new byte[(int) ConfigManager.buffer.get()]; + FileInputStream is = new FileInputStream(path.toFile()); + while (true) { + int i = is.read(data); + if (i < 0) break; + md5.update(data, 0, i); + + } + is.close(); + byte[] hash = md5.digest(); + String checksum = new BigInteger(1, hash).toString(16); + return checksum; + } catch (IOException | NoSuchAlgorithmException e) { + ABCore.errorLogger.accept("ERROR CALCULATING HASH FOR FILE! " + path.getFileName()); + ABCore.errorLogger.accept("It will be backed up anyway."); + ABCore.logStackTrace(e); + return Integer.toString(new Random().nextInt()); + } + } + + + public static void performRename(File location) { + //Renames all incomplete backups to no longer have the incomplete marker. This is only done after a successful backup! + for (String string : new String[]{"/zips/", "/snapshots/", "/differential/", "/incremental/"}) { + File file = new File(location, string); + for (String backupName : file.list()) { + if (backupName.contains("incomplete")) { + File file2 = new File(file, backupName); + File file3 = new File(file, backupName.replace("incomplete", "backup")); + file2.renameTo(file3); + } + } + } + } + + private void performDelete(File location) { + //Purges all incomplete backups. This is only done after a cancelled or failed backup! + for (String string : new String[]{"/zips/", "/snapshots/", "/differential/", "/incremental/"}) { + File file = new File(location, string); + for (String backupName : file.list()) { + if (backupName.contains("incomplete")) { + File file2 = new File(file, backupName); + file2.delete(); + } + } + } + } + + private static boolean matchesBlacklist(Path path) { + for (Pattern pattern : blacklist) { + Matcher matcher = pattern.matcher(path.toString().replace("\\", "/")); + if (matcher.matches()) return true; + } + + return false; + } + + +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/BackupManifest.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/BackupManifest.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/BackupManifest.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/BackupManifest.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/DifferentialManifest.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/DifferentialManifest.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/DifferentialManifest.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/DifferentialManifest.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/HashList.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/HashList.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/HashList.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/backups/gson/HashList.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java similarity index 97% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java index 2f12e24a..5c736f97 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ClientConfigManager.java @@ -1,136 +1,136 @@ -package co.uk.mommyheather.advancedbackups.core.config; - -import co.uk.mommyheather.advancedbackups.core.ABCore; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.BooleanValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ConfigValidationEnum; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.LongValue; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Properties; -import java.util.stream.Collectors; - -public class ClientConfigManager { - - private static HashMap entries = new HashMap<>(); - - - public static void register(String key, ConfigTypes configType) { - entries.put(key, configType); - } - - - public static final BooleanValue showProgress = new BooleanValue("config.advancedbackups.showProgress", true, ClientConfigManager::register); - - public static final BooleanValue darkMode = new BooleanValue("config.advancedbackups.darkToasts", true, ClientConfigManager::register); - - public static final LongValue progressTextRed = new LongValue("config.advancedbackups.colours.progress.red", 82, 0, 255, ClientConfigManager::register); - public static final LongValue progressTextGreen = new LongValue("config.advancedbackups.colours.progress.green", 255, 0, 255, ClientConfigManager::register); - public static final LongValue progressTextBlue = new LongValue("config.advancedbackups.colours.progress.blue", 82, 0, 255, ClientConfigManager::register); - - public static final LongValue errorTextRed = new LongValue("config.advancedbackups.colours.error.red", 255, 0, 255, ClientConfigManager::register); - public static final LongValue errorTextGreen = new LongValue("config.advancedbackups.colours.error.green", 50, 0, 255, ClientConfigManager::register); - public static final LongValue errorTextBlue = new LongValue("config.advancedbackups.colours.error.blue", 50, 0, 255, ClientConfigManager::register); - - public static final LongValue progressBarRed = new LongValue("config.advancedbackups.colours.bar.red", 88, 0, 255, ClientConfigManager::register); - public static final LongValue progressBarGreen = new LongValue("config.advancedbackups.colours.bar.green", 242, 0, 255, ClientConfigManager::register); - public static final LongValue progressBarBlue = new LongValue("config.advancedbackups.colours.bar.blue", 82, 0, 255, ClientConfigManager::register); - - public static final LongValue progressBackgroundRed = new LongValue("config.advancedbackups.colours.background.red", 255, 0, 255, ClientConfigManager::register); - public static final LongValue progressBackgroundGreen = new LongValue("config.advancedbackups.colours.background.green", 255, 0, 255, ClientConfigManager::register); - public static final LongValue progressBackgroundBlue = new LongValue("config.advancedbackups.colours.background.blue", 255, 0, 255, ClientConfigManager::register); - - - public static void loadOrCreateConfig() { - // Called when the config needs to be loaded, but one may not exist. - // Creates a new config it one doesn't exist, then loads it. - File dir = new File("./config"); - if (!dir.exists()) { - dir.mkdirs(); - } - File file = new File(dir, "AdvancedBackups-client.properties"); - if (!file.exists()) { - writeConfig(); - } - loadConfig(); - } - - private static void writeConfig() { - // Called to write to a config file. - // Create a complete properties file in the cwd, including any existing changes - ABCore.infoLogger.accept("Preparing to write to client properties file..."); - File file = new File("./config/AdvancedBackups-client.properties"); - try { - file.createNewFile(); - file.setWritable(true); - InputStream is = ClientConfigManager.class.getClassLoader().getResourceAsStream("advancedbackups-client-properties.txt"); - - String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)) - .lines().collect(Collectors.joining("\n")); - - for (String key : entries.keySet()) { - text = text.replace(key, key + "=" + entries.get(key).save()); - } - - FileWriter writer = new FileWriter(file); - writer.write(text); - writer.close(); - } catch (IOException e) { - // TODO : Scream to user - ABCore.logStackTrace(e); - } - } - - - private static void loadConfig() { - //Load the config file. - Properties props = new Properties(); - File file = new File("./config/AdvancedBackups-client.properties"); - FileReader reader; - try { - reader = new FileReader(file); - props.load(reader); - reader.close(); - } catch (IOException e) { - // TODO : Scream to user - ABCore.logStackTrace(e); - return; - } - - ArrayList missingProps = new ArrayList<>(); - - for (String key : entries.keySet()) { - if (!props.containsKey(key)) { - missingProps.add(key); - ABCore.warningLogger.accept("Missing key : " + key); - continue; - } - ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); - if (valid != ConfigValidationEnum.VALID) { - missingProps.add(key); - ABCore.warningLogger.accept(valid.getError() + " : " + key); - continue; - - } - entries.get(key).load(props.getProperty(key)); - } - - if (!missingProps.isEmpty()) { - ABCore.warningLogger.accept("The following properties were missing from the loaded file :"); - for (String string : missingProps) { - ABCore.warningLogger.accept(string); - } - ABCore.warningLogger.accept("Client properties file will be regenerated! Existing config values will be preserved."); - - writeConfig(); - } - } -} +package co.uk.mommyheather.advancedbackups.core.config; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.BooleanValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ConfigValidationEnum; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.LongValue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Properties; +import java.util.stream.Collectors; + +public class ClientConfigManager { + + private static HashMap entries = new HashMap<>(); + + + public static void register(String key, ConfigTypes configType) { + entries.put(key, configType); + } + + + public static final BooleanValue showProgress = new BooleanValue("config.advancedbackups.showProgress", true, ClientConfigManager::register); + + public static final BooleanValue darkMode = new BooleanValue("config.advancedbackups.darkToasts", true, ClientConfigManager::register); + + public static final LongValue progressTextRed = new LongValue("config.advancedbackups.colours.progress.red", 82, 0, 255, ClientConfigManager::register); + public static final LongValue progressTextGreen = new LongValue("config.advancedbackups.colours.progress.green", 255, 0, 255, ClientConfigManager::register); + public static final LongValue progressTextBlue = new LongValue("config.advancedbackups.colours.progress.blue", 82, 0, 255, ClientConfigManager::register); + + public static final LongValue errorTextRed = new LongValue("config.advancedbackups.colours.error.red", 255, 0, 255, ClientConfigManager::register); + public static final LongValue errorTextGreen = new LongValue("config.advancedbackups.colours.error.green", 50, 0, 255, ClientConfigManager::register); + public static final LongValue errorTextBlue = new LongValue("config.advancedbackups.colours.error.blue", 50, 0, 255, ClientConfigManager::register); + + public static final LongValue progressBarRed = new LongValue("config.advancedbackups.colours.bar.red", 88, 0, 255, ClientConfigManager::register); + public static final LongValue progressBarGreen = new LongValue("config.advancedbackups.colours.bar.green", 242, 0, 255, ClientConfigManager::register); + public static final LongValue progressBarBlue = new LongValue("config.advancedbackups.colours.bar.blue", 82, 0, 255, ClientConfigManager::register); + + public static final LongValue progressBackgroundRed = new LongValue("config.advancedbackups.colours.background.red", 255, 0, 255, ClientConfigManager::register); + public static final LongValue progressBackgroundGreen = new LongValue("config.advancedbackups.colours.background.green", 255, 0, 255, ClientConfigManager::register); + public static final LongValue progressBackgroundBlue = new LongValue("config.advancedbackups.colours.background.blue", 255, 0, 255, ClientConfigManager::register); + + + public static void loadOrCreateConfig() { + // Called when the config needs to be loaded, but one may not exist. + // Creates a new config it one doesn't exist, then loads it. + File dir = new File("./config"); + if (!dir.exists()) { + dir.mkdirs(); + } + File file = new File(dir, "AdvancedBackups-client.properties"); + if (!file.exists()) { + writeConfig(); + } + loadConfig(); + } + + private static void writeConfig() { + // Called to write to a config file. + // Create a complete properties file in the cwd, including any existing changes + ABCore.infoLogger.accept("Preparing to write to client properties file..."); + File file = new File("./config/AdvancedBackups-client.properties"); + try { + file.createNewFile(); + file.setWritable(true); + InputStream is = ClientConfigManager.class.getClassLoader().getResourceAsStream("advancedbackups-client-properties.txt"); + + String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)) + .lines().collect(Collectors.joining("\n")); + + for (String key : entries.keySet()) { + text = text.replace(key, key + "=" + entries.get(key).save()); + } + + FileWriter writer = new FileWriter(file); + writer.write(text); + writer.close(); + } catch (IOException e) { + // TODO : Scream to user + ABCore.logStackTrace(e); + } + } + + + private static void loadConfig() { + //Load the config file. + Properties props = new Properties(); + File file = new File("./config/AdvancedBackups-client.properties"); + FileReader reader; + try { + reader = new FileReader(file); + props.load(reader); + reader.close(); + } catch (IOException e) { + // TODO : Scream to user + ABCore.logStackTrace(e); + return; + } + + ArrayList missingProps = new ArrayList<>(); + + for (String key : entries.keySet()) { + if (!props.containsKey(key)) { + missingProps.add(key); + ABCore.warningLogger.accept("Missing key : " + key); + continue; + } + ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); + if (valid != ConfigValidationEnum.VALID) { + missingProps.add(key); + ABCore.warningLogger.accept(valid.getError() + " : " + key); + continue; + + } + entries.get(key).load(props.getProperty(key)); + } + + if (!missingProps.isEmpty()) { + ABCore.warningLogger.accept("The following properties were missing from the loaded file :"); + for (String string : missingProps) { + ABCore.warningLogger.accept(string); + } + ABCore.warningLogger.accept("Client properties file will be regenerated! Existing config values will be preserved."); + + writeConfig(); + } + } +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java similarity index 98% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java index dda7c378..efeb5c2b 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigManager.java @@ -1,231 +1,231 @@ -package co.uk.mommyheather.advancedbackups.core.config; - -import co.uk.mommyheather.advancedbackups.core.ABCore; -import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; -import co.uk.mommyheather.advancedbackups.core.backups.ThreadedBackup; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.BooleanValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ConfigValidationEnum; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.FloatValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.FreeStringValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.LongValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.StringArrayValue; -import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ValidatedStringValue; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -public class ConfigManager { - - private static HashMap entries = new HashMap<>(); - - - public static void register(String key, ConfigTypes configType) { - entries.put(key, configType); - } - - - public static final BooleanValue enabled = new BooleanValue("config.advancedbackups.enabled", true, ConfigManager::register); - public static final BooleanValue save = new BooleanValue("config.advancedbackups.save", true, ConfigManager::register); - public static final BooleanValue toggleSave = new BooleanValue("config.advancedbackups.togglesave", true, ConfigManager::register); - public static final LongValue buffer = new LongValue("config.advancedbackups.buffer", 1048576, 1024, Integer.MAX_VALUE, ConfigManager::register); //5mb - public static final BooleanValue flush = new BooleanValue("config.advancedbackups.flush", false, ConfigManager::register); - public static final BooleanValue activity = new BooleanValue("config.advancedbackups.activity", true, ConfigManager::register); - public static final StringArrayValue blacklist = new StringArrayValue("config.advancedbackups.blacklist", new String[]{"session.lock", "*_old"}, ConfigManager::register); - public static final ValidatedStringValue type = new ValidatedStringValue("config.advancedbackups.type", "differential", new String[]{"zip", "differential", "incremental"}, ConfigManager::register); - public static final FreeStringValue path = new FreeStringValue("config.advancedbackups.path", "./backups", ConfigManager::register); - public static final FloatValue minFrequency = new FloatValue("config.advancedbackups.frequency.min", 0.25F, 0F, 500F, ConfigManager::register); - public static final FloatValue maxFrequency = new FloatValue("config.advancedbackups.frequency.max", 24F, 0.5F, 500F, ConfigManager::register); - public static final BooleanValue uptime = new BooleanValue("config.advancedbackups.frequency.uptime", true, ConfigManager::register); - public static final StringArrayValue timesArray = new StringArrayValue("config.advancedbackups.frequency.schedule", new String[]{"1:00"}, ConfigManager::register); - public static final BooleanValue shutdown = new BooleanValue("config.advancedbackups.frequency.shutdown", false, ConfigManager::register); - public static final BooleanValue startup = new BooleanValue("config.advancedbackups.frequency.startup", false, ConfigManager::register); - public static final LongValue delay = new LongValue("config.advancedbackups.frequency.delay", 30, 5, 1000, ConfigManager::register); - /* - * New logging options! - * Clients = OPS, ALL, NONE. OPS is default, means operator permission is required. ALL is all clients with the mod. NONE disabled. - * Client frequency - how often progress is sent to clients. Only the latest one is sent, and the client toasts persist until a complete, failed or cancelled notification is recieved. - * Console - enable or disable console logging for backup progress. Start / finish are always logged. - * Console frequency - how often progress is logged in console. - */ - public static final ValidatedStringValue clients = new ValidatedStringValue("config.advancedbackups.logging.clients", "ops", new String[]{"ops", "all", "none"}, ConfigManager::register); - public static final LongValue clientFrequency = new LongValue("config.advancedbackups.logging.clientfrequency", 500L, 0L, Long.MAX_VALUE, ConfigManager::register); - public static final BooleanValue console = new BooleanValue("config.advancedbackups.logging.console", true, ConfigManager::register); - public static final LongValue consoleFrequency = new LongValue("config.advancedbackups.logging.consolefrequency", 5000L, 0L, Long.MAX_VALUE, ConfigManager::register); - public static final LongValue compression = new LongValue("config.advancedbackups.zips.compression", 4, 1, 9, ConfigManager::register); - public static final LongValue length = new LongValue("config.advancedbackups.chains.length", 50, 5, 500, ConfigManager::register); - public static final BooleanValue compressChains = new BooleanValue("config.advancedbackups.chains.compress", true, ConfigManager::register); - public static final BooleanValue smartChains = new BooleanValue("config.advancedbackups.chains.smart", true, ConfigManager::register); - public static final FloatValue chainsPercent = new FloatValue("config.advancedbackups.chains.maxpercent", 50F, 1F, 100F, ConfigManager::register); - public static final FloatValue size = new FloatValue("config.advancedbackups.purge.size", 50F, 0F, Float.MAX_VALUE, ConfigManager::register); - public static final LongValue daysToKeep = new LongValue("config.advancedbackups.purge.days", 0L, 0L, Long.MAX_VALUE, ConfigManager::register); - public static final LongValue backupsToKeep = new LongValue("config.advancedbackups.purge.count", 0L, 0L, Long.MAX_VALUE, ConfigManager::register); - public static final BooleanValue purgeIncrementals = new BooleanValue("config.advancedbackups.purge.incrementals", true, ConfigManager::register); - public static final LongValue incrementalChains = new LongValue("config.advancedbackups.purge.incrementalchains", 1, 1, Long.MAX_VALUE, ConfigManager::register); - - - public static void loadOrCreateConfig() { - // Called when the config needs to be loaded, but one may not exist. - // Creates a new config it one doesn't exist, then loads it. - File dir = new File("./config"); - if (!dir.exists()) { - dir.mkdirs(); - } - File file = new File("./AdvancedBackups.properties"); - if (file.exists()) { - migrateConfig(); - } - file = new File(dir, "AdvancedBackups.properties"); - if (!file.exists()) { - writeConfig(); - } - loadConfig(); - } - - private static void writeConfig() { - // Called to write to a config file. - // Create a complete properties file in the cwd, including any existing changes - ABCore.infoLogger.accept("Preparing to write to properties file..."); - File file = new File("./config/AdvancedBackups.properties"); - try { - file.createNewFile(); - file.setWritable(true); - InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream("advancedbackups-properties.txt"); - - String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)) - .lines().collect(Collectors.joining("\n")); - - for (String key : entries.keySet()) { - Matcher matcher = Pattern.compile(Pattern.quote(key) + "$", Pattern.MULTILINE).matcher(text); - text = matcher.replaceAll(key + "=" + entries.get(key).save()); - } - - FileWriter writer = new FileWriter(file); - writer.write(text); - writer.close(); - } catch (IOException e) { - // TODO : Scream to user - ABCore.logStackTrace(e); - } - } - - - private static void loadConfig() { - //Load the config file. - Properties props = new Properties(); - File file = new File("./config/AdvancedBackups.properties"); - FileReader reader; - try { - reader = new FileReader(file); - props.load(reader); - reader.close(); - } catch (IOException e) { - // TODO : Scream to user - ABCore.logStackTrace(e); - return; - } - - ArrayList missingProps = new ArrayList<>(); - - for (String key : entries.keySet()) { - if (!props.containsKey(key)) { - missingProps.add(key); - ABCore.warningLogger.accept("Missing key : " + key); - continue; - } - ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); - if (valid != ConfigValidationEnum.VALID) { - missingProps.add(key); - ABCore.warningLogger.accept(valid.getError() + " : " + key); - continue; - - } - entries.get(key).load(props.getProperty(key)); - } - - if (props.containsKey("config.advancedbackups.size")) { - ABCore.warningLogger.accept("Migrating old config value :"); - ABCore.warningLogger.accept("config.advancedbackups.size -> config.advancedbackups.purge.size"); - - size.load(props.getProperty("config.advancedbackups.size")); - - } - - if (!missingProps.isEmpty()) { - ABCore.warningLogger.accept("The following properties were missing from the loaded file :"); - for (String string : missingProps) { - ABCore.warningLogger.accept(string); - } - ABCore.warningLogger.accept("Properties file will be regenerated! Existing config values will be preserved."); - - writeConfig(); - } - - - BackupWrapper.configuredPlaytime = new ArrayList<>(); - for (String time : timesArray.get()) { - String[] hm = time.split(":"); - long hours = Long.parseLong(hm[0]) * 3600000L; - long mins = Long.parseLong(hm[1]) * 60000; - BackupWrapper.configuredPlaytime.add(hours + mins); - } - - ABCore.backupPath = path.get() + "/" + (ABCore.worldDir.getParent().toFile().getName()); - - ThreadedBackup.blacklist.clear(); - - for (String string : blacklist.get()) { - - string = string.replace("\\", "/"); - string = string.replaceAll("[^a-zA-Z0-9*]", "\\\\$0"); - string = "^" + string.replace("*", ".*") + "$"; - - ThreadedBackup.blacklist.add(Pattern.compile(string, Pattern.CASE_INSENSITIVE)); - } - } - - - private static void migrateConfig() { - //Load the config file. - Properties props = new Properties(); - File file = new File("./AdvancedBackups.properties"); - FileReader reader; - try { - reader = new FileReader(file); - props.load(reader); - reader.close(); - file.delete(); - } catch (IOException e) { - // TODO : Scream to user - ABCore.logStackTrace(e); - return; - } - - - for (String key : entries.keySet()) { - if (!props.containsKey(key)) { - continue; - } - ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); - if (valid != ConfigValidationEnum.VALID) { - continue; - - } - entries.get(key).load(props.getProperty(key)); - } - - ABCore.warningLogger.accept("Config in old location detected! Migrating."); - writeConfig(); - } -} +package co.uk.mommyheather.advancedbackups.core.config; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.backups.ThreadedBackup; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.BooleanValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ConfigValidationEnum; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.FloatValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.FreeStringValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.LongValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.StringArrayValue; +import co.uk.mommyheather.advancedbackups.core.config.ConfigTypes.ValidatedStringValue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class ConfigManager { + + private static HashMap entries = new HashMap<>(); + + + public static void register(String key, ConfigTypes configType) { + entries.put(key, configType); + } + + + public static final BooleanValue enabled = new BooleanValue("config.advancedbackups.enabled", true, ConfigManager::register); + public static final BooleanValue save = new BooleanValue("config.advancedbackups.save", true, ConfigManager::register); + public static final BooleanValue toggleSave = new BooleanValue("config.advancedbackups.togglesave", true, ConfigManager::register); + public static final LongValue buffer = new LongValue("config.advancedbackups.buffer", 1048576, 1024, Integer.MAX_VALUE, ConfigManager::register); //5mb + public static final BooleanValue flush = new BooleanValue("config.advancedbackups.flush", false, ConfigManager::register); + public static final BooleanValue activity = new BooleanValue("config.advancedbackups.activity", true, ConfigManager::register); + public static final StringArrayValue blacklist = new StringArrayValue("config.advancedbackups.blacklist", new String[]{"session.lock", "*_old"}, ConfigManager::register); + public static final ValidatedStringValue type = new ValidatedStringValue("config.advancedbackups.type", "differential", new String[]{"zip", "differential", "incremental"}, ConfigManager::register); + public static final FreeStringValue path = new FreeStringValue("config.advancedbackups.path", "./backups", ConfigManager::register); + public static final FloatValue minFrequency = new FloatValue("config.advancedbackups.frequency.min", 0.25F, 0F, 500F, ConfigManager::register); + public static final FloatValue maxFrequency = new FloatValue("config.advancedbackups.frequency.max", 24F, 0.5F, 500F, ConfigManager::register); + public static final BooleanValue uptime = new BooleanValue("config.advancedbackups.frequency.uptime", true, ConfigManager::register); + public static final StringArrayValue timesArray = new StringArrayValue("config.advancedbackups.frequency.schedule", new String[]{"1:00"}, ConfigManager::register); + public static final BooleanValue shutdown = new BooleanValue("config.advancedbackups.frequency.shutdown", false, ConfigManager::register); + public static final BooleanValue startup = new BooleanValue("config.advancedbackups.frequency.startup", false, ConfigManager::register); + public static final LongValue delay = new LongValue("config.advancedbackups.frequency.delay", 30, 5, 1000, ConfigManager::register); + /* + * New logging options! + * Clients = OPS, ALL, NONE. OPS is default, means operator permission is required. ALL is all clients with the mod. NONE disabled. + * Client frequency - how often progress is sent to clients. Only the latest one is sent, and the client toasts persist until a complete, failed or cancelled notification is recieved. + * Console - enable or disable console logging for backup progress. Start / finish are always logged. + * Console frequency - how often progress is logged in console. + */ + public static final ValidatedStringValue clients = new ValidatedStringValue("config.advancedbackups.logging.clients", "ops", new String[]{"ops", "all", "none"}, ConfigManager::register); + public static final LongValue clientFrequency = new LongValue("config.advancedbackups.logging.clientfrequency", 500L, 0L, Long.MAX_VALUE, ConfigManager::register); + public static final BooleanValue console = new BooleanValue("config.advancedbackups.logging.console", true, ConfigManager::register); + public static final LongValue consoleFrequency = new LongValue("config.advancedbackups.logging.consolefrequency", 5000L, 0L, Long.MAX_VALUE, ConfigManager::register); + public static final LongValue compression = new LongValue("config.advancedbackups.zips.compression", 4, 1, 9, ConfigManager::register); + public static final LongValue length = new LongValue("config.advancedbackups.chains.length", 50, 5, 500, ConfigManager::register); + public static final BooleanValue compressChains = new BooleanValue("config.advancedbackups.chains.compress", true, ConfigManager::register); + public static final BooleanValue smartChains = new BooleanValue("config.advancedbackups.chains.smart", true, ConfigManager::register); + public static final FloatValue chainsPercent = new FloatValue("config.advancedbackups.chains.maxpercent", 50F, 1F, 100F, ConfigManager::register); + public static final FloatValue size = new FloatValue("config.advancedbackups.purge.size", 50F, 0F, Float.MAX_VALUE, ConfigManager::register); + public static final LongValue daysToKeep = new LongValue("config.advancedbackups.purge.days", 0L, 0L, Long.MAX_VALUE, ConfigManager::register); + public static final LongValue backupsToKeep = new LongValue("config.advancedbackups.purge.count", 0L, 0L, Long.MAX_VALUE, ConfigManager::register); + public static final BooleanValue purgeIncrementals = new BooleanValue("config.advancedbackups.purge.incrementals", true, ConfigManager::register); + public static final LongValue incrementalChains = new LongValue("config.advancedbackups.purge.incrementalchains", 1, 1, Long.MAX_VALUE, ConfigManager::register); + + + public static void loadOrCreateConfig() { + // Called when the config needs to be loaded, but one may not exist. + // Creates a new config it one doesn't exist, then loads it. + File dir = new File("./config"); + if (!dir.exists()) { + dir.mkdirs(); + } + File file = new File("./AdvancedBackups.properties"); + if (file.exists()) { + migrateConfig(); + } + file = new File(dir, "AdvancedBackups.properties"); + if (!file.exists()) { + writeConfig(); + } + loadConfig(); + } + + private static void writeConfig() { + // Called to write to a config file. + // Create a complete properties file in the cwd, including any existing changes + ABCore.infoLogger.accept("Preparing to write to properties file..."); + File file = new File("./config/AdvancedBackups.properties"); + try { + file.createNewFile(); + file.setWritable(true); + InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream("advancedbackups-properties.txt"); + + String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)) + .lines().collect(Collectors.joining("\n")); + + for (String key : entries.keySet()) { + Matcher matcher = Pattern.compile(Pattern.quote(key) + "$", Pattern.MULTILINE).matcher(text); + text = matcher.replaceAll(key + "=" + entries.get(key).save()); + } + + FileWriter writer = new FileWriter(file); + writer.write(text); + writer.close(); + } catch (IOException e) { + // TODO : Scream to user + ABCore.logStackTrace(e); + } + } + + + private static void loadConfig() { + //Load the config file. + Properties props = new Properties(); + File file = new File("./config/AdvancedBackups.properties"); + FileReader reader; + try { + reader = new FileReader(file); + props.load(reader); + reader.close(); + } catch (IOException e) { + // TODO : Scream to user + ABCore.logStackTrace(e); + return; + } + + ArrayList missingProps = new ArrayList<>(); + + for (String key : entries.keySet()) { + if (!props.containsKey(key)) { + missingProps.add(key); + ABCore.warningLogger.accept("Missing key : " + key); + continue; + } + ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); + if (valid != ConfigValidationEnum.VALID) { + missingProps.add(key); + ABCore.warningLogger.accept(valid.getError() + " : " + key); + continue; + + } + entries.get(key).load(props.getProperty(key)); + } + + if (props.containsKey("config.advancedbackups.size")) { + ABCore.warningLogger.accept("Migrating old config value :"); + ABCore.warningLogger.accept("config.advancedbackups.size -> config.advancedbackups.purge.size"); + + size.load(props.getProperty("config.advancedbackups.size")); + + } + + if (!missingProps.isEmpty()) { + ABCore.warningLogger.accept("The following properties were missing from the loaded file :"); + for (String string : missingProps) { + ABCore.warningLogger.accept(string); + } + ABCore.warningLogger.accept("Properties file will be regenerated! Existing config values will be preserved."); + + writeConfig(); + } + + + BackupWrapper.configuredPlaytime = new ArrayList<>(); + for (String time : timesArray.get()) { + String[] hm = time.split(":"); + long hours = Long.parseLong(hm[0]) * 3600000L; + long mins = Long.parseLong(hm[1]) * 60000; + BackupWrapper.configuredPlaytime.add(hours + mins); + } + + ABCore.backupPath = path.get() + "/" + (ABCore.worldDir.getParent().toFile().getName()); + + ThreadedBackup.blacklist.clear(); + + for (String string : blacklist.get()) { + + string = string.replace("\\", "/"); + string = string.replaceAll("[^a-zA-Z0-9*]", "\\\\$0"); + string = "^" + string.replace("*", ".*") + "$"; + + ThreadedBackup.blacklist.add(Pattern.compile(string, Pattern.CASE_INSENSITIVE)); + } + } + + + private static void migrateConfig() { + //Load the config file. + Properties props = new Properties(); + File file = new File("./AdvancedBackups.properties"); + FileReader reader; + try { + reader = new FileReader(file); + props.load(reader); + reader.close(); + file.delete(); + } catch (IOException e) { + // TODO : Scream to user + ABCore.logStackTrace(e); + return; + } + + + for (String key : entries.keySet()) { + if (!props.containsKey(key)) { + continue; + } + ConfigValidationEnum valid = entries.get(key).validate(props.getProperty(key)); + if (valid != ConfigValidationEnum.VALID) { + continue; + + } + entries.get(key).load(props.getProperty(key)); + } + + ABCore.warningLogger.accept("Config in old location detected! Migrating."); + writeConfig(); + } +} \ No newline at end of file diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigTypes.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigTypes.java similarity index 100% rename from src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigTypes.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/core/config/ConfigTypes.java diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java b/core/src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java similarity index 85% rename from src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java rename to core/src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java index aab74ffc..72ee69d9 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java +++ b/core/src/main/java/co/uk/mommyheather/advancedbackups/interfaces/IClientContactor.java @@ -1,77 +1,77 @@ -package co.uk.mommyheather.advancedbackups.interfaces; - -import co.uk.mommyheather.advancedbackups.core.ABCore; -import co.uk.mommyheather.advancedbackups.core.backups.BackupStatusInstance; - -public interface IClientContactor { - - /* - * This shouldn't be called anymore! - * Would be private, but that's J9+ and this is J8. - */ - @Deprecated - public void backupStarting(boolean all); - - /* - * This shouldn't be called anymore! - * Would be private, but that's J9+ and this is J8. - */ - @Deprecated - public void backupProgress(int current, int max, boolean all); - - /* - * This shouldn't be called anymore! - * Would be private, but that's J9+ and this is J8. - */ - @Deprecated - public void backupComplete(boolean all); - - /* - * This shouldn't be called anymore! - * Would be private, but that's J9+ and this is J8. - */ - @Deprecated - public void backupFailed(boolean all); - - /* - * This shouldn't be called anymore! - * Would be private, but that's J9+ and this is J8. - */ - @Deprecated - public void backupCancelled(boolean all); - - - public default void handle(BackupStatusInstance instance, boolean all) { - switch (instance.getState()) { - case STARTING: { - backupStarting(all); - break; - } - case STARTED: { - backupProgress(instance.getProgress(), instance.getMax(), all); - break; - } - case COMPLETE: { - backupComplete(all); - break; - } - case FAILED: { - backupFailed(all); - break; - } - case CANCELLED: { - backupCancelled(all); - break; - } - - case INVALID: { - ABCore.errorLogger.accept("Backup state of INVALID was attempted to be sent to clients!"); - ABCore.logStackTrace(new Exception()); - } - - } - - } - - -} +package co.uk.mommyheather.advancedbackups.interfaces; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupStatusInstance; + +public interface IClientContactor { + + /* + * This shouldn't be called anymore! + * Would be private, but that's J9+ and this is J8. + */ + @Deprecated + public void backupStarting(boolean all); + + /* + * This shouldn't be called anymore! + * Would be private, but that's J9+ and this is J8. + */ + @Deprecated + public void backupProgress(int current, int max, boolean all); + + /* + * This shouldn't be called anymore! + * Would be private, but that's J9+ and this is J8. + */ + @Deprecated + public void backupComplete(boolean all); + + /* + * This shouldn't be called anymore! + * Would be private, but that's J9+ and this is J8. + */ + @Deprecated + public void backupFailed(boolean all); + + /* + * This shouldn't be called anymore! + * Would be private, but that's J9+ and this is J8. + */ + @Deprecated + public void backupCancelled(boolean all); + + + public default void handle(BackupStatusInstance instance, boolean all) { + switch (instance.getState()) { + case STARTING: { + this.backupStarting(all); + break; + } + case STARTED: { + this.backupProgress(instance.getProgress(), instance.getMax(), all); + break; + } + case COMPLETE: { + this.backupComplete(all); + break; + } + case FAILED: { + this.backupFailed(all); + break; + } + case CANCELLED: { + this.backupCancelled(all); + break; + } + + case INVALID: { + ABCore.errorLogger.accept("Backup state of INVALID was attempted to be sent to clients!"); + ABCore.logStackTrace(new Exception()); + } + + } + + } + + +} \ No newline at end of file diff --git a/src/main/resources/advancedbackups-client-properties.txt b/core/src/main/resources/advancedbackups-client-properties.txt similarity index 100% rename from src/main/resources/advancedbackups-client-properties.txt rename to core/src/main/resources/advancedbackups-client-properties.txt diff --git a/src/main/resources/advancedbackups-properties.txt b/core/src/main/resources/advancedbackups-properties.txt similarity index 97% rename from src/main/resources/advancedbackups-properties.txt rename to core/src/main/resources/advancedbackups-properties.txt index 9ffb189f..8892b5ee 100644 --- a/src/main/resources/advancedbackups-properties.txt +++ b/core/src/main/resources/advancedbackups-properties.txt @@ -161,4 +161,4 @@ config.advancedbackups.chains.smart #What % of a full backup is allowed to be contained in a partial before forcing it into a full backup. Useful for reducing partial backup size. #Range : 1-100 #Default : 50 -config.advancedbackups.chains.maxpercent +config.advancedbackups.chains.maxpercent \ No newline at end of file diff --git a/src/main/resources/advancedbackups-readme.txt b/core/src/main/resources/advancedbackups-readme.txt similarity index 100% rename from src/main/resources/advancedbackups-readme.txt rename to core/src/main/resources/advancedbackups-readme.txt diff --git a/customTasks.gradle b/customTasks.gradle deleted file mode 100644 index 97beec9e..00000000 --- a/customTasks.gradle +++ /dev/null @@ -1,18 +0,0 @@ -def copyDestinations = ['../1.7.10-forge', '../1.12-forge', '../1.16-forge', '../1.18-forge', '../1.19.2-forge', '../1.19.3-forge', '../1.20-forge', '../1.20.2-forge', '../1.20.6-forge' - ,'../1.20.2-neoforge', '../1.20.4-neoforge', '../1.20.6-neoforge', '../1.21-neoforge' - , '../1.18-fabric', '../1.19.2-fabric', '../1.19.3-fabric', '../1.20-fabric', '../1.20.2-fabric', '../1.20.6-fabric', '../1.21-fabric' - ,'../1.21-spigot'] - - -task copyToOtherVersions - - -copyDestinations.eachWithIndex { outputDir, index -> - task "copyTo-${outputDir.substring(3)}" (type: Copy, dependsOn: 'shadowJar') { - doNotTrackState("Must run!") - from jar - into outputDir - } - copyToOtherVersions.dependsOn tasks["copyTo-${outputDir.substring(3)}"] - copyToOtherVersions.mustRunAfter tasks["copyTo-${outputDir.substring(3)}"] -} \ No newline at end of file diff --git a/fabric/1.18/README.md b/fabric/1.18/README.md new file mode 100644 index 00000000..a1bde000 --- /dev/null +++ b/fabric/1.18/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.18 + +This is the branch specifically for fabric 1.18. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.18/build.gradle b/fabric/1.18/build.gradle new file mode 100644 index 00000000..c94ccef4 --- /dev/null +++ b/fabric/1.18/build.gradle @@ -0,0 +1,99 @@ +plugins { + id 'fabric-loom' version '1.2-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.18/gradle.properties b/fabric/1.18/gradle.properties new file mode 100644 index 00000000..f0cad445 --- /dev/null +++ b/fabric/1.18/gradle.properties @@ -0,0 +1,21 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.18.2 +yarn_mappings=1.18.2+build.4 +loader_version=0.14.19 + +# Mod Properties +version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advanced-backups +modloaderName =fabric +minecraftVersion =1.18 + +# Dependencies +fabric_version=0.76.0+1.18.2 + + diff --git a/fabric/1.18/gradle/wrapper/gradle-wrapper.jar b/fabric/1.18/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.18/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.18/gradle/wrapper/gradle-wrapper.properties b/fabric/1.18/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.18/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.18/gradlew b/fabric/1.18/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.18/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.18/gradlew.bat b/fabric/1.18/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.18/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.18/settings.gradle b/fabric/1.18/settings.gradle new file mode 100644 index 00000000..1665c3ba --- /dev/null +++ b/fabric/1.18/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.18' diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..4fb0bfe7 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,152 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static MinecraftServer server; + + public static final ArrayList players = new ArrayList<>(); + + @Override + public void onInitialize() { + + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, isDedicated) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + + ServerPlayNetworking.registerGlobalReceiver(NetworkHandler.TOAST_SUBSCRIBE_ID, PacketToastSubscribe::handle); + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..38bd25ef --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..c77f9e6b --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,51 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register() { + ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + //Originally checked for permission level 0. No point checking whether that caused the issue when just a return true here works + }).then(ClientCommandManager.literal("start").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new ChatMessageC2SPacket("/backup start")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new ChatMessageC2SPacket("/backup reload-config")); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new ChatMessageC2SPacket("/backup reset-chain")); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new ChatMessageC2SPacket("/backup snapshot")); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new ChatMessageC2SPacket("/backup cancel")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..c3fc669c --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,128 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + @Override + public Visibility draw(MatrixStack matrix, ToastManager manager, long startTime) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + manager.drawTexture(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + manager.getClient().getItemRenderer().renderGuiItemIcon(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + DrawableHelper.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + DrawableHelper.fill(matrix, 3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + manager.getClient().textRenderer.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + DrawableHelper.fill(matrix, 4, 28, Math.max(3, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..7dd8f303 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,60 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.minecraft.client.MinecraftClient; + +public class ClientWrapper implements ClientModInitializer { + + public static void handle(MinecraftClient client, PacketBackupStatus message) { + + client.execute(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + client.getToastManager().add(new BackupToast()); + } + + }); + + } + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.STATUS_PACKET_ID, PacketBackupStatus::handle); + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + sender.sendPacket(NetworkHandler.TOAST_SUBSCRIBE_ID, packet.write(PacketByteBufs.create())); + }); + + AdvancedBackupsClientCommand.register(); + + } +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..2527a3e9 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the fabric 1.18 equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..6ff65483 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,22 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public class NetworkHandler { + + public static final Identifier STATUS_PACKET_ID = new Identifier("advancedbackups", "backup_status"); + public static final Identifier TOAST_SUBSCRIBE_ID = new Identifier("advancedbackups", "toast_subscribe"); + + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, STATUS_PACKET_ID, packet.write(PacketByteBufs.create())); + + } + + +} diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..b663f688 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.PacketByteBuf; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + public void read(PacketByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + return buf; + + } + + + + public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketBackupStatus message = new PacketBackupStatus(); + message.read(buf); + + ClientWrapper.handle(client, message); + + + + } + + } diff --git a/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..78362a93 --- /dev/null +++ b/fabric/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + public void read(PacketByteBuf buf) { + enable = buf.readBoolean(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(enable); + + return buf; + + } + + + public static void handle(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketToastSubscribe message = new PacketToastSubscribe(); + message.read(buf); + + + server.execute(() -> { + if (message.enable && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + }); + + + + + + } + + + +} diff --git a/fabric/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..0a1fdf4b --- /dev/null +++ b/fabric/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} diff --git a/fabric/1.18/src/main/resources/fabric.mod.json b/fabric/1.18/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..b9331a07 --- /dev/null +++ b/fabric/1.18/src/main/resources/fabric.mod.json @@ -0,0 +1,30 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.14.19", + "minecraft": "~1.18.2", + "java": ">=17", + "fabric-api": "*" + } +} \ No newline at end of file diff --git a/fabric/1.18/src/main/resources/pack.mcmeta b/fabric/1.18/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.18/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.19.2/README.md b/fabric/1.19.2/README.md new file mode 100644 index 00000000..c6254984 --- /dev/null +++ b/fabric/1.19.2/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.19 + +This is the branch specifically for fabric 1.19. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.19.2/build.gradle b/fabric/1.19.2/build.gradle new file mode 100644 index 00000000..756d668e --- /dev/null +++ b/fabric/1.19.2/build.gradle @@ -0,0 +1,99 @@ +plugins { + id 'fabric-loom' version '1.3-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.19.2/gradle.properties b/fabric/1.19.2/gradle.properties new file mode 100644 index 00000000..be497eff --- /dev/null +++ b/fabric/1.19.2/gradle.properties @@ -0,0 +1,21 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.19.2 +yarn_mappings=1.19.2+build.28 +loader_version=0.14.22 + +# Mod Properties +version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advanced-backups +modloaderName =fabric +minecraftVersion =1.19.2 + +# Dependencies +fabric_version=0.76.1+1.19.2 + + diff --git a/fabric/1.19.2/gradle/wrapper/gradle-wrapper.jar b/fabric/1.19.2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.19.2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.19.2/gradle/wrapper/gradle-wrapper.properties b/fabric/1.19.2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.19.2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.19.2/gradlew b/fabric/1.19.2/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.19.2/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.19.2/gradlew.bat b/fabric/1.19.2/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.19.2/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.19.2/settings.gradle b/fabric/1.19.2/settings.gradle new file mode 100644 index 00000000..33093018 --- /dev/null +++ b/fabric/1.19.2/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.19.2' diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..e223c4a4 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,150 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + + ServerPlayNetworking.registerGlobalReceiver(NetworkHandler.TOAST_SUBSCRIBE_ID, PacketToastSubscribe::handle); + + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..38bd25ef --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..fb2721b9 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,63 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.message.ArgumentSignatureDataMap; +import net.minecraft.network.message.LastSeenMessageList.Acknowledgment; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + //Originally checked for permission level 0, no point checking if that actually caused problems + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.consumeAcknowledgment(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, false, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.consumeAcknowledgment(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, false, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.consumeAcknowledgment(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, false, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.consumeAcknowledgment(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, false, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.consumeAcknowledgment(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, false, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..2a21d4c0 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,128 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(MatrixStack matrix, ToastManager manager, long startTime) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + manager.drawTexture(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + manager.getClient().getItemRenderer().renderGuiItemIcon(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + DrawableHelper.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + DrawableHelper.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + manager.getClient().textRenderer.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + DrawableHelper.fill(matrix, 4, 28, Math.max(3, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..53c1afd7 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.PacketByteBuf; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.STATUS_PACKET_ID, ClientWrapper::handle); + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + sender.sendPacket(NetworkHandler.TOAST_SUBSCRIBE_ID, packet.write(PacketByteBufs.create())); + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + } + + + + public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketBackupStatus message = new PacketBackupStatus(); + message.read(buf); + + client.execute(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + client.getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..638e62bf --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,23 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public class NetworkHandler { + + public static Identifier STATUS_PACKET_ID = new Identifier("advancedbackups", "backup_status"); + public static final Identifier TOAST_SUBSCRIBE_ID = new Identifier("advancedbackups", "toast_subscribe"); + + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, STATUS_PACKET_ID, packet.write(PacketByteBufs.create())); + + } + + + +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..747ccc1b --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + public void read(PacketByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + return buf; + + } + + +} diff --git a/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..28c99f76 --- /dev/null +++ b/fabric/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + public void read(PacketByteBuf buf) { + enable = buf.readBoolean(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(enable); + + return buf; + + } + + + public static void handle(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketToastSubscribe message = new PacketToastSubscribe(); + message.read(buf); + + + server.execute(() -> { + if (message.enable && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + }); + + + + + + } + + + +} \ No newline at end of file diff --git a/fabric/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..66caf08d --- /dev/null +++ b/fabric/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_cancelled": "Backup cancelled!", + "advancedbackups.backup_finished": "Backup complete!" +} \ No newline at end of file diff --git a/fabric/1.19.2/src/main/resources/fabric.mod.json b/fabric/1.19.2/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..8c190de3 --- /dev/null +++ b/fabric/1.19.2/src/main/resources/fabric.mod.json @@ -0,0 +1,30 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.14.22", + "minecraft": "~1.19.2", + "java": ">=17", + "fabric-api": "*" + } +} \ No newline at end of file diff --git a/fabric/1.19.2/src/main/resources/pack.mcmeta b/fabric/1.19.2/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.19.2/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.19.3/README.md b/fabric/1.19.3/README.md new file mode 100644 index 00000000..e138f081 --- /dev/null +++ b/fabric/1.19.3/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.19.3 + +This is the branch specifically for fabric 1.19.3 / 1.19.4 +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.19.3/build.gradle b/fabric/1.19.3/build.gradle new file mode 100644 index 00000000..57b73e9a --- /dev/null +++ b/fabric/1.19.3/build.gradle @@ -0,0 +1,102 @@ +plugins { + id 'fabric-loom' version '1.3-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +loom { + accessWidenerPath = file("src/main/resources/advancedbackups.accesswidener") +} + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.19.3/gradle.properties b/fabric/1.19.3/gradle.properties new file mode 100644 index 00000000..957e4820 --- /dev/null +++ b/fabric/1.19.3/gradle.properties @@ -0,0 +1,21 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.19.4 +yarn_mappings=1.19.4+build.2 +loader_version=0.14.24 + +# Mod Properties +version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advanced-backups +modloaderName =fabric +minecraftVersion =1.19.3 + +# Dependencies +fabric_version=0.87.1+1.19.4 + + diff --git a/fabric/1.19.3/gradle/wrapper/gradle-wrapper.jar b/fabric/1.19.3/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.19.3/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.19.3/gradle/wrapper/gradle-wrapper.properties b/fabric/1.19.3/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.19.3/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.19.3/gradlew b/fabric/1.19.3/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.19.3/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.19.3/gradlew.bat b/fabric/1.19.3/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.19.3/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.19.3/settings.gradle b/fabric/1.19.3/settings.gradle new file mode 100644 index 00000000..535da8f0 --- /dev/null +++ b/fabric/1.19.3/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.19.3' diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..e223c4a4 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,150 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + + ServerPlayNetworking.registerGlobalReceiver(NetworkHandler.TOAST_SUBSCRIBE_ID, PacketToastSubscribe::handle); + + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..0c5ac9be --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(Text.of("This command can only be ran on the client!"), false); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..eabc7e96 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,62 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.message.ArgumentSignatureDataMap; +import net.minecraft.network.message.LastSeenMessageList.Acknowledgment; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..d6193b63 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,128 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(MatrixStack matrix, ToastManager manager, long startTime) { + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + DrawableHelper.drawTexture(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + manager.getClient().getItemRenderer().renderInGui(matrix, stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + DrawableHelper.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + DrawableHelper.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + manager.getClient().textRenderer.draw(matrix, I18n.translate(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + manager.getClient().textRenderer.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + cancelled = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + DrawableHelper.fill(matrix, 4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..53c1afd7 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.PacketByteBuf; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.STATUS_PACKET_ID, ClientWrapper::handle); + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + sender.sendPacket(NetworkHandler.TOAST_SUBSCRIBE_ID, packet.write(PacketByteBufs.create())); + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + } + + + + public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketBackupStatus message = new PacketBackupStatus(); + message.read(buf); + + client.execute(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + client.getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..3aa3c276 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,22 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public class NetworkHandler { + + public static Identifier STATUS_PACKET_ID = new Identifier("advancedbackups", "backup_status"); + public static final Identifier TOAST_SUBSCRIBE_ID = new Identifier("advancedbackups", "toast_subscribe"); + + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, STATUS_PACKET_ID, packet.write(PacketByteBufs.create())); + + } + + + +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..747ccc1b --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + public void read(PacketByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + return buf; + + } + + +} diff --git a/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..28c99f76 --- /dev/null +++ b/fabric/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + public void read(PacketByteBuf buf) { + enable = buf.readBoolean(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(enable); + + return buf; + + } + + + public static void handle(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketToastSubscribe message = new PacketToastSubscribe(); + message.read(buf); + + + server.execute(() -> { + if (message.enable && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + }); + + + + + + } + + + +} \ No newline at end of file diff --git a/fabric/1.19.3/src/main/resources/advancedbackups.accesswidener b/fabric/1.19.3/src/main/resources/advancedbackups.accesswidener new file mode 100644 index 00000000..7ed2cc67 --- /dev/null +++ b/fabric/1.19.3/src/main/resources/advancedbackups.accesswidener @@ -0,0 +1,4 @@ +accessWidener v2 named + +#Required for commands for now, should rework this at some point +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; \ No newline at end of file diff --git a/fabric/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..66caf08d --- /dev/null +++ b/fabric/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_cancelled": "Backup cancelled!", + "advancedbackups.backup_finished": "Backup complete!" +} \ No newline at end of file diff --git a/fabric/1.19.3/src/main/resources/fabric.mod.json b/fabric/1.19.3/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..6024ee33 --- /dev/null +++ b/fabric/1.19.3/src/main/resources/fabric.mod.json @@ -0,0 +1,31 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "accessWidener" : "advancedbackups.accesswidener", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.14.22", + "minecraft": ">=1.19.3", + "java": ">=17", + "fabric-api": "*" + } +} diff --git a/fabric/1.19.3/src/main/resources/pack.mcmeta b/fabric/1.19.3/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.19.3/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.20.2/README.md b/fabric/1.20.2/README.md new file mode 100644 index 00000000..8c973847 --- /dev/null +++ b/fabric/1.20.2/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.20.2 + +This is the branch specifically for fabric 1.20.2. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.20.2/build.gradle b/fabric/1.20.2/build.gradle new file mode 100644 index 00000000..4b591d83 --- /dev/null +++ b/fabric/1.20.2/build.gradle @@ -0,0 +1,102 @@ +plugins { + id 'fabric-loom' version '1.4-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +loom { + accessWidenerPath = file("src/main/resources/advancedbackups.accesswidener") +} + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.20.2/gradle.properties b/fabric/1.20.2/gradle.properties new file mode 100644 index 00000000..c2803258 --- /dev/null +++ b/fabric/1.20.2/gradle.properties @@ -0,0 +1,21 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.20.2 +yarn_mappings=1.20.2+build.4 +loader_version=0.14.24 + +# Mod Properties +version=2.0 +mod_version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advancedbackups +modloaderName =fabric +minecraftVersion =1.20.2 + +# Dependencies +fabric_version=0.90.4+1.20.2 + diff --git a/fabric/1.20.2/gradle/wrapper/gradle-wrapper.jar b/fabric/1.20.2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.20.2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.20.2/gradle/wrapper/gradle-wrapper.properties b/fabric/1.20.2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.20.2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.20.2/gradlew b/fabric/1.20.2/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.20.2/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.20.2/gradlew.bat b/fabric/1.20.2/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.20.2/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.20.2/settings.gradle b/fabric/1.20.2/settings.gradle new file mode 100644 index 00000000..369e988c --- /dev/null +++ b/fabric/1.20.2/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.20.2' diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..56295a07 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,150 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + ServerPlayNetworking.registerGlobalReceiver(NetworkHandler.TOAST_SUBSCRIBE_ID, PacketToastSubscribe::handle); + + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..b25fcf01 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(() -> Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..eabc7e96 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,62 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.message.ArgumentSignatureDataMap; +import net.minecraft.network.message.LastSeenMessageList.Acknowledgment; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..5e07eee1 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,129 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final Identifier TEXTURE = new Identifier("toast/advancement"); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(DrawContext context, ToastManager manager, long startTime) { + context.drawGuiTexture(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + + context.drawItemWithoutEntity(stack, 8, 8);; + + float percent = finished ? 100 : (float) progress / (float) max; + + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + context.drawText(manager.getClient().textRenderer, title, 25, 11, textColour, false); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + context.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..53c1afd7 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.PacketByteBuf; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.STATUS_PACKET_ID, ClientWrapper::handle); + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + sender.sendPacket(NetworkHandler.TOAST_SUBSCRIBE_ID, packet.write(PacketByteBufs.create())); + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + } + + + + public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketBackupStatus message = new PacketBackupStatus(); + message.read(buf); + + client.execute(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + client.getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..638e62bf --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,23 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public class NetworkHandler { + + public static Identifier STATUS_PACKET_ID = new Identifier("advancedbackups", "backup_status"); + public static final Identifier TOAST_SUBSCRIBE_ID = new Identifier("advancedbackups", "toast_subscribe"); + + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, STATUS_PACKET_ID, packet.write(PacketByteBufs.create())); + + } + + + +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..747ccc1b --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + public void read(PacketByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + return buf; + + } + + +} diff --git a/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..28c99f76 --- /dev/null +++ b/fabric/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + public void read(PacketByteBuf buf) { + enable = buf.readBoolean(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(enable); + + return buf; + + } + + + public static void handle(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketToastSubscribe message = new PacketToastSubscribe(); + message.read(buf); + + + server.execute(() -> { + if (message.enable && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + }); + + + + + + } + + + +} \ No newline at end of file diff --git a/fabric/1.20.2/src/main/resources/advancedbackups.accesswidener b/fabric/1.20.2/src/main/resources/advancedbackups.accesswidener new file mode 100644 index 00000000..7ed2cc67 --- /dev/null +++ b/fabric/1.20.2/src/main/resources/advancedbackups.accesswidener @@ -0,0 +1,4 @@ +accessWidener v2 named + +#Required for commands for now, should rework this at some point +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; \ No newline at end of file diff --git a/fabric/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/fabric/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/fabric/1.20.2/src/main/resources/fabric.mod.json b/fabric/1.20.2/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..d73a73ab --- /dev/null +++ b/fabric/1.20.2/src/main/resources/fabric.mod.json @@ -0,0 +1,31 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "accessWidener" : "advancedbackups.accesswidener", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.14.22", + "minecraft": ">=1.20", + "java": ">=17", + "fabric-api": "*" + } +} diff --git a/fabric/1.20.2/src/main/resources/pack.mcmeta b/fabric/1.20.2/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.20.2/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.20.6/README.md b/fabric/1.20.6/README.md new file mode 100644 index 00000000..09fc99bd --- /dev/null +++ b/fabric/1.20.6/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.20.6 + +This is the branch specifically for fabric 1.20.6. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.20.6/build.gradle b/fabric/1.20.6/build.gradle new file mode 100644 index 00000000..fb682fa5 --- /dev/null +++ b/fabric/1.20.6/build.gradle @@ -0,0 +1,102 @@ +plugins { + id 'fabric-loom' version '1.6-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +loom { + accessWidenerPath = file("src/main/resources/advancedbackups.accesswidener") +} + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 21 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.20.6/gradle.properties b/fabric/1.20.6/gradle.properties new file mode 100644 index 00000000..3aef5f05 --- /dev/null +++ b/fabric/1.20.6/gradle.properties @@ -0,0 +1,22 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.20.5 +yarn_mappings=1.20.5+build.1 +loader_version=0.15.10 + +# Mod Properties +version=2.0 +mod_version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advancedbackups +modloaderName =fabric +#We support 1.20.6, but it shouldn't be used so we won't show in the jar name. +minecraftVersion =1.20.6 + +# Dependencies +fabric_version=0.97.5+1.20.5 + diff --git a/fabric/1.20.6/gradle/wrapper/gradle-wrapper.jar b/fabric/1.20.6/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.20.6/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.20.6/gradle/wrapper/gradle-wrapper.properties b/fabric/1.20.6/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.20.6/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.20.6/gradlew b/fabric/1.20.6/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.20.6/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.20.6/gradlew.bat b/fabric/1.20.6/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.20.6/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.20.6/settings.gradle b/fabric/1.20.6/settings.gradle new file mode 100644 index 00000000..ad70fbd7 --- /dev/null +++ b/fabric/1.20.6/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.20.6' diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..0cbe8505 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,153 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + PayloadTypeRegistry.playS2C().register(PacketBackupStatus.ID, PacketBackupStatus.CODEC); + PayloadTypeRegistry.playC2S().register(PacketToastSubscribe.ID, PacketToastSubscribe.CODEC); + + ServerPlayNetworking.registerGlobalReceiver(PacketToastSubscribe.ID, PacketToastSubscribe::handle); + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..b25fcf01 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(() -> Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..8b79e09f --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,53 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config")); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain")); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot")); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..5e07eee1 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,129 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final Identifier TEXTURE = new Identifier("toast/advancement"); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(DrawContext context, ToastManager manager, long startTime) { + context.drawGuiTexture(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + + context.drawItemWithoutEntity(stack, 8, 8);; + + float percent = finished ? 100 : (float) progress / (float) max; + + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + context.drawText(manager.getClient().textRenderer, title, 25, 11, textColour, false); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + context.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..8670dc7a --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,76 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.minecraft.client.MinecraftClient; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + if (ClientPlayNetworking.canSend(packet.getId())) { + //Make sure a server can receive the packet before trying to send! + ClientPlayNetworking.send(packet); + } + else { + ABCore.warningLogger.accept("Refusing to send packet " + packet + " as the server cannot accept it!"); + } + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + ClientPlayNetworking.registerGlobalReceiver(PacketBackupStatus.ID, ClientWrapper::handle); + + + } + + + + public static void handle(PacketBackupStatus message, ClientPlayNetworking.Context context) { + + + + MinecraftClient.getInstance().execute(() -> { + BackupToast.starting = message.starting(); + BackupToast.started = message.started(); + BackupToast.failed = message.failed(); + BackupToast.finished = message.finished(); + BackupToast.cancelled = message.cancelled(); + + BackupToast.progress = message.progress(); + BackupToast.max = message.max(); + + if (!BackupToast.exists) { + BackupToast.exists = true; + MinecraftClient.getInstance().getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..c8d6daf4 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,16 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; + +public class NetworkHandler { + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, packet); + + } + + + +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..87659b07 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,38 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; + +public record PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, int max) implements CustomPayload { + + public static final Id ID = CustomPayload.id("advancedbackups:backup_status"); + + public static final PacketCodec CODEC = PacketCodec.of((packet, buf) -> { + buf.writeBoolean(packet.starting); + buf.writeBoolean(packet.started); + buf.writeBoolean(packet.failed); + buf.writeBoolean(packet.finished); + buf.writeBoolean(packet.cancelled); + buf.writeInt(packet.progress); + buf.writeInt(packet.max); + }, + + buf -> new PacketBackupStatus( + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readInt(), + buf.readInt() + )); + + + @Override + public Id getId() { + return ID; + } + + +} diff --git a/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..a476ba85 --- /dev/null +++ b/fabric/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,44 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.network.ServerPlayerEntity; + +public record PacketToastSubscribe(boolean enable) implements CustomPayload { + + public static final Id ID = CustomPayload.id("advancedbackups:toast_subscribe"); + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public static final PacketCodec CODEC = PacketCodec.tuple(PacketCodecs.BOOL, PacketToastSubscribe::enable, PacketToastSubscribe::new); + + + public static void handle(PacketToastSubscribe message, ServerPlayNetworking.Context context) { + + ServerPlayerEntity player = context.player(); + + if (message.enable() && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable()) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + } + + + + @Override + public Id getId() { + return ID; + } + + + +} \ No newline at end of file diff --git a/fabric/1.20.6/src/main/resources/advancedbackups.accesswidener b/fabric/1.20.6/src/main/resources/advancedbackups.accesswidener new file mode 100644 index 00000000..7ed2cc67 --- /dev/null +++ b/fabric/1.20.6/src/main/resources/advancedbackups.accesswidener @@ -0,0 +1,4 @@ +accessWidener v2 named + +#Required for commands for now, should rework this at some point +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; \ No newline at end of file diff --git a/fabric/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/fabric/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/fabric/1.20.6/src/main/resources/fabric.mod.json b/fabric/1.20.6/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..118fa15f --- /dev/null +++ b/fabric/1.20.6/src/main/resources/fabric.mod.json @@ -0,0 +1,31 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "accessWidener" : "advancedbackups.accesswidener", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.15.10", + "minecraft": ">=1.20.5", + "java": ">=21", + "fabric-api": "*" + } +} \ No newline at end of file diff --git a/fabric/1.20.6/src/main/resources/pack.mcmeta b/fabric/1.20.6/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.20.6/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.20/README.md b/fabric/1.20/README.md new file mode 100644 index 00000000..09db287c --- /dev/null +++ b/fabric/1.20/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.20 + +This is the branch specifically for fabric 1.20. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.20/build.gradle b/fabric/1.20/build.gradle new file mode 100644 index 00000000..c69976b0 --- /dev/null +++ b/fabric/1.20/build.gradle @@ -0,0 +1,101 @@ +plugins { + id 'fabric-loom' version '1.3-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +loom { + accessWidenerPath = file("src/main/resources/advancedbackups.accesswidener") +} + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.20/gradle.properties b/fabric/1.20/gradle.properties new file mode 100644 index 00000000..d170e8aa --- /dev/null +++ b/fabric/1.20/gradle.properties @@ -0,0 +1,22 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.20 +yarn_mappings=1.20+build.1 +loader_version=0.14.22 + +# Mod Properties +version=2.0 +mod_version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advancedbackups +modloaderName =fabric +minecraftVersion =1.20 + +# Dependencies +fabric_version=0.83.0+1.20 + + diff --git a/fabric/1.20/gradle/wrapper/gradle-wrapper.jar b/fabric/1.20/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.20/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.20/gradle/wrapper/gradle-wrapper.properties b/fabric/1.20/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.20/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.20/gradlew b/fabric/1.20/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.20/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.20/gradlew.bat b/fabric/1.20/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.20/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.20/settings.gradle b/fabric/1.20/settings.gradle new file mode 100644 index 00000000..df54f372 --- /dev/null +++ b/fabric/1.20/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.20' diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..56295a07 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,150 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + ServerPlayNetworking.registerGlobalReceiver(NetworkHandler.TOAST_SUBSCRIBE_ID, PacketToastSubscribe::handle); + + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..b25fcf01 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(() -> Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..eabc7e96 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,62 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.message.ArgumentSignatureDataMap; +import net.minecraft.network.message.LastSeenMessageList.Acknowledgment; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + Acknowledgment acknowledgment = MinecraftClient.getInstance().player.networkHandler.lastSeenMessagesCollector.collect().update(); + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel", Instant.now(), 0L, ArgumentSignatureDataMap.EMPTY, acknowledgment)); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..28114239 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,127 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Formatting; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(DrawContext context, ToastManager manager, long startTime) { + context.drawTexture(TEXTURE, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + + context.drawItemWithoutEntity(stack, 8, 8);; + + float percent = finished ? 100 : (float) progress / (float) max; + + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + context.drawText(manager.getClient().textRenderer, title, 25, 11, textColour, false); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + context.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..53c1afd7 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.PacketByteBuf; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.STATUS_PACKET_ID, ClientWrapper::handle); + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + sender.sendPacket(NetworkHandler.TOAST_SUBSCRIBE_ID, packet.write(PacketByteBufs.create())); + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + } + + + + public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketBackupStatus message = new PacketBackupStatus(); + message.read(buf); + + client.execute(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + client.getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..638e62bf --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,23 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public class NetworkHandler { + + public static Identifier STATUS_PACKET_ID = new Identifier("advancedbackups", "backup_status"); + public static final Identifier TOAST_SUBSCRIBE_ID = new Identifier("advancedbackups", "toast_subscribe"); + + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, STATUS_PACKET_ID, packet.write(PacketByteBufs.create())); + + } + + + +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..747ccc1b --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + public void read(PacketByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + return buf; + + } + + +} diff --git a/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..28c99f76 --- /dev/null +++ b/fabric/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + public void read(PacketByteBuf buf) { + enable = buf.readBoolean(); + } + + public PacketByteBuf write(PacketByteBuf buf) { + buf.writeBoolean(enable); + + return buf; + + } + + + public static void handle(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { + + PacketToastSubscribe message = new PacketToastSubscribe(); + message.read(buf); + + + server.execute(() -> { + if (message.enable && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + }); + + + + + + } + + + +} \ No newline at end of file diff --git a/fabric/1.20/src/main/resources/advancedbackups.accesswidener b/fabric/1.20/src/main/resources/advancedbackups.accesswidener new file mode 100644 index 00000000..7ed2cc67 --- /dev/null +++ b/fabric/1.20/src/main/resources/advancedbackups.accesswidener @@ -0,0 +1,4 @@ +accessWidener v2 named + +#Required for commands for now, should rework this at some point +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; \ No newline at end of file diff --git a/fabric/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/fabric/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/fabric/1.20/src/main/resources/fabric.mod.json b/fabric/1.20/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..d73a73ab --- /dev/null +++ b/fabric/1.20/src/main/resources/fabric.mod.json @@ -0,0 +1,31 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "accessWidener" : "advancedbackups.accesswidener", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.14.22", + "minecraft": ">=1.20", + "java": ">=17", + "fabric-api": "*" + } +} diff --git a/fabric/1.20/src/main/resources/pack.mcmeta b/fabric/1.20/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.20/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/fabric/1.21/README.md b/fabric/1.21/README.md new file mode 100644 index 00000000..5b8d590a --- /dev/null +++ b/fabric/1.21/README.md @@ -0,0 +1,4 @@ +# Fabric - 1.21 + +This is the branch specifically for fabric 1.21. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/fabric/1.21/build.gradle b/fabric/1.21/build.gradle new file mode 100644 index 00000000..62db7add --- /dev/null +++ b/fabric/1.21/build.gradle @@ -0,0 +1,102 @@ +plugins { + id 'fabric-loom' version '1.7-SNAPSHOT' + id 'maven-publish' +} + +apply from: '../../global.properties' + +group = project.maven_group +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +loom { + accessWidenerPath = file("src/main/resources/advancedbackups.accesswidener") +} + + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 21 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/fabric/1.21/gradle.properties b/fabric/1.21/gradle.properties new file mode 100644 index 00000000..60d3a070 --- /dev/null +++ b/fabric/1.21/gradle.properties @@ -0,0 +1,22 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.21 +yarn_mappings=1.21+build.2 +loader_version=0.15.11 + +# Mod Properties +version=2.0 +mod_version=2.0 +maven_group=co.uk.mommyheather.advancedbackups +archives_base_name=advancedbackups +modloaderName =fabric +#We support 1.20.6, but it shouldn't be used so we won't show in the jar name. +minecraftVersion =1.21 + +# Dependencies +fabric_version=0.100.3+1.21 + diff --git a/fabric/1.21/gradle/wrapper/gradle-wrapper.jar b/fabric/1.21/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/fabric/1.21/gradle/wrapper/gradle-wrapper.jar differ diff --git a/fabric/1.21/gradle/wrapper/gradle-wrapper.properties b/fabric/1.21/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/fabric/1.21/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/fabric/1.21/gradlew b/fabric/1.21/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/fabric/1.21/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/fabric/1.21/gradlew.bat b/fabric/1.21/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/fabric/1.21/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/fabric/1.21/settings.gradle b/fabric/1.21/settings.gradle new file mode 100644 index 00000000..d8266a5a --- /dev/null +++ b/fabric/1.21/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = 'fabric-1.21' diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..0cbe8505 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,153 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.impl.FabricLoaderImpl; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +public class AdvancedBackups implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + + public static final Logger LOGGER = LoggerFactory.getLogger("advanced-backups"); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @Override + public void onInitialize() { + + ServerLifecycleEvents.SERVER_STARTING.register((server) -> { + AdvancedBackups.server = server; + ABCore.worldName = server.getSaveProperties().getLevelName(); + ABCore.worldDir = server.getSavePath(WorldSavePath.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = new File(FabricLoaderImpl.INSTANCE.getModContainer("advancedbackups").get().getOrigin().getPaths().get(0).toAbsolutePath().toString()); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + }); + + ServerLifecycleEvents.SERVER_STARTED.register((server) -> { + BackupWrapper.checkStartupBackups(); + }); + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + BackupWrapper.checkShutdownBackups(); + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + ABCore.setActivity(true); + }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + AdvancedBackupsCommand.register(dispatcher); + }); + + + ServerTickEvents.END_SERVER_TICK.register((server) -> { + BackupTimer.check(); + }); + + PayloadTypeRegistry.playS2C().register(PacketBackupStatus.ID, PacketBackupStatus.CODEC); + PayloadTypeRegistry.playC2S().register(PacketToastSubscribe.ID, PacketToastSubscribe.CODEC); + + ServerPlayNetworking.registerGlobalReceiver(PacketToastSubscribe.ID, PacketToastSubscribe::handle); + + + } + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && !level.savingDisabled) { + level.savingDisabled = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (ServerWorld level : server.getWorlds()) { + if (level != null && level.savingDisabled) { + level.savingDisabled = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = AdvancedBackups.server; + server.saveAll(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerManager().getPlayerList(); + ABCore.setActivity(!players.isEmpty()); + } +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..b25fcf01 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(CommandManager.literal("backup").requires((runner) -> { + return !AdvancedBackups.server.isDedicated() || runner.hasPermissionLevel(3); + }).then(CommandManager.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendFeedback(() -> Text.of(response), true); + }); + return 1; + })) + + .then(CommandManager.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendFeedback(() -> Text.of("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..8b79e09f --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,53 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket; +import net.minecraft.text.Text; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(ClientCommandManager.literal("backup").requires((runner) -> { + return true; + }).then(ClientCommandManager.literal("start").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup start")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-config").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reload-config")); + return 1; + })) + + .then(ClientCommandManager.literal("reset-chain").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup reset-chain")); + return 1; + })) + + .then(ClientCommandManager.literal("snapshot").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup snapshot")); + return 1; + })) + + .then(ClientCommandManager.literal("cancel").executes((runner) -> { + MinecraftClient.getInstance().player.networkHandler.sendPacket(new CommandExecutionC2SPacket("backup cancel")); + return 1; + })) + + .then(ClientCommandManager.literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendFeedback(Text.of(response)); + }); + return 1; + })) + + ); + } + + +} \ No newline at end of file diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..df2766b1 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,125 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.Toast; +import net.minecraft.client.toast.ToastManager; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Identifier; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final Identifier TEXTURE = Identifier.of("toast/advancement"); + + public static String title = "You shouldn't see this!"; + public static int textColour = 0; + + @Override + public Visibility draw(DrawContext context, ToastManager manager, long startTime) { + context.drawGuiTexture(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.getHeight(), this.getWidth(), this.getHeight()); + + context.drawItemWithoutEntity(stack, 8, 8);; + + float percent = finished ? 100 : (float) progress / (float) max; + + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.translate("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + context.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + context.drawText(manager.getClient().textRenderer, I18n.translate(title), 25, 11, textColour, false); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.translate("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.translate("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + context.drawText(manager.getClient().textRenderer, title, 25, 11, textColour, false); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + context.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..0de8fc0a --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = AdvancedBackups.server; + List players = server.getPlayerManager().getPlayerList(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getUuidAsString())) continue; + if (!server.isDedicated() || player.hasPermissionLevel(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..8670dc7a --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,76 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.minecraft.client.MinecraftClient; + +public class ClientWrapper implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + ABCore.infoLogger = AdvancedBackups.infoLogger; + ABCore.warningLogger = AdvancedBackups.warningLogger; + ABCore.errorLogger = AdvancedBackups.errorLogger; + + ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { + ClientConfigManager.loadOrCreateConfig(); + }); + + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + PacketToastSubscribe packet = new PacketToastSubscribe(ClientConfigManager.showProgress.get()); + if (ClientPlayNetworking.canSend(packet.getId())) { + //Make sure a server can receive the packet before trying to send! + ClientPlayNetworking.send(packet); + } + else { + ABCore.warningLogger.accept("Refusing to send packet " + packet + " as the server cannot accept it!"); + } + }); + + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + AdvancedBackupsClientCommand.register(dispatcher); + + }); + + + ClientPlayNetworking.registerGlobalReceiver(PacketBackupStatus.ID, ClientWrapper::handle); + + + } + + + + public static void handle(PacketBackupStatus message, ClientPlayNetworking.Context context) { + + + + MinecraftClient.getInstance().execute(() -> { + BackupToast.starting = message.starting(); + BackupToast.started = message.started(); + BackupToast.failed = message.failed(); + BackupToast.finished = message.finished(); + BackupToast.cancelled = message.cancelled(); + + BackupToast.progress = message.progress(); + BackupToast.max = message.max(); + + if (!BackupToast.exists) { + BackupToast.exists = true; + MinecraftClient.getInstance().getToastManager().add(new BackupToast()); + } + + }); + + + } + +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..46a47b01 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//What is the modern equivalent..? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..c8d6daf4 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,16 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.network.ServerPlayerEntity; + +public class NetworkHandler { + + public static void sendToClient(ServerPlayerEntity player, PacketBackupStatus packet) { + + ServerPlayNetworking.send(player, packet); + + } + + + +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..a4e3f084 --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,39 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.Identifier; + +public record PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, int max) implements CustomPayload { + + public static final Id ID = new CustomPayload.Id(Identifier.of("advancedbackups:backup_status")); + + public static final PacketCodec CODEC = PacketCodec.of((packet, buf) -> { + buf.writeBoolean(packet.starting); + buf.writeBoolean(packet.started); + buf.writeBoolean(packet.failed); + buf.writeBoolean(packet.finished); + buf.writeBoolean(packet.cancelled); + buf.writeInt(packet.progress); + buf.writeInt(packet.max); + }, + + buf -> new PacketBackupStatus( + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readInt(), + buf.readInt() + )); + + + @Override + public Id getId() { + return ID; + } + + +} diff --git a/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..09382dfc --- /dev/null +++ b/fabric/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +public record PacketToastSubscribe(boolean enable) implements CustomPayload { + + public static final Id ID = new CustomPayload.Id(Identifier.of("advancedbackups:toast_subscribe")); + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public static final PacketCodec CODEC = PacketCodec.tuple(PacketCodecs.BOOL, PacketToastSubscribe::enable, PacketToastSubscribe::new); + + + public static void handle(PacketToastSubscribe message, ServerPlayNetworking.Context context) { + + ServerPlayerEntity player = context.player(); + + if (message.enable() && !AdvancedBackups.players.contains(player.getUuidAsString())) { + AdvancedBackups.players.add(player.getUuidAsString()); + } + else if (!message.enable()) { + AdvancedBackups.players.remove(player.getUuidAsString()); + } + + } + + + + @Override + public Id getId() { + return ID; + } + + + +} \ No newline at end of file diff --git a/fabric/1.21/src/main/resources/advancedbackups.accesswidener b/fabric/1.21/src/main/resources/advancedbackups.accesswidener new file mode 100644 index 00000000..7ed2cc67 --- /dev/null +++ b/fabric/1.21/src/main/resources/advancedbackups.accesswidener @@ -0,0 +1,4 @@ +accessWidener v2 named + +#Required for commands for now, should rework this at some point +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; \ No newline at end of file diff --git a/fabric/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json b/fabric/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/fabric/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/fabric/1.21/src/main/resources/fabric.mod.json b/fabric/1.21/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..13190e66 --- /dev/null +++ b/fabric/1.21/src/main/resources/fabric.mod.json @@ -0,0 +1,31 @@ +{ + "schemaVersion": 1, + "id": "advancedbackups", + "version": "${version}", + "accessWidener" : "advancedbackups.accesswidener", + "name": "Advanced Backups", + "description": "An extremely advanced backup mod.\n\nSupports many backup types.\n\nConfig file and github contain documentation.", + "authors": [ + "Mommy Heather" + ], + "contact": { + "homepage": "https://github.com/mommyheather/advancedbackups" + }, + "license": "BSD", + "icon": "assets/advanced-backups/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "co.uk.mommyheather.advancedbackups.AdvancedBackups" + ], + "client": [ + "co.uk.mommyheather.advancedbackups.client.ClientWrapper" + ] + }, + "depends": { + "fabricloader": ">=0.15.10", + "minecraft": ">=1.20.6", + "java": ">=21", + "fabric-api": "*" + } +} \ No newline at end of file diff --git a/fabric/1.21/src/main/resources/pack.mcmeta b/fabric/1.21/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/fabric/1.21/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.12/README.md b/forge/1.12/README.md new file mode 100644 index 00000000..59c5927e --- /dev/null +++ b/forge/1.12/README.md @@ -0,0 +1,7 @@ +# Forge - 1.12 + +This is the branch specifically for forge 1.12. +Any differences will be listed below. For full documentation, see the `core` branch. + + +- Flush config value is ignored, but this is meaningless for most users as in most cases it should be kept at the default `false` diff --git a/forge/1.12/build.gradle b/forge/1.12/build.gradle new file mode 100644 index 00000000..c907ea2c --- /dev/null +++ b/forge/1.12/build.gradle @@ -0,0 +1,144 @@ +plugins { + id 'eclipse' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '5.1.+' +} + +apply from: '../../global.properties' + +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly. + + + +task prepareVSCodeRun(type: Copy) { + from jar + into "run/mods" + rename (".*", "advancedbackups-vscode.jar") + //output of the jar task is copied into run/mods, with the name "advancedbackups-vscode.jar", overwriting any existing one +} + +prepareVSCodeRun.mustRunAfter jar + + +minecraft { + // The mappings can be changed at any time, and must be in the following format. + // snapshot_YYYYMMDD Snapshot are built nightly. + // stable_# Stables are built at the discretion of the MCP team. + // Use non-default mappings at your own risk. they may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + //mappings channel: 'snapshot', version: '20171003-1.12' + mappings channel: 'snapshot', version: '20171003-1.12' + // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. + + // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + client { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + + // Recommended logging level for the console + property 'forge.logging.console.level', 'debug' + } + + server { + + // Recommended logging data for a userdev environment + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + + // Recommended logging level for the console + property 'forge.logging.console.level', 'debug' + } + } +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed + // that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.12.2-14.23.5.2855' + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // You may put jars on which you depend on in ./libs or you may define them like so.. + // compile "some.group:artifact:version:classifier" + // compile "some.group:artifact:version" + + // Real examples + // compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env + // compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env + + // The 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime. + // provided 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // These dependencies get remapped to your current MCP mappings + // deobf 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html + +} + +// Example for how to get properties into the manifest for reading by the runtime.. +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + mod_version: version + ] + inputs.properties replaceProperties + + filesMatching(['mcmod.info']) { + expand replaceProperties + [project: project] + } +} + +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version": "${version}", + "Implementation-Vendor" :"mommyheather", + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish task +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +//publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file:///${project.projectDir}/mcmodsrepo" + } + } +} diff --git a/forge/1.12/gradle.properties b/forge/1.12/gradle.properties new file mode 100644 index 00000000..1587f7a0 --- /dev/null +++ b/forge/1.12/gradle.properties @@ -0,0 +1,7 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + +modloaderName =forge +minecraftVersion =1.12 \ No newline at end of file diff --git a/forge/1.12/gradle/wrapper/gradle-wrapper.jar b/forge/1.12/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..afba1092 Binary files /dev/null and b/forge/1.12/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.12/gradle/wrapper/gradle-wrapper.properties b/forge/1.12/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..c7d437bb --- /dev/null +++ b/forge/1.12/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.12/gradlew b/forge/1.12/gradlew new file mode 100755 index 00000000..65dcd68d --- /dev/null +++ b/forge/1.12/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.12/gradlew.bat b/forge/1.12/gradlew.bat new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/forge/1.12/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.12/settings.gradle b/forge/1.12/settings.gradle new file mode 100644 index 00000000..24ae1cb2 --- /dev/null +++ b/forge/1.12/settings.gradle @@ -0,0 +1,10 @@ +pluginManagement { + repositories { + maven { + name 'MinecraftForge' + url 'https://maven.minecraftforge.net/' + } + } +} + +rootProject.name = 'forge-1.12' diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..976c2401 --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,201 @@ +package co.uk.mommyheather.advancedbackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.apache.logging.log4j.Logger; + +import co.uk.mommyheather.advancedbackups.client.ClientBridge; +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastTest; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.WorldServer; +import net.minecraftforge.client.event.ClientChatEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.fml.common.Mod.EventHandler; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.event.FMLServerStartedEvent; +import net.minecraftforge.fml.common.event.FMLServerStartingEvent; +import net.minecraftforge.fml.common.event.FMLServerStoppingEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.PlayerEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; +import net.minecraftforge.fml.relauncher.Side; + + +@Mod(modid = AdvancedBackups.MODID, name = AdvancedBackups.NAME, acceptableRemoteVersions = "*") +@EventBusSubscriber +public class AdvancedBackups +{ + public static final String MODID = "advancedbackups"; + public static final String NAME = "Advanced Backups"; + + private static Logger LOGGER; + public static Consumer infoLogger; + public static Consumer warningLogger; + public static Consumer errorLogger; + + public static final ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @EventHandler + public void preInit(FMLPreInitializationEvent event) + { + LOGGER = event.getModLog(); + infoLogger = LOGGER::info; + warningLogger = LOGGER::warn; + errorLogger = LOGGER::error; + } + + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + NetworkHandler.registerMessages(); + } + + @EventHandler + public void onServerStarting(FMLServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().worlds[0].getWorldInfo().getWorldName(); + + //Yes, this works. Yes, it feels FUCKING ILLEGAL + if (event.getSide() == Side.SERVER) { + ABCore.worldDir = new File(event.getServer().getFolderName(), "./").toPath(); + } + else { + ABCore.worldDir = new File("saves/" + event.getServer().getFolderName(), "./").toPath(); + } + // the extra ./ is because some of the code in core calls a getParent as it was required when devving in my forge 1.18 instance, but versions earlier than 1.16 do not have this requirement + + server = event.getServer(); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + + ABCore.modJar = Loader.instance().getIndexedModList().get("advancedbackups").getSource(); + //ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + + + event.registerServerCommand(new AdvancedBackupsCommand()); + + } + + @EventHandler + public void onServerStarted(FMLServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @EventHandler + public void onServerStopping(FMLServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + @SubscribeEvent + public void onPlayerConnect(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void onTickEnd(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + @SubscribeEvent + public void onConnectedToServer(PlayerLoggedInEvent event) { + + //Here's our answer. + //Player logs in. Server sends them a packet. Client responds with a different packet to indicate their wish to subscribe to toasts. + //It means a single packet at login is sent to clients without the mod, but it should be dropped fine. + if (event.player instanceof EntityPlayerMP) { + NetworkHandler.HANDLER.sendTo(new PacketToastTest(), (EntityPlayerMP) event.player); + } + + + /*ClientConfigManager.loadOrCreateConfig(); + + //NetworkHandler.HANDLER.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + + + //You serious? If I use the above line, the packet is just never received. + //DONE : rework this in a nicer way. Use something other than a fucking threaded five second delay. + new Thread(() -> { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + + } + NetworkHandler.HANDLER.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + }).start();*/ + } + + @SubscribeEvent + public void onClientChat(ClientChatEvent event) { + ClientBridge.onClientChat(event); + } + + + + public static final String savesDisabledMessage = "\n\n\n***************************************\nSAVING DISABLED - PREPARING FOR BACKUP!\n***************************************"; + public static final String savesEnabledMessage = "\n\n\n*********************************\nSAVING ENABLED - BACKUP COMPLETE!\n*********************************"; + public static final String saveCompleteMessage = "\n\n\n*************************************\nSAVE COMPLETE - PREPARING FOR BACKUP!\n*************************************"; + + + public static void disableSaving() { + for (WorldServer level : server.worlds) { + if (level != null && !level.disableLevelSaving) { + level.disableLevelSaving = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + for (WorldServer level : server.worlds) { + if (level != null && level.disableLevelSaving) { + level.disableLevelSaving = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean unused) { //no flush bool in 1.12 either + server.saveAllWorlds(false); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..451761be --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,195 @@ +package co.uk.mommyheather.advancedbackups; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.command.CommandException; + +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.text.TextComponentString; +import net.minecraftforge.server.command.CommandTreeBase; + +public class AdvancedBackupsCommand extends CommandTreeBase +{ + public AdvancedBackupsCommand() + { + addSubcommand(new Start()); + addSubcommand(new Reload()); + addSubcommand(new ReloadClient()); + addSubcommand(new ResetChain()); + addSubcommand(new Snapshot()); + addSubcommand(new Cancel()); + } + + @Override + public String getName() + { + return "backup"; + } + + + @Override + public int getRequiredPermissionLevel() + { + return 3; + } + + @Override + public String getUsage(ICommandSender icommandsender) + { + return "/backup (check|start|reload-config|reload-client-config|snapshot|cancel)"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + + public static class Reload extends CommandTreeBase { + public Reload(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + CoreCommandSystem.reloadConfig((response) -> { + sender.sendMessage(new TextComponentString(response)); + }); + } + @Override + public String getName() + { + return "reload-config"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.reload-config.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } + + public static class ReloadClient extends CommandTreeBase { + public ReloadClient(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + sender.sendMessage(new TextComponentString("This command can only be executed on a client!")); + } + @Override + public String getName() + { + return "reload-client-config"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.reload-client-config.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } + + public static class Start extends CommandTreeBase { + public Start(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + CoreCommandSystem.startBackup((response) -> { + sender.sendMessage(new TextComponentString(response)); + }); + } + @Override + public String getName() + { + return "start"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.start.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } + + public static class ResetChain extends CommandTreeBase { + public ResetChain(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + CoreCommandSystem.resetChainLength((response) -> { + sender.sendMessage(new TextComponentString(response)); + }); + } + @Override + public String getName() + { + return "reset-chain"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.resetchain.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } + + public static class Snapshot extends CommandTreeBase { + public Snapshot(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + CoreCommandSystem.snapshot((response) -> { + sender.sendMessage(new TextComponentString(response)); + }); + } + @Override + public String getName() + { + return "snapshot"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.snapshot.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } + + public static class Cancel extends CommandTreeBase { + public Cancel(){} + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + CoreCommandSystem.cancelBackup((response) -> { + sender.sendMessage(new TextComponentString(response)); + }); + } + @Override + public String getName() + { + return "cancel"; + } + @Override + public String getUsage(ICommandSender sender) { + return "commands.backup.cancel.usage"; + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.checkPermission(server, sender); + } + } +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..ff411cbc --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,125 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.toasts.GuiToast; +import net.minecraft.client.gui.toasts.IToast; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.resources.I18n; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; + +public class BackupToast implements IToast { + + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour = 0; + private static String title = "You shouldn't see this!"; + + private int progressBarColor = ColourHelper.colour(255, ClientConfigManager.progressBarRed.get(), ClientConfigManager.progressBarGreen.get(), ClientConfigManager.progressBarBlue.get()); + + + @Override + public Visibility draw(GuiToast toastGui, long delta) { + toastGui.getMinecraft().getTextureManager().bindTexture(TEXTURE_TOASTS); + GlStateManager.color(1.0F, 1.0F, 1.0F); + toastGui.drawTexturedModalRect(0, 0, 0,ClientConfigManager.darkMode.get() ? 0 : 32, 160, 32); + RenderHelper.enableGUIStandardItemLighting(); + toastGui.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + Gui.drawRect(4, 28, 156, 29, ColourHelper.colour + (255, ClientConfigManager.progressBackgroundRed.get(), ClientConfigManager.progressBackgroundGreen.get(), ClientConfigManager.progressBackgroundBlue.get())); + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + toastGui.getMinecraft().fontRenderer.drawString(title, 25, 12, textColour); + if (title.equals(I18n.format("advancedbackups.backup_finished"))) Gui.drawRect(4, 28, 156, 29, progressBarColor); + return Visibility.HIDE; + } + + + + + if (starting) { + title = I18n.format("advancedbackups.backup_starting"); + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + } + else if (started) { + title = I18n.format("advancedbackups.progress", round(percent * 100)); + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + } + else if (failed) { + title = I18n.format("advancedbackups.backup_failed"); + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + title = I18n.format("advancedbackups.backup_cancelled"); + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + title = I18n.format("advancedbackups.backup_finished"); + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else title = "You shouldn't see this!"; + + toastGui.getMinecraft().fontRenderer.drawString(title, 25, 12, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + cancelled = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + + if (progress > 0 || finished) Gui.drawRect(4, 28, (int) f, 29, progressBarColor); + + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientBridge.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientBridge.java new file mode 100644 index 00000000..05eefc45 --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientBridge.java @@ -0,0 +1,39 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientChatEvent; + +public class ClientBridge { + + public static void handle(PacketBackupStatus message) { + + Minecraft.getMinecraft().addScheduledTask(() -> { + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getMinecraft().getToastGui().add(new BackupToast()); + } + + }); + + } + + + public static void onClientChat(ClientChatEvent event) { + if (event.getMessage().equals("/backup reload-client-config")) { + event.setCanceled(true); + CoreCommandSystem.reloadClientConfig(Minecraft.getMinecraft().player::sendChatMessage); + } + } + +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..07684735 --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,73 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.entity.player.EntityPlayerMP; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + List players = AdvancedBackups.server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (EntityPlayerMP player : players) { + if (!AdvancedBackups.players.contains(player.getGameProfile().getId().toString())) continue; + if (!AdvancedBackups.server.isDedicatedServer() || player.canUseCommand(3, "backup") || all) { + NetworkHandler.HANDLER.sendTo(packet, player); + } + } + } + + @Override + public void backupFailed(boolean all) { + List players = AdvancedBackups.server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (EntityPlayerMP player : players) { + if (!AdvancedBackups.players.contains(player.getGameProfile().getId().toString())) continue; + if (!AdvancedBackups.server.isDedicatedServer() || player.canUseCommand(3, "backup") || all) { + NetworkHandler.HANDLER.sendTo(packet, player); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + List players = AdvancedBackups.server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (EntityPlayerMP player : players) { + if (!AdvancedBackups.players.contains(player.getGameProfile().getId().toString())) continue; + if (!AdvancedBackups.server.isDedicatedServer() || player.canUseCommand(3, "backup") || all) { + NetworkHandler.HANDLER.sendTo(packet, player); + } + } + } + + @Override + public void backupStarting(boolean all) { + List players = AdvancedBackups.server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (EntityPlayerMP player : players) { + if (!AdvancedBackups.players.contains(player.getGameProfile().getId().toString())) continue; + if (!AdvancedBackups.server.isDedicatedServer() || player.canUseCommand(3, "backup") || all) { + NetworkHandler.HANDLER.sendTo(packet, player); + } + } + } + + @Override + public void backupCancelled(boolean all) { + List players = AdvancedBackups.server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (EntityPlayerMP player : players) { + if (!AdvancedBackups.players.contains(player.getGameProfile().getId().toString())) continue; + if (!AdvancedBackups.server.isDedicatedServer() || player.canUseCommand(3, "backup") || all) { + NetworkHandler.HANDLER.sendTo(packet, player); + } + } + } + +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..a02d0c68 --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,30 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..6c38246a --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,24 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraftforge.fml.common.network.NetworkRegistry; +import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import net.minecraftforge.fml.relauncher.Side; + +public class NetworkHandler { + public static SimpleNetworkWrapper HANDLER; + private static int packetId = 0; + + + public static int nextID() { + return packetId++; + } + + public static void registerMessages() { + HANDLER = NetworkRegistry.INSTANCE.newSimpleChannel(AdvancedBackups.MODID); + // Register messages which are sent from the client to the server here: + HANDLER.registerMessage(PacketBackupStatus.Handler.class, PacketBackupStatus.class, nextID(), Side.CLIENT); + HANDLER.registerMessage(PacketToastSubscribe.Handler.class, PacketToastSubscribe.class, nextID(), Side.SERVER); + HANDLER.registerMessage(PacketToastTest.Handler.class, PacketToastTest.class, nextID(), Side.CLIENT); + } +} diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..8600293e --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,78 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ClientBridge; +import io.netty.buffer.ByteBuf; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +public class PacketBackupStatus implements IMessage{ + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + @Override + public void fromBytes(ByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketBackupStatus message, MessageContext ctx) { + + ClientBridge.handle(message); + + + return null; + + } + + } + + + + +} \ No newline at end of file diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..6ef4c04b --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,58 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import io.netty.buffer.ByteBuf; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +public class PacketToastSubscribe implements IMessage{ + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe() { + + } + + @Override + public void fromBytes(ByteBuf buf) { + enable = buf.readBoolean(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeBoolean(enable); + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketToastSubscribe message, MessageContext ctx) { + + String uuid = ctx.getServerHandler().player.getGameProfile().getId().toString(); + + + if (message.enable && !AdvancedBackups.players.contains(uuid)) { + AdvancedBackups.players.add(uuid); + } + else if (!message.enable) { + AdvancedBackups.players.remove(uuid); + } + + + return null; + + } + + } + + + + +} \ No newline at end of file diff --git a/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastTest.java b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastTest.java new file mode 100644 index 00000000..50c298bc --- /dev/null +++ b/forge/1.12/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastTest.java @@ -0,0 +1,49 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import io.netty.buffer.ByteBuf; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +public class PacketToastTest implements IMessage{ + + + + public PacketToastTest(boolean enable) { + + } + + public PacketToastTest() { + + } + + @Override + public void fromBytes(ByteBuf buf) { + + } + + @Override + public void toBytes(ByteBuf buf) { + + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketToastTest message, MessageContext ctx) { + + ClientConfigManager.loadOrCreateConfig(); + NetworkHandler.HANDLER.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + + return null; + + } + + } + + + + +} \ No newline at end of file diff --git a/forge/1.12/src/main/resources/assets/advancedbackups/lang/en_us.lang b/forge/1.12/src/main/resources/assets/advancedbackups/lang/en_us.lang new file mode 100644 index 00000000..37f62bc9 --- /dev/null +++ b/forge/1.12/src/main/resources/assets/advancedbackups/lang/en_us.lang @@ -0,0 +1,5 @@ +advancedbackups.backup_starting=Backup starting! +advancedbackups.progress=Backup ongoing: %s%% +advancedbackups.backup_failed=Backup failed!\nCheck log for more info. +advancedbackups.backup_cancelled=Backup cancelled! +advancedbackups.backup_finished=Backup complete! \ No newline at end of file diff --git a/forge/1.12/src/main/resources/mcmod.info b/forge/1.12/src/main/resources/mcmod.info new file mode 100644 index 00000000..5e32ad6c --- /dev/null +++ b/forge/1.12/src/main/resources/mcmod.info @@ -0,0 +1,15 @@ +[ +{ + "modid": "advancedbackups", + "name": "Advanced Backups", + "description": "Powerful backup mod.", + "version": "${mod_version}", + "url": "https://github.com/MommyHeather/AdvancedBackups", + "updateUrl": "", + "authorList": ["MommyHeather"], + "credits": "", + "logoFile": "", + "screenshots": [], + "dependencies": [] +} +] diff --git a/forge/1.12/src/main/resources/pack.mcmeta b/forge/1.12/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..40182676 --- /dev/null +++ b/forge/1.12/src/main/resources/pack.mcmeta @@ -0,0 +1,7 @@ +{ + "pack": { + "description": "examplemod resources", + "pack_format": 3, + "_comment": "A pack_format of 3 should be used starting with Minecraft 1.11. All resources, including language files, should be lowercase (eg: en_us.lang). A pack_format of 2 will load your mod resources with LegacyV2Adapter, which requires language files to have uppercase letters (eg: en_US.lang)." + } +} diff --git a/forge/1.16/README.md b/forge/1.16/README.md new file mode 100644 index 00000000..77ad2bc7 --- /dev/null +++ b/forge/1.16/README.md @@ -0,0 +1,4 @@ +# Forge - 1.16 + +This is the branch specifically for forge 1.16. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.16/build.gradle b/forge/1.16/build.gradle new file mode 100644 index 00000000..e30b7a5b --- /dev/null +++ b/forge/1.16/build.gradle @@ -0,0 +1,199 @@ +buildscript { + repositories { + maven { url = 'https://files.minecraftforge.net/maven' } + mavenCentral() + } + dependencies { + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '4.1.+', changing: true + } +} +apply plugin: 'net.minecraftforge.gradle' +// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. +apply plugin: 'eclipse' +apply plugin: 'maven-publish' + +apply from: '../../global.properties' + +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +java.toolchain.languageVersion = JavaLanguageVersion.of(8) // Mojang ships Java 8 to end users, so your mod should target Java 8. + +println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch')) +minecraft { + // The mappings can be changed at any time, and must be in the following format. + // Channel: Version: + // snapshot YYYYMMDD Snapshot are built nightly. + // stable # Stables are built at the discretion of the MCP team. + // official MCVersion Official field/method names from Mojang mapping files + // + // You must be aware of the Mojang license when using the 'official' mappings. + // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md + // + // Use non-default mappings at your own risk. they may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: 'official', version: '1.16.5' + // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. + + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + client { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be changed as needed. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + mods { + AdvancedBackups { + source sourceSets.main + } + } + } + + server { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be changed as needed. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + mods { + AdvancedBackups { + source sourceSets.main + } + } + } + + data { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be changed as needed. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', 'AdvancedBackups', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + + mods { + AdvancedBackups { + source sourceSets.main + } + } + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed + // that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.16.5-36.1.0' + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // You may put jars on which you depend on in ./libs or you may define them like so.. + // compile "some.group:artifact:version:classifier" + // compile "some.group:artifact:version" + + // Real examples + // compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env + // compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env + + // The 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime. + // provided 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // These dependencies get remapped to your current MCP mappings + // deobf 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html + +} + +tasks.withType(ProcessResources).configureEach { + def replaceProperties = [ + mod_version: version + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading by the runtime.. +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version": "${version}", + "Implementation-Vendor" :"mommyheather", + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish task +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +//publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file:///${project.projectDir}/mcmodsrepo" + } + } +} diff --git a/forge/1.16/gradle.properties b/forge/1.16/gradle.properties new file mode 100644 index 00000000..ed335388 --- /dev/null +++ b/forge/1.16/gradle.properties @@ -0,0 +1,7 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + +modloaderName =forge +minecraftVersion =1.16 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/forge/1.16/gradle/wrapper/gradle-wrapper.jar similarity index 59% rename from gradle/wrapper/gradle-wrapper.jar rename to forge/1.16/gradle/wrapper/gradle-wrapper.jar index 249e5832..e708b1c0 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/forge/1.16/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/forge/1.16/gradle/wrapper/gradle-wrapper.properties similarity index 90% rename from gradle/wrapper/gradle-wrapper.properties rename to forge/1.16/gradle/wrapper/gradle-wrapper.properties index 9809c5cf..53b9e380 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/forge/1.16/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.4-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/forge/1.16/gradlew similarity index 100% rename from gradlew rename to forge/1.16/gradlew diff --git a/forge/1.16/gradlew.bat b/forge/1.16/gradlew.bat new file mode 100644 index 00000000..107acd32 --- /dev/null +++ b/forge/1.16/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.16/settings.gradle b/forge/1.16/settings.gradle new file mode 100644 index 00000000..7175fd71 --- /dev/null +++ b/forge/1.16/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'forge-1.16' diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..4d0d3391 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,156 @@ +package co.uk.mommyheather.advancedbackups; + + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.server.ServerWorld; +import net.minecraft.world.storage.FolderName; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.event.server.FMLServerStartedEvent; +import net.minecraftforge.fml.event.server.FMLServerStartingEvent; +import net.minecraftforge.fml.event.server.FMLServerStoppingEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.fml.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogManager.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(FMLServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(FolderName.ROOT); + + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(FMLServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(FMLServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + @SubscribeEvent + public void onPlayerConneccted(PlayerLoggedInEvent event) { + ABCore.activity = true; + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + + public static final String savesDisabledMessage = "\n\n\n***************************************\nSAVING DISABLED - PREPARING FOR BACKUP!\n***************************************"; + public static final String savesEnabledMessage = "\n\n\n*********************************\nSAVING ENABLED - BACKUP COMPLETE!\n*********************************"; + public static final String saveCompleteMessage = "\n\n\n*************************************\nSAVE COMPLETE - PREPARING FOR BACKUP!\n*************************************"; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerWorld level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerWorld level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveAllChunks(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..e68622b4 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraft.util.text.StringTextComponent; +import net.minecraftforge.fml.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(new StringTextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(new StringTextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(new StringTextComponent("This command can only be ran on a client!"), true); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(new StringTextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(new StringTextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(new StringTextComponent(response), true); + }); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..658d7c87 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,132 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.systems.RenderSystem; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.AbstractGui; +import net.minecraft.client.gui.toasts.IToast; +import net.minecraft.client.gui.toasts.ToastGui; +import net.minecraft.client.resources.I18n; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.ColorHelper; + +public class BackupToast implements IToast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(MatrixStack matrix, ToastGui toastGui, long delta) { + toastGui.getMinecraft().getTextureManager().bind(TEXTURE); + RenderSystem.color3f(1.0F, 1.0F, 1.0F); + toastGui.blit(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + toastGui.getMinecraft().getItemRenderer().renderAndDecorateFakeItem(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + AbstractGui.fill(matrix, 4, 28, 156, 29, ColorHelper.PackedColor.color + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + AbstractGui.fill(matrix, 3, 28, 156, 29, ColorHelper.PackedColor.color + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + + if (starting) { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColorHelper.PackedColor.color(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + + } + + else { + title = "You shouldn't see this!"; + } + + toastGui.getMinecraft().font.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + AbstractGui.fill(matrix, 4, 28, Math.max(3, (int) f), 29, ColorHelper.PackedColor.color + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..38b20f57 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.fml.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayerEntity player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..e3ec213d --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,60 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientChatEvent; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.network.NetworkEvent.Context; + +public class ClientWrapper { + + public static void handle(Supplier ctx, PacketBackupStatus packet) { + + ctx.get().enqueueWork(() -> { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + }); + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onClientChat); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void onClientChat(ClientChatEvent event) { + if (event.getMessage().equals("/backup reload-client-config")) { + event.setCanceled(true); + + CoreCommandSystem.reloadClientConfig(Minecraft.getInstance().player::chat); + } + + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggedInEvent event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..6037e8d6 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.network.NetworkDirection; +import net.minecraftforge.fml.network.NetworkRegistry; +import net.minecraftforge.fml.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumer(PacketBackupStatus::handle) + .add(); + + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumer(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayerEntity player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..5b4b4abb --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,64 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(PacketBuffer buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(PacketBuffer buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(Supplier ctx) { + ClientWrapper.handle(ctx, this); + ctx.get().setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..e4753921 --- /dev/null +++ b/forge/1.16/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkDirection; +import net.minecraftforge.fml.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(PacketBuffer buf) { + enable = buf.readBoolean(); + } + + public void toBytes(PacketBuffer buf) { + buf.writeBoolean(enable); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (enable && !AdvancedBackups.players.contains(ctx.get().getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.get().getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.get().getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/forge/1.16/src/main/resources/META-INF/accesstransformer.cfg b/forge/1.16/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..7b120a32 --- /dev/null +++ b/forge/1.16/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.server.MinecraftServer field_71310_m # storageSource \ No newline at end of file diff --git a/forge/1.16/src/main/resources/META-INF/mods.toml b/forge/1.16/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..2952ac3f --- /dev/null +++ b/forge/1.16/src/main/resources/META-INF/mods.toml @@ -0,0 +1,58 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the forge version +loaderVersion="[32,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions. +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="BSD 3-Clause" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="advancedbackups" #mandatory +# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it +# {file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata +# see the associated build.gradle script for how to populate this completely automatically during a build +version="${mod_version}" #mandatory + # A display name for the mod +displayName="Advanced Backups" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="Thanks for this example mod goes to Java" #optional +# A text field displayed in the mod UI +authors="Mommy Heather" #optional +# The description text for the mod (multi line!) (#mandatory) +description=''' +A powerful and highly configurable backup mod. Visit https://github.com/MommyHeather/AdvancedBackups for more info. +''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.advancedbackups]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="[32,)" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.advancedbackups]] + modId="minecraft" + mandatory=true +# This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="[1.16,1.17)" + ordering="NONE" + side="BOTH" diff --git a/forge/1.16/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.16/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..66caf08d --- /dev/null +++ b/forge/1.16/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_cancelled": "Backup cancelled!", + "advancedbackups.backup_finished": "Backup complete!" +} \ No newline at end of file diff --git a/forge/1.16/src/main/resources/pack.mcmeta b/forge/1.16/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..c79a3626 --- /dev/null +++ b/forge/1.16/src/main/resources/pack.mcmeta @@ -0,0 +1,7 @@ +{ + "pack": { + "description": "examplemod resources", + "pack_format": 6, + "_comment": "A pack_format of 6 requires json lang files and some texture changes from 1.16.2. Note: we require v6 pack meta for all mods." + } +} diff --git a/forge/1.18/README.md b/forge/1.18/README.md new file mode 100644 index 00000000..7063825c --- /dev/null +++ b/forge/1.18/README.md @@ -0,0 +1,4 @@ +# Forge - 1.18 + +This is the branch specifically for forge 1.18. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.18/build.gradle b/forge/1.18/build.gradle new file mode 100644 index 00000000..7d21f1bc --- /dev/null +++ b/forge/1.18/build.gradle @@ -0,0 +1,229 @@ +buildscript { + repositories { + // These repositories are only for Gradle plugins, put any other repositories in the repository block further below + maven { url = 'https://maven.minecraftforge.net' } + mavenCentral() + } + dependencies { + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true + } +} +// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. +plugins { + id 'eclipse' + id 'maven-publish' +} +apply plugin: 'net.minecraftforge.gradle' + +apply from: '../../global.properties' + +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://github.com/ParchmentMC/Parchment/wiki/Getting-Started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: 'official', version: '1.18.2' + + // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default. + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + client { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + server { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + data { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', 'advancedbackups', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + + mods { + advancedbackups { + source sourceSets.main + } + } + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +repositories { + // Put repositories for dependencies here + // ForgeGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so: + // flatDir { + // dir 'libs' + // } +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + // Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed + // that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.18.2-40.2.1' + //extraLibs 'com.sun.jna.platform:5.13.0' + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Real mod deobf dependency examples - these get remapped to your current mappings + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency + // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency + + // Examples using mod jars from ./libs + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +tasks.withType(ProcessResources).configureEach { + def replaceProperties = [ + mod_version: version + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +// publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/forge/1.18/gradle.properties b/forge/1.18/gradle.properties new file mode 100644 index 00000000..0cb3e73b --- /dev/null +++ b/forge/1.18/gradle.properties @@ -0,0 +1,9 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + + +modloaderName =forge +minecraftVersion =1.18 \ No newline at end of file diff --git a/forge/1.18/gradle/wrapper/gradle-wrapper.jar b/forge/1.18/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..afba1092 Binary files /dev/null and b/forge/1.18/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.18/gradle/wrapper/gradle-wrapper.properties b/forge/1.18/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..c7d437bb --- /dev/null +++ b/forge/1.18/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.18/gradlew b/forge/1.18/gradlew new file mode 100755 index 00000000..65dcd68d --- /dev/null +++ b/forge/1.18/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.18/gradlew.bat b/forge/1.18/gradlew.bat new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/forge/1.18/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.18/settings.gradle b/forge/1.18/settings.gradle new file mode 100644 index 00000000..8def20d2 --- /dev/null +++ b/forge/1.18/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'forge-1.18' diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..30b468f8 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,173 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + @SubscribeEvent + public void onTickEnd(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + +*************************************** +SAVING DISABLED - PREPARING FOR BACKUP! +*************************************** +"""; + public static final String savesEnabledMessage = """ + + +********************************* +SAVING ENABLED - BACKUP COMPLETE! +********************************* +"""; + public static final String saveCompleteMessage = """ + + +************************************* +SAVE COMPLETE - PREPARING FOR BACKUP! +************************************* +"""; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(Boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..c5fb94d6 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.TextComponent; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(new TextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(new TextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(new TextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(new TextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(new TextComponent(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(new TextComponent("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..e4a485f2 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,131 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(PoseStack matrix, ToastComponent toastGui, long delta) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + toastGui.blit(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + toastGui.getMinecraft().getItemRenderer().renderGuiItem(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + Gui.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + Gui.fill(matrix, 3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + toastGui.getMinecraft().font.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + Gui.fill(matrix, 4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..e16307ad --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,51 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientChatEvent; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onClientChat); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void onClientChat(ClientChatEvent event) { + if (event.getMessage().equals("/backup reload-client-config")) { + event.setCanceled(true); + + CoreCommandSystem.reloadClientConfig(Minecraft.getInstance().player::chat); + } + + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggedInEvent event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..0eff6bc3 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in 1.18? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..3d87e79a --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumer(PacketBackupStatus::handle) + .add(); + + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumer(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..4bc03330 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,68 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + ctx.get().setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..49bbcf02 --- /dev/null +++ b/forge/1.18/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getSender() == null) return; + if (enable && !AdvancedBackups.players.contains(ctx.get().getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.get().getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.get().getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/forge/1.18/src/main/resources/META-INF/mods.toml b/forge/1.18/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..cf04c977 --- /dev/null +++ b/forge/1.18/src/main/resources/META-INF/mods.toml @@ -0,0 +1,62 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the forge version +loaderVersion="[40,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions. +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="MIT" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="advancedbackups" #mandatory +# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it +# {file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata +# see the associated build.gradle script for how to populate this completely automatically during a build +version="${mod_version}" #mandatory + # A display name for the mod +displayName="Advanced Backups" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="advancedbackups.png" #optional +# A text field displayed in the mod UI +#credits="Thanks for this example mod goes to Java" #optional +# A text field displayed in the mod UI +#authors="Love, Cheese and small house plants" #optional +# The description text for the mod (multi line!) (#mandatory) +description=''' +An extremely advanced backup mod. + +Supports many backup types. + +Config file and github contain documentation. +''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.advancedbackups]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="[40,)" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.advancedbackups]] + modId="minecraft" + mandatory=true +# This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="[1.18.2,1.19)" + ordering="NONE" + side="BOTH" diff --git a/forge/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/forge/1.18/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/forge/1.18/src/main/resources/pack.mcmeta b/forge/1.18/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.18/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.19.2/README.md b/forge/1.19.2/README.md new file mode 100644 index 00000000..11effd38 --- /dev/null +++ b/forge/1.19.2/README.md @@ -0,0 +1,4 @@ +# Forge - 1.19 + +This is the branch specifically for forge 1.19. +Any differences will be listed below. For full documentation, see the `core` branch. \ No newline at end of file diff --git a/forge/1.19.2/build.gradle b/forge/1.19.2/build.gradle new file mode 100644 index 00000000..c6937412 --- /dev/null +++ b/forge/1.19.2/build.gradle @@ -0,0 +1,191 @@ +plugins { + id 'eclipse' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '5.1.+' +} + +apply from: '../../global.properties' + +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://github.com/ParchmentMC/Parchment/wiki/Getting-Started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: 'official', version: '1.19.2' + + // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default. + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + client { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + server { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + data { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', 'advancedbackups', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + + mods { + advancedbackups { + source sourceSets.main + } + } + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + // Put repositories for dependencies here + // ForgeGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so: + // flatDir { + // dir 'libs' + // } +} + +dependencies { + // Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed + // that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.19.2-43.2.0' + //implementation files('dependencies/jansi-2.4.0.jar') + //extraLibs files('dependencies/jansi-2.4.0.jar') + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Real mod deobf dependency examples - these get remapped to your current mappings + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency + // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency + + // Examples using mod jars from ./libs + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +tasks.withType(ProcessResources).configureEach { + def replaceProperties = [ + mod_version: version + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +// publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/forge/1.19.2/gradle.properties b/forge/1.19.2/gradle.properties new file mode 100644 index 00000000..0ada9a94 --- /dev/null +++ b/forge/1.19.2/gradle.properties @@ -0,0 +1,9 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + + +modloaderName =forge +minecraftVersion =1.19.2 \ No newline at end of file diff --git a/forge/1.19.2/gradle/wrapper/gradle-wrapper.jar b/forge/1.19.2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..afba1092 Binary files /dev/null and b/forge/1.19.2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.19.2/gradle/wrapper/gradle-wrapper.properties b/forge/1.19.2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..c7d437bb --- /dev/null +++ b/forge/1.19.2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.19.2/gradlew b/forge/1.19.2/gradlew new file mode 100755 index 00000000..65dcd68d --- /dev/null +++ b/forge/1.19.2/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.19.2/gradlew.bat b/forge/1.19.2/gradlew.bat new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/forge/1.19.2/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.19.2/settings.gradle b/forge/1.19.2/settings.gradle new file mode 100644 index 00000000..f7c14937 --- /dev/null +++ b/forge/1.19.2/settings.gradle @@ -0,0 +1,8 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { url = 'https://maven.minecraftforge.net/' } + } +} + +rootProject.name = 'forge-1.19.2' diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..30b468f8 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,173 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + @SubscribeEvent + public void onTickEnd(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + +*************************************** +SAVING DISABLED - PREPARING FOR BACKUP! +*************************************** +"""; + public static final String savesEnabledMessage = """ + + +********************************* +SAVING ENABLED - BACKUP COMPLETE! +********************************* +"""; + public static final String saveCompleteMessage = """ + + +************************************* +SAVE COMPLETE - PREPARING FOR BACKUP! +************************************* +"""; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(Boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..dcac6da8 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(Component.literal("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..b6eae5ca --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,65 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, false, Minecraft.getInstance().player.connection.generateMessageAcknowledgements())); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, false, Minecraft.getInstance().player.connection.generateMessageAcknowledgements())); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, false, Minecraft.getInstance().player.connection.generateMessageAcknowledgements())); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, false, Minecraft.getInstance().player.connection.generateMessageAcknowledgements())); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, false, Minecraft.getInstance().player.connection.generateMessageAcknowledgements())); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..700bcc16 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,130 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(PoseStack matrix, ToastComponent toastGui, long delta) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + toastGui.blit(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + toastGui.getMinecraft().getItemRenderer().renderGuiItem(stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + Gui.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + Gui.fill(matrix, 3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + toastGui.getMinecraft().font.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + Gui.fill(matrix, 4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..8ed0cb03 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..3d87e79a --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumer(PacketBackupStatus::handle) + .add(); + + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumer(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..4bc03330 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,68 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + ctx.get().setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..49bbcf02 --- /dev/null +++ b/forge/1.19.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getSender() == null) return; + if (enable && !AdvancedBackups.players.contains(ctx.get().getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.get().getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.get().getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/forge/1.19.2/src/main/resources/META-INF/mods.toml b/forge/1.19.2/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..ee6771cd --- /dev/null +++ b/forge/1.19.2/src/main/resources/META-INF/mods.toml @@ -0,0 +1,63 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the forge version +loaderVersion="[40,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions. +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="BSD" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="advancedbackups" #mandatory +# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it +# {file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata +# see the associated build.gradle script for how to populate this completely automatically during a build +version="${mod_version}" #mandatory + # A display name for the mod +displayName="Advanced Backups" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="advancedbackups.png" #optional +# A text field displayed in the mod UI +#credits="Thanks for this example mod goes to Java" #optional +# A text field displayed in the mod UI +#authors="Love, Cheese and small house plants" #optional +# The description text for the mod (multi line!) (#mandatory) +description=''' +An extremely advanced backup mod. + +Supports many backup types. + +Config file and github contain documentation. +''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.advancedbackups]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="[40,)" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.advancedbackups]] + modId="minecraft" + mandatory=true +# This version range declares a minimum of the current minecraft version up to but not including the next major version +# 1.19.3 is a break - as such, we can't support it with this branch, and we'll need a new one... fuck me + versionRange="[1.19,1.19.3)" + ordering="NONE" + side="BOTH" diff --git a/forge/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..66caf08d --- /dev/null +++ b/forge/1.19.2/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_cancelled": "Backup cancelled!", + "advancedbackups.backup_finished": "Backup complete!" +} \ No newline at end of file diff --git a/forge/1.19.2/src/main/resources/pack.mcmeta b/forge/1.19.2/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.19.2/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.19.3/README.md b/forge/1.19.3/README.md new file mode 100644 index 00000000..50337d0b --- /dev/null +++ b/forge/1.19.3/README.md @@ -0,0 +1,4 @@ +# Forge - 1.19.3 + +This is the branch specifically for forge 1.19.3 / 1.19.4. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.19.3/build.gradle b/forge/1.19.3/build.gradle new file mode 100644 index 00000000..40fb4ba5 --- /dev/null +++ b/forge/1.19.3/build.gradle @@ -0,0 +1,192 @@ +plugins { + id 'eclipse' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '[6.0,6.2)' +} + +apply from: '../../global.properties' + +group = 'co.uk.mommyheather.advancedbackups' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://github.com/ParchmentMC/Parchment/wiki/Getting-Started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: 'official', version: '1.19.4' + + // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default. + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + client { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + server { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', 'advancedbackups' + + mods { + advancedbackups { + source sourceSets.main + } + } + } + + data { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'REGISTRIES' + + property 'forge.logging.console.level', 'debug' + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', 'advancedbackups', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + + mods { + advancedbackups { + source sourceSets.main + } + } + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + // Put repositories for dependencies here + // ForgeGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so: + // flatDir { + // dir 'libs' + // } +} + +dependencies { + // Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed + // that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.19.4-45.2.4' + //implementation files('dependencies/jansi-2.4.0.jar') + //extraLibs files('dependencies/jansi-2.4.0.jar') + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Real mod deobf dependency examples - these get remapped to your current mappings + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency + // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency + + // Examples using mod jars from ./libs + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +tasks.withType(ProcessResources).configureEach { + def replaceProperties = [ + mod_version: version + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +jar { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +// publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/forge/1.19.3/gradle.properties b/forge/1.19.3/gradle.properties new file mode 100644 index 00000000..44f431f2 --- /dev/null +++ b/forge/1.19.3/gradle.properties @@ -0,0 +1,9 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + + +modloaderName =forge +minecraftVersion =1.19.3 \ No newline at end of file diff --git a/forge/1.19.3/gradle/wrapper/gradle-wrapper.jar b/forge/1.19.3/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/forge/1.19.3/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.19.3/gradle/wrapper/gradle-wrapper.properties b/forge/1.19.3/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/forge/1.19.3/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.19.3/gradlew b/forge/1.19.3/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/forge/1.19.3/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.19.3/gradlew.bat b/forge/1.19.3/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/forge/1.19.3/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.19.3/settings.gradle b/forge/1.19.3/settings.gradle new file mode 100644 index 00000000..8ead78fa --- /dev/null +++ b/forge/1.19.3/settings.gradle @@ -0,0 +1,8 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { url = 'https://maven.minecraftforge.net/' } + } +} + +rootProject.name = 'forge-1.19.3' diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..0557d39a --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,171 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.TickEvent.Phase; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onTickEnd(TickEvent.ServerTickEvent event) { + if (event.phase != Phase.END) return; + BackupTimer.check(); + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..dcac6da8 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(Component.literal("This command can only be ran on the client!"), true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..49cf372e --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,73 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraftforge.client.ClientCommandSourceStack; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(Component.literal(response), true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..d1c81bb3 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,131 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(PoseStack matrix, ToastComponent toastGui, long delta) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + toastGui.blit(matrix, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + toastGui.getMinecraft().getItemRenderer().renderAndDecorateFakeItem(matrix, stack, 8, 8); + + float percent = finished ? 100 : (float) progress / (float) max; + + Gui.fill(matrix, 4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + Gui.fill(matrix, 3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + toastGui.getMinecraft().font.draw(matrix, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + title = "You shouldn't see this!"; + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + toastGui.getMinecraft().font.draw(matrix, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + Gui.fill(matrix, 4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..8ed0cb03 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..3d87e79a --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumer(PacketBackupStatus::handle) + .add(); + + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumer(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..add520b0 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,69 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.progress = progress; + this.cancelled = cancelled; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + ctx.get().setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..49bbcf02 --- /dev/null +++ b/forge/1.19.3/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getSender() == null) return; + if (enable && !AdvancedBackups.players.contains(ctx.get().getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.get().getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.get().getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/forge/1.19.3/src/main/resources/META-INF/accesstransformer.cfg b/forge/1.19.3/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..e456e70b --- /dev/null +++ b/forge/1.19.3/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener f_244346_ # lastSeenMessages \ No newline at end of file diff --git a/forge/1.19.3/src/main/resources/META-INF/mods.toml b/forge/1.19.3/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..ac1b72c1 --- /dev/null +++ b/forge/1.19.3/src/main/resources/META-INF/mods.toml @@ -0,0 +1,63 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the forge version +loaderVersion="[40,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions. +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="BSD" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="advancedbackups" #mandatory +# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it +# {file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata +# see the associated build.gradle script for how to populate this completely automatically during a build +version="${mod_version}" #mandatory + # A display name for the mod +displayName="Advanced Backups" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="advancedbackups.png" #optional +# A text field displayed in the mod UI +#credits="Thanks for this example mod goes to Java" #optional +# A text field displayed in the mod UI +#authors="Love, Cheese and small house plants" #optional +# The description text for the mod (multi line!) (#mandatory) +description=''' +An extremely advanced backup mod. + +Supports many backup types. + +Config file and github contain documentation. +''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.advancedbackups]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="[40,)" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.advancedbackups]] + modId="minecraft" + mandatory=true +# This version range declares a minimum of the current minecraft version up to but not including the next major version +# 1.19.3 is a break - as such, we can't support it with this branch, and we'll need a new one... fuck me + versionRange="[1.19.3,1.20)" + ordering="NONE" + side="BOTH" diff --git a/forge/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..cc68dae1 --- /dev/null +++ b/forge/1.19.3/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,6 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!" +} \ No newline at end of file diff --git a/forge/1.19.3/src/main/resources/pack.mcmeta b/forge/1.19.3/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.19.3/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.20.2/README.md b/forge/1.20.2/README.md new file mode 100644 index 00000000..92844ff3 --- /dev/null +++ b/forge/1.20.2/README.md @@ -0,0 +1,4 @@ +# Forge - 1.20.2 + +This is the branch specifically for forge 1.20.2. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.20.2/build.gradle b/forge/1.20.2/build.gradle new file mode 100644 index 00000000..48b6e51b --- /dev/null +++ b/forge/1.20.2/build.gradle @@ -0,0 +1,226 @@ +plugins { + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '[6.0.14,6.2)' + id 'org.spongepowered.mixin' version '0.7.+' +} + +apply from: '../../global.properties' + +group = mod_group_id + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + +task copyVSCodeResources (dependsOn: 'processResources', type: Copy) { + from 'build/resources' + into 'bin/' +} + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/NeoForged/NeoForm/blob/main/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://parchmentmc.org/docs/getting-started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: mapping_channel, version: mapping_version + + // When true, this property will have all Eclipse/IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game. + // In most cases, it is not necessary to enable. + // enableEclipsePrepareRuns = true + // enableIdeaPrepareRuns = true + + // This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game. + // It is REQUIRED to be set to true for this template to function. + // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html + copyIdeResources = true + + // When true, this property will add the folder name of all declared run configurations to generated IDE run configurations. + // The folder name can be set on a run configuration using the "folderName" property. + // By default, the folder name of a run configuration is the name of the Gradle project containing it. + // generateRunFolders = true + + // This property enables access transformers for use in development. + // They will be applied to the Minecraft artifact. + // The access transformer file can be anywhere in the project. + // However, it must be at "META-INF/accesstransformer.cfg" in the final mod jar to be loaded by Forge. + // This default location is a best practice to automatically put the file in the right place in the final jar. + // See https://docs.neoforged.net/docs/1.20.x/advanced/accesstransformers/ for more information. + + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + // applies to all the run configs below + configureEach { + workingDirectory project.file("run/${it.name}") + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + mods { + "${mod_id}" { + source sourceSets.main + } + } + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', mod_id + } + + server { + property 'forge.enabledGameTestNamespaces', mod_id + args '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + property 'forge.enabledGameTestNamespaces', mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + // Put repositories for dependencies here + // NeoGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver + // flatDir { + // dir 'libs' + // } +} + +dependencies { + // Specify the version of Minecraft to use. + // Any artifact can be supplied so long as it has a "userdev" classifier artifact and is a compatible patcher artifact. + // The "userdev" classifier will be requested and setup by NeoGradle. + // If the group id is "net.minecraft" and the artifact id is one of ["client", "server", "joined"], + // then special handling is done to allow a setup of a vanilla dependency without the use of an external repository. + minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" + + // Example mod dependency with JEI - using fg.deobf() ensures the dependency is remapped to your development mappings + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-common-api:${jei_version}") + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-forge-api:${jei_version}") + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}-forge:${jei_version}") + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range, + forge_version: forge_version, forge_version_range: forge_version_range, + loader_version_range: loader_version_range, + mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors: mod_authors, mod_description: mod_description, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } + // This is the preferred method to reobfuscate your jar file + finalizedBy 'reobfJar' +} + +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing: +// tasks.named('publish').configure { +// dependsOn 'reobfJar' +// } + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/forge/1.20.2/gradle.properties b/forge/1.20.2/gradle.properties new file mode 100644 index 00000000..e5f82103 --- /dev/null +++ b/forge/1.20.2/gradle.properties @@ -0,0 +1,65 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Neo version to get a valid artifact +minecraft_version=1.20.2 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20.2,1.21) +# The Forge version must agree with the Minecraft version to get a valid artifact +forge_version=48.0.31 +# The Forge version range can use any version of Forge as bounds or match the loader version range +forge_version_range=[48,) +# The loader version range can only use the major version of Forge/FML as bounds +loader_version_range=[48,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.2 + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=15 + + +modloaderName =forge +minecraftVersion =1.20.2 \ No newline at end of file diff --git a/forge/1.20.2/gradle/wrapper/gradle-wrapper.jar b/forge/1.20.2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/forge/1.20.2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.20.2/gradle/wrapper/gradle-wrapper.properties b/forge/1.20.2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/forge/1.20.2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.20.2/gradlew b/forge/1.20.2/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/forge/1.20.2/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.20.2/gradlew.bat b/forge/1.20.2/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/forge/1.20.2/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.20.2/settings.gradle b/forge/1.20.2/settings.gradle new file mode 100644 index 00000000..808294b0 --- /dev/null +++ b/forge/1.20.2/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'MinecraftForge' + url = 'https://maven.minecraftforge.net/' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + +rootProject.name = 'forge-1.20.2' diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..084534ce --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,172 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..d73383d5 --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!"); + }, true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..236f914d --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,75 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraftforge.client.ClientCommandSourceStack; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..243e4401 --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = new ResourceLocation("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..47771b8c --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus message) { + + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.INSTANCE.send(new PacketToastSubscribe(ClientConfigManager.showProgress.get()), Minecraft.getInstance().getConnection().getConnection()); + //NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..55344d1d --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,38 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.ChannelBuilder; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = ChannelBuilder.named(new ResourceLocation("advancedbackups", "main")).clientAcceptedVersions((status, version) -> true).simpleChannel(); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumerNetworkThread(PacketBackupStatus::handle) + .add(); + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumerNetworkThread(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.send(packet, player.connection.getConnection()); + } + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..64b4c617 --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,66 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.event.network.CustomPayloadEvent; +import net.minecraftforge.fml.LogicalSide; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public static boolean handle(PacketBackupStatus message, CustomPayloadEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(message); + } + }); + ctx.setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..f9faf535 --- /dev/null +++ b/forge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,40 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.event.network.CustomPayloadEvent; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public static boolean handle(PacketToastSubscribe message, CustomPayloadEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getSender() == null) return; + if (message.enable && !AdvancedBackups.players.contains(ctx.getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.getSender().getStringUUID()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(ctx.getSender().getStringUUID()); + } + }); + ctx.setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg b/forge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..e456e70b --- /dev/null +++ b/forge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener f_244346_ # lastSeenMessages \ No newline at end of file diff --git a/forge/1.20.2/src/main/resources/META-INF/mods.toml b/forge/1.20.2/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..450de68c --- /dev/null +++ b/forge/1.20.2/src/main/resources/META-INF/mods.toml @@ -0,0 +1,61 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="${forge_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="BOTH" +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/forge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/forge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/forge/1.20.2/src/main/resources/pack.mcmeta b/forge/1.20.2/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.20.2/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.20.6/README.md b/forge/1.20.6/README.md new file mode 100644 index 00000000..6f5daf68 --- /dev/null +++ b/forge/1.20.6/README.md @@ -0,0 +1,4 @@ +# Forge - 1.20.6 + +This is the branch specifically for forge 1.20.6. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.20.6/build.gradle b/forge/1.20.6/build.gradle new file mode 100644 index 00000000..524c4787 --- /dev/null +++ b/forge/1.20.6/build.gradle @@ -0,0 +1,228 @@ +plugins { + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '[6.0.24,6.2)' +} + +apply from: '../../global.properties' + +group = mod_group_id + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + +task copyVSCodeResources (dependsOn: 'processResources', type: Copy) { + from 'build/resources' + into 'bin/' +} + +// Mojang ships Java 21 to end users in 1.20.5+, so your mod should target Java 21. +java.toolchain.languageVersion = JavaLanguageVersion.of(21) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/NeoForged/NeoForm/blob/main/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://parchmentmc.org/docs/getting-started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: mapping_channel, version: mapping_version + + // Tell FG to not automtically create the reobf tasks, as we now use Official mappings at runtime, If you don't use them at dev time then you'll have to fix your reobf yourself. + reobf = false + + // When true, this property will have all Eclipse/IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game. + // In most cases, it is not necessary to enable. + // enableEclipsePrepareRuns = true + // enableIdeaPrepareRuns = true + + // This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game. + // It is REQUIRED to be set to true for this template to function. + // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html + copyIdeResources = true + + // When true, this property will add the folder name of all declared run configurations to generated IDE run configurations. + // The folder name can be set on a run configuration using the "folderName" property. + // By default, the folder name of a run configuration is the name of the Gradle project containing it. + // generateRunFolders = true + + // This property enables access transformers for use in development. + // They will be applied to the Minecraft artifact. + // The access transformer file can be anywhere in the project. + // However, it must be at "META-INF/accesstransformer.cfg" in the final mod jar to be loaded by Forge. + // This default location is a best practice to automatically put the file in the right place in the final jar. + // See https://docs.neoforged.net/docs/1.20.x/advanced/accesstransformers/ for more information. + + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + // applies to all the run configs below + configureEach { + workingDirectory project.file('run') + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', mod_id + } + + server { + property 'forge.enabledGameTestNamespaces', mod_id + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + property 'forge.enabledGameTestNamespaces', mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above + workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + // Put repositories for dependencies here + // NeoGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver + // flatDir { + // dir 'libs' + // } +} + +dependencies { + // Specify the version of Minecraft to use. + // Any artifact can be supplied so long as it has a "userdev" classifier artifact and is a compatible patcher artifact. + // The "userdev" classifier will be requested and setup by NeoGradle. + // If the group id is "net.minecraft" and the artifact id is one of ["client", "server", "joined"], + // then special handling is done to allow a setup of a vanilla dependency without the use of an external repository. + minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" + + // Example mod dependency with JEI - using fg.deobf() ensures the dependency is remapped to your development mappings + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-common-api:${jei_version}") + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-forge-api:${jei_version}") + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}-forge:${jei_version}") + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html + + // Hack fix for now, force jopt-simple to be exactly 5.0.4 because Mojang ships that version, but some transitive dependencies request 6.0+ + implementation('net.sf.jopt-simple:jopt-simple:5.0.4') { version { strictly '5.0.4' } } +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range, + forge_version: forge_version, forge_version_range: forge_version_range, + loader_version_range: loader_version_range, + mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors: mod_authors, mod_description: mod_description, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} + +// Merge the resources and classes into the same directory. +// This is done because java expects modules to be in a single directory. +// And if we have it in multiple we have to do performance intensive hacks like having the UnionFileSystem +// This will eventually be migrated to ForgeGradle so modders don't need to manually do it. But that is later. +sourceSets.each { + def dir = layout.buildDirectory.dir("sourcesSets/$it.name") + it.output.resourcesDir = dir + it.java.destinationDirectory = dir +} diff --git a/forge/1.20.6/gradle.properties b/forge/1.20.6/gradle.properties new file mode 100644 index 00000000..d0986fab --- /dev/null +++ b/forge/1.20.6/gradle.properties @@ -0,0 +1,65 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Forge version to get a valid artifact +minecraft_version=1.20.6 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20.6,1.21) +# The Forge version must agree with the Minecraft version to get a valid artifact +forge_version=50.1.3 +# The Forge version range can use any version of Forge as bounds or match the loader version range +forge_version_range=[0,) +# The loader version range can only use the major version of Forge/FML as bounds +loader_version_range=[0,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.6 + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=15 + + +modloaderName =forge +minecraftVersion =1.20.6 \ No newline at end of file diff --git a/forge/1.20.6/gradle/wrapper/gradle-wrapper.jar b/forge/1.20.6/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/forge/1.20.6/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.20.6/gradle/wrapper/gradle-wrapper.properties b/forge/1.20.6/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/forge/1.20.6/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.20.6/gradlew b/forge/1.20.6/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/forge/1.20.6/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.20.6/gradlew.bat b/forge/1.20.6/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/forge/1.20.6/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.20.6/settings.gradle b/forge/1.20.6/settings.gradle new file mode 100644 index 00000000..8e6fd9be --- /dev/null +++ b/forge/1.20.6/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'MinecraftForge' + url = 'https://maven.minecraftforge.net/' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' +} + +rootProject.name = 'forge-1.20.6' diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..084534ce --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,172 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..d73383d5 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!"); + }, true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..ae3bf251 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,65 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraftforge.client.ClientCommandSourceStack; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start")); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config")); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain")); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot")); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel")); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..243e4401 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = new ResourceLocation("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..47771b8c --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus message) { + + BackupToast.starting = message.starting; + BackupToast.started = message.started; + BackupToast.failed = message.failed; + BackupToast.finished = message.finished; + BackupToast.cancelled = message.cancelled; + + BackupToast.progress = message.progress; + BackupToast.max = message.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.INSTANCE.send(new PacketToastSubscribe(ClientConfigManager.showProgress.get()), Minecraft.getInstance().getConnection().getConnection()); + //NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..3c5d7c2f --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,36 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.ChannelBuilder; +import net.minecraftforge.network.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = ChannelBuilder.named(new ResourceLocation("advancedbackups", "main")).clientAcceptedVersions((status, version) -> true).simpleChannel(); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumerNetworkThread(PacketBackupStatus::handle) + .add(); + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumerNetworkThread(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.send(packet, player.connection.getConnection()); + } + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..7e944c01 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,66 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.PacketFlow; +import net.minecraftforge.event.network.CustomPayloadEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public static boolean handle(PacketBackupStatus message, CustomPayloadEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getConnection().getReceiving() == PacketFlow.CLIENTBOUND) { + ClientWrapper.handle(message); + } + }); + ctx.setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..f9faf535 --- /dev/null +++ b/forge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,40 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.event.network.CustomPayloadEvent; + +public class PacketToastSubscribe { + + public boolean enable; + + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public static boolean handle(PacketToastSubscribe message, CustomPayloadEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getSender() == null) return; + if (message.enable && !AdvancedBackups.players.contains(ctx.getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.getSender().getStringUUID()); + } + else if (!message.enable) { + AdvancedBackups.players.remove(ctx.getSender().getStringUUID()); + } + }); + ctx.setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg b/forge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..e456e70b --- /dev/null +++ b/forge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener f_244346_ # lastSeenMessages \ No newline at end of file diff --git a/forge/1.20.6/src/main/resources/META-INF/mods.toml b/forge/1.20.6/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..f3e20eb9 --- /dev/null +++ b/forge/1.20.6/src/main/resources/META-INF/mods.toml @@ -0,0 +1,61 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="${forge_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="CLIENT" +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/forge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/forge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/forge/1.20.6/src/main/resources/pack.mcmeta b/forge/1.20.6/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.20.6/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.20/README.md b/forge/1.20/README.md new file mode 100644 index 00000000..5e146fc9 --- /dev/null +++ b/forge/1.20/README.md @@ -0,0 +1,4 @@ +# Forge - 1.20 + +This is the branch specifically for forge 1.20. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/forge/1.20/build.gradle b/forge/1.20/build.gradle new file mode 100644 index 00000000..bc753939 --- /dev/null +++ b/forge/1.20/build.gradle @@ -0,0 +1,231 @@ +plugins { + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.neoforged.gradle' version '[6.0.18,6.2)' + id 'org.spongepowered.mixin' version '0.7.+' +} + +apply from: '../../global.properties' + +group = mod_group_id + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + +task copyVSCodeResources (dependsOn: 'processResources', type: Copy) { + from 'build/resources' + into 'bin/' +} + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" +minecraft { + // The mappings can be changed at any time and must be in the following format. + // Channel: Version: + // official MCVersion Official field/method names from Mojang mapping files + // parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official + // + // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. + // See more information here: https://github.com/NeoForged/NeoForm/blob/main/Mojang.md + // + // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge + // Additional setup is needed to use their mappings: https://parchmentmc.org/docs/getting-started + // + // Use non-default mappings at your own risk. They may not always work. + // Simply re-run your setup task after changing the mappings to update your workspace. + mappings channel: mapping_channel, version: mapping_version + + // When true, this property will have all Eclipse/IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game. + // In most cases, it is not necessary to enable. + // enableEclipsePrepareRuns = true + // enableIdeaPrepareRuns = true + + // This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game. + // It is REQUIRED to be set to true for this template to function. + // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html + copyIdeResources = true + + // When true, this property will add the folder name of all declared run configurations to generated IDE run configurations. + // The folder name can be set on a run configuration using the "folderName" property. + // By default, the folder name of a run configuration is the name of the Gradle project containing it. + // generateRunFolders = true + + // This property enables access transformers for use in development. + // They will be applied to the Minecraft artifact. + // The access transformer file can be anywhere in the project. + // However, it must be at "META-INF/accesstransformer.cfg" in the final mod jar to be loaded by Forge. + // This default location is a best practice to automatically put the file in the right place in the final jar. + // See https://docs.neoforged.net/docs/1.20.x/advanced/accesstransformers/ for more information. + + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') + + // Default run configurations. + // These can be tweaked, removed, or duplicated as needed. + runs { + // applies to all the run configs below + configureEach { + workingDirectory project.file("run/${it.name}") + + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + property 'forge.logging.console.level', 'debug' + + mods { + "${mod_id}" { + source sourceSets.main + } + } + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + property 'forge.enabledGameTestNamespaces', mod_id + } + + server { + property 'forge.enabledGameTestNamespaces', mod_id + args '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + property 'forge.enabledGameTestNamespaces', mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') + } + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + // Put repositories for dependencies here + // NeoGradle automatically adds the Forge maven and Maven Central for you + + // If you have mod jar dependencies in ./libs, you can declare them as a repository like so. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver + // flatDir { + // dir 'libs' + // } + maven { + url "https://cursemaven.com" + } +} + +dependencies { + // Specify the version of Minecraft to use. + // Any artifact can be supplied so long as it has a "userdev" classifier artifact and is a compatible patcher artifact. + // The "userdev" classifier will be requested and setup by NeoGradle. + // If the group id is "net.minecraft" and the artifact id is one of ["client", "server", "joined"], + // then special handling is done to allow a setup of a vanilla dependency without the use of an external repository. + minecraft "net.neoforged:forge:${minecraft_version}-${neo_version}" + + // Example mod dependency with JEI - using fg.deobf() ensures the dependency is remapped to your development mappings + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-common-api:${jei_version}") + // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-forge-api:${jei_version}") + // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}-forge:${jei_version}") + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + + minecraftLibrary files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + implementation fg.deobf('curse.maven:jei-322036:4680512') + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, + neo_version : neo_version, neo_version_range: neo_version_range, + loader_version_range: loader_version_range, + mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } + // This is the preferred method to reobfuscate your jar file + finalizedBy 'reobfJar' +} + +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing: +// tasks.named('publish').configure { +// dependsOn 'reobfJar' +// } + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + artifact jar + } + } + repositories { + maven { + url "file://${project.projectDir}/mcmodsrepo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/forge/1.20/gradle.properties b/forge/1.20/gradle.properties new file mode 100644 index 00000000..3a84c56a --- /dev/null +++ b/forge/1.20/gradle.properties @@ -0,0 +1,63 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Neo version to get a valid artifact +minecraft_version=1.20.1 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20,1.22) +# The Neo version must agree with the Minecraft version to get a valid artifact +neo_version=47.1.65 +# The Neo version range can use any version of Neo as bounds or match the loader version range +neo_version_range=[46,) +# The loader version range can only use the major version of Neo/FML as bounds +loader_version_range=[46,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.1 + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=15 + + +modloaderName =forge +minecraftVersion =1.20 \ No newline at end of file diff --git a/forge/1.20/gradle/wrapper/gradle-wrapper.jar b/forge/1.20/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..d64cd491 Binary files /dev/null and b/forge/1.20/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.20/gradle/wrapper/gradle-wrapper.properties b/forge/1.20/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..a80b22ce --- /dev/null +++ b/forge/1.20/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.20/gradlew b/forge/1.20/gradlew new file mode 100755 index 00000000..1aa94a42 --- /dev/null +++ b/forge/1.20/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.20/gradlew.bat b/forge/1.20/gradlew.bat new file mode 100644 index 00000000..25da30db --- /dev/null +++ b/forge/1.20/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.20/settings.gradle b/forge/1.20/settings.gradle new file mode 100644 index 00000000..359817e6 --- /dev/null +++ b/forge/1.20/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'NeoForged' + url = 'https://maven.neoforged.net/releases' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + +rootProject.name = 'forge-1.20' diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..3c067bbb --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,172 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + NetworkHandler.register(); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..fcf02799 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!") ; + }, true); + return 1; + })) + + ); + } + + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..25fd2a48 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,75 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraftforge.client.ClientCommandSourceStack; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; //no point fucking with the check + }).then(literal("start").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..80adedc8 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,128 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blit(TEXTURE, 0, 0, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..e8cb1c4e --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..f2c3ef25 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + MinecraftForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..7e72feba --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumerNetworkThread(PacketBackupStatus::handle) + .add(); + + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumerNetworkThread(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..fcfe2e41 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,70 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.BackupToast; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + ctx.get().setPacketHandled(true); + return true; + + } + +} diff --git a/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..49bbcf02 --- /dev/null +++ b/forge/1.20/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(Supplier ctx) { + ctx.get().enqueueWork(() -> { + if (ctx.get().getSender() == null) return; + if (enable && !AdvancedBackups.players.contains(ctx.get().getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.get().getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.get().getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/forge/1.20/src/main/resources/META-INF/accesstransformer.cfg b/forge/1.20/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..e456e70b --- /dev/null +++ b/forge/1.20/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener f_244346_ # lastSeenMessages \ No newline at end of file diff --git a/forge/1.20/src/main/resources/META-INF/mods.toml b/forge/1.20/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..c9e2c40c --- /dev/null +++ b/forge/1.20/src/main/resources/META-INF/mods.toml @@ -0,0 +1,70 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="forge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="${neo_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.${mod_id}]] + modId="minecraft" + mandatory=true + # This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="${minecraft_version_range}" + ordering="NONE" + side="BOTH" + +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/forge/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json b/forge/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/forge/1.20/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/forge/1.20/src/main/resources/pack.mcmeta b/forge/1.20/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.20/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/forge/1.7.10/README.md b/forge/1.7.10/README.md new file mode 100644 index 00000000..0e115931 --- /dev/null +++ b/forge/1.7.10/README.md @@ -0,0 +1,8 @@ +# Forge - 1.7.10 + +This is the branch specifically for forge 1.7.10. +Any differences will be listed below. For full documentation, see the `core` branch. + + +- Backup progress is sent to ops via form of a text overlay, rather than a toast. +- Flush config value is ignored, but this is meaningless for most users as in most cases it should be kept at the default `false` diff --git a/forge/1.7.10/build.gradle.kts b/forge/1.7.10/build.gradle.kts new file mode 100644 index 00000000..6d360f54 --- /dev/null +++ b/forge/1.7.10/build.gradle.kts @@ -0,0 +1,233 @@ + +plugins { + id("java-library") + id("maven-publish") + id("org.jetbrains.gradle.plugin.idea-ext") version "1.1.7" + id("eclipse") + id("com.gtnewhorizons.retrofuturagradle") version "1.2.5" +} + +apply { + from ("../../global.properties") +} +val abCoreLibPath: String by project +//version = "2.0" +group = "co.uk.mommyheather.advancedbackups" // http://maven.apache.org/guides/mini/guide-naming-conventions.html +//archivesBaseName = "AdvancedBackups-forge-1.7.10" + + +// Set the toolchain version to decouple the Java we run Gradle with from the Java used to compile and run the mod +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(8)) + // Azul covers the most platforms for Java 8 toolchains, crucially including MacOS arm64 + vendor.set(org.gradle.jvm.toolchain.JvmVendorSpec.AZUL) + } + // Generate sources and javadocs jars when building and publishing + //withSourcesJar() + //withJavadocJar() +} + +// Most RFG configuration lives here, see the JavaDoc for com.gtnewhorizons.retrofuturagradle.MinecraftExtension +minecraft { + mcVersion.set("1.7.10") + + // Username for client run configurations + username.set("Developer") + + // Generate a field named VERSION with the mod version in the injected Tags class + injectedTags.put("VERSION", project.version) + + // If you need the old replaceIn mechanism, prefer the injectTags task because it doesn't inject a javac plugin. + // tagReplacementFiles.add("RfgExampleMod.java") + + // Enable assertions in the mod's package when running the client or server + extraRunJvmArguments.add("-ea:${project.group}") + + // If needed, add extra tweaker classes like for mixins. + // extraTweakClasses.add("org.spongepowered.asm.launch.MixinTweaker") + + // Exclude some Maven dependency groups from being automatically included in the reobfuscated runs + groupsToExcludeFromAutoReobfMapping.addAll("com.diffplug", "com.diffplug.durian", "net.industrial-craft") +} + +// Generates a class named rfg.examplemod.Tags with the mod version in it, you can find it at +tasks.injectTags.configure { + outputClassName.set("${project.group}.Tags") +} + +// Put the version from gradle into mcmod.info +tasks.processResources.configure { + inputs.property("version", project.version) + + filesMatching("mcmod.info") { + expand(mapOf("modVersion" to project.version)) + } +} + +// Create a new dependency type for runtime-only dependencies that don't get included in the maven publication +val runtimeOnlyNonPublishable: Configuration by configurations.creating { + description = "Runtime only dependencies that are not published alongside the jar" + isCanBeConsumed = false + isCanBeResolved = false +} +listOf(configurations.runtimeClasspath, configurations.testRuntimeClasspath).forEach { + it.configure { + extendsFrom( + runtimeOnlyNonPublishable + ) + } +} + +// Add an access tranformer +// tasks.deobfuscateMergedJarToSrg.configure {accessTransformerFiles.from("src/main/resources/META-INF/mymod_at.cfg")} + +// Dependencies +repositories { + maven { + name = "OvermindDL1 Maven" + url = uri("https://gregtech.overminddl1.com/") + mavenContent { + excludeGroup("net.minecraftforge") // missing the `universal` artefact + } + } + maven { + name = "GTNH Maven" + url = uri("http://jenkins.usrv.eu:8081/nexus/content/groups/public/") + isAllowInsecureProtocol = true + } +} + + +val extraLibs by configurations.creating +//stick the dependency in the jar + + +dependencies { + // Adds NotEnoughItems and its dependencies (CCL&CCC) to runClient/runServer + runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.3.39-GTNH:dev") + + implementation (files(abCoreLibPath)) + extraLibs (files(abCoreLibPath)) + + // Example: grab the ic2 jar from curse maven and deobfuscate + // api(rfg.deobf("curse.maven:ic2-242638:2353971")) + // Example: grab the ic2 jar from libs/ in the workspace and deobfuscate + // api(rfg.deobf(project.files("libs/ic2.jar"))) +} + +// Publishing to a Maven repository +publishing { + publications { + create("maven") { + from(components["java"]) + } + } + repositories { + // Example: publishing to the GTNH Maven repository + maven { + url = uri("http://jenkins.usrv.eu:8081/nexus/content/repositories/releases") + isAllowInsecureProtocol = true + credentials { + username = System.getenv("MAVEN_USER") ?: "NONE" + password = System.getenv("MAVEN_PASSWORD") ?: "NONE" + } + } + } +} + +tasks.withType { + from(zipTree(abCoreLibPath)) + into("/") + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + //from(zipTree("dependencies/jna-platform-5.13.0.jar")) + //into("/") + //duplicatesStrategy = DuplicatesStrategy.EXCLUDE + manifest { + attributes["Main-Class"] = "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI" + attributes["Implementation-Version"] = "${version}" + } +} + +tasks.jar { + archiveBaseName.set("AdvancedBackups-forge-1.7.10") +} + + + +// IDE Settings +eclipse { + classpath { + isDownloadSources = true + isDownloadJavadoc = true + } +} + +/*idea { + module { + isDownloadJavadoc = true + isDownloadSources = true + inheritOutputDirs = true // Fix resources in IJ-Native runs + } + project { + this.withGroovyBuilder { + "settings" { + "runConfigurations" { + val self = this.delegate as RunConfigurationContainer + self.add(Gradle("1. Run Client").apply { + setProperty("taskNames", listOf("runClient")) + }) + self.add(Gradle("2. Run Server").apply { + setProperty("taskNames", listOf("runServer")) + }) + self.add(Gradle("3. Run Obfuscated Client").apply { + setProperty("taskNames", listOf("runObfClient")) + }) + self.add(Gradle("4. Run Obfuscated Server").apply { + setProperty("taskNames", listOf("runObfServer")) + })*/ + /* + These require extra configuration in IntelliJ, so are not enabled by default + self.add(Application("Run Client (IJ Native, Deprecated)", project).apply { + mainClass = "GradleStart" + moduleName = project.name + ".ideVirtualMain" + afterEvaluate { + val runClient = tasks.runClient.get() + workingDirectory = runClient.workingDir.absolutePath + programParameters = runClient.calculateArgs(project).map { '"' + it + '"' }.joinToString(" ") + jvmArgs = runClient.calculateJvmArgs(project).map { '"' + it + '"' }.joinToString(" ") + + ' ' + runClient.systemProperties.map { "\"-D" + it.key + '=' + it.value.toString() + '"' } + .joinToString(" ") + } + }) + self.add(Application("Run Server (IJ Native, Deprecated)", project).apply { + mainClass = "GradleStartServer" + moduleName = project.name + ".ideVirtualMain" + afterEvaluate { + val runServer = tasks.runServer.get() + workingDirectory = runServer.workingDir.absolutePath + programParameters = runServer.calculateArgs(project).map { '"' + it + '"' }.joinToString(" ") + jvmArgs = runServer.calculateJvmArgs(project).map { '"' + it + '"' }.joinToString(" ") + + ' ' + runServer.systemProperties.map { "\"-D" + it.key + '=' + it.value.toString() + '"' } + .joinToString(" ") + } + }) + *//* + } + "compiler" { + val self = this.delegate as org.jetbrains.gradle.ext.IdeaCompilerConfiguration + afterEvaluate { + self.javac.moduleJavacAdditionalOptions = mapOf( + (project.name + ".main") to + tasks.compileJava.get().options.compilerArgs.map { '"' + it + '"' }.joinToString(" ") + ) + } + } + } + } + } +}*/ + +tasks.processIdeaSettings.configure { + dependsOn(tasks.injectTags) +} diff --git a/forge/1.7.10/gradle/wrapper/gradle-wrapper.jar b/forge/1.7.10/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..ccebba77 Binary files /dev/null and b/forge/1.7.10/gradle/wrapper/gradle-wrapper.jar differ diff --git a/forge/1.7.10/gradle/wrapper/gradle-wrapper.properties b/forge/1.7.10/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..bdc9a83b --- /dev/null +++ b/forge/1.7.10/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/forge/1.7.10/gradlew b/forge/1.7.10/gradlew new file mode 100755 index 00000000..79a61d42 --- /dev/null +++ b/forge/1.7.10/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/forge/1.7.10/gradlew.bat b/forge/1.7.10/gradlew.bat new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/forge/1.7.10/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/forge/1.7.10/settings.gradle.kts b/forge/1.7.10/settings.gradle.kts new file mode 100644 index 00000000..0a2a8960 --- /dev/null +++ b/forge/1.7.10/settings.gradle.kts @@ -0,0 +1,24 @@ +rootProject.name = "forge-1.7.10" + +pluginManagement { + repositories { + maven { + // RetroFuturaGradle + name = "GTNH Maven" + url = uri("http://jenkins.usrv.eu:8081/nexus/content/groups/public/") + isAllowInsecureProtocol = true + mavenContent { + includeGroup("com.gtnewhorizons") + includeGroup("com.gtnewhorizons.retrofuturagradle") + } + } + gradlePluginPortal() + mavenCentral() + mavenLocal() + } +} + +plugins { + // Automatic toolchain provisioning + id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0" +} \ No newline at end of file diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..a8c31cda --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,198 @@ +package co.uk.mommyheather.advancedbackups; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.management.ServerConfigurationManager; +import net.minecraft.util.IProgressUpdate; +import net.minecraft.world.MinecraftException; +import net.minecraft.world.WorldServer; + +import org.apache.logging.log4j.Logger; + +import co.uk.mommyheather.advancedbackups.client.ABClientContactor; +import co.uk.mommyheather.advancedbackups.client.ABClientRenderer; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartedEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import net.minecraftforge.common.MinecraftForge; + +import java.io.File; +import java.util.ArrayList; +import java.util.function.Consumer; + +@Mod(modid = AdvancedBackups.MODID, name = AdvancedBackups.NAME, acceptableRemoteVersions = "*") +public class AdvancedBackups +{ + public static final String MODID = "advancedbackups"; + public static final String NAME = "Advanced Backups"; + + private static Logger LOGGER; + public static Consumer infoLogger; + public static Consumer warningLogger; + public static Consumer errorLogger; + + public static ArrayList players = new ArrayList<>(); + + public static MinecraftServer server; + + @EventHandler + public void preInit(FMLPreInitializationEvent event) + { + LOGGER = event.getModLog(); + infoLogger = LOGGER::info; + warningLogger = LOGGER::warn; + errorLogger = LOGGER::error; + } + + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); + FMLCommonHandler.instance().bus().register(this); + MinecraftForge.EVENT_BUS.register(ABClientRenderer.INSTANCE); + FMLCommonHandler.instance().bus().register(ABClientRenderer.INSTANCE); + NetworkHandler.init(); + } + + @EventHandler + public void onServerStarting(FMLServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().worldServers[0].getWorldInfo().getWorldName(); + + //Yes, this works. Yes, it feels FUCKING ILLEGAL + if (event.getSide() == Side.SERVER) { + ABCore.worldDir = new File(event.getServer().getFolderName(), "./").toPath(); + } + else { + ABCore.worldDir = new File("saves/" + event.getServer().getFolderName(), "./").toPath(); + } + // the extra ./ is because some of the code in core calls a getParent as it was required when devving in my forge 1.18 instance, but versions earlier than 1.16 do not have this requirement + + server = event.getServer(); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + ABCore.clientContactor = new ABClientContactor(); + ABCore.resetActivity = AdvancedBackups::resetActivity; + + event.registerServerCommand(new AdvancedBackupsCommand()); + + ABCore.modJar = Loader.instance().getIndexedModList().get("advancedbackups").getSource(); + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + + @EventHandler + public void onServerStarted(FMLServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @EventHandler + public void onServerStopping(FMLServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + @SubscribeEvent + public void onPlayerConnected(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void onTickEnd(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = "\n\n\n***************************************\nSAVING DISABLED - PREPARING FOR BACKUP!\n***************************************"; + public static final String savesEnabledMessage = "\n\n\n*********************************\nSAVING ENABLED - BACKUP COMPLETE!\n*********************************"; + public static final String saveCompleteMessage = "\n\n\n*************************************\nSAVE COMPLETE - PREPARING FOR BACKUP!\n*************************************"; + + + //fun fact : this boolean is named wrong in MCP mappings! + //reference : net.minecraft.command.server.CommandSaveOff and CommandSaveOn + //notice how off sets the boolean to true, and on sets it to false! + public static void disableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (WorldServer level : server.worldServers) { + if (level != null && !level.levelSaving) { + level.levelSaving = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = AdvancedBackups.server; + for (WorldServer level : server.worldServers) { + if (level != null && level.levelSaving) { + level.levelSaving = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean unused) { //flush doesn't seem to be an option in 1.7.10 + try { + MinecraftServer server = AdvancedBackups.server; + if (server.getConfigurationManager() != null) + { + server.getConfigurationManager().saveAllPlayerData(); + } + + int i; + WorldServer worldserver; + boolean flag; + + for (i = 0; i < server.worldServers.length; ++i) + { + if (server.worldServers[i] != null) + { + worldserver = server.worldServers[i]; + flag = worldserver.levelSaving; + worldserver.levelSaving = false; + worldserver.saveAllChunks(true, (IProgressUpdate)null); + worldserver.levelSaving = flag; + } + } + + warningLogger.accept(saveCompleteMessage); + } catch (MinecraftException e) { + // TODO Scream at user + errorLogger.accept("FAILED TO SAVE WORLD!"); + e.printStackTrace(); + } + } + + + public static void resetActivity() { + ServerConfigurationManager configurationManager = server.getConfigurationManager(); + ABCore.setActivity(!configurationManager.playerEntityList.isEmpty()); + } + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..09138844 --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,142 @@ +package co.uk.mommyheather.advancedbackups; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketClientReload; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; + +import net.minecraft.command.ICommandSender; +import net.minecraft.command.WrongUsageException; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.ChatComponentText; + +public class AdvancedBackupsCommand extends CommandBase +{ + public AdvancedBackupsCommand() + { + /*addSubcommand(new Check()); + addSubcommand(new Start()); + addSubcommand(new Force()); + addSubcommand(new Reload());*/ + } + + @Override + public String getCommandName() + { + return "backup"; + } + + + @Override + public int getRequiredPermissionLevel() + { + return 3; + } + + public String getCommandUsage(ICommandSender icommandsender) + { + return "/backup (start|reload-config|reload-client-config|reset-chain|snapshot|cancel)"; + } + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) + { + return !AdvancedBackups.server.isDedicatedServer() || super.canCommandSenderUseCommand(sender); + } + + + + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException { + if (args.length == 0) + { + throw new WrongUsageException(getCommandUsage(sender)); + } + else if ("start".equals(args[0])) + { + Start.execute(sender); + } + else if ("reload-config".equals(args[0])) + { + Reload.execute(sender); + } + else if ("reload-client-config".equals(args[0])) + { + ReloadClient.execute(sender); + } + else if ("reset-chain".equals(args[0])) { + ResetChain.execute(sender); + } + else if ("snapshot".equals(args[0])) { + Snapshot.execute(sender); + } + else if ("cancel".equals(args[0])) { + Cancel.execute(sender); + } + else + { + throw new WrongUsageException(getCommandUsage(sender)); + } + } + + public static class Reload { + public static void execute(ICommandSender sender) { + CoreCommandSystem.reloadConfig((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + } + } + + public static class ReloadClient { + public static void execute(ICommandSender sender) { + if (sender instanceof EntityPlayerMP) { + EntityPlayerMP player = (EntityPlayerMP) sender; + NetworkHandler.HANDLER.sendTo(new PacketClientReload(), player); + return; + } + sender.addChatMessage(new ChatComponentText("This can only be ran on the client!")); + /* + CoreCommandSystem.reloadClientConfig((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + */ + } + } + + public static class Start { + public static void execute(ICommandSender sender) { + CoreCommandSystem.startBackup((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + } + } + + public static class ResetChain { + public static void execute(ICommandSender sender) { + CoreCommandSystem.resetChainLength((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + } + } + + public static class Snapshot { + public static void execute(ICommandSender sender) { + CoreCommandSystem.snapshot((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + } + + } + + public static class Cancel { + public static void execute(ICommandSender sender) { + CoreCommandSystem.cancelBackup((response) -> { + sender.addChatMessage(new ChatComponentText(response)); + }); + } + + } + + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientContactor.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientContactor.java new file mode 100644 index 00000000..d05e2f0d --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientContactor.java @@ -0,0 +1,94 @@ +package co.uk.mommyheather.advancedbackups.client; + + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.management.ServerConfigurationManager; + +@SuppressWarnings("unchecked") +public class ABClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + ServerConfigurationManager configurationManager = AdvancedBackups.server.getConfigurationManager(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + configurationManager.playerEntityList.forEach((player) -> { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + if (!AdvancedBackups.players.contains(playerMP.getGameProfile().getId().toString())) return; + //if they can run the command, or are in singleplayer, or config says all players, they should receive info on active backups. + if (playerMP.canCommandSenderUseCommand(3, "advancedbackups") || !AdvancedBackups.server.isDedicatedServer() || all) { + NetworkHandler.HANDLER.sendTo(packet, playerMP); + } + } + }); + } + + @Override + public void backupFailed(boolean all) { + ServerConfigurationManager configurationManager = AdvancedBackups.server.getConfigurationManager(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + configurationManager.playerEntityList.forEach((player) -> { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + if (!AdvancedBackups.players.contains(playerMP.getGameProfile().getId().toString())) return; + //if they can run the command, or are in singleplayer, or config says all players, they should receive info on active backups. + if (playerMP.canCommandSenderUseCommand(3, "advancedbackups") || !AdvancedBackups.server.isDedicatedServer() || all) { + NetworkHandler.HANDLER.sendTo(packet, playerMP); + } + } + }); + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + ServerConfigurationManager configurationManager = AdvancedBackups.server.getConfigurationManager(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + configurationManager.playerEntityList.forEach((player) -> { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + if (!AdvancedBackups.players.contains(playerMP.getGameProfile().getId().toString())) return; + //if they can run the command, or are in singleplayer, or config says all players, they should receive info on active backups. + if (playerMP.canCommandSenderUseCommand(3, "advancedbackups") || !AdvancedBackups.server.isDedicatedServer() || all) { + NetworkHandler.HANDLER.sendTo(packet, playerMP); + } + } + }); + } + + @Override + public void backupStarting(boolean all) { + ServerConfigurationManager configurationManager = AdvancedBackups.server.getConfigurationManager(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + configurationManager.playerEntityList.forEach((player) -> { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + if (!AdvancedBackups.players.contains(playerMP.getGameProfile().getId().toString())) return; + //if they can run the command, or are in singleplayer, or config says all players, they should receive info on active backups. + if (playerMP.canCommandSenderUseCommand(3, "advancedbackups") || !AdvancedBackups.server.isDedicatedServer() || all) { + NetworkHandler.HANDLER.sendTo(packet, playerMP); + } + } + }); + } + + @Override + public void backupCancelled(boolean all) { + ServerConfigurationManager configurationManager = AdvancedBackups.server.getConfigurationManager(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + configurationManager.playerEntityList.forEach((player) -> { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + if (!AdvancedBackups.players.contains(playerMP.getGameProfile().getId().toString())) return; + //if they can run the command, or are in singleplayer, or config says all players, they should receive info on active backups. + if (playerMP.canCommandSenderUseCommand(3, "advancedbackups") || !AdvancedBackups.server.isDedicatedServer() || all) { + NetworkHandler.HANDLER.sendTo(packet, playerMP); + } + } + }); + } + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientRenderer.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientRenderer.java new file mode 100644 index 00000000..2745dd5b --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/client/ABClientRenderer.java @@ -0,0 +1,103 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.realmsclient.gui.ChatFormatting; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.network.FMLNetworkEvent.ClientConnectedToServerEvent; +import net.minecraft.client.resources.I18n; +import net.minecraftforge.client.event.RenderGameOverlayEvent; + +public class ABClientRenderer { + + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + + private static long time; + private static boolean timeSet = false; + + + public static final ABClientRenderer INSTANCE = new ABClientRenderer(); + + + @SubscribeEvent + public void onRenderEvent(RenderGameOverlayEvent.Text event) { + if (starting) { + event.left.add(ChatFormatting.GREEN + I18n.format("advancedbackups.backup_starting")); + } + else if (started) { + float percent = (float) progress / (float) max; + event.left.add(ChatFormatting.GREEN + I18n.format("advancedbackups.progress", round(percent * 100))); + } + else if (failed) { + event.left.add(ChatFormatting.RED + I18n.format("advancedbackups.backup_failed")); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + event.left.add(ChatFormatting.GREEN + I18n.format("advancedbackups.backup_finished")); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + event.left.add(ChatFormatting.RED + I18n.format("advancedbackups.backup_cancelled")); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + cancelled = false; + progress = 0; + max = 0; + timeSet = false; + } + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + + + @SubscribeEvent + public void onServerConnected(ClientConnectedToServerEvent event) { + //may aswell just do it here. 1.7 is so horribly documented + ClientConfigManager.loadOrCreateConfig(); + + + //NetworkHandler.HANDLER.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + + //You serious? If I use the above line, the packet is just never received. + //TODO : rework this in a nicer way. Use something other than a fucking threaded one second delay. + new Thread(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + + } + NetworkHandler.HANDLER.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + }).start(); + } + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..d44e1877 --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,19 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; + +public class NetworkHandler { + + public static final SimpleNetworkWrapper HANDLER = new SimpleNetworkWrapper(AdvancedBackups.MODID); + + public static void init() + { + HANDLER.registerMessage(new PacketBackupStatus.Handler(), PacketBackupStatus.class, 1, Side.CLIENT); + HANDLER.registerMessage(new PacketToastSubscribe.Handler(), PacketToastSubscribe.class, 2, Side.SERVER); + HANDLER.registerMessage(new PacketClientReload.Handler(), PacketClientReload.class, 3, Side.CLIENT); + } + +} + diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..d8bd5e94 --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,85 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ABClientRenderer; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; + +public class PacketBackupStatus implements IMessage{ + public boolean starting; + public boolean started; + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + public PacketBackupStatus() { + + } + + @Override + public void fromBytes(ByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketBackupStatus message, MessageContext ctx) { + + ABClientRenderer.starting = message.starting; + ABClientRenderer.started = message.started; + ABClientRenderer.failed = message.failed; + ABClientRenderer.finished = message.finished; + ABClientRenderer.cancelled = message.cancelled; + + ABClientRenderer.progress = message.progress; + ABClientRenderer.max = message.max; + + + return null; + + } + + } + + + + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketClientReload.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketClientReload.java new file mode 100644 index 00000000..6afbb2c8 --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketClientReload.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; + +//I hate this. +public class PacketClientReload implements IMessage{ + + + public PacketClientReload(boolean enable) { + + } + + public PacketClientReload () { + + } + + @Override + public void fromBytes(ByteBuf buf) { + + } + + @Override + public void toBytes(ByteBuf buf) { + + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketClientReload message, MessageContext ctx) { + + CoreCommandSystem.reloadClientConfig(Minecraft.getMinecraft().thePlayer::sendChatMessage); + return null; + + } + + } + +} diff --git a/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..92d0a8a5 --- /dev/null +++ b/forge/1.7.10/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,53 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; + +public class PacketToastSubscribe implements IMessage{ + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public PacketToastSubscribe () { + + } + + @Override + public void fromBytes(ByteBuf buf) { + enable = buf.readBoolean(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeBoolean(enable); + } + + + public static class Handler implements IMessageHandler { + + @Override + public IMessage onMessage(PacketToastSubscribe message, MessageContext ctx) { + + String uuid = ctx.getServerHandler().playerEntity.getGameProfile().getId().toString(); + + + if (message.enable && !AdvancedBackups.players.contains(uuid)) { + AdvancedBackups.players.add(uuid); + } + else if (!message.enable) { + AdvancedBackups.players.remove(uuid); + } + + return null; + + } + + } + +} diff --git a/forge/1.7.10/src/main/resources/assets/advancedbackups/lang/en_US.lang b/forge/1.7.10/src/main/resources/assets/advancedbackups/lang/en_US.lang new file mode 100644 index 00000000..d17123fa --- /dev/null +++ b/forge/1.7.10/src/main/resources/assets/advancedbackups/lang/en_US.lang @@ -0,0 +1,5 @@ +advancedbackups.backup_starting=Backup starting! +advancedbackups.progress=Backup in progress : %s%% +advancedbackups.backup_failed=Backup failed : check log for more info. +advancedbackups.backup_cancelled=Backup cancelled! +advancedbackups.backup_finished=Backup complete! \ No newline at end of file diff --git a/forge/1.7.10/src/main/resources/mcmod.info b/forge/1.7.10/src/main/resources/mcmod.info new file mode 100644 index 00000000..20d03144 --- /dev/null +++ b/forge/1.7.10/src/main/resources/mcmod.info @@ -0,0 +1,13 @@ +[ + { + "modid": "advancedbackups", + "name": "Advanced Backups", + "version": "${modVersion}", + "mcversion": "1.7.10", + "description": "A highly advanced backup mod.", + "url": "https://github.com/MommyHeather/AdvancedBackups", + "credits": "", + "authorList": ["mommyheather"], + "dependencies": [] + } +] diff --git a/forge/1.7.10/src/main/resources/pack.mcmeta b/forge/1.7.10/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/forge/1.7.10/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/global.properties b/global.properties new file mode 100644 index 00000000..d5a4535f --- /dev/null +++ b/global.properties @@ -0,0 +1,3 @@ +ext { + abCoreLibPath = "../../core/build/libs/advancedbackups-corelib.jar" +} diff --git a/neoforge/1.20.2/README.md b/neoforge/1.20.2/README.md new file mode 100644 index 00000000..46760a8d --- /dev/null +++ b/neoforge/1.20.2/README.md @@ -0,0 +1,4 @@ +# Neoforge - 1.20.2 + +This is the branch specifically for neoforge 1.20.2. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/neoforge/1.20.2/build.gradle b/neoforge/1.20.2/build.gradle new file mode 100644 index 00000000..25548f4e --- /dev/null +++ b/neoforge/1.20.2/build.gradle @@ -0,0 +1,180 @@ +plugins { + id 'java-library' + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.neoforged.gradle.userdev' version '7.0.26' +} + +apply from: '../../global.properties' + +group = mod_group_id + +version = "${version != 'unspecified' ? version : '0.1-dev'}" //debuggins sake +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + mavenLocal() +} + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + + + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') +//minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager + +// Default run configurations. +// These can be tweaked, removed, or duplicated as needed. +runs { + // applies to all the run configs below + configureEach { + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + systemProperty 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + systemProperty 'forge.logging.console.level', 'debug' + + modSource project.sourceSets.main + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + server { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + programArgument '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath() + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + + +minecraft { + accessTransformers { + file('src/main/resources/META-INF/accesstransformer.cfg') + } +} + +dependencies { + // Specify the version of Minecraft to use. + // Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above. + // The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version. + // You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader. + // And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version. + // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. + implementation "net.neoforged:neoforge:${neo_version}" + + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Example mod dependency with JEI + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" + // compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}" + // runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}" + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation "blank:coolmod-${mc_version}:${coolmod_version}" + + // Example mod dependency using a file as dependency + // implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar") + + // Example project dependency using a sister or child project: + // implementation project(":myproject") + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, + neo_version : neo_version, neo_version_range: neo_version_range, + loader_version_range: loader_version_range, + mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + from components.java + } + } + repositories { + maven { + url "file://${project.projectDir}/repo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/neoforge/1.20.2/gradle.properties b/neoforge/1.20.2/gradle.properties new file mode 100644 index 00000000..ba912535 --- /dev/null +++ b/neoforge/1.20.2/gradle.properties @@ -0,0 +1,65 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Neo version to get a valid artifact +minecraft_version=1.20.2 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20.2,1.21) +# The Neo version must agree with the Minecraft version to get a valid artifact +neo_version=20.2.35-beta +# The Neo version range can use any version of Neo as bounds or match the loader version range +neo_version_range=[20.2,) +# The loader version range can only use the major version of Neo/FML as bounds +loader_version_range=[1,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.2 + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=18 + + +modloaderName =neoforge +minecraftVersion =1.20.2 \ No newline at end of file diff --git a/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.jar b/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.properties b/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/neoforge/1.20.2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/neoforge/1.20.2/gradlew b/neoforge/1.20.2/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/neoforge/1.20.2/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/neoforge/1.20.2/gradlew.bat b/neoforge/1.20.2/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/neoforge/1.20.2/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/neoforge/1.20.2/settings.gradle b/neoforge/1.20.2/settings.gradle new file mode 100644 index 00000000..bb205047 --- /dev/null +++ b/neoforge/1.20.2/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'NeoForged' + url = 'https://maven.neoforged.net/releases' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + +rootProject.name = 'neoforge-1.20.2' diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..956b2283 --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,173 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.ModList; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.RegisterCommandsEvent; +import net.neoforged.neoforge.event.TickEvent; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.event.server.ServerStartedEvent; +import net.neoforged.neoforge.event.server.ServerStartingEvent; +import net.neoforged.neoforge.event.server.ServerStoppingEvent; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups() + { + // Register ourselves for server and other game events we are interested in + NeoForge.EVENT_BUS.register(this); + NetworkHandler.register(); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..8dd1e01e --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!") ; + }, true); + return 1; + })) + + ); + } + + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..4ddf9623 --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..243e4401 --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = new ResourceLocation("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..5f63dcf3 --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..7c4dc90d --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; +import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; +import net.neoforged.neoforge.common.NeoForge; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + NeoForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + NeoForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..11fcc02b --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,46 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.NetworkRegistry; +import net.neoforged.neoforge.network.PlayNetworkDirection; +import net.neoforged.neoforge.network.simple.SimpleChannel; + +public class NetworkHandler { + + private static final String PROTOCOL_VERSION = "1"; + private static int id = 0; + private static int id() { + return id++; + } + + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation("advancedbackups", "main"), + () -> PROTOCOL_VERSION, + (version) -> true, + (version) -> true + ); + + + public static void register() { + INSTANCE.messageBuilder(PacketBackupStatus.class, id()) + .encoder(PacketBackupStatus::toBytes) + .decoder(buf -> new PacketBackupStatus(buf)) + .consumerNetworkThread(PacketBackupStatus::handle) + .add(); + INSTANCE.messageBuilder(PacketToastSubscribe.class, id()) + .encoder(PacketToastSubscribe::toBytes) + .decoder(buf -> new PacketToastSubscribe(buf)) + .consumerNetworkThread(PacketToastSubscribe::handle) + .add(); + } + + public static void sendToClient(ServerPlayer player, Object packet) { + INSTANCE.sendTo(packet, player.connection.connection, PlayNetworkDirection.PLAY_TO_CLIENT); + } + + public static void sendToServer(Object packet) { + INSTANCE.sendToServer(packet); + } + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..9021d25e --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,70 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.client.BackupToast; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.neoforged.fml.LogicalSide; +import net.neoforged.neoforge.network.NetworkEvent; + +public class PacketBackupStatus { + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public boolean handle(NetworkEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getDirection().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + ctx.setPacketHandled(true); + return true; + + } + +} diff --git a/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..6aa9b72e --- /dev/null +++ b/neoforge/1.20.2/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,41 @@ +package co.uk.mommyheather.advancedbackups.network; + +import java.util.function.Supplier; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.neoforged.neoforge.network.NetworkEvent; + +public class PacketToastSubscribe { + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + public void toBytes(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(NetworkEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (ctx.getSender() == null) return; + if (enable && !AdvancedBackups.players.contains(ctx.getSender().getStringUUID())) { + AdvancedBackups.players.add(ctx.getSender().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.getSender().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/neoforge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg b/neoforge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..9d2bce8b --- /dev/null +++ b/neoforge/1.20.2/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener lastSeenMessages # lastSeenMessages \ No newline at end of file diff --git a/neoforge/1.20.2/src/main/resources/META-INF/mods.toml b/neoforge/1.20.2/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..3435b2b2 --- /dev/null +++ b/neoforge/1.20.2/src/main/resources/META-INF/mods.toml @@ -0,0 +1,73 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="neoforge" #mandatory + # Does this dependency have to exist - if not, ordering below must be specified + mandatory=true #mandatory + # The version range of the dependency + versionRange="${neo_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.${mod_id}]] + modId="minecraft" + mandatory=true + # This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="${minecraft_version_range}" + ordering="NONE" + side="BOTH" + +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/neoforge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json b/neoforge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/neoforge/1.20.2/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/neoforge/1.20.2/src/main/resources/pack.mcmeta b/neoforge/1.20.2/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/neoforge/1.20.2/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/neoforge/1.20.4/README.md b/neoforge/1.20.4/README.md new file mode 100644 index 00000000..7e22fb4a --- /dev/null +++ b/neoforge/1.20.4/README.md @@ -0,0 +1,4 @@ +# Neoforge - 1.20.4 + +This is the branch specifically for neoforge 1.20.4 +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/neoforge/1.20.4/build.gradle b/neoforge/1.20.4/build.gradle new file mode 100644 index 00000000..2d565791 --- /dev/null +++ b/neoforge/1.20.4/build.gradle @@ -0,0 +1,179 @@ +plugins { + id 'java-library' + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.neoforged.gradle.userdev' version '7.0.80' +} + +apply from: '../../global.properties' + +group = mod_group_id + +version = "${version != 'unspecified' ? version : '0.1-dev'}" //debuggins sake +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + mavenLocal() +} + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + + + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') +//minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager + +// Default run configurations. +// These can be tweaked, removed, or duplicated as needed. +runs { + // applies to all the run configs below + configureEach { + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + systemProperty 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + systemProperty 'forge.logging.console.level', 'debug' + + modSource project.sourceSets.main + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + server { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + programArgument '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath() + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + + +minecraft { + accessTransformers { + file('src/main/resources/META-INF/accesstransformer.cfg') + } +} + +dependencies { + // Specify the version of Minecraft to use. + // Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above. + // The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version. + // You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader. + // And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version. + // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. + implementation "net.neoforged:neoforge:${neo_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Example mod dependency with JEI + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" + // compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}" + // runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}" + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation "blank:coolmod-${mc_version}:${coolmod_version}" + + // Example mod dependency using a file as dependency + // implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar") + + // Example project dependency using a sister or child project: + // implementation project(":myproject") + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, + neo_version : neo_version, neo_version_range: neo_version_range, + loader_version_range: loader_version_range, + mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + from components.java + } + } + repositories { + maven { + url "file://${project.projectDir}/repo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/neoforge/1.20.4/gradle.properties b/neoforge/1.20.4/gradle.properties new file mode 100644 index 00000000..e7fa91be --- /dev/null +++ b/neoforge/1.20.4/gradle.properties @@ -0,0 +1,66 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Forge version to get a valid artifact +minecraft_version=1.20.4 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20.4,1.21) +# The Forge version must agree with the Minecraft version to get a valid artifact +neo_version=20.4.173 +# The Forge version range can use any version of Forge as bounds or match the loader version range +neo_version_range=[20.4,) +# The loader version range can only use the major version of Forge/FML as bounds +loader_version_range=[2,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.4 + + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=18 + + +modloaderName =neoforge +minecraftVersion =1.20.4 \ No newline at end of file diff --git a/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.jar b/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.jar differ diff --git a/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.properties b/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/neoforge/1.20.4/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/neoforge/1.20.4/gradlew b/neoforge/1.20.4/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/neoforge/1.20.4/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/neoforge/1.20.4/gradlew.bat b/neoforge/1.20.4/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/neoforge/1.20.4/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/neoforge/1.20.4/settings.gradle b/neoforge/1.20.4/settings.gradle new file mode 100644 index 00000000..8a8f11cf --- /dev/null +++ b/neoforge/1.20.4/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'NeoForged' + url = 'https://maven.neoforged.net/releases' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + +rootProject.name = 'neoforge-1.20.4' diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..b8bc748d --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,174 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.ModList; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.RegisterCommandsEvent; +import net.neoforged.neoforge.event.TickEvent; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.event.server.ServerStartedEvent; +import net.neoforged.neoforge.event.server.ServerStartingEvent; +import net.neoforged.neoforge.event.server.ServerStoppingEvent; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups(IEventBus modEventBus) + { + // Register ourselves for server and other game events we are interested in + NeoForge.EVENT_BUS.register(this); + modEventBus.addListener(NetworkHandler::onRegisterPayloadHandler); + modEventBus.addListener(this::clientSetup); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(TickEvent.ServerTickEvent event) { + if (!event.phase.equals(TickEvent.Phase.END)) return; + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..8dd1e01e --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!") ; + }, true); + return 1; + })) + + ); + } + + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..4ddf9623 --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.time.Instant; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.LastSeenMessages.Update; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Update acknowledgment = Minecraft.getInstance().player.connection.lastSeenMessages.generateAndApplyUpdate().update(); + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel", Instant.now(), 0, + ArgumentSignatures.EMPTY, acknowledgment)); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..243e4401 --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = new ResourceLocation("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..5f63dcf3 --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..7c4dc90d --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; +import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; +import net.neoforged.neoforge.common.NeoForge; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet) { + BackupToast.starting = packet.starting; + BackupToast.started = packet.started; + BackupToast.failed = packet.failed; + BackupToast.finished = packet.finished; + BackupToast.cancelled = packet.cancelled; + + BackupToast.progress = packet.progress; + BackupToast.max = packet.max; + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + NeoForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + NeoForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + NetworkHandler.sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..d5281c2f --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,32 @@ +package co.uk.mommyheather.advancedbackups.network; + +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.PacketDistributor; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; +import net.neoforged.neoforge.network.registration.IPayloadRegistrar; + +public class NetworkHandler { + + + public static void onRegisterPayloadHandler(RegisterPayloadHandlerEvent event) { + final IPayloadRegistrar registrar = event.registrar("advancedbackups") + .versioned("1.0") + .optional(); + + registrar.play(PacketToastSubscribe.ID, PacketToastSubscribe::new, handler -> handler + .server(PacketToastSubscribe::handle)); + + registrar.play(PacketBackupStatus.ID, PacketBackupStatus::new, handler -> handler + .client(PacketBackupStatus::handle)); + } + + public static void sendToClient(ServerPlayer player, MSG message) { + PacketDistributor.PLAYER.with(player).send(message); + } + + public static void sendToServer(MSG message) { + PacketDistributor.SERVER.noArg().send(message); + } + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..dd9c99d5 --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,74 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.fml.LogicalSide; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; + +public class PacketBackupStatus implements CustomPacketPayload { + + public static final ResourceLocation ID = new ResourceLocation("advancedbackups", "backup_status"); + + @Override + public ResourceLocation id() { + return ID; + } + + public boolean starting; + public boolean started; + + public boolean failed; + public boolean finished; + public boolean cancelled; + + public int progress; + public int max; + + + public PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, + int max) { + this.starting = starting; + this.started = started; + this.failed = failed; + this.finished = finished; + this.cancelled = cancelled; + this.progress = progress; + this.max = max; + } + + + public PacketBackupStatus(FriendlyByteBuf buf) { + starting = buf.readBoolean(); + started = buf.readBoolean(); + failed = buf.readBoolean(); + finished = buf.readBoolean(); + cancelled = buf.readBoolean(); + + progress = buf.readInt(); + max = buf.readInt(); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBoolean(starting); + buf.writeBoolean(started); + buf.writeBoolean(failed); + buf.writeBoolean(finished); + buf.writeBoolean(cancelled); + + buf.writeInt(progress); + buf.writeInt(max); + } + + public void handle(PlayPayloadContext ctx) { + ctx.workHandler().submitAsync(() -> { + if (ctx.flow().getReceptionSide() == LogicalSide.CLIENT) { + ClientWrapper.handle(this); + } + }); + + } + +} diff --git a/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..47855acf --- /dev/null +++ b/neoforge/1.20.4/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,50 @@ +package co.uk.mommyheather.advancedbackups.network; + + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; + +public class PacketToastSubscribe implements CustomPacketPayload { + + public static final ResourceLocation ID = new ResourceLocation("advancedbackups", "toast_subscribe"); + + @Override + public ResourceLocation id() { + return ID; + } + + private boolean enable; + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + + public PacketToastSubscribe(FriendlyByteBuf buf) { + enable = buf.readBoolean(); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBoolean(enable); + } + + public boolean handle(PlayPayloadContext ctx) { + ctx.workHandler().submitAsync(() -> { + if (!ctx.player().isPresent()) return; + if (enable && !AdvancedBackups.players.contains(ctx.player().get().getStringUUID())) { + AdvancedBackups.players.add(ctx.player().get().getStringUUID()); + } + else if (!enable) { + AdvancedBackups.players.remove(ctx.player().get().getStringUUID()); + } + }); + + return true; + + } + +} diff --git a/neoforge/1.20.4/src/main/resources/META-INF/accesstransformer.cfg b/neoforge/1.20.4/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..9d2bce8b --- /dev/null +++ b/neoforge/1.20.4/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener lastSeenMessages # lastSeenMessages \ No newline at end of file diff --git a/neoforge/1.20.4/src/main/resources/META-INF/mods.toml b/neoforge/1.20.4/src/main/resources/META-INF/mods.toml new file mode 100644 index 00000000..3721a573 --- /dev/null +++ b/neoforge/1.20.4/src/main/resources/META-INF/mods.toml @@ -0,0 +1,70 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="neoforge" #mandatory + # The version range of the dependency + versionRange="${neo_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="BOTH" +# Here's another dependency +[[dependencies.${mod_id}]] + modId="minecraft" + # This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="${minecraft_version_range}" + ordering="NONE" + side="BOTH" + +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/neoforge/1.20.4/src/main/resources/assets/advancedbackups/lang/en_us.json b/neoforge/1.20.4/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/neoforge/1.20.4/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/neoforge/1.20.4/src/main/resources/pack.mcmeta b/neoforge/1.20.4/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/neoforge/1.20.4/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/neoforge/1.20.6/README.md b/neoforge/1.20.6/README.md new file mode 100644 index 00000000..6abb5af8 --- /dev/null +++ b/neoforge/1.20.6/README.md @@ -0,0 +1,4 @@ +# Neoforge - 1.20.6 + +This is the branch specifically for neoforge 1.20.6. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/neoforge/1.20.6/build.gradle b/neoforge/1.20.6/build.gradle new file mode 100644 index 00000000..dce59bf8 --- /dev/null +++ b/neoforge/1.20.6/build.gradle @@ -0,0 +1,179 @@ +plugins { + id 'java-library' + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.neoforged.gradle.userdev' version '7.0.109' +} + +apply from: '../../global.properties' + +group = mod_group_id + +version = "${version != 'unspecified' ? version : '0.1-dev'}" //debuggins sake +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + mavenLocal() +} + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + + + +// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. +java.toolchain.languageVersion = JavaLanguageVersion.of(21) + +//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') +//minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager + +// Default run configurations. +// These can be tweaked, removed, or duplicated as needed. +runs { + // applies to all the run configs below + configureEach { + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + systemProperty 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + systemProperty 'forge.logging.console.level', 'debug' + + modSource project.sourceSets.main + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + server { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + programArgument '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath() + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + + +minecraft { + accessTransformers { + file('src/main/resources/META-INF/accesstransformer.cfg') + } +} + +dependencies { + // Specify the version of Minecraft to use. + // Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above. + // The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version. + // You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader. + // And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version. + // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. + implementation "net.neoforged:neoforge:${neo_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Example mod dependency with JEI + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" + // compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}" + // runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}" + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation "blank:coolmod-${mc_version}:${coolmod_version}" + + // Example mod dependency using a file as dependency + // implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar") + + // Example project dependency using a sister or child project: + // implementation project(":myproject") + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, + neo_version : neo_version, neo_version_range: neo_version_range, + loader_version_range: loader_version_range, + mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/neoforge.mods.toml']) { + expand replaceProperties + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + from components.java + } + } + repositories { + maven { + url "file://${project.projectDir}/repo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/neoforge/1.20.6/gradle.properties b/neoforge/1.20.6/gradle.properties new file mode 100644 index 00000000..a0a82f95 --- /dev/null +++ b/neoforge/1.20.6/gradle.properties @@ -0,0 +1,67 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + + +## Environment Properties + +# The Minecraft version must agree with the Forge version to get a valid artifact +minecraft_version=1.20.5 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.20.5,1.21) +# The Forge version must agree with the Minecraft version to get a valid artifact +neo_version=20.5.17-beta +# The Forge version range can use any version of Forge as bounds or match the loader version range +neo_version_range=[20.5,21) +# The loader version range can only use the major version of Forge/FML as bounds +loader_version_range=[2,) +# The mapping channel to use for mappings. +# The default set of supported mapping channels are ["official", "snapshot", "snapshot_nodoc", "stable", "stable_nodoc"]. +# Additional mapping channels can be registered through the "channelProviders" extension in a Gradle plugin. +# +# | Channel | Version | | +# |-----------|----------------------|--------------------------------------------------------------------------------| +# | official | MCVersion | Official field/method names from Mojang mapping files | +# | parchment | YYYY.MM.DD-MCVersion | Open community-sourced parameter names and javadocs layered on top of official | +# +# You must be aware of the Mojang license when using the 'official' or 'parchment' mappings. +# See more information here: https://github.com/neoforged/NeoForm/blob/main/Mojang.md +# +# Parchment is an unofficial project maintained by ParchmentMC, separate from Minecraft Forge. +# Additional setup is needed to use their mappings, see https://parchmentmc.org/docs/getting-started +mapping_channel=official +# The mapping version to query from the mapping channel. +# This must match the format required by the mapping channel. +mapping_version=1.20.5 + + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=18 + + +modloaderName =neoforge +#We support 1.20.5, but it shouldn't be used so we won't mention it here. +minecraftVersion =1.20.6 \ No newline at end of file diff --git a/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.jar b/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.jar differ diff --git a/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.properties b/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/neoforge/1.20.6/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/neoforge/1.20.6/gradlew b/neoforge/1.20.6/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/neoforge/1.20.6/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/neoforge/1.20.6/gradlew.bat b/neoforge/1.20.6/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/neoforge/1.20.6/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/neoforge/1.20.6/settings.gradle b/neoforge/1.20.6/settings.gradle new file mode 100644 index 00000000..cf03de4a --- /dev/null +++ b/neoforge/1.20.6/settings.gradle @@ -0,0 +1,15 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'NeoForged' + url = 'https://maven.neoforged.net/releases' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + +rootProject.name = 'neoforge-1.20.6' diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..b6c00d1f --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,173 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.ModList; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.RegisterCommandsEvent; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.event.server.ServerStartedEvent; +import net.neoforged.neoforge.event.server.ServerStartingEvent; +import net.neoforged.neoforge.event.server.ServerStoppingEvent; +import net.neoforged.neoforge.event.tick.ServerTickEvent; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups(IEventBus modEventBus) + { + // Register ourselves for server and other game events we are interested in + NeoForge.EVENT_BUS.register(this); + modEventBus.addListener(NetworkHandler::onRegisterPayloadHandler); + modEventBus.addListener(this::clientSetup); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(ServerTickEvent.Post event) { + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..8dd1e01e --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!") ; + }, true); + return 1; + })) + + ); + } + + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..3ab34d38 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start")); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config")); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain")); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot")); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel")); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..243e4401 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = new ResourceLocation("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..5f63dcf3 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..3f22d24b --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,68 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.Objects; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; +import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import net.neoforged.neoforge.network.registration.NetworkRegistry; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet, IPayloadContext context) { + BackupToast.starting = packet.starting(); + BackupToast.started = packet.started(); + BackupToast.failed = packet.failed(); + BackupToast.finished = packet.finished(); + BackupToast.cancelled = packet.cancelled(); + + BackupToast.progress = packet.progress(); + BackupToast.max = packet.max(); + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + NeoForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + NeoForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + + + public static void sendToServer(MSG message) { + //We do this to implement custom checks! + ServerboundCustomPayloadPacket packet = new ServerboundCustomPayloadPacket(message); + + ClientPacketListener listener = Objects.requireNonNull(Minecraft.getInstance().getConnection()); + try { + NetworkRegistry.checkPacket(packet, listener); + } + catch (UnsupportedOperationException e) { + ABCore.warningLogger.accept("Refusing to send packet " + message + " to server as the serve cannot receive it."); + return; + } + listener.connection.send(packet); + } + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..f903c7ee --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,25 @@ +package co.uk.mommyheather.advancedbackups.network; + + +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.PacketDistributor; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; +import net.neoforged.neoforge.network.registration.PayloadRegistrar; + +public class NetworkHandler { + + + public static void onRegisterPayloadHandler(RegisterPayloadHandlersEvent event) { + final PayloadRegistrar registrar = event.registrar("1").optional(); //this .optional() seems to be required, but will it be a problem for our packets with non-neo servers...? + + registrar.commonToClient(PacketBackupStatus.ID, PacketBackupStatus.CODEC, PacketBackupStatus::handle); + registrar.commonToServer(PacketToastSubscribe.ID, PacketToastSubscribe.CODEC, PacketToastSubscribe::handle); + } + + public static void sendToClient(ServerPlayer player, MSG message) { + PacketDistributor.sendToPlayer(player, message); + } + + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..df9a0633 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,47 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.BackupToast; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public record PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, int max) implements CustomPacketPayload { + + public static final CustomPacketPayload.Type ID = new CustomPacketPayload.Type(new ResourceLocation("advancedbackups:backup_status")); + + public static final StreamCodec CODEC = StreamCodec.of((buf, packet) -> { + buf.writeBoolean(packet.starting); + buf.writeBoolean(packet.started); + buf.writeBoolean(packet.failed); + buf.writeBoolean(packet.finished); + buf.writeBoolean(packet.cancelled); + buf.writeInt(packet.progress); + buf.writeInt(packet.max); + }, + + buf -> new PacketBackupStatus( + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readInt(), + buf.readInt() + )); + + + @Override + public CustomPacketPayload.Type type() { + return ID; + } + + public static void handle(PacketBackupStatus packet, IPayloadContext context) { + ClientWrapper.handle(packet, context); + } + + +} diff --git a/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..868bbd96 --- /dev/null +++ b/neoforge/1.20.6/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public record PacketToastSubscribe(boolean enable) implements CustomPacketPayload { + + public static final CustomPacketPayload.Type ID = new CustomPacketPayload.Type<>(new ResourceLocation("advancedbackups:toast_subscribe")); + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public static final StreamCodec CODEC = StreamCodec.composite(ByteBufCodecs.BOOL, PacketToastSubscribe::enable, PacketToastSubscribe::new); + + + public static void handle(PacketToastSubscribe message, IPayloadContext context) { + + Player player = context.player(); + + if (message.enable() && !AdvancedBackups.players.contains(player.getStringUUID())) { + AdvancedBackups.players.add(player.getStringUUID()); + } + else if (!message.enable()) { + AdvancedBackups.players.remove(player.getStringUUID()); + } + + } + + + + @Override + public CustomPacketPayload.Type type() { + return ID; + } + + + +} \ No newline at end of file diff --git a/neoforge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg b/neoforge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..9d2bce8b --- /dev/null +++ b/neoforge/1.20.6/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.multiplayer.ClientPacketListener lastSeenMessages # lastSeenMessages \ No newline at end of file diff --git a/neoforge/1.20.6/src/main/resources/META-INF/neoforge.mods.toml b/neoforge/1.20.6/src/main/resources/META-INF/neoforge.mods.toml new file mode 100644 index 00000000..9e0f97f1 --- /dev/null +++ b/neoforge/1.20.6/src/main/resources/META-INF/neoforge.mods.toml @@ -0,0 +1,70 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="neoforge" #mandatory + # The version range of the dependency + versionRange="${neo_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="CLIENT" +# Here's another dependency +[[dependencies.${mod_id}]] + modId="minecraft" + # This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="${minecraft_version_range}" + ordering="NONE" + side="BOTH" + +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/neoforge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json b/neoforge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/neoforge/1.20.6/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/neoforge/1.20.6/src/main/resources/pack.mcmeta b/neoforge/1.20.6/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/neoforge/1.20.6/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/neoforge/1.21/README.md b/neoforge/1.21/README.md new file mode 100644 index 00000000..e29a5e89 --- /dev/null +++ b/neoforge/1.21/README.md @@ -0,0 +1,4 @@ +# Neoforge - 1.21 + +This is the branch specifically for neoforge 1.20.1. +Any differences will be listed below. For full documentation, see the `core` branch. diff --git a/neoforge/1.21/build.gradle b/neoforge/1.21/build.gradle new file mode 100644 index 00000000..2609be96 --- /dev/null +++ b/neoforge/1.21/build.gradle @@ -0,0 +1,190 @@ +plugins { + id 'java-library' + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.neoforged.gradle.userdev' version '7.0.142' +} + + +tasks.named('wrapper', Wrapper).configure { + // Define wrapper values here so as to not have to always do so when updating gradlew.properties. + // Switching this to Wrapper.DistributionType.ALL will download the full gradle sources that comes with + // documentation attached on cursor hover of gradle classes and methods. However, this comes with increased + // file size for Gradle. If you do switch this to ALL, run the Gradle wrapper task twice afterwards. + // (Verify by checking gradle/wrapper/gradle-wrapper.properties to see if distributionUrl now points to `-all`) + distributionType = Wrapper.DistributionType.BIN +} + +apply from: '../../global.properties' + +group = mod_group_id + +version = "${version != 'unspecified' ? version : '0.1-dev'}" //debuggins sake + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +repositories { + mavenLocal() +} + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + + + +// Mojang ships Java 17 to end users in 1.20.5+, so your mod should target Java 21. +java.toolchain.languageVersion = JavaLanguageVersion.of(21) + +//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') +//minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager + +// Default run configurations. +// These can be tweaked, removed, or duplicated as needed. +runs { + // applies to all the run configs below + configureEach { + // Recommended logging data for a userdev environment + // The markers can be added/remove as needed separated by commas. + // "SCAN": For mods scan. + // "REGISTRIES": For firing of registry events. + // "REGISTRYDUMP": For getting the contents of all registries. + systemProperty 'forge.logging.markers', 'REGISTRIES' + + // Recommended logging level for the console + // You can set various levels here. + // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels + systemProperty 'forge.logging.console.level', 'debug' + + modSource project.sourceSets.main + } + + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + server { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + programArgument '--nogui' + } + + // This run config launches GameTestServer and runs all registered gametests, then exits. + // By default, the server will crash when no gametests are provided. + // The gametest system is also enabled by default for other run configs under the /test command. + gameTestServer { + systemProperty 'forge.enabledGameTestNamespaces', project.mod_id + } + + data { + // example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it + // workingDirectory project.file('run-data') + + // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. + programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath() + } +} + +// Include resources generated by data generators. +sourceSets.main.resources { srcDir 'src/generated/resources' } + + +minecraft { + accessTransformers { + file('src/main/resources/META-INF/accesstransformer.cfg') + } +} + +dependencies { + // Specify the version of Minecraft to use. + // Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above. + // The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version. + // You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader. + // And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version. + // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. + implementation "net.neoforged:neoforge:${neo_version}" + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) + + // Example mod dependency with JEI + // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime + // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" + // compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}" + // runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}" + + // Example mod dependency using a mod jar from ./libs with a flat dir repository + // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar + // The group id is ignored when searching -- in this case, it is "blank" + // implementation "blank:coolmod-${mc_version}:${coolmod_version}" + + // Example mod dependency using a file as dependency + // implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar") + + // Example project dependency using a sister or child project: + // implementation project(":myproject") + + // For more info: + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html +} + +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, + neo_version : neo_version, neo_version_range: neo_version_range, + loader_version_range: loader_version_range, + mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: version, + mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + ] + inputs.properties replaceProperties + + filesMatching(['META-INF/neoforge.mods.toml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +// Example configuration to allow publishing using the maven-publish plugin +publishing { + publications { + register('mavenJava', MavenPublication) { + from components.java + } + } + repositories { + maven { + url "file://${project.projectDir}/repo" + } + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation +} diff --git a/neoforge/1.21/gradle.properties b/neoforge/1.21/gradle.properties new file mode 100644 index 00000000..b8036f7d --- /dev/null +++ b/neoforge/1.21/gradle.properties @@ -0,0 +1,52 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false +org.gradle.debug=false + +#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings +# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started +neogradle.subsystems.parchment.minecraftVersion=1.20.6 +neogradle.subsystems.parchment.mappingsVersion=2024.05.01 +# Environment Properties +# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge +# The Minecraft version must agree with the Neo version to get a valid artifact +minecraft_version=1.21 +# The Minecraft version range can use any release version of Minecraft as bounds. +# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly +# as they do not follow standard versioning conventions. +minecraft_version_range=[1.21,1.21.1) +# The Neo version must agree with the Minecraft version to get a valid artifact +neo_version=21.0.0-beta +# The Neo version range can use any version of Neo as bounds +neo_version_range=[21.0.0-beta,) +# The loader version range can only use the major version of FML as bounds +loader_version_range=[4,) + + + +## Mod Properties + +# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} +# Must match the String constant located in the main mod class annotated with @Mod. +mod_id=advancedbackups +# The human-readable display name for the mod. +mod_name=Advanced Backups +# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. +mod_license=BSD +# The mod version. See https://semver.org/ +mod_version=2.1.2 +# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. +# This should match the base package used for the mod sources. +# See https://maven.apache.org/guides/mini/guide-naming-conventions.html +mod_group_id=co.uk.mommyheather.advancedbackups +# The authors of the mod. This is a simple text string that is used for display purposes in the mod list. +mod_authors=Heather White +# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. +mod_description=A highly advanced and configurable backup mod.\Github contains documentation. +# Pack version - this changes each minecraft release, in general. +pack_format_number=18 + + +modloaderName =neoforge +#We support 1.20.5, but it shouldn't be used so we won't mention it here. +minecraftVersion =1.21 \ No newline at end of file diff --git a/neoforge/1.21/gradle/wrapper/gradle-wrapper.jar b/neoforge/1.21/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/neoforge/1.21/gradle/wrapper/gradle-wrapper.jar differ diff --git a/neoforge/1.21/gradle/wrapper/gradle-wrapper.properties b/neoforge/1.21/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/neoforge/1.21/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/neoforge/1.21/gradlew b/neoforge/1.21/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/neoforge/1.21/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/neoforge/1.21/gradlew.bat b/neoforge/1.21/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/neoforge/1.21/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/neoforge/1.21/settings.gradle b/neoforge/1.21/settings.gradle new file mode 100644 index 00000000..69e7ddc0 --- /dev/null +++ b/neoforge/1.21/settings.gradle @@ -0,0 +1,13 @@ +pluginManagement { + repositories { + mavenLocal() + gradlePluginPortal() + maven { url = 'https://maven.neoforged.net/releases' } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0' +} + +rootProject.name = 'neoforge-1.21' diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java new file mode 100644 index 00000000..3e757d78 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackups.java @@ -0,0 +1,172 @@ +package co.uk.mommyheather.advancedbackups; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import co.uk.mommyheather.advancedbackups.client.ClientContactor; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.ModList; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.RegisterCommandsEvent; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.event.server.ServerStartedEvent; +import net.neoforged.neoforge.event.server.ServerStartingEvent; +import net.neoforged.neoforge.event.server.ServerStoppingEvent; +import net.neoforged.neoforge.event.tick.ServerTickEvent; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +@Mod("advancedbackups") +public class AdvancedBackups +{ + + private static final Logger LOGGER = LogUtils.getLogger(); + + public static final Consumer infoLogger = LOGGER::info; + public static final Consumer warningLogger = LOGGER::warn; + public static final Consumer errorLogger = LOGGER::error; + + public static final ArrayList players = new ArrayList<>(); + + + public AdvancedBackups(IEventBus modEventBus) + { + // Register ourselves for server and other game events we are interested in + NeoForge.EVENT_BUS.register(this); + modEventBus.addListener(NetworkHandler::onRegisterPayloadHandler); + modEventBus.addListener(this::clientSetup); + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + } + + @SubscribeEvent + public void onServerStarting(ServerStartingEvent event) + { + // Do something when the server starts + ABCore.worldName = event.getServer().getWorldData().getLevelName(); + ABCore.worldDir = event.getServer().getWorldPath(LevelResource.ROOT); + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + ABCore.clientContactor = new ClientContactor(); + ABCore.modJar = ModList.get().getModFileById("advancedbackups").getFile().getFilePath().toFile(); + + + ConfigManager.loadOrCreateConfig(); + LOGGER.info("Config loaded!!"); + + } + + public void clientSetup(FMLClientSetupEvent e) { + ClientWrapper.init(e); + } + + + @SubscribeEvent + public void onServerStarted(ServerStartedEvent event) { + BackupWrapper.checkStartupBackups(); + } + + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + BackupWrapper.checkShutdownBackups(); + } + + + @SubscribeEvent + public void onPlayerJoined(PlayerEvent.PlayerLoggedInEvent event) { + ABCore.setActivity(true); + } + + @SubscribeEvent + public void registerCommands(RegisterCommandsEvent event){ + AdvancedBackupsCommand.register(event.getDispatcher()); + } + + + @SubscribeEvent + public void onPostTick(ServerTickEvent.Post event) { + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = """ + + + *************************************** + SAVING DISABLED - PREPARING FOR BACKUP! + *************************************** + """; + public static final String savesEnabledMessage = """ + + + ********************************* + SAVING ENABLED - BACKUP COMPLETE! + ********************************* + """; + public static final String saveCompleteMessage = """ + + + ************************************* + SAVE COMPLETE - PREPARING FOR BACKUP! + ************************************* + """; + + + public static void disableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && !level.noSave) { + level.noSave = true; + } + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + for (ServerLevel level : server.getAllLevels()) { + if (level != null && level.noSave) { + level.noSave = false; + } + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean flush) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + server.saveEverything(true, flush, true); + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + ABCore.setActivity(!players.isEmpty()); + } + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..8dd1e01e --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/AdvancedBackupsCommand.java @@ -0,0 +1,71 @@ +package co.uk.mommyheather.advancedbackups; + +import com.mojang.brigadier.CommandDispatcher; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class AdvancedBackupsCommand { + public static void register(CommandDispatcher stack) { + stack.register(Commands.literal("backup").requires((runner) -> { + return !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || runner.hasPermission(3); + }).then(Commands.literal("start").executes((runner) -> { + CoreCommandSystem.startBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-config").executes((runner) -> { + CoreCommandSystem.reloadConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reset-chain").executes((runner) -> { + CoreCommandSystem.resetChainLength((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("snapshot").executes((runner) -> { + CoreCommandSystem.snapshot((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("cancel").executes((runner) -> { + CoreCommandSystem.cancelBackup((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response) ; + }, true); + }); + return 1; + })) + + .then(Commands.literal("reload-client-config").executes((runner) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal("This command can only be ran on the client!") ; + }, true); + return 1; + })) + + ); + } + + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java new file mode 100644 index 00000000..3ab34d38 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/AdvancedBackupsClientCommand.java @@ -0,0 +1,59 @@ +package co.uk.mommyheather.advancedbackups.client; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; + +public class AdvancedBackupsClientCommand { + public static void register(CommandDispatcher commandDispatcher) { + commandDispatcher.register(literal("backup").requires((runner) -> { + return true; + }).then(literal("start").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup start")); + return 1; + })) + + .then(literal("reload-config").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reload-config")); + return 1; + })) + + .then(literal("reset-chain").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup reset-chain")); + return 1; + })) + + .then(literal("snapshot").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup snapshot")); + return 1; + })) + + .then(literal("cancel").executes((runner) -> { + Minecraft.getInstance().player.connection.send(new ServerboundChatCommandPacket("backup cancel")); + return 1; + })) + + .then(literal("reload-client-config").executes((runner) -> { + CoreCommandSystem.reloadClientConfig((response) -> { + runner.getSource().sendSuccess(() -> { + return Component.literal(response); + }, true); + }); + return 1; + })) + + ); + } + + //I'm no fan of having this here but it seems required + public static LiteralArgumentBuilder literal(String literal) { + return LiteralArgumentBuilder.literal(literal); + } + + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java new file mode 100644 index 00000000..4c71b099 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/BackupToast.java @@ -0,0 +1,124 @@ +package co.uk.mommyheather.advancedbackups.client; + +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.toasts.Toast; +import net.minecraft.client.gui.components.toasts.ToastComponent; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +public class BackupToast implements Toast { + + public static boolean starting; + public static boolean started; + public static boolean failed; + public static boolean finished; + public static boolean cancelled; + + public static int progress; + public static int max; + + public static boolean exists = false; + + private static long time; + private static boolean timeSet = false; + + public static final ItemStack stack = new ItemStack(Items.PAPER); + private static final ResourceLocation TEXTURE = ResourceLocation.parse("toast/advancement"); + + private int textColour; + private String title = "You shouldn't see this!"; + + + @Override + public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { + graphics.blitSprite(TEXTURE, 0, ClientConfigManager.darkMode.get() ? 0 : this.height(), this.width(), this.height()); + graphics.renderFakeItem(stack, 8, 8); + + + float percent = finished ? 100 : (float) progress / (float) max; + + graphics.fill(4, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBackgroundRed.get(), (int) ClientConfigManager.progressBackgroundGreen.get(), (int) ClientConfigManager.progressBackgroundBlue.get())); + + float f = Math.min(156, ( + 156 * percent + )); + + if (!exists) { + if (title.equals(I18n.get("advancedbackups.backup_finished"))){ + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + graphics.fill(3, 28, 156, 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + } + else { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + graphics.drawString(toastGui.getMinecraft().font, I18n.get(title), 25, 11, textColour); + } + return Visibility.HIDE; + } + + + if (starting) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_starting"); + } + else if (started) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.progress", round(percent * 100)); + } + else if (failed) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_failed"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (finished) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.progressTextRed.get(), (int) ClientConfigManager.progressTextGreen.get(), (int) ClientConfigManager.progressTextBlue.get()); + title = I18n.get("advancedbackups.backup_finished"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + else if (cancelled) { + textColour = ColourHelper.colour(255, (int) ClientConfigManager.errorTextRed.get(), (int) ClientConfigManager.errorTextGreen.get(), (int) ClientConfigManager.errorTextBlue.get()); + title = I18n.get("advancedbackups.backup_cancelled"); + if (!timeSet) { + time = System.currentTimeMillis(); + timeSet = true; + } + } + + graphics.drawString(toastGui.getMinecraft().font, title, 25, 11, textColour); + + if (timeSet && System.currentTimeMillis() >= time + 5000) { + starting = false; + started = false; + failed = false; + finished = false; + progress = 0; + max = 0; + timeSet = false; + exists = false; + return Visibility.HIDE; + } + + graphics.fill(4, 28, Math.max(4, (int) f), 29, ColourHelper.colour + (255, (int) ClientConfigManager.progressBarRed.get(), (int) ClientConfigManager.progressBarGreen.get(), (int) ClientConfigManager.progressBarBlue.get())); + + return Visibility.SHOW; + + } + + + private static String round (float value) { + return String.format("%.1f", value); + } + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java new file mode 100644 index 00000000..5f63dcf3 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientContactor.java @@ -0,0 +1,79 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.List; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import co.uk.mommyheather.advancedbackups.network.NetworkHandler; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +public class ClientContactor implements IClientContactor { + + @Override + public void backupComplete(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, true, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupFailed(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, true, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, true, false, false, false, progress, max); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupStarting(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(true, false, false, false, false, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } + + @Override + public void backupCancelled(boolean all) { + MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + List players = server.getPlayerList().getPlayers(); + PacketBackupStatus packet = new PacketBackupStatus(false, false, false, false, true, 0, 0); + for (ServerPlayer player : players) { + if (!AdvancedBackups.players.contains(player.getStringUUID())) continue; + if (!server.isDedicatedServer() || player.hasPermissions(3) || all) { + NetworkHandler.sendToClient(player, packet); + } + } + } +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java new file mode 100644 index 00000000..3f22d24b --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ClientWrapper.java @@ -0,0 +1,68 @@ +package co.uk.mommyheather.advancedbackups.client; + +import java.util.Objects; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.config.ClientConfigManager; +import co.uk.mommyheather.advancedbackups.network.PacketBackupStatus; +import co.uk.mommyheather.advancedbackups.network.PacketToastSubscribe; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; +import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import net.neoforged.neoforge.network.registration.NetworkRegistry; + +public class ClientWrapper { + + public static void handle(PacketBackupStatus packet, IPayloadContext context) { + BackupToast.starting = packet.starting(); + BackupToast.started = packet.started(); + BackupToast.failed = packet.failed(); + BackupToast.finished = packet.finished(); + BackupToast.cancelled = packet.cancelled(); + + BackupToast.progress = packet.progress(); + BackupToast.max = packet.max(); + + if (!BackupToast.exists) { + BackupToast.exists = true; + Minecraft.getInstance().getToasts().addToast(new BackupToast()); + } + } + + public static void init(FMLClientSetupEvent e) { + NeoForge.EVENT_BUS.addListener(ClientWrapper::registerClientCommands); + NeoForge.EVENT_BUS.addListener(ClientWrapper::onServerConnected); + ClientConfigManager.loadOrCreateConfig(); + } + + public static void registerClientCommands(RegisterClientCommandsEvent event) { + AdvancedBackupsClientCommand.register(event.getDispatcher()); + } + + public static void onServerConnected(ClientPlayerNetworkEvent.LoggingIn event) { + sendToServer(new PacketToastSubscribe(ClientConfigManager.showProgress.get())); + } + + + public static void sendToServer(MSG message) { + //We do this to implement custom checks! + ServerboundCustomPayloadPacket packet = new ServerboundCustomPayloadPacket(message); + + ClientPacketListener listener = Objects.requireNonNull(Minecraft.getInstance().getConnection()); + try { + NetworkRegistry.checkPacket(packet, listener); + } + catch (UnsupportedOperationException e) { + ABCore.warningLogger.accept("Refusing to send packet " + message + " to server as the serve cannot receive it."); + return; + } + listener.connection.send(packet); + } + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java new file mode 100644 index 00000000..dd559425 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/client/ColourHelper.java @@ -0,0 +1,31 @@ +package co.uk.mommyheather.advancedbackups.client; + +//A colour helper, loosely based on that of vanilla 1.16. +//Where is it in modern? +public class ColourHelper { + + public static int alpha(int in) { + return in >>> 24; + } + + public static int red(int in) { + return in >> 16 & 255; + } + + public static int green(int in) { + return in >> 8 & 255; + } + + public static int blue(int in) { + return in & 255; + } + + public static int colour(int a, int r, int g, int b) { + return a << 24 | r << 16 | g << 8 | b; + } + + public static int colour(int a, long r, long g, long b) { + return colour(a, (int) r, (int) g, (int) b); + } + +} \ No newline at end of file diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java new file mode 100644 index 00000000..f903c7ee --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/NetworkHandler.java @@ -0,0 +1,25 @@ +package co.uk.mommyheather.advancedbackups.network; + + +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.PacketDistributor; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; +import net.neoforged.neoforge.network.registration.PayloadRegistrar; + +public class NetworkHandler { + + + public static void onRegisterPayloadHandler(RegisterPayloadHandlersEvent event) { + final PayloadRegistrar registrar = event.registrar("1").optional(); //this .optional() seems to be required, but will it be a problem for our packets with non-neo servers...? + + registrar.commonToClient(PacketBackupStatus.ID, PacketBackupStatus.CODEC, PacketBackupStatus::handle); + registrar.commonToServer(PacketToastSubscribe.ID, PacketToastSubscribe.CODEC, PacketToastSubscribe::handle); + } + + public static void sendToClient(ServerPlayer player, MSG message) { + PacketDistributor.sendToPlayer(player, message); + } + + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java new file mode 100644 index 00000000..3e243a09 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketBackupStatus.java @@ -0,0 +1,48 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.client.BackupToast; +import co.uk.mommyheather.advancedbackups.client.ClientWrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public record PacketBackupStatus(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, int max) implements CustomPacketPayload { + + public static final CustomPacketPayload.Type ID = new CustomPacketPayload.Type(ResourceLocation.parse("advancedbackups:backup_status")); + + public static final StreamCodec CODEC = StreamCodec.of((buf, packet) -> { + buf.writeBoolean(packet.starting); + buf.writeBoolean(packet.started); + buf.writeBoolean(packet.failed); + buf.writeBoolean(packet.finished); + buf.writeBoolean(packet.cancelled); + buf.writeInt(packet.progress); + buf.writeInt(packet.max); + }, + + buf -> new PacketBackupStatus( + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readBoolean(), + buf.readInt(), + buf.readInt() + )); + + + @Override + public CustomPacketPayload.Type type() { + return ID; + } + + + public static void handle(PacketBackupStatus packet, IPayloadContext context) { + ClientWrapper.handle(packet, context); + } + + +} diff --git a/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java new file mode 100644 index 00000000..43aebf68 --- /dev/null +++ b/neoforge/1.21/src/main/java/co/uk/mommyheather/advancedbackups/network/PacketToastSubscribe.java @@ -0,0 +1,45 @@ +package co.uk.mommyheather.advancedbackups.network; + +import co.uk.mommyheather.advancedbackups.AdvancedBackups; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public record PacketToastSubscribe(boolean enable) implements CustomPacketPayload { + + public static final CustomPacketPayload.Type ID = new CustomPacketPayload.Type<>(ResourceLocation.parse("advancedbackups:toast_subscribe")); + + public PacketToastSubscribe(boolean enable) { + this.enable = enable; + } + + public static final StreamCodec CODEC = StreamCodec.composite(ByteBufCodecs.BOOL, PacketToastSubscribe::enable, PacketToastSubscribe::new); + + + public static void handle(PacketToastSubscribe message, IPayloadContext context) { + + Player player = context.player(); + + if (message.enable() && !AdvancedBackups.players.contains(player.getStringUUID())) { + AdvancedBackups.players.add(player.getStringUUID()); + } + else if (!message.enable()) { + AdvancedBackups.players.remove(player.getStringUUID()); + } + + } + + + + @Override + public CustomPacketPayload.Type type() { + return ID; + } + + + +} \ No newline at end of file diff --git a/neoforge/1.21/src/main/resources/META-INF/accesstransformer.cfg b/neoforge/1.21/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 00000000..e81ac357 --- /dev/null +++ b/neoforge/1.21/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1,2 @@ +public net.minecraft.client.multiplayer.ClientPacketListener lastSeenMessages # lastSeenMessages +public net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl connection # connection \ No newline at end of file diff --git a/neoforge/1.21/src/main/resources/META-INF/neoforge.mods.toml b/neoforge/1.21/src/main/resources/META-INF/neoforge.mods.toml new file mode 100644 index 00000000..9e0f97f1 --- /dev/null +++ b/neoforge/1.21/src/main/resources/META-INF/neoforge.mods.toml @@ -0,0 +1,70 @@ +# This is an example mods.toml file. It contains the data relating to the loading mods. +# There are several mandatory fields (#mandatory), and many more that are optional (#optional). +# The overall format is standard TOML format, v0.5.0. +# Note that there are a couple of TOML lists in this file. +# Find more information on toml format here: https://github.com/toml-lang/toml +# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml +modLoader="javafml" #mandatory +# A version range to match for said mod loader - for regular FML @Mod it will be the the FML version. This is currently 47. +loaderVersion="${loader_version_range}" #mandatory +# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties. +# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here. +license="${mod_license}" +# A URL to refer people to when problems occur with this mod +#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional +# A list of mods - how many allowed here is determined by the individual mod loader +[[mods]] #mandatory +# The modid of the mod +modId="${mod_id}" #mandatory +# The version number of the mod +version="${mod_version}" #mandatory +# A display name for the mod +displayName="${mod_name}" #mandatory +# A URL to query for updates for this mod. See the JSON update specification https://docs.neoforged.net/docs/misc/updatechecker/ +#updateJSONURL="https://change.me.example.invalid/updates.json" #optional +# A URL for the "homepage" for this mod, displayed in the mod UI +#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional +# A file name (in the root of the mod JAR) containing a logo for display +#logoFile="examplemod.png" #optional +# A text field displayed in the mod UI +#credits="" #optional +# A text field displayed in the mod UI +authors="${mod_authors}" #optional +# Display Test controls the display for your mod in the server connection screen +# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. +# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod. +# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component. +# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value. +# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. +#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional) + +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" + +# The description text for the mod (multi line!) (#mandatory) +description='''${mod_description}''' +# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. +[[dependencies.${mod_id}]] #optional + # the modid of the dependency + modId="neoforge" #mandatory + # The version range of the dependency + versionRange="${neo_version_range}" #mandatory + # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory + # BEFORE - This mod is loaded BEFORE the dependency + # AFTER - This mod is loaded AFTER the dependency + ordering="NONE" + # Side this dependency is applied on - BOTH, CLIENT, or SERVER + side="CLIENT" +# Here's another dependency +[[dependencies.${mod_id}]] + modId="minecraft" + # This version range declares a minimum of the current minecraft version up to but not including the next major version + versionRange="${minecraft_version_range}" + ordering="NONE" + side="BOTH" + +# Features are specific properties of the game environment, that you may want to declare you require. This example declares +# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't +# stop your mod loading on the server for example. +#[features.${mod_id}] +#openGLVersion="[3.2,)" diff --git a/neoforge/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json b/neoforge/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json new file mode 100644 index 00000000..474ea8d6 --- /dev/null +++ b/neoforge/1.21/src/main/resources/assets/advancedbackups/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "advancedbackups.backup_starting" : "Backup starting!", + "advancedbackups.progress": "Backup ongoing: %1$s%%", + "advancedbackups.backup_failed": "Backup failed!\nCheck log for more info.", + "advancedbackups.backup_finished": "Backup complete!", + "advancedbackups.backup_cancelled": "Backup cancelled!" +} \ No newline at end of file diff --git a/neoforge/1.21/src/main/resources/pack.mcmeta b/neoforge/1.21/src/main/resources/pack.mcmeta new file mode 100644 index 00000000..877382f4 --- /dev/null +++ b/neoforge/1.21/src/main/resources/pack.mcmeta @@ -0,0 +1,8 @@ +{ + "pack": { + "description": "advancedbackups resources", + "pack_format": 9, + "forge:resource_pack_format": 8, + "forge:data_pack_format": 9 + } +} diff --git a/spigot/1.21/build.gradle b/spigot/1.21/build.gradle new file mode 100644 index 00000000..dc905915 --- /dev/null +++ b/spigot/1.21/build.gradle @@ -0,0 +1,92 @@ +/* + * This file was generated by the Gradle 'init' task. + */ + +plugins { + id 'java-library' + id 'maven-publish' + id 'io.github.patrick.remapper' version "1.4.2" +} + +apply from: '../../global.properties' + +group = 'computer.heather.AdvancedBackups' +description = 'AdvancedBackups' +java.sourceCompatibility = JavaVersion.VERSION_21 + +base { + archivesName = 'AdvancedBackups-' + modloaderName + "-" + minecraftVersion +} + +repositories { + maven { + url = uri('https://hub.spigotmc.org/nexus/content/repositories/snapshots/') + } + + maven { + url = uri('https://repo.maven.apache.org/maven2/') + } + mavenCentral() + mavenLocal() +} + +configurations { + // configuration that holds jars to include in the jar + extraLibs +} + +dependencies { + compileOnly("org.spigotmc:spigot:1.21.1-R0.1-SNAPSHOT:remapped-mojang") + + implementation files(abCoreLibPath) + extraLibs files(abCoreLibPath) +} + + + +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + version: version + ] + inputs.properties replaceProperties + + filesMatching(['plugin.yml']) { + expand replaceProperties + [project: project] + } +} + +// Example for how to get properties into the manifest for reading at runtime. +tasks.named('jar', Jar).configure { + manifest { + attributes([ + "Main-Class" : "co.uk.mommyheather.advancedbackups.cli.AdvancedBackupsCLI", + "Specification-Title" : "advancedbackups", + "Specification-Vendor" : "mommyheather", + "Specification-Version" : "1", // We are version 1 of ourselves + "Implementation-Title" : project.name, + "Implementation-Version" : project.jar.archiveVersion, + "Implementation-Vendor" : "mommyheather", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } + from { + configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +tasks { + remap { + version.set("1.21.1") + } + build { + dependsOn(remap) + } +} + +publishing { + publications { + maven(MavenPublication) { + from(components.java) + } + } +} diff --git a/spigot/1.21/gradle.properties b/spigot/1.21/gradle.properties new file mode 100644 index 00000000..d2afdcf2 --- /dev/null +++ b/spigot/1.21/gradle.properties @@ -0,0 +1,6 @@ +version = 0.1-DEV + + +modloaderName =spigot +#We'll target 1.21, if it works on 1.20.6 then *cool* +minecraftVersion =1.21 \ No newline at end of file diff --git a/spigot/1.21/gradle/wrapper/gradle-wrapper.jar b/spigot/1.21/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/spigot/1.21/gradle/wrapper/gradle-wrapper.jar differ diff --git a/spigot/1.21/gradle/wrapper/gradle-wrapper.properties b/spigot/1.21/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..9355b415 --- /dev/null +++ b/spigot/1.21/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/spigot/1.21/gradlew b/spigot/1.21/gradlew new file mode 100755 index 00000000..f5feea6d --- /dev/null +++ b/spigot/1.21/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/spigot/1.21/gradlew.bat b/spigot/1.21/gradlew.bat new file mode 100644 index 00000000..9d21a218 --- /dev/null +++ b/spigot/1.21/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/spigot/1.21/settings.gradle b/spigot/1.21/settings.gradle new file mode 100644 index 00000000..204e49e0 --- /dev/null +++ b/spigot/1.21/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'spigot-1.21' diff --git a/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackups.java b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackups.java new file mode 100644 index 00000000..f4b50740 --- /dev/null +++ b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackups.java @@ -0,0 +1,157 @@ +package computer.heather.AdvancedBackups; + +import java.io.File; +import java.util.ArrayList; +import java.util.function.Consumer; + +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import co.uk.mommyheather.advancedbackups.core.backups.BackupTimer; +import co.uk.mommyheather.advancedbackups.core.backups.BackupWrapper; +import co.uk.mommyheather.advancedbackups.core.config.ConfigManager; +import computer.heather.AdvancedBackups.network.ClientContactor; +import computer.heather.AdvancedBackups.network.PacketListener; + +public class AdvancedBackups extends JavaPlugin implements Listener { + + private static boolean enabled = false; + public static Server server = null; + + public static Consumer infoLogger; + public static Consumer warningLogger; + public static Consumer errorLogger; + + //List of players that have asked to see packets + public static final ArrayList players = new ArrayList<>(); + + @Override + public void onEnable() { + enabled = true; + + ABCore.disableSaving = AdvancedBackups::disableSaving; + ABCore.enableSaving = AdvancedBackups::enableSaving; + ABCore.saveOnce = AdvancedBackups::saveOnce; + + AdvancedBackups.server = getServer(); + + ABCore.worldName = getServer().getWorlds().get(0).getName(); + ABCore.worldDir = new File(getServer().getWorlds().get(0).getWorldFolder(), "./").toPath(); //need to have this parent because of some forge compatability bullshit.. if i knew how much I'd need to do this I really would've just called getParent on the offending versions + ABCore.modJar = getFile().getAbsoluteFile(); + + infoLogger = getLogger()::info; + warningLogger = getLogger()::warning; + errorLogger = getLogger()::severe; + + ABCore.infoLogger = infoLogger; + ABCore.warningLogger = warningLogger; + ABCore.errorLogger = errorLogger; + + this.getCommand("backup").setExecutor(new AdvancedBackupsCommand()); + getServer().getPluginManager().registerEvents(this, this); + + ConfigManager.loadOrCreateConfig(); + + server.getMessenger().registerIncomingPluginChannel(this, "advancedbackups:toast_subscribe", new PacketListener()); + server.getMessenger().registerOutgoingPluginChannel(this, "advancedbackups:backup_status"); + + + ABCore.clientContactor = new ClientContactor(); + + ABCore.resetActivity = AdvancedBackups::resetActivity; + + new BukkitRunnable() { + @Override + public void run() { + AdvancedBackups.this.onTickEnd(); + } + }.runTaskTimer(this, 0, 0); + } + + @Override + public void onDisable() { + enabled = false; + BackupWrapper.checkShutdownBackups(); + } + + public boolean getEnabled() { + return enabled; + } + + @EventHandler + public void onServerLoad(ServerLoadEvent event) { + BackupWrapper.checkStartupBackups(); + } + + + /* handled in onDisable + @EventHandler + public void onServerStop( event) { + BackupWrapper.checkShutdownBackups();; + }*/ + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + /*Might not need to do this + //Gets us the actual player class, not bukkit's strange reimpl + ServerPlayer player = MinecraftServer.getServer().getPlayerList().getPlayer(event.getPlayer().getUniqueId()); + player.connection.networkmanager*/ + + ABCore.setActivity(true); + } + + public void onTickEnd() { + BackupTimer.check(); + } + + + public static final String savesDisabledMessage = "\n\n\n***************************************\nSAVING DISABLED - PREPARING FOR BACKUP!\n***************************************"; + public static final String savesEnabledMessage = "\n\n\n*********************************\nSAVING ENABLED - BACKUP COMPLETE!\n*********************************"; + public static final String saveCompleteMessage = "\n\n\n*************************************\nSAVE COMPLETE - PREPARING FOR BACKUP!\n*************************************"; + + public static void disableSaving() { + for (World level : server.getWorlds()) { + level.setAutoSave(false); + } + warningLogger.accept(savesDisabledMessage); + } + + public static void enableSaving() { + for (World level : server.getWorlds()) { + level.setAutoSave(true); + } + warningLogger.accept(savesEnabledMessage); + } + + public static void saveOnce(boolean unused) { + + server.savePlayers(); + boolean flag; + + for (World level : server.getWorlds()) { + flag = level.isAutoSave(); + level.setAutoSave(false); + level.save(); + level.setAutoSave(flag); + } + + warningLogger.accept(saveCompleteMessage); + } + + + public static void resetActivity() { + ABCore.setActivity(!server.getOnlinePlayers().isEmpty()); + } + + + + + +} \ No newline at end of file diff --git a/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackupsCommand.java b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackupsCommand.java new file mode 100644 index 00000000..f1ae16b1 --- /dev/null +++ b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/AdvancedBackupsCommand.java @@ -0,0 +1,110 @@ +package computer.heather.AdvancedBackups; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.util.StringUtil; + +import co.uk.mommyheather.advancedbackups.core.CoreCommandSystem; + + +// /backup start | reload-config | reload-client-config | snapshot | reset-chain +public class AdvancedBackupsCommand implements CommandExecutor, TabCompleter { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) + { + return false; + } + else if ("start".equals(args[0])) + { + Start.execute(sender); + } + else if ("reload-config".equals(args[0])) + { + Reload.execute(sender); + } + else if ("reload-client-config".equals(args[0])) + { + ReloadClient.execute(sender); + } + else if ("reset-chain".equals(args[0])) { + ResetChain.execute(sender); + } + else if ("snapshot".equals(args[0])) { + Snapshot.execute(sender); + } + else if ("cancel".equals(args[0])) { + Cancel.execute(sender); + } + else + { + return false; + } + return true; + } + + public static class Reload { + public static void execute(CommandSender sender) { + CoreCommandSystem.reloadConfig((response) -> { + sender.sendMessage(response); + }); + } + } + + public static class ReloadClient { + public static void execute(CommandSender sender) { + sender.sendMessage("This can only be ran on the client!"); + } + } + + public static class Start { + public static void execute(CommandSender sender) { + CoreCommandSystem.startBackup((response) -> { + sender.sendMessage(response); + }); + } + } + + public static class ResetChain { + public static void execute(CommandSender sender) { + CoreCommandSystem.resetChainLength((response) -> { + sender.sendMessage(response); + }); + } + } + + public static class Snapshot { + public static void execute(CommandSender sender) { + CoreCommandSystem.snapshot((response) -> { + sender.sendMessage(response); + }); + } + + } + + public static class Cancel { + public static void execute(CommandSender sender) { + CoreCommandSystem.cancelBackup((response) -> { + sender.sendMessage(response); + }); + } + + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + final List completions = new ArrayList<>(); + StringUtil.copyPartialMatches(args[0], Arrays.asList(new String[] {"start", "reload-config", "reload-client-config", "reset-chain", "snapshot", "cancel"}), completions); + + return completions; + } + + +} diff --git a/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/ClientContactor.java b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/ClientContactor.java new file mode 100644 index 00000000..7eda2858 --- /dev/null +++ b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/ClientContactor.java @@ -0,0 +1,81 @@ +package computer.heather.AdvancedBackups.network; + +import co.uk.mommyheather.advancedbackups.interfaces.IClientContactor; +import computer.heather.AdvancedBackups.AdvancedBackups; + +public class ClientContactor implements IClientContactor { + + private static byte[] toBytes(boolean starting, boolean started, boolean failed, boolean finished, boolean cancelled, int progress, int max) { + byte[] ret = new byte[13]; + + ret[0] = (byte) (starting ? 1 : 0); + ret[1] = (byte) (started ? 1 : 0); + ret[2] = (byte) (failed ? 1 : 0); + ret[3] = (byte) (finished ? 1 : 0); + ret[4] = (byte) (cancelled ? 1 : 0); + + ret[5] = (byte) ((progress & 0xFF000000) >> 24); + ret[6] = (byte) ((progress & 0x00FF0000) >> 16); + ret[7] = (byte) ((progress & 0x0000FF00) >> 8); + ret[8] = (byte) ((progress & 0x000000FF) >> 0); + + ret[9] = (byte) ((max & 0xFF000000) >> 24); + ret[10] = (byte) ((max & 0x00FF0000) >> 16); + ret[11] = (byte) ((max & 0x0000FF00) >> 8); + ret[12] = (byte) ((max & 0x000000FF) >> 0); + + return ret; + } + + //starting, started, failed, finished, cancelled, progress, max + @Override + public void backupComplete(boolean all) { + byte[] bytes = toBytes(false, false, false, true, false, 0, 0); + + AdvancedBackups.server.getOnlinePlayers().forEach((player) -> { + if (!AdvancedBackups.players.contains(player.getUniqueId().toString())) return; + if (player.isOp() || all) player.sendPluginMessage(AdvancedBackups.getPlugin(AdvancedBackups.class), "advancedbackups:backup_status", bytes); + }); + } + + @Override + public void backupFailed(boolean all) { + byte[] bytes = toBytes(false, false, true, false, false, 0, 0); + + AdvancedBackups.server.getOnlinePlayers().forEach((player) -> { + if (!AdvancedBackups.players.contains(player.getUniqueId().toString())) return; + if (player.isOp() || all) player.sendPluginMessage(AdvancedBackups.getPlugin(AdvancedBackups.class), "advancedbackups:backup_status", bytes); + }); + } + + @Override + public void backupProgress(int progress, int max, boolean all) { + byte[] bytes = toBytes(false, true, false, false, false, progress, max); + + AdvancedBackups.server.getOnlinePlayers().forEach((player) -> { + if (!AdvancedBackups.players.contains(player.getUniqueId().toString())) return; + if (player.isOp() || all) player.sendPluginMessage(AdvancedBackups.getPlugin(AdvancedBackups.class), "advancedbackups:backup_status", bytes); + }); + } + + @Override + public void backupStarting(boolean all) { + byte[] bytes = toBytes(true, false, false, false, false, 0, 0); + + AdvancedBackups.server.getOnlinePlayers().forEach((player) -> { + if (!AdvancedBackups.players.contains(player.getUniqueId().toString())) return; + if (player.isOp() || all) player.sendPluginMessage(AdvancedBackups.getPlugin(AdvancedBackups.class), "advancedbackups:backup_status", bytes); + }); + } + + @Override + public void backupCancelled(boolean all) { + byte[] bytes = toBytes(false, false, false, false, true, 0, 0); + + AdvancedBackups.server.getOnlinePlayers().forEach((player) -> { + if (!AdvancedBackups.players.contains(player.getUniqueId().toString())) return; + if (player.isOp() || all) player.sendPluginMessage(AdvancedBackups.getPlugin(AdvancedBackups.class), "advancedbackups:backup_status", bytes); + }); + } + +} diff --git a/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/PacketListener.java b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/PacketListener.java new file mode 100644 index 00000000..0eee9eca --- /dev/null +++ b/spigot/1.21/src/main/java/computer/heather/AdvancedBackups/network/PacketListener.java @@ -0,0 +1,25 @@ +package computer.heather.AdvancedBackups.network; + +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import co.uk.mommyheather.advancedbackups.core.ABCore; +import computer.heather.AdvancedBackups.AdvancedBackups; + +public class PacketListener implements PluginMessageListener { + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + String uuid = player.getUniqueId().toString(); + Boolean subscribe = message[0] != 0; + ABCore.infoLogger.accept("Player has chosen to " + (subscribe ? "accept" : "reject") + " progress updates."); + + if (!AdvancedBackups.players.contains(uuid) && subscribe) { + AdvancedBackups.players.add(uuid); + } + else if (!subscribe) { + AdvancedBackups.players.remove(uuid); + } + } + +} diff --git a/spigot/1.21/src/main/resources/plugin.yml b/spigot/1.21/src/main/resources/plugin.yml new file mode 100644 index 00000000..c1bd8c23 --- /dev/null +++ b/spigot/1.21/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +name: AdvancedBackups +version: ${version} +api-version: '1.21' +main: computer.heather.AdvancedBackups.AdvancedBackups +commands: + backup: + description: Commands for Advanced Backups. + usage: /backup start | reload-config | reload-client-config | snapshot | reset-chain + permission: advancedbackups.backup \ No newline at end of file