Skip to content

Commit

Permalink
Add: auto apply resource pack
Browse files Browse the repository at this point in the history
  • Loading branch information
xfl03 committed Jan 15, 2023
1 parent ebfe484 commit 2d926f2
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 5 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Common
on:
workflow_call:
inputs:
type:
required: true
type: string
publish-task:
required: false
type: string
is-snapshot:
required: false
type: boolean
default: true

jobs:
build-common:
name: Build Common
runs-on: ubuntu-latest
environment: Build
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
cache: 'gradle'
-
name: Build
env:
IS_SNAPSHOT: ${{ inputs.is-snapshot }}
run: |
./gradlew clean build --stacktrace
-
name: Publish
if: ${{ inputs.publish-task }}
env:
CURSEFORGE_API_KEY: ${{ secrets.CURSEFORGE_API_KEY }}
MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }}
run: |
export GIT_COMMIT_DESC=$(git log --format=%B -n 1 $GITHUB_SHA)
./gradlew ${{ inputs.publish-task }} --info --stacktrace
-
uses: actions/upload-artifact@v3
with:
name: CustomSkinLoader-${{ inputs.type }}-${{ github.run_number }}
path: build/libs
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
本Mod用于自动更新「简体中文资源包」。

## 下载
- CurseForge
- [CurseForge](https://www.curseforge.com/minecraft/mc-mods/i18nupdatemod)
- Modrinth

## 支持的版本
- Minecraft:1.6-1.19.3都支持(不代表这些版本都有对应的「简体中文资源包」)
- Minecraft:1.12.2-1.19.3都支持(不代表这些版本都有对应的「简体中文资源包」、未来会添加更多版本支持
- Mod加载器:Forge、Fabric、Quilt都支持(有需求的话也可以增加LiteLoader、Rift支持)
- Java:8-19都支持

Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "i18nupdatemod"
version = "3.1.0-SNAPSHOT"
version = "3.2.0"

java {
toolchain {
Expand Down Expand Up @@ -62,6 +62,7 @@ dependencies {
implementation("commons-codec:commons-codec:1.15")
implementation("org.ow2.asm:asm:9.4")
implementation("org.jetbrains:annotations:24.0.0")
implementation("com.google.code.gson:gson:2.10.1")
}

tasks.test {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/i18nupdatemod/I18nUpdateMod.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package i18nupdatemod;

import i18nupdatemod.core.AssetConfig;
import i18nupdatemod.core.GameConfig;
import i18nupdatemod.core.ResourcePack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -24,11 +25,23 @@ public static void init(Path minecraftPath, String minecraftVersion, String load

ResourcePack.resourcePackPath = minecraftPath.resolve("resourcepacks");
ResourcePack.temporaryPath = Paths.get(userHome, "." + MOD_ID, minecraftVersion);

int minecraftMajorVersion = Integer.parseInt(minecraftVersion.split("\\.")[1]);

try {
//Get asset
Map<AssetConfig.Type, String> assets = AssetConfig.getAsset(minecraftVersion, loader);

//Update resource pack
ResourcePack languagePack =
new ResourcePack(assets.get(AssetConfig.Type.FILE_NAME));
languagePack.checkUpdate(assets.get(AssetConfig.Type.FILE_URL), assets.get(AssetConfig.Type.MD5_URL));

//Apply resource pack
GameConfig config = new GameConfig(minecraftPath.resolve("options.txt"));
config.addResourcePack("Minecraft-Mod-Language-Modpack",
(minecraftMajorVersion <= 12 ? "" : "file/") + assets.get(AssetConfig.Type.FILE_NAME));
config.writeToFile();
} catch (Exception e) {
LOGGER.warn("Failed to update resource pack: {}", e.toString());
// e.printStackTrace();
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/i18nupdatemod/core/GameConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package i18nupdatemod.core;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import i18nupdatemod.I18nUpdateMod;
import org.apache.commons.io.FileUtils;

import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GameConfig {
private static final Gson GSON = new Gson();
private static final Type STRING_LIST_TYPE = new TypeToken<List<String>>() {
}.getType();
protected Map<String, String> configs = new LinkedHashMap<>();
private final Path configFile;

public GameConfig(Path configFile) throws Exception {
this.configFile = configFile;
if (!Files.exists(configFile)) {
return;
}
this.configs = FileUtils.readLines(configFile.toFile(), StandardCharsets.UTF_8).stream()
.map(it -> it.split(":", 2))
.collect(Collectors.toMap(it -> it[0], it -> it[1], (a, b) -> a, LinkedHashMap::new));
}

public void writeToFile() throws Exception {
FileUtils.writeLines(configFile.toFile(), configs.entrySet().stream()
.map(it -> it.getKey() + ":" + it.getValue()).collect(Collectors.toList()));
}

public void addResourcePack(String baseName, String resourcePack) {
List<String> resourcePacks = GSON.fromJson(
configs.computeIfAbsent("resourcePacks", it -> "[]"), STRING_LIST_TYPE);
//Remove other Minecraft Mod Language Pack
resourcePacks = resourcePacks.stream().filter(it -> !it.contains(baseName)).collect(Collectors.toList());
resourcePacks.add(resourcePack);
configs.put("resourcePacks", GSON.toJson(resourcePacks));
I18nUpdateMod.LOGGER.info("Resource Packs: {}", configs.get("resourcePacks"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import i18nupdatemod.util.Reflection;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.impl.FabricLoaderImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -35,7 +34,7 @@ private String getMcVersion(){

}
try {
//Fabric
//Quilt
return (String) Reflection.clazz("org.quiltmc.loader.impl.QuiltLoaderImpl")
.get("INSTANCE")
.get("getGameProvider()")
Expand Down

0 comments on commit 2d926f2

Please sign in to comment.