From a9fc25a83fa0649d9db4d4da6c75f77e8178592a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=9D=E5=A4=8F=E5=90=8C=E5=AD=A6?= <2411829240@qq.com> Date: Sun, 28 Jan 2024 00:18:30 +0800 Subject: [PATCH 1/2] support i18n --- .../com/sintinium/oauth/GuiEventHandler.java | 9 +- .../oauth/gui/LoginLoadingScreen.java | 23 +++-- .../com/sintinium/oauth/gui/LoginScreen.java | 99 ++++++++++++------- .../sintinium/oauth/gui/LoginTypeScreen.java | 9 +- .../resources/assets/oauth/lang/en_US.lang | 22 +++++ .../resources/assets/oauth/lang/zh_CN.lang | 22 +++++ 6 files changed, 135 insertions(+), 49 deletions(-) create mode 100644 src/main/resources/assets/oauth/lang/en_US.lang create mode 100644 src/main/resources/assets/oauth/lang/zh_CN.lang diff --git a/src/main/java/com/sintinium/oauth/GuiEventHandler.java b/src/main/java/com/sintinium/oauth/GuiEventHandler.java index d807f92..74f99e4 100644 --- a/src/main/java/com/sintinium/oauth/GuiEventHandler.java +++ b/src/main/java/com/sintinium/oauth/GuiEventHandler.java @@ -6,6 +6,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiMultiplayer; +import net.minecraft.client.resources.I18n; import net.minecraftforge.client.event.GuiScreenEvent; import com.sintinium.oauth.gui.LoginTypeScreen; @@ -16,7 +17,7 @@ public class GuiEventHandler { - private static TextWidget statusText = new TextWidget(10 + 66 + 3, 12, "Status: loading"); + private static TextWidget statusText = new TextWidget(10 + 66 + 3, 12, I18n.format("oauth.status.loading")); @SubscribeEvent public void multiplayerScreenOpen(GuiScreenEvent.InitGuiEvent.Post event) { @@ -27,16 +28,16 @@ public void multiplayerScreenOpen(GuiScreenEvent.InitGuiEvent.Post event) { // Method addButtonMethod = ObfuscationReflectionHelper.findMethod(Screen.class, "func_230480_a_", // Widget.class); List buttonList = new ArrayList<>(); - GuiButton loginButton = new GuiButton(29183, 10, 6, 66, 20, "Oauth Login"); + GuiButton loginButton = new GuiButton(29183, 10, 6, 66, 20, I18n.format("oauth.btn.oauth.login")); // p_onPress_1_ -> buttonList.add(loginButton); Thread thread = new Thread(() -> { boolean isOnline = LoginUtil.isOnline(); if (isOnline) { - statusText.setText("Status: online"); + statusText.setText(I18n.format("oauth.status.online")); statusText.setColor(0x55FF55); } else { - statusText.setText("Status: offline"); + statusText.setText(I18n.format("oauth.status.offline")); statusText.setColor(0xFF5555); } }); diff --git a/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java b/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java index 4e383df..fef66d4 100644 --- a/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java +++ b/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java @@ -5,10 +5,11 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.resources.I18n; public class LoginLoadingScreen extends GuiScreenCustom { - private String loadingText = "Loading"; + private String loadingText = I18n.format("oauth.text.loading"); private int dots = 0; private String renderText = loadingText; @@ -17,7 +18,7 @@ public class LoginLoadingScreen extends GuiScreenCustom { private int tick = 0; private Runnable onCancel; private boolean isMicrosoft; - private String title = "Logging in"; + private String title = I18n.format("oauth.screen.title.microsoft"); private AtomicReference updateText = new AtomicReference<>(); protected LoginLoadingScreen(GuiScreen multiplayerScreen, GuiScreen callingScreen, Runnable onCancel, @@ -26,7 +27,7 @@ protected LoginLoadingScreen(GuiScreen multiplayerScreen, GuiScreen callingScree this.lastScreen = callingScreen; this.onCancel = onCancel; this.isMicrosoft = isMicrosoft; - updateText.set("Check your browser"); + updateText.set(I18n.format("oauth.text.check.browser")); } public void updateText(String text) { @@ -35,10 +36,18 @@ public void updateText(String text) { @Override public void initGui() { - this.addButton(new ActionButton(0, this.width / 2 - 100, this.height / 2 + 60, 200, 20, "Cancel", () -> { - onCancel.run(); - Minecraft.getMinecraft().displayGuiScreen(lastScreen); - })); + this.addButton( + new ActionButton( + 0, + this.width / 2 - 100, + this.height / 2 + 60, + 200, + 20, + I18n.format("gui.cancel"), + () -> { + onCancel.run(); + Minecraft.getMinecraft().displayGuiScreen(lastScreen); + })); } @Override diff --git a/src/main/java/com/sintinium/oauth/gui/LoginScreen.java b/src/main/java/com/sintinium/oauth/gui/LoginScreen.java index 3a407f8..359533e 100644 --- a/src/main/java/com/sintinium/oauth/gui/LoginScreen.java +++ b/src/main/java/com/sintinium/oauth/gui/LoginScreen.java @@ -10,6 +10,7 @@ import net.minecraft.client.gui.GuiMultiplayer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.resources.I18n; import org.lwjgl.input.Keyboard; @@ -25,7 +26,7 @@ public class LoginScreen extends GuiScreenCustom { private OAuthCheckbox savePasswordWidget; private GuiTextField usernameWidget; private AtomicReference status = new AtomicReference<>(); - private String title = "OAuth Login"; + private String title = I18n.format("oauth.screen.title.mojang"); private List toRun = new CopyOnWriteArrayList<>(); @@ -90,9 +91,10 @@ public void initGui() { this.savePasswordWidget = this.addButton( new OAuthCheckbox( 4, - this.width / 2 - this.fontRendererObj.getStringWidth("Save password") - 25, + this.width / 2 - this.fontRendererObj.getStringWidth(I18n.format("oauth.text.savepassword")) + - 25, this.height / 2 + 1 + 2, - "Save password", + I18n.format("oauth.text.savepassword"), false)); Runnable savePw = () -> { @@ -103,34 +105,53 @@ public void initGui() { } }; - this.mojangLoginButton = this - .addButton(new ResponsiveButton(2, this.width / 2 - 100, this.height / 2 + 36, 200, 20, "Login", () -> { - Thread thread = new Thread(() -> { - if (usernameWidget.getText().isEmpty()) { - toRun.add(() -> this.status.set("Missing username!")); - } else { - Optional didSuccessfullyLogIn = LoginUtil - .loginMojangOrLegacy(usernameWidget.getText(), passwordWidget.getText()); - savePw.run(); - if (!didSuccessfullyLogIn.isPresent()) { - toRun.add(() -> this.status.set("You seem to be offline. Check your connection!")); - } else if (!didSuccessfullyLogIn.get()) { - toRun.add(() -> this.status.set("Wrong password or username!")); - } else { - LoginUtil.updateOnlineStatus(); - toRun.add(() -> Minecraft.getMinecraft().displayGuiScreen(multiplayerScreen)); + this.mojangLoginButton = this.addButton( + new ResponsiveButton( + 2, + this.width / 2 - 100, + this.height / 2 + 36, + 200, + 20, + I18n.format("oauth.btn.login"), + () -> { + Thread thread = new Thread(() -> { + if (usernameWidget.getText().isEmpty()) { + toRun.add(() -> this.status.set(I18n.format("oauth.text.username.missing"))); + } else { + Optional didSuccessfullyLogIn = LoginUtil + .loginMojangOrLegacy(usernameWidget.getText(), passwordWidget.getText()); + savePw.run(); + if (!didSuccessfullyLogIn.isPresent()) { + toRun.add(() -> this.status.set(I18n.format("oauth.text.maybe.offline"))); + } else if (!didSuccessfullyLogIn.get()) { + toRun.add( + () -> this.status + .set(I18n.format("oauth.text.usernameorpassword.wrong"))); + } else { + LoginUtil.updateOnlineStatus(); + toRun.add(() -> Minecraft.getMinecraft().displayGuiScreen(multiplayerScreen)); + } + } + }); + thread.start(); + }, + this::updateLoginButton, + () -> this.mojangLoginButton.displayString = I18n.format("oauth.btn.login"))); + + this.addButton( + new ActionButton( + 3, + this.width / 2 - 100, + this.height / 2 + 60, + 200, + 20, + I18n.format("gui.cancel"), + () -> { + if (!this.savePasswordWidget.isChecked()) { + removeLoginInfo(); } - } - }); - thread.start(); - }, this::updateLoginButton, () -> this.mojangLoginButton.displayString = "Login")); - - this.addButton(new ActionButton(3, this.width / 2 - 100, this.height / 2 + 60, 200, 20, "Cancel", () -> { - if (!this.savePasswordWidget.isChecked()) { - removeLoginInfo(); - } - Minecraft.getMinecraft().displayGuiScreen(lastScreen); - })); + Minecraft.getMinecraft().displayGuiScreen(lastScreen); + })); this.cleanUp(); @@ -157,9 +178,9 @@ private void onEdited(int id, String value) { private void updateLoginButton() { if (this.passwordWidget.getText().isEmpty()) { - this.mojangLoginButton.displayString = "Login Offline"; + this.mojangLoginButton.displayString = I18n.format("btn.login.offline"); } else { - this.mojangLoginButton.displayString = "Login"; + this.mojangLoginButton.displayString = I18n.format("btn.login"); } } @@ -208,8 +229,18 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawBackground(0); drawCenteredString(mc.fontRenderer, title, width / 2, 17, 16777215); - drawString(mc.fontRenderer, "Username/Email", this.width / 2 - 100, this.height / 2 - 60 - 12, 10526880); - drawString(mc.fontRenderer, "Password", this.width / 2 - 100, this.height / 2 - 20 - 12, 10526880); + drawString( + mc.fontRenderer, + I18n.format("oauth.text.usernameoremail"), + this.width / 2 - 100, + this.height / 2 - 60 - 12, + 10526880); + drawString( + mc.fontRenderer, + I18n.format("oauth.text.password"), + this.width / 2 - 100, + this.height / 2 - 20 - 12, + 10526880); if (status.get() != null) { drawCenteredString(mc.fontRenderer, status.get(), width / 2, height / 2 + 20, 0xFF0000); diff --git a/src/main/java/com/sintinium/oauth/gui/LoginTypeScreen.java b/src/main/java/com/sintinium/oauth/gui/LoginTypeScreen.java index e44dbb4..6006e99 100644 --- a/src/main/java/com/sintinium/oauth/gui/LoginTypeScreen.java +++ b/src/main/java/com/sintinium/oauth/gui/LoginTypeScreen.java @@ -3,6 +3,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiMultiplayer; +import net.minecraft.client.resources.I18n; import com.sintinium.oauth.login.LoginUtil; import com.sintinium.oauth.login.MicrosoftLogin; @@ -28,7 +29,7 @@ public void initGui() { this.height / 2 - 20 - 2, 200, 20, - "Mojang Login", + I18n.format("oauth.btn.login.mojang"), () -> { Minecraft.getMinecraft().displayGuiScreen(new LoginScreen(this, lastScreen)); })); this.addButton( new ActionButton( @@ -37,7 +38,7 @@ public void initGui() { this.height / 2 + 2, 200, 20, - "Microsoft Login", + I18n.format("oauth.btn.login.microsoft"), () -> { final MicrosoftLogin login = new MicrosoftLogin(); LoginLoadingScreen loadingScreen = new LoginLoadingScreen( @@ -64,7 +65,7 @@ public void initGui() { this.height / 2 + 60, 200, 20, - "Cancel", + I18n.format("gui.cancel"), () -> { Minecraft.getMinecraft().displayGuiScreen(lastScreen); })); } @@ -82,7 +83,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawBackground(0); drawCenteredString( Minecraft.getMinecraft().fontRenderer, - "Select Account Type", + I18n.format("oauth.screen.title"), this.width / 2, this.height / 2 - 60, 0xFFFFFF); diff --git a/src/main/resources/assets/oauth/lang/en_US.lang b/src/main/resources/assets/oauth/lang/en_US.lang new file mode 100644 index 0000000..88ae91d --- /dev/null +++ b/src/main/resources/assets/oauth/lang/en_US.lang @@ -0,0 +1,22 @@ +oauth.btn.oauth.login=Oauth Login +oauth.status.loading=Status: loading +oauth.status.online=Status: online +oauth.status.offline=Status: offline + +oauth.screen.title=Select Account Type +oauth.btn.login.mojang=Mojang Login +oauth.btn.login.microsoft=Microsoft Login + +oauth.screen.title.mojang=OAuth Login +oauth.text.usernameoremail=Username/Email +oauth.text.password=Password +oauth.text.savepassword=Save password +oauth.text.username.missing=Missing username! +oauth.text.maybe.offline=You seem to be offline. Check your connection! +oauth.text.usernameorpassword.wrong=Wrong password or username! +oauth.btn.login=Login +oauth.btn.login.offline=Login Offline + +oauth.screen.title.microsoft=Logging in +oauth.text.loading=Loading +oauth.text.check.browser=Check your browser diff --git a/src/main/resources/assets/oauth/lang/zh_CN.lang b/src/main/resources/assets/oauth/lang/zh_CN.lang new file mode 100644 index 0000000..95cc4ee --- /dev/null +++ b/src/main/resources/assets/oauth/lang/zh_CN.lang @@ -0,0 +1,22 @@ +oauth.btn.oauth.login=Oauth登录 +oauth.status.loading=状态: 加载中 +oauth.status.online=状态: 在线 +oauth.status.offline=状态: 离线 + +oauth.screen.title=选择账号类型 +oauth.btn.login.mojang=Mojang登录 +oauth.btn.login.microsoft=Microsoft登录 + +oauth.screen.title.mojang=OAuth登录 +oauth.text.usernameoremail=用户名/邮箱 +oauth.text.password=密码 +oauth.text.savepassword=记住密码 +oauth.text.username.missing=缺失用户名! +oauth.text.maybe.offline=你好像处于离线。检查你的连接! +oauth.text.usernameorpassword.wrong=账号或密码错误! +oauth.btn.login=登录 +oauth.btn.login.offline=离线登录 + +oauth.screen.title.microsoft=Microsoft登录 +oauth.text.loading=登录中 +oauth.text.check.browser=请查看浏览器 From 48d44e7a8703aa28f449fd241a96033989c2cc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=9D=E5=A4=8F=E5=90=8C=E5=AD=A6?= <2411829240@qq.com> Date: Sun, 28 Jan 2024 00:20:50 +0800 Subject: [PATCH 2/2] Add title to Microsoft login screen --- .../java/com/sintinium/oauth/gui/LoginLoadingScreen.java | 1 + src/main/resources/assets/oauth/lang/zh_CN.lang | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java b/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java index fef66d4..c02b14e 100644 --- a/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java +++ b/src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java @@ -78,6 +78,7 @@ public void updateScreen() { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawBackground(0); + drawCenteredString(mc.fontRenderer, title, width / 2, 17, 16777215); drawCenteredString( Minecraft.getMinecraft().fontRenderer, renderText, diff --git a/src/main/resources/assets/oauth/lang/zh_CN.lang b/src/main/resources/assets/oauth/lang/zh_CN.lang index 95cc4ee..3841a7c 100644 --- a/src/main/resources/assets/oauth/lang/zh_CN.lang +++ b/src/main/resources/assets/oauth/lang/zh_CN.lang @@ -4,8 +4,8 @@ oauth.status.online=状态: 在线 oauth.status.offline=状态: 离线 oauth.screen.title=选择账号类型 -oauth.btn.login.mojang=Mojang登录 -oauth.btn.login.microsoft=Microsoft登录 +oauth.btn.login.mojang=Mojang 登录 +oauth.btn.login.microsoft=Microsoft 登录 oauth.screen.title.mojang=OAuth登录 oauth.text.usernameoremail=用户名/邮箱 @@ -17,6 +17,6 @@ oauth.text.usernameorpassword.wrong=账号或密码错误! oauth.btn.login=登录 oauth.btn.login.offline=离线登录 -oauth.screen.title.microsoft=Microsoft登录 +oauth.screen.title.microsoft=Microsoft 登录 oauth.text.loading=登录中 oauth.text.check.browser=请查看浏览器