Skip to content

Commit

Permalink
添加modpackinfo.json功能 #14
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Jan 11, 2025
1 parent adb8a7a commit 166182d
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import top.vmctcn.vmtranslationupdate.modpack.ModpackInfoReader;
import top.vmctcn.vmtranslationupdate.screen.SuggestModScreen;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;

Expand All @@ -18,7 +19,7 @@ public class ModEvents {
public static boolean firstTitleScreenShown = false;

public static void playerJoinEvent(ServerPlayerEntity player) {
String localVersion = ModConfigUtil.getConfig().modPackTranslationVersion;
String localVersion = ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getVersion();
String onlineVersion = VersionCheckUtil.getOnlineVersion();

if (ModConfigUtil.getConfig().checkModPackTranslationUpdate) {
Expand All @@ -31,9 +32,9 @@ public static void playerJoinEvent(ServerPlayerEntity player) {
if (!localVersion.equals(onlineVersion)) {
player.sendMessage(Text.translatable("vmtranslationupdate.message.update", localVersion, onlineVersion));
Text message = Text.translatable("vmtranslationupdate.message.update2")
.append(Text.translatable(ModConfigUtil.getConfig().modPackTranslationUrl)
.append(Text.translatable(ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getUrl())
.setStyle(Style.EMPTY
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ModConfigUtil.getConfig().modPackTranslationUrl))
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getUrl()))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("vmtranslationupdate.message.hover")))
.withColor(Formatting.AQUA)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.vmctcn.vmtranslationupdate.modpack.ModpackInfo;
import top.vmctcn.vmtranslationupdate.modpack.ModpackInfoReader;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;

public class VMTranslationUpdate {
public static final String MOD_ID = "vmtranslationupdate";
public static final Logger LOGGER = LoggerFactory.getLogger("VMTranslationUpdateMod");

public static void init() {
if (ModConfigUtil.getConfig().testMode) {
ModpackInfo.Modpack modpack = ModpackInfoReader.getModpackInfo().getModpack();
ModpackInfo.Translation translation = modpack.getTranslation();

LOGGER.warn("Modpack Name: {}", modpack.getName());
LOGGER.warn("Modpack Version: {}", modpack.getVersion());
LOGGER.warn("Modpack Translation URL: {}", translation.getUrl());
LOGGER.warn("Modpack Translation Update Check URL: {}", translation.getUpdateCheckUrl());
LOGGER.warn("Modpack Translation Language: {}", translation.getLanguage());
LOGGER.warn("Modpack Translation Version: {}", translation.getVersion());
LOGGER.warn("Modpack Translation Resource Pack Name: {}", translation.getResourcePackName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@

@Config(name = VMTranslationUpdate.MOD_ID)
public class ModConfigs implements ConfigData {
public boolean testMode = false;

public boolean autoSwitchLanguage = true;
public String switchLanguage = "zh_cn";

public boolean autoDownloadVMTranslationPack = false;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public ResPackSource translationPackSource = ResPackSource.GITEE;

public boolean checkModPackTranslationUpdate = true;
public String modPackTranslationUpdateCheckUrl = "https://gitee.com/Wulian233/vmtu/raw/main/update/example.txt";
public String modPackTranslationUrl = "https://vmct-cn.top/modpacks/example/";

public String modPackTranslationVersion = "1.0.0";

public boolean i18nUpdateModCheck = true;
public boolean vaultPatcherCheck = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package top.vmctcn.vmtranslationupdate.modpack;

public class ModpackInfo {
private Modpack modpack;

public Modpack getModpack() {
return modpack;
}

public static class Modpack {
private String name;
private String version;
private Translation translation;

public String getName() {
return name;
}

public String getVersion() {
return version;
}

public Translation getTranslation() {
return translation;
}
}

public static class Translation {
private String url;
private String language;
private String version;
private String updateCheckUrl;
private String resourcePackName;

public String getUrl() {
return url;
}

public String getLanguage() {
return language;
}

public String getVersion() {
return version;
}

public String getUpdateCheckUrl() {
return updateCheckUrl;
}

public String getResourcePackName() {
return resourcePackName;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package top.vmctcn.vmtranslationupdate.modpack;

import com.google.gson.Gson;
import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtucore.ModPlatform;

import java.io.FileReader;
import java.io.Reader;
import java.nio.file.Path;

public class ModpackInfoReader {
private static final Gson GSON = new Gson();
private static ModpackInfo modpackInfo;
private static final Path gamePath = ModPlatform.INSTANCE.getGameDir();

static {
init();
}

public static void init() {
try (Reader reader = new FileReader(gamePath.resolve("modpackinfo.json").toString())) {
modpackInfo = GSON.fromJson(reader, ModpackInfo.class);
} catch (Exception e) {
VMTranslationUpdate.LOGGER.warn("Error getting index: " + e);
}
}

public static ModpackInfo getModpackInfo() {
return modpackInfo;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package top.vmctcn.vmtranslationupdate.respack;

import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtranslationupdate.modpack.ModpackInfoReader;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;
import top.vmctcn.vmtucore.ModPlatform;
import top.vmctcn.vmtucore.VMTUCore;
Expand All @@ -11,10 +12,10 @@

public class GameOptionsSetter {
public static void init(Path gamePath) {
if (ModConfigUtil.getConfig().autoSwitchLanguage && ModConfigUtil.getConfig().switchLanguage != null) {
if (ModConfigUtil.getConfig().autoSwitchLanguage && ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getLanguage() != null) {
try {
GameOptionsWriter config = new GameOptionsWriter(gamePath.resolve("options.txt"));
config.switchLanguage(ModConfigUtil.getConfig().switchLanguage);
config.switchLanguage(ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getLanguage());
} catch (IOException e) {
VMTranslationUpdate.LOGGER.warn("Failed to switch language: ", e);
}
Expand All @@ -23,7 +24,7 @@ public static void init(Path gamePath) {
if (ModConfigUtil.getConfig().autoDownloadVMTranslationPack) {
String gameVersion = ModPlatform.INSTANCE.getGameVersion();
ResPackSource resPackSource = ModConfigUtil.getConfig().translationPackSource;
VMTUCore.init(gamePath, gameVersion, "VM汉化组模组汉化包1.20", resPackSource.getUrl());
VMTUCore.init(gamePath, gameVersion, ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getResourcePackName(), resPackSource.getUrl());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.vmctcn.vmtranslationupdate.verlayer;
package top.vmctcn.vmtranslationupdate.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.util.Identifier;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;
import top.vmctcn.vmtranslationupdate.util.ScreenUtil;
import top.vmctcn.vmtranslationupdate.verlayer.ScreenHelper;

public class SuggestModScreen extends Screen {
public final Screen lastScreen;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package top.vmctcn.vmtranslationupdate.util;

import top.vmctcn.vmtranslationupdate.modpack.ModpackInfoReader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
Expand All @@ -9,7 +11,7 @@
public class VersionCheckUtil {
public static String getOnlineVersion() {
try {
URI uri = URI.create(ModConfigUtil.getConfig().modPackTranslationUpdateCheckUrl);
URI uri = URI.create(ModpackInfoReader.getModpackInfo().getModpack().getTranslation().getUpdateCheckUrl());
URLConnection connection = uri.toURL().openConnection();

String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
"vmtranslationupdate.warn.text": "%s is not yet installed, which may result in incomplete localization of some mods! Please click the button below to jump to the download page.",

"text.autoconfig.vmtranslationupdate.title": "VM Translation Update Config",
"text.autoconfig.vmtranslationupdate.option.testMode": "Test Mode",
"text.autoconfig.vmtranslationupdate.option.autoSwitchLanguage": "Auto switch language",
"text.autoconfig.vmtranslationupdate.option.autoDownloadVMTranslationPack": "Auto download translation pack",
"text.autoconfig.vmtranslationupdate.option.translationPackSource": "Translation pack source",
"text.autoconfig.vmtranslationupdate.option.translationPackSource.@Tooltip": "Choose translation pack download source",
"text.autoconfig.vmtranslationupdate.option.checkModPackTranslationUpdate": "Auto-detect modpack translation version update",
"text.autoconfig.vmtranslationupdate.option.i18nUpdateModCheck": "Detecting if I18nUpdateMod is already installed",
"text.autoconfig.vmtranslationupdate.option.vaultPatcherCheck": "Detect if VaultPatcher is installed",

"text.autoconfig.vmtranslationupdate.option.switchLanguage": "Switch language to",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUpdateCheckUrl": "Update link",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUrl": "Download link",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationVersion": "Modpack translation version"
"text.autoconfig.vmtranslationupdate.option.vaultPatcherCheck": "Detect if VaultPatcher is installed"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
"vmtranslationupdate.warn.text": "%s 尚未安装,这可能导致某些模组§c汉化不完全§r!请点击下面的按钮跳转到下载页面。",

"text.autoconfig.vmtranslationupdate.title": "VM汉化更新检测配置",
"text.autoconfig.vmtranslationupdate.option.testMode": "开发测试模式",
"text.autoconfig.vmtranslationupdate.option.autoSwitchLanguage": "自动切换语言",
"text.autoconfig.vmtranslationupdate.option.checkModPackTranslationUpdate": "自动检测整合包汉化版本更新",
"text.autoconfig.vmtranslationupdate.option.i18nUpdateModCheck": "检测I18nUpdateMod是否已经安装",
"text.autoconfig.vmtranslationupdate.option.vaultPatcherCheck": "检测VaultPatcher是否已经安装",

"text.autoconfig.vmtranslationupdate.option.switchLanguage": "切换语言为",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUpdateCheckUrl": "更新检测链接",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUrl": "下载链接",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationVersion": "整合包翻译版本",
"text.autoconfig.vmtranslationupdate.option.autoDownloadVMTranslationPack": "自动下载汉化资源包",
"text.autoconfig.vmtranslationupdate.option.translationPackSource": "汉化资源包下载源",
"text.autoconfig.vmtranslationupdate.option.translationPackSource.@Tooltip": "指定汉化资源包的下载来源"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
"vmtranslationupdate.warn.text": "%s 尚未安裝,這可能導致某些模組§c中文更新檔更新不完全§r!請點擊下面的按鈕跳轉到下載頁面。",

"text.autoconfig.vmtranslationupdate.title": "VM中文更新檔更新檢測配寘",
"text.autoconfig.vmtranslationupdate.option.testMode": "開發測試模式",
"text.autoconfig.vmtranslationupdate.option.autoSwitchLanguage": "自動切換語言",
"text.autoconfig.vmtranslationupdate.option.checkModPackTranslationUpdate": "自動檢測模組包版本更新",
"text.autoconfig.vmtranslationupdate.option.i18nUpdateModCheck": "檢測I18nUpdateMod是否已經安裝",
"text.autoconfig.vmtranslationupdate.option.vaultPatcherCheck": "檢測VaultPatcher是否已經安裝",

"text.autoconfig.vmtranslationupdate.option.switchLanguage": "切換語言为",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUpdateCheckUrl": "更新檢測鏈接",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationUrl": "下載鏈接",
"text.autoconfig.vmtranslationupdate.option.modPackTranslationVersion": "整合包翻譯版本",
"text.autoconfig.vmtranslationupdate.option.autoDownloadVMTranslationPack": "自動下載簡體中文模組補充翻譯資源包",
"text.autoconfig.vmtranslationupdate.option.translationPackSource": "簡體中文模組補充翻譯資源包下載源",
"text.autoconfig.vmtranslationupdate.option.translationPackSource.@Tooltip": "指定簡體中文模組補充翻譯資源包的下載來源"
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/top/vmctcn/vmtucore/ModPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import top.vmctcn.vmtucore.util.ServiceHelper;

import java.nio.file.Path;

public interface ModPlatform {
ModPlatform INSTANCE = ServiceHelper.loadService(ModPlatform.class);

String getGameVersion();

Path getGameDir();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.loader.api.FabricLoader;
import top.vmctcn.vmtranslationupdate.ModEvents;
import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtranslationupdate.respack.GameOptionsSetter;

public class VMTranslationUpdateClientFabric implements ClientModInitializer {
@Override
public void onInitializeClient() {
VMTranslationUpdate.init();

GameOptionsSetter.init(FabricLoader.getInstance().getGameDir());

ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package top.vmctcn.vmtucore.fabric;

import com.google.auto.service.AutoService;
import net.fabricmc.loader.api.FabricLoader;
import top.vmctcn.vmtucore.ModPlatform;
import top.vmctcn.vmtucore.util.ReflectionHelper;

import java.nio.file.Path;

@AutoService(ModPlatform.class)
public class ModPlatformImpl implements ModPlatform {
@Override
Expand All @@ -28,4 +31,9 @@ public String getGameVersion() {
}
return null;
}

@Override
public Path getGameDir() {
return FabricLoader.getInstance().getGameDir();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package top.vmctcn.vmtranslationupdate.neoforge;

import net.minecraft.client.gui.screen.Screen;
import net.neoforged.fml.ModContainer;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;

import java.util.function.Function;

public class NeoHelper {
public static void registerConfigScreen(ModContainer modContainer, Function<Screen, Screen> screenFunction) {
modContainer.registerExtensionPoint(IConfigScreenFactory.class, (client, screen) -> screenFunction.apply(screen));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
public class VMTranslationUpdateClientNeoForge {
public VMTranslationUpdateClientNeoForge(ModContainer modContainer) {
if (FMLLoader.getDist().isClient()) {
VMTranslationUpdate.init();

GameOptionsSetter.init(FMLPaths.GAMEDIR.get());

modContainer.registerExtensionPoint(IConfigScreenFactory.class, (client, screen) -> AutoConfig.getConfigScreen(ModConfigs.class, screen).get());
NeoHelper.registerConfigScreen(modContainer, screen -> AutoConfig.getConfigScreen(ModConfigs.class, screen).get());

NeoForge.EVENT_BUS.addListener(PlayerEvent.PlayerLoggedInEvent.class, event -> {
ModEvents.playerJoinEvent((ServerPlayerEntity) event.getEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import cpw.mods.modlauncher.Launcher;
import net.neoforged.fml.loading.FMLPaths;
import top.vmctcn.vmtucore.ModPlatform;
import top.vmctcn.vmtucore.VMTUCore;
import top.vmctcn.vmtucore.util.ReflectionHelper;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;

@AutoService(ModPlatform.class)
public class ModPlatformImpl implements ModPlatform {
Expand Down Expand Up @@ -39,4 +41,9 @@ public String getGameVersion() {
}
return null;
}

@Override
public Path getGameDir() {
return FMLPaths.GAMEDIR.get();
}
}

0 comments on commit 166182d

Please sign in to comment.