Improved code

This commit is contained in:
RaphiMC 2023-11-30 17:41:34 +01:00
parent 9e9b921eef
commit de82e4c598
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
3 changed files with 23 additions and 11 deletions

View File

@ -25,14 +25,12 @@ public class VersionEnumConverter implements ValueConverter<VersionEnum> {
@Override @Override
public VersionEnum convert(String s) { public VersionEnum convert(String s) {
for (VersionEnum version : VersionEnum.getAllVersions()) { final VersionEnum version = VersionEnum.fromProtocolName(s);
if (version.getName().equalsIgnoreCase(s)) return version; if (version == VersionEnum.UNKNOWN) {
} throw new ValueConversionException("Unable to find version '" + s + "'");
for (VersionEnum version : VersionEnum.getAllVersions()) {
if (version.getProtocol().getIncludedVersions().contains(s)) return version;
} }
throw new ValueConversionException("Unable to find version '" + s + "'"); return version;
} }
@Override @Override

View File

@ -19,6 +19,7 @@ package net.raphimc.viaproxy.saves.impl;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.viaproxy.saves.AbstractSave; import net.raphimc.viaproxy.saves.AbstractSave;
import javax.swing.*; import javax.swing.*;
@ -76,6 +77,17 @@ public class UISave extends AbstractSave {
} }
} }
public void loadComboBoxVersionEnum(final String key, final JComboBox<VersionEnum> comboBox) {
if (this.values.containsKey(key)) {
try {
String value = String.valueOf(this.values.get(key));
VersionEnum version = VersionEnum.valueOf(value);
comboBox.setSelectedItem(version);
} catch (Throwable ignored) {
}
}
}
public void loadSpinner(final String key, final JSpinner spinner) { public void loadSpinner(final String key, final JSpinner spinner) {
if (this.values.containsKey(key)) { if (this.values.containsKey(key)) {
try { try {

View File

@ -134,7 +134,7 @@ public class GeneralTab extends AUITab {
this.betaCraftAuth.setSelected(false); this.betaCraftAuth.setSelected(false);
} }
}); });
ViaProxy.getSaveManager().uiSave.loadComboBox("server_version", this.serverVersion); ViaProxy.getSaveManager().uiSave.loadComboBoxVersionEnum("server_version", this.serverVersion);
GBC.create(body).grid(0, gridy++).weightx(1).insets(0, BORDER_PADDING, 0, BORDER_PADDING).fill(GridBagConstraints.HORIZONTAL).add(this.serverVersion); GBC.create(body).grid(0, gridy++).weightx(1).insets(0, BORDER_PADDING, 0, BORDER_PADDING).fill(GridBagConstraints.HORIZONTAL).add(this.serverVersion);
} }
{ {
@ -191,7 +191,9 @@ public class GeneralTab extends AUITab {
private void onClose(final UICloseEvent event) { private void onClose(final UICloseEvent event) {
UISave save = ViaProxy.getSaveManager().uiSave; UISave save = ViaProxy.getSaveManager().uiSave;
save.put("server_address", this.serverAddress.getText()); save.put("server_address", this.serverAddress.getText());
save.put("server_version", String.valueOf(this.serverVersion.getSelectedIndex())); if (this.serverVersion.getSelectedItem() instanceof VersionEnum version) {
save.put("server_version", version.name());
}
save.put("auth_method", String.valueOf(this.authMethod.getSelectedIndex())); save.put("auth_method", String.valueOf(this.authMethod.getSelectedIndex()));
save.put("betacraft_auth", String.valueOf(this.betaCraftAuth.isSelected())); save.put("betacraft_auth", String.valueOf(this.betaCraftAuth.isSelected()));
ViaProxy.getSaveManager().save(); ViaProxy.getSaveManager().save();
@ -218,8 +220,8 @@ public class GeneralTab extends AUITab {
} }
private void start() { private void start() {
final Object selectedItem = this.serverVersion.getSelectedItem(); final Object selectedVersion = this.serverVersion.getSelectedItem();
if (!(selectedItem instanceof VersionEnum)) { if (!(selectedVersion instanceof VersionEnum)) {
this.frame.showError(I18n.get("tab.general.error.no_server_version_selected")); this.frame.showError(I18n.get("tab.general.error.no_server_version_selected"));
return; return;
} }
@ -229,7 +231,7 @@ public class GeneralTab extends AUITab {
this.frame.showWarning("<html><div style='text-align: center;'>" + I18n.get("tab.general.warning.ban_warning.line1") + "<br><b>" + I18n.get("tab.general.warning.risk") + "</b></div></html>"); this.frame.showWarning("<html><div style='text-align: center;'>" + I18n.get("tab.general.warning.ban_warning.line1") + "<br><b>" + I18n.get("tab.general.warning.risk") + "</b></div></html>");
} }
if (VersionEnum.bedrockLatest.equals(selectedItem) && ViaProxy.getSaveManager().uiSave.get("notice.bedrock_warning") == null) { if (VersionEnum.bedrockLatest.equals(selectedVersion) && ViaProxy.getSaveManager().uiSave.get("notice.bedrock_warning") == null) {
ViaProxy.getSaveManager().uiSave.put("notice.bedrock_warning", "true"); ViaProxy.getSaveManager().uiSave.put("notice.bedrock_warning", "true");
ViaProxy.getSaveManager().save(); ViaProxy.getSaveManager().save();