Added offline accounts support to ui

This commit is contained in:
Lenni0451 2023-01-07 14:04:18 +01:00
parent 8f35fef853
commit 609dd41587
3 changed files with 45 additions and 20 deletions

View File

@ -1,6 +1,8 @@
package net.raphimc.viaproxy.saves.impl; package net.raphimc.viaproxy.saves.impl;
import com.google.gson.*; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.raphimc.mcauth.MinecraftAuth; import net.raphimc.mcauth.MinecraftAuth;
import net.raphimc.mcauth.step.java.StepGameOwnership; import net.raphimc.mcauth.step.java.StepGameOwnership;
import net.raphimc.mcauth.step.java.StepMCProfile; import net.raphimc.mcauth.step.java.StepMCProfile;
@ -9,7 +11,10 @@ import net.raphimc.viaproxy.saves.AbstractSave;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class AccountsSave extends AbstractSave { public class AccountsSave extends AbstractSave {
@ -48,16 +53,18 @@ public class AccountsSave extends AbstractSave {
return array; return array;
} }
public void addAccount(final StepMCProfile.MCProfile profile) { public StepMCProfile.MCProfile addAccount(final StepMCProfile.MCProfile profile) {
this.accounts.add(profile); this.accounts.add(profile);
return profile;
} }
public void addAccount(final int index, final StepMCProfile.MCProfile profile) { public StepMCProfile.MCProfile addAccount(final int index, final StepMCProfile.MCProfile profile) {
this.accounts.add(index, profile); this.accounts.add(index, profile);
return profile;
} }
public void addOfflineAccount(final String name) { public StepMCProfile.MCProfile addOfflineAccount(final String name) {
this.addAccount(new StepMCProfile.MCProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes()), name, null, new StepGameOwnership.GameOwnership(Collections.emptyList(), null))); return this.addAccount(new StepMCProfile.MCProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes()), name, null, new StepGameOwnership.GameOwnership(Collections.emptyList(), null)));
} }
public void removeAccount(final StepMCProfile.MCProfile profile) { public void removeAccount(final StepMCProfile.MCProfile profile) {

View File

@ -94,7 +94,7 @@ public class GeneralTab extends AUITab {
authMethodLabel.setBounds(10, 200, 400, 20); authMethodLabel.setBounds(10, 200, 400, 20);
contentPane.add(authMethodLabel); contentPane.add(authMethodLabel);
this.authMethod = new JComboBox<>(new String[]{"Use stored account", "Use OpenAuthMod"}); this.authMethod = new JComboBox<>(new String[]{"Use selected account", "Use OpenAuthMod"});
this.authMethod.setBounds(10, 220, 465, 20); this.authMethod.setBounds(10, 220, 465, 20);
contentPane.add(this.authMethod); contentPane.add(this.authMethod);
} }

View File

@ -18,13 +18,13 @@ import java.util.concurrent.TimeoutException;
public class OnlineModeTab extends AUITab { public class OnlineModeTab extends AUITab {
private JList<String> accountsList; private JList<String> accountsList;
private JButton addAccountButton; private JButton addMicrosoftAccountButton;
private AddAccountPopup addAccountPopup; private AddAccountPopup addAccountPopup;
private Thread addThread; private Thread addThread;
public OnlineModeTab(final ViaProxyUI frame) { public OnlineModeTab(final ViaProxyUI frame) {
super(frame, "Online Mode"); super(frame, "Accounts");
} }
@Override @Override
@ -91,7 +91,7 @@ public class OnlineModeTab extends AUITab {
JPopupMenu contextMenu = new JPopupMenu(); JPopupMenu contextMenu = new JPopupMenu();
{ {
JMenuItem selectItem = new JMenuItem("Use to join online mode servers"); JMenuItem selectItem = new JMenuItem("Select account");
selectItem.addActionListener(e -> { selectItem.addActionListener(e -> {
int index = this.accountsList.getSelectedIndex(); int index = this.accountsList.getSelectedIndex();
if (index != -1) this.markSelected(index); if (index != -1) this.markSelected(index);
@ -141,10 +141,23 @@ public class OnlineModeTab extends AUITab {
this.accountsList.setComponentPopupMenu(contextMenu); this.accountsList.setComponentPopupMenu(contextMenu);
} }
{ {
this.addAccountButton = new JButton("Add Account"); JButton addOfflineAccountButton = new JButton("Add Offline Account");
this.addAccountButton.setBounds(300, 300, 175, 20); addOfflineAccountButton.setBounds(10, 300, 230, 20);
this.addAccountButton.addActionListener(event -> { addOfflineAccountButton.addActionListener(event -> {
this.addAccountButton.setEnabled(false); String username = JOptionPane.showInputDialog(this.frame, "Enter your offline mode Username:", "Add Offline Account", JOptionPane.PLAIN_MESSAGE);
if (username != null) {
StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.addOfflineAccount(username);
ViaProxy.saveManager.save();
this.addAccount(account);
}
});
contentPane.add(addOfflineAccountButton);
}
{
this.addMicrosoftAccountButton = new JButton("Add Microsoft Account");
this.addMicrosoftAccountButton.setBounds(245, 300, 230, 20);
this.addMicrosoftAccountButton.addActionListener(event -> {
this.addMicrosoftAccountButton.setEnabled(false);
this.addThread = new Thread(() -> { this.addThread = new Thread(() -> {
try { try {
StepMCProfile.MCProfile profile = MinecraftAuth.requestJavaLogin(msaDeviceCode -> { StepMCProfile.MCProfile profile = MinecraftAuth.requestJavaLogin(msaDeviceCode -> {
@ -159,8 +172,7 @@ public class OnlineModeTab extends AUITab {
this.closePopup(); this.closePopup();
ViaProxy.saveManager.accountsSave.addAccount(profile); ViaProxy.saveManager.accountsSave.addAccount(profile);
ViaProxy.saveManager.save(); ViaProxy.saveManager.save();
DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel(); this.addAccount(profile);
model.addElement(profile.name());
this.frame.showInfo("The account " + profile.name() + " was added successfully."); this.frame.showInfo("The account " + profile.name() + " was added successfully.");
}); });
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
@ -179,14 +191,14 @@ public class OnlineModeTab extends AUITab {
this.addThread.setDaemon(true); this.addThread.setDaemon(true);
this.addThread.start(); this.addThread.start();
}); });
contentPane.add(this.addAccountButton); contentPane.add(this.addMicrosoftAccountButton);
} }
} }
@Override @Override
public void setReady() { public void setReady() {
final DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel(); ViaProxy.saveManager.accountsSave.getAccounts().forEach(this::addAccount);
ViaProxy.saveManager.accountsSave.getAccounts().forEach(account -> model.addElement(account.name())); DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel();
if (!model.isEmpty()) this.markSelected(0); if (!model.isEmpty()) this.markSelected(0);
} }
@ -195,7 +207,13 @@ public class OnlineModeTab extends AUITab {
this.addAccountPopup.setVisible(false); this.addAccountPopup.setVisible(false);
this.addAccountPopup.dispose(); this.addAccountPopup.dispose();
this.addAccountPopup = null; this.addAccountPopup = null;
this.addAccountButton.setEnabled(true); this.addMicrosoftAccountButton.setEnabled(true);
}
private void addAccount(final StepMCProfile.MCProfile account) {
DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel();
if (account.prevResult().items().isEmpty()) model.addElement(account.name() + " (Offline)");
else model.addElement(account.name() + " (Microsoft)");
} }
private void markSelected(final int index) { private void markSelected(final int index) {