Skip to content

Commit

Permalink
添加Gui跨版本兼容层,screenAfterInitEvent方法移动到ModEvents,修改coremod检测方式
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Jan 10, 2025
1 parent 007eb09 commit b804119
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
20 changes: 20 additions & 0 deletions common/src/main/java/top/vmctcn/vmtranslationupdate/ModEvents.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package top.vmctcn.vmtranslationupdate;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import top.vmctcn.vmtranslationupdate.screen.SuggestModScreen;
import top.vmctcn.vmtranslationupdate.util.ModConfigUtil;

import top.vmctcn.vmtranslationupdate.util.VersionCheckUtil;

public class ModEvents {
public static boolean firstTitleScreenShown = false;

public static void playerJoinEvent(ServerPlayerEntity player) {
String localVersion = ModConfigUtil.getConfig().modPackTranslationVersion;
String onlineVersion = VersionCheckUtil.getOnlineVersion();
Expand All @@ -36,4 +42,18 @@ public static void playerJoinEvent(ServerPlayerEntity player) {
}
}
}

public static void screenAfterInitEvent(Screen screen) {
if (firstTitleScreenShown || !(screen instanceof TitleScreen)) {
return;
}

String language = MinecraftClient.getInstance().getLanguageManager().getLanguage();

if ("zh_cn".equals(language)) {
MinecraftClient.getInstance().setScreen(new SuggestModScreen(screen));
}

firstTitleScreenShown = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand All @@ -17,8 +16,6 @@ public class VMTranslationUpdate {
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) {
try {
GameOptionsUtil.createInitFile(OPTIONS_FILE.toFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
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;
import top.vmctcn.vmtranslationupdate.verlayer.ScreenHelper;

public class SuggestModScreen extends Screen {
public final Screen lastScreen;
Expand Down Expand Up @@ -52,12 +52,12 @@ public void renderBackground(DrawContext context, int mouseX, int mouseY, float

//Render header and footer separators
RenderSystem.enableBlend();
ScreenUtil.resetShaderColor(context);
ScreenHelper.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);
ScreenHelper.drawGuiTexture(context, headerIdentifier, 0, 40 - 2, 0.0F, 0.0F, this.width, 2, 32, 2);
ScreenHelper.drawGuiTexture(context, footerIdentifier, 0, this.height - 50, 0.0F, 0.0F, this.width, 2, 32, 2);
ScreenHelper.resetShaderColor(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
package top.vmctcn.vmtranslationupdate.util;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.apache.commons.lang3.StringUtils;
import top.vmctcn.vmtranslationupdate.screen.SuggestModScreen;

public class ScreenUtil {
public static boolean i18nUpdateModPresent = false;
public static boolean vaultPatcherPresent = false;
public static boolean firstTitleScreenShown = false;
public static boolean i18nUpdateModPresent = isCoreModClassLoaded("i18nupdatemod.I18nUpdateMod");
public static boolean vaultPatcherPresent = isCoreModClassLoaded("me.fengming.vaultpatcher_asm.VaultPatcher");
public static final Text downloadButtonText = Text.translatable("mco.brokenworld.download");
public static final Text ignoreButtonText = Text.translatable("selectWorld.backupJoinSkipButton");

public static void screenAfterInitEvent(Screen screen) {
if (firstTitleScreenShown || !(screen instanceof TitleScreen)) {
return;
}

String language = MinecraftClient.getInstance().getLanguageManager().getLanguage();

if ("zh_cn".equals(language)) {
MinecraftClient.getInstance().setScreen(new SuggestModScreen(screen));
}

firstTitleScreenShown = true;
}

public static void checkModsLoaded() {
public static boolean isCoreModClassLoaded(String className) {
try {
Class.forName("i18nupdatemod.I18nUpdateMod");
i18nUpdateModPresent = true;
Class.forName(className);
return true; // 类存在,coremod已加载
} catch (ClassNotFoundException e) {
i18nUpdateModPresent = false;
}

try {
Class.forName("me.fengming.vaultpatcher_asm.VaultPatcher");
vaultPatcherPresent = true;
} catch (ClassNotFoundException e) {
vaultPatcherPresent = false;
return false; // 类不存在
}
}

Expand All @@ -59,10 +33,6 @@ public static void openUrlOnScreen(MinecraftClient client, Screen screen, String
}
}

public static void resetShaderColor(DrawContext context) {
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
}

public static Text getSuggestScreenTitle() {
Text titleText = Text.empty();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package top.vmctcn.vmtranslationupdate.verlayer;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.Identifier;

public class ScreenHelper {
public static void drawGuiTexture(DrawContext context, Identifier sprite, int x, int y, float u, float v, int width, int height, int textureWidth, int textureHeight) {
context.drawTexture(RenderLayer::getGuiTextured, sprite, x, y, u, v, width, height, textureWidth, textureHeight);
}

public static void resetShaderColor(DrawContext context) {
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import net.fabricmc.api.ClientModInitializer;
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;

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

ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
ScreenUtil.screenAfterInitEvent(screen);
ModEvents.screenAfterInitEvent(screen);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.client.event.ScreenEvent;
Expand All @@ -15,7 +14,6 @@
import top.vmctcn.vmtranslationupdate.ModEvents;
import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtranslationupdate.config.ModConfigs;
import top.vmctcn.vmtranslationupdate.util.ScreenUtil;

@Mod(value = VMTranslationUpdate.MOD_ID, dist = Dist.CLIENT)
public class VMTranslationUpdateClientNeoForge {
Expand All @@ -32,7 +30,7 @@ public VMTranslationUpdateClientNeoForge(ModContainer modContainer) {
});

forgeEventBus.addListener(ScreenEvent.Init.Pre.class, event -> {
ScreenUtil.screenAfterInitEvent(event.getScreen());
ModEvents.screenAfterInitEvent(event.getScreen());
});
}
}
Expand Down

0 comments on commit b804119

Please sign in to comment.