make config io in other thread, only save config if favicon changed, launcher: hide empty cells

This commit is contained in:
Bixilon 2020-08-31 22:07:22 +02:00
parent 8f5365f738
commit dd5c864d60
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 29 additions and 12 deletions

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.config;
import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.gui.main.Server;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.util.mojang.api.MojangAccount;
import org.yaml.snakeyaml.Yaml;
@ -27,7 +28,7 @@ import java.util.Map;
import java.util.UUID;
public class Configuration {
LinkedHashMap<String, Object> config;
final LinkedHashMap<String, Object> config;
public Configuration(String filename) throws IOException {
@ -171,15 +172,23 @@ public class Configuration {
}
public void saveToFile(String filename) {
Yaml yaml = new Yaml();
FileWriter writer;
try {
writer = new FileWriter(Config.homeDir + "config/" + filename);
} catch (IOException e) {
e.printStackTrace();
return;
}
yaml.dump(config, writer);
Thread thread = new Thread(() -> {
Yaml yaml = new Yaml();
FileWriter writer;
try {
writer = new FileWriter(Config.homeDir + "config/" + filename);
} catch (IOException e) {
e.printStackTrace();
return;
}
synchronized (config) {
yaml.dump(config, writer);
}
Log.verbose(String.format("Configuration saved to file %s", filename));
});
thread.setName("IO-Thread");
thread.start();
}
public ArrayList<MojangAccount> getMojangAccounts() {

View File

@ -57,6 +57,8 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
public MenuItem optionsEdit;
@FXML
public MenuItem optionsDelete;
@FXML
public MenuButton optionsMenu;
boolean canConnect = false;
@FXML
private Label serverName;
@ -90,9 +92,11 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
protected void updateItem(Server server, boolean empty) {
super.updateItem(server, empty);
root.setVisible(!empty);
if (empty) {
return;
}
if (server == null) {
return;
}
@ -165,9 +169,12 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
}
motd.setText(ping.getMotd().getRawMessage());
if (ping.getFavicon() != null) {
icon.setImage(ping.getFavicon());
if (ping.getBase64EncodedFavicon().equals(server.getBase64Favicon())) {
return;
}
server.setBase64Favicon(ping.getBase64EncodedFavicon());
server.saveToConfig();
icon.setImage(ping.getFavicon());
}
}));
@ -242,6 +249,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
public void delete() {
listView.getItems().remove(server);
server.delete();
Log.info(String.format("Deleted server (name=\"%s\", address=\"%s\")", server.getName(), server.getAddress()));
}
public void connect() {

View File

@ -26,7 +26,7 @@
AnchorPane.topAnchor="40.0"/>
<Label fx:id="version" layoutX="350.0" layoutY="8.0" text="Connecting..." AnchorPane.rightAnchor="110.0"
AnchorPane.topAnchor="5.0"/>
<MenuButton id="options" layoutX="389.0" layoutY="81.0" mnemonicParsing="false" text="⋮"
<MenuButton fx:id="optionsMenu" layoutX="389.0" layoutY="81.0" mnemonicParsing="false" text="⋮"
AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<items>
<MenuItem fx:id="optionsDelete" mnemonicParsing="false" text="Delete"/>