Skip to content

Commit

Permalink
优化语言设置方法,修复更新检测出现问题时提示两次的问题,再次修复SuggestModScreen背景问题
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Jan 10, 2025
1 parent 75aa30c commit 007eb09
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ classes/
.metadata
.vscode
.settings
*.launch
*.launch
/.architectury-transformer/
32 changes: 19 additions & 13 deletions common/src/main/java/top/vmctcn/vmtranslationupdate/ModEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@

public class ModEvents {
public static void playerJoinEvent(ServerPlayerEntity player) {
String name = player.getName().getString();
String localVersion = ModConfigUtil.getConfig().modPackTranslationVersion;
String onlineVersion = VersionCheckUtil.getOnlineVersion(player);
String onlineVersion = VersionCheckUtil.getOnlineVersion();

if (ModConfigUtil.getConfig().checkModPackTranslationUpdate && !localVersion.equals(onlineVersion)) {
player.sendMessage(Text.translatable("vmtranslationupdate.message.update", name, localVersion, onlineVersion));
Text message = Text.translatable("vmtranslationupdate.message.update2")
.append(Text.translatable(ModConfigUtil.getConfig().modPackTranslationUrl)
.setStyle(Style.EMPTY
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ModConfigUtil.getConfig().modPackTranslationUrl))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("vmtranslationupdate.message.hover")))
.withColor(Formatting.AQUA)
))
.append(Text.translatable("vmtranslationupdate.message.update3"));
if (ModConfigUtil.getConfig().checkModPackTranslationUpdate) {
if (onlineVersion.isEmpty()) {
player.sendMessage(Text.translatable("vmtranslationupdate.message.error"), false);
VMTranslationUpdate.LOGGER.warn("Error fetching modpack translation version");
return;
}

player.sendMessage(message);
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)
.setStyle(Style.EMPTY
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ModConfigUtil.getConfig().modPackTranslationUrl))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("vmtranslationupdate.message.hover")))
.withColor(Formatting.AQUA)
))
.append(Text.translatable("vmtranslationupdate.message.update3"));
player.sendMessage(message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package top.vmctcn.vmtranslationupdate;

import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.vmctcn.vmtranslationupdate.util.GameOptionsUtil;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;
import top.vmctcn.vmtranslationupdate.util.ScreenUtil;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class VMTranslationUpdate {
public static final String MOD_ID = "vmtranslationupdate";
static MinecraftClient client = MinecraftClient.getInstance();
public static final Logger LOGGER = LoggerFactory.getLogger("VMTranslationUpdateMod");
public static final Path OPTIONS_FILE = Paths.get(MinecraftClient.getInstance().runDirectory.toString(), "options.txt");

public static void init() {
ScreenUtil.checkModsLoaded();

if (ModConfigUtil.getConfig().autoSwitchLanguage && ModConfigUtil.getConfig().switchLanguage != null) {
client.options.language = ModConfigUtil.getConfig().switchLanguage;
try {
GameOptionsUtil.createInitFile(OPTIONS_FILE.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package top.vmctcn.vmtranslationupdate.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;
import top.vmctcn.vmtranslationupdate.util.ScreenUtil;

Expand Down Expand Up @@ -43,6 +46,20 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
context.drawCenteredTextWithShadow(this.textRenderer, ScreenUtil.getSuggestScreenText(), this.width / 2, 70, -1);
}

@Override
public void renderBackground(DrawContext context, int mouseX, int mouseY, float partial) {
super.renderBackground(context, mouseX, mouseY, partial);

//Render header and footer separators
RenderSystem.enableBlend();
ScreenUtil.resetShaderColor(context);
Identifier headerIdentifier = MinecraftClient.getInstance().world == null ? Screen.HEADER_SEPARATOR_TEXTURE : Screen.INWORLD_HEADER_SEPARATOR_TEXTURE;
Identifier footerIdentifier = MinecraftClient.getInstance().world == null ? Screen.FOOTER_SEPARATOR_TEXTURE : Screen.INWORLD_FOOTER_SEPARATOR_TEXTURE;
context.drawTexture(RenderLayer::getGuiTextured, headerIdentifier, 0, 40 - 2, 0.0F, 0.0F, this.width, 2, 32, 2);
context.drawTexture(RenderLayer::getGuiTextured, footerIdentifier, 0, this.height - 50, 0.0F, 0.0F, this.width, 2, 32, 2);
ScreenUtil.resetShaderColor(context);
}

@Override
public void close() {
MinecraftClient.getInstance().setScreen(this.lastScreen);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package top.vmctcn.vmtranslationupdate.util;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class GameOptionsUtil {
private static final String DEFAULT_LANG = "lang:" + ModConfigUtil.getConfig().switchLanguage;
private static final String INIT_OPTIONS = DEFAULT_LANG;

public static void createInitFile(File optionsFile) throws IOException {
if (!optionsFile.exists()) {
FileUtils.write(optionsFile, INIT_OPTIONS, StandardCharsets.UTF_8);
} else {
changeFile(optionsFile);
}
}

private static void changeFile(File optionsFile) throws IOException {
List<String> options = FileUtils.readLines(optionsFile, StandardCharsets.UTF_8);
List<String> output = Lists.newArrayList();
boolean hasLang = false;

for (String line : options) {
if (line.startsWith("lang:")) {
line = DEFAULT_LANG;
hasLang = true;
}

output.add(line);
}

if (!hasLang) {
output.add(DEFAULT_LANG);
}

FileUtils.writeLines(optionsFile, StandardCharsets.UTF_8.name(), output, "\n");
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package top.vmctcn.vmtranslationupdate.util;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

public class VersionCheckUtil {
public static String getOnlineVersion(PlayerEntity player) {
public static String getOnlineVersion() {
try {
URI uri = URI.create(ModConfigUtil.getConfig().modPackTranslationUpdateCheckUrl);
URLConnection connection = uri.toURL().openConnection();
Expand All @@ -23,7 +20,6 @@ public static String getOnlineVersion(PlayerEntity player) {
return reader.readLine();
}
} catch (Exception e) {
player.sendMessage(Text.translatable("vmtranslationupdate.message.error"), false);
return "";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"modmenu.nameTranslation.vmtranslationupdate": "VM Translation Update",
"modmenu.descriptionTranslation.vmtranslationupdate": "The VM Chinese translate Group mod is used to check Chinese localization patches and some other utility features.",

"vmtranslationupdate.message.update": "[VM Chinese Group]: Hello %s, your Chinese localization patch is outdated! Current version is %s, latest version is %s.",
"vmtranslationupdate.message.update": "[VM Chinese Group]: your Chinese localization patch is outdated! Current version is %s, latest version is %s.",
"vmtranslationupdate.message.update2": "Please click on the right ",
"vmtranslationupdate.message.update3": " to download the new version.",
"vmtranslationupdate.message.error": "[VM Chinese Group]: Error! There is an issue with the Chinese localization update detection.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"modmenu.nameTranslation.vmtranslationupdate": "VM汉化更新",
"modmenu.descriptionTranslation.vmtranslationupdate": "VM汉化组用于检查汉化更新和一些其他实用功能的模组",

"vmtranslationupdate.message.update": "[VM汉化组]:你好%s,当前汉化已过时,请更新你的汉化!当前版本为%s,最新版为%s。",
"vmtranslationupdate.message.update": "[VM汉化组]:当前汉化已过时,请更新汉化!当前版本为%s,最新版为%s。",
"vmtranslationupdate.message.update2": "请点击右侧",
"vmtranslationupdate.message.update3": "下载新版本。",
"vmtranslationupdate.message.error": "[VM汉化组]:错误!汉化更新检测出现问题。",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"modmenu.nameTranslation.vmtranslationupdate": "VM中文更新檔更新",
"modmenu.descriptionTranslation.vmtranslationupdate": "VM中文翻譯組用於檢查中文更新檔更新和一些其他實用功能的模組。",

"vmtranslationupdate.message.update": "[VM中文翻譯組]:你好%s,當前漢化已過時,請更新你的中文更新檔!當前版本為%s,最新版為%s。",
"vmtranslationupdate.message.update": "[VM中文翻譯組]:當前漢化已過時,請更新你的中文更新檔!當前版本為%s,最新版為%s。",
"vmtranslationupdate.message.update2": "請點擊右側",
"vmtranslationupdate.message.update3": "下載新版本。",
"vmtranslationupdate.message.error": "[VM中文翻譯組]:錯誤!漢化更新檢測出現問題。",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package top.vmctcn.vmtranslationupdate.fabric;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import top.vmctcn.vmtranslationupdate.ModEvents;
import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtranslationupdate.util.ScreenUtil;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;

public class VMTranslationUpdateClientFabric implements ClientModInitializer {
@Override
Expand Down

0 comments on commit 007eb09

Please sign in to comment.