From 86228db43f5af5fcafb26e870198842ebe14c7e0 Mon Sep 17 00:00:00 2001 From: Lenni0451 <20379977+Lenni0451@users.noreply.github.com> Date: Wed, 1 Feb 2023 22:26:34 +0100 Subject: [PATCH] Improved code --- .../raphimc/viaproxy/ui/impl/AccountsTab.java | 107 +++++++----------- .../net/raphimc/viaproxy/util/TFunction.java | 25 ++++ 2 files changed, 66 insertions(+), 66 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/util/TFunction.java diff --git a/src/main/java/net/raphimc/viaproxy/ui/impl/AccountsTab.java b/src/main/java/net/raphimc/viaproxy/ui/impl/AccountsTab.java index e46e7b4..2ced84d 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/impl/AccountsTab.java +++ b/src/main/java/net/raphimc/viaproxy/ui/impl/AccountsTab.java @@ -18,14 +18,14 @@ package net.raphimc.viaproxy.ui.impl; import net.raphimc.mcauth.MinecraftAuth; -import net.raphimc.mcauth.step.bedrock.StepMCChain; -import net.raphimc.mcauth.step.java.StepMCProfile; +import net.raphimc.mcauth.step.msa.StepMsaDeviceCode; import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.cli.options.Options; import net.raphimc.viaproxy.saves.impl.accounts.Account; import net.raphimc.viaproxy.ui.AUITab; import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.popups.AddAccountPopup; +import net.raphimc.viaproxy.util.TFunction; import javax.swing.*; import java.awt.event.KeyAdapter; @@ -33,6 +33,8 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; +import java.util.function.Function; public class AccountsTab extends AUITab { @@ -178,38 +180,7 @@ public class AccountsTab extends AUITab { this.addMicrosoftAccountButton.setBounds(168, 300, 153, 20); this.addMicrosoftAccountButton.addActionListener(event -> { this.addMicrosoftAccountButton.setEnabled(false); - this.addThread = new Thread(() -> { - try { - StepMCProfile.MCProfile profile = MinecraftAuth.requestJavaLogin(msaDeviceCode -> { - SwingUtilities.invokeLater(() -> { - new AddAccountPopup(this.frame, msaDeviceCode, popup -> this.addAccountPopup = popup, () -> { - this.closePopup(); - this.addThread.interrupt(); - }); - }); - }); - SwingUtilities.invokeLater(() -> { - this.closePopup(); - Account account = ViaProxy.saveManager.accountsSave.addAccount(profile); - ViaProxy.saveManager.save(); - this.addAccount(account); - this.frame.showInfo("The account " + profile.name() + " was added successfully."); - }); - } catch (InterruptedException ignored) { - } catch (TimeoutException e) { - SwingUtilities.invokeLater(() -> { - this.closePopup(); - this.frame.showError("The login request timed out.\nPlease login within 60 seconds."); - }); - } catch (Throwable t) { - SwingUtilities.invokeLater(() -> { - this.closePopup(); - this.frame.showException(t); - }); - } - }, "Add Account Thread"); - this.addThread.setDaemon(true); - this.addThread.start(); + this.handleLogin(MinecraftAuth::requestJavaLogin, profile -> ViaProxy.saveManager.accountsSave.addAccount(profile)); }); contentPane.add(this.addMicrosoftAccountButton); } @@ -218,38 +189,7 @@ public class AccountsTab extends AUITab { this.addBedrockAccountButton.setBounds(326, 300, 149, 20); this.addBedrockAccountButton.addActionListener(event -> { this.addBedrockAccountButton.setEnabled(false); - this.addThread = new Thread(() -> { - try { - StepMCChain.MCChain chain = MinecraftAuth.requestBedrockLogin(msaDeviceCode -> { - SwingUtilities.invokeLater(() -> { - new AddAccountPopup(this.frame, msaDeviceCode, popup -> this.addAccountPopup = popup, () -> { - this.closePopup(); - this.addThread.interrupt(); - }); - }); - }); - SwingUtilities.invokeLater(() -> { - this.closePopup(); - Account account = ViaProxy.saveManager.accountsSave.addAccount(chain); - ViaProxy.saveManager.save(); - this.addAccount(account); - this.frame.showInfo("The account " + chain.displayName() + " was added successfully."); - }); - } catch (InterruptedException ignored) { - } catch (TimeoutException e) { - SwingUtilities.invokeLater(() -> { - this.closePopup(); - this.frame.showError("The login request timed out.\nPlease login within 60 seconds."); - }); - } catch (Throwable t) { - SwingUtilities.invokeLater(() -> { - this.closePopup(); - this.frame.showException(t); - }); - } - }, "Add Account Thread"); - this.addThread.setDaemon(true); - this.addThread.start(); + this.handleLogin(MinecraftAuth::requestBedrockLogin, chain -> ViaProxy.saveManager.accountsSave.addAccount(chain)); }); contentPane.add(this.addBedrockAccountButton); } @@ -325,4 +265,39 @@ public class AccountsTab extends AUITab { } } + private void handleLogin(final TFunction, T> requestHandler, final Function addHandler) { + this.addThread = new Thread(() -> { + try { + T profile = requestHandler.apply(msaDeviceCode -> { + SwingUtilities.invokeLater(() -> { + new AddAccountPopup(this.frame, msaDeviceCode, popup -> this.addAccountPopup = popup, () -> { + this.closePopup(); + this.addThread.interrupt(); + }); + }); + }); + SwingUtilities.invokeLater(() -> { + this.closePopup(); + Account account = addHandler.apply(profile); + ViaProxy.saveManager.save(); + this.addAccount(account); + this.frame.showInfo("The account " + account.getName() + " was added successfully."); + }); + } catch (InterruptedException ignored) { + } catch (TimeoutException e) { + SwingUtilities.invokeLater(() -> { + this.closePopup(); + this.frame.showError("The login request timed out.\nPlease login within 60 seconds."); + }); + } catch (Throwable t) { + SwingUtilities.invokeLater(() -> { + this.closePopup(); + this.frame.showException(t); + }); + } + }, "Add Account Thread"); + this.addThread.setDaemon(true); + this.addThread.start(); + } + } diff --git a/src/main/java/net/raphimc/viaproxy/util/TFunction.java b/src/main/java/net/raphimc/viaproxy/util/TFunction.java new file mode 100644 index 0000000..7715be6 --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/util/TFunction.java @@ -0,0 +1,25 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaproxy.util; + +@FunctionalInterface +public interface TFunction { + + R apply(T t) throws Throwable; + +}