Skip to content

Commit

Permalink
Merge pull request #4 from wohaopa/master
Browse files Browse the repository at this point in the history
Support i18n
  • Loading branch information
Dream-Master authored Jan 27, 2024
2 parents f8f9479 + 48d44e7 commit 8566150
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 49 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/sintinium/oauth/GuiEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -27,16 +28,16 @@ public void multiplayerScreenOpen(GuiScreenEvent.InitGuiEvent.Post event) {
// Method addButtonMethod = ObfuscationReflectionHelper.findMethod(Screen.class, "func_230480_a_",
// Widget.class);
List<GuiButton> 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);
}
});
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/com/sintinium/oauth/gui/LoginLoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> updateText = new AtomicReference<>();

protected LoginLoadingScreen(GuiScreen multiplayerScreen, GuiScreen callingScreen, Runnable onCancel,
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -69,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,
Expand Down
99 changes: 65 additions & 34 deletions src/main/java/com/sintinium/oauth/gui/LoginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,7 +26,7 @@ public class LoginScreen extends GuiScreenCustom {
private OAuthCheckbox savePasswordWidget;
private GuiTextField usernameWidget;
private AtomicReference<String> status = new AtomicReference<>();
private String title = "OAuth Login";
private String title = I18n.format("oauth.screen.title.mojang");

private List<Runnable> toRun = new CopyOnWriteArrayList<>();

Expand Down Expand Up @@ -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 = () -> {
Expand All @@ -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<Boolean> 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<Boolean> 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();

Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/sintinium/oauth/gui/LoginTypeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -64,7 +65,7 @@ public void initGui() {
this.height / 2 + 60,
200,
20,
"Cancel",
I18n.format("gui.cancel"),
() -> { Minecraft.getMinecraft().displayGuiScreen(lastScreen); }));
}

Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/assets/oauth/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions src/main/resources/assets/oauth/lang/zh_CN.lang
Original file line number Diff line number Diff line change
@@ -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=请查看浏览器

0 comments on commit 8566150

Please sign in to comment.