diff --git a/engine/src/main/java/org/terasology/config/NetworkConfig.java b/engine/src/main/java/org/terasology/config/NetworkConfig.java index f9f2a5e12a5..d187cbaade0 100644 --- a/engine/src/main/java/org/terasology/config/NetworkConfig.java +++ b/engine/src/main/java/org/terasology/config/NetworkConfig.java @@ -17,10 +17,11 @@ package org.terasology.config; import com.google.common.collect.Lists; + import org.terasology.engine.TerasologyConstants; +import java.util.Collections; import java.util.List; -import java.util.Optional; /** * @author Immortius @@ -64,20 +65,16 @@ public void setServerPort(int serverPort) { this.serverPort = serverPort; } - public void add(ServerInfo serverInfo) { + public void addServerInfo(ServerInfo serverInfo) { servers.add(serverInfo); } - public void remove(ServerInfo info) { + public void removeServerInfo(ServerInfo info) { servers.remove(info); } - public List getServers() { - return servers; - } - - public void setServers(List servers) { - this.servers = servers; + public List getServerInfos() { + return Collections.unmodifiableList(servers); } public String getMasterServer() { diff --git a/engine/src/main/java/org/terasology/config/ServerInfo.java b/engine/src/main/java/org/terasology/config/ServerInfo.java index 65580cb2e36..03045e5c6a9 100644 --- a/engine/src/main/java/org/terasology/config/ServerInfo.java +++ b/engine/src/main/java/org/terasology/config/ServerInfo.java @@ -18,8 +18,6 @@ import com.google.common.base.Preconditions; -import java.util.Optional; - /** * @author Immortius @@ -29,7 +27,7 @@ public class ServerInfo { private String address; private String owner; private int port; - private boolean active; + private boolean active = true; ServerInfo() { // for serialization purposes diff --git a/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/AddServerPopup.java b/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/AddServerPopup.java index 4d0ffd2e48c..67cf64f5f02 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/AddServerPopup.java +++ b/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/AddServerPopup.java @@ -37,8 +37,6 @@ public class AddServerPopup extends CoreScreenLayer { public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:addServerPopup!instance"); - @In - private Config config; private UIText nameText; private UIText ownerText; private UIText addressText; @@ -72,8 +70,6 @@ public void onActivated(UIWidget button) { // create new serverInfo = new ServerInfo(name, address, port); serverInfo.setOwner(owner); - - config.getNetwork().add(serverInfo); } else { // update existing serverInfo.setName(name); diff --git a/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/JoinGameScreen.java b/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/JoinGameScreen.java index 5438f235a7b..a738646d0bb 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/JoinGameScreen.java +++ b/engine/src/main/java/org/terasology/rendering/nui/layers/mainMenu/JoinGameScreen.java @@ -42,7 +42,6 @@ import org.terasology.rendering.nui.UIWidget; import org.terasology.rendering.nui.WidgetUtil; import org.terasology.rendering.nui.databinding.BindHelper; -import org.terasology.rendering.nui.databinding.DefaultBinding; import org.terasology.rendering.nui.databinding.IntToStringBinding; import org.terasology.rendering.nui.databinding.ReadOnlyBinding; import org.terasology.rendering.nui.itemRendering.StringTextRenderer; @@ -86,9 +85,10 @@ public class JoinGameScreen extends CoreScreenLayer { private UIList visibleList; private List listedServers = new ArrayList<>(); - private List customServers = new ArrayList<>(); - private boolean updateComplete = false; + private Predicate activeServersOnly = server -> server.isActive(); + + private boolean updateComplete; @Override public void initialise() { @@ -99,12 +99,14 @@ public void initialise() { UIList customServerList = find("customServerList", UIList.class); if (customServerList != null) { - configureServerList(customServerList, customServers); + customServerList.setList(config.getNetwork().getServerInfos()); + configureServerList(customServerList); } UIList onlineServerList = find("onlineServerList", UIList.class); if (onlineServerList != null) { - configureServerList(onlineServerList, listedServers); + onlineServerList.setList(listedServers); + configureServerList(onlineServerList); } ActivateEventListener activateCustom = e -> { @@ -154,13 +156,8 @@ public void update(float delta) { updateComplete = true; } - Predicate onlyActive = server -> server.isActive(); - - customServers.clear(); - customServers.addAll(Collections2.filter(config.getNetwork().getServers(), onlyActive)); - listedServers.clear(); - listedServers.addAll(Collections2.filter(downloader.getServers(), onlyActive)); + listedServers.addAll(Collections2.filter(downloader.getServers(), activeServersOnly)); } } @@ -200,9 +197,8 @@ public JoinStatus call() throws InterruptedException { } - private void configureServerList(final UIList serverList, List servers) { + private void configureServerList(final UIList serverList) { - serverList.bindList(new DefaultBinding>(servers)); serverList.subscribe(new ItemActivateEventListener() { @Override public void onItemActivated(UIWidget widget, ServerInfo item) { @@ -314,17 +310,29 @@ public void onActivated(UIWidget button) { private void bindCustomButtons() { + UIList customServerList = find("customServerList", UIList.class); + ReadOnlyBinding localSelectedServerOnly = new ReadOnlyBinding() { + @Override + public Boolean get() { + return customServerList.getSelection() != null; + } + }; + UIButton add = find("add", UIButton.class); if (add != null) { add.subscribe(button -> { AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class); // select the entry if added successfully - popup.onSuccess(item -> visibleList.setSelection(item)); + popup.onSuccess(item -> { + config.getNetwork().addServerInfo(item); + visibleList.setSelection(item); + }); }); } UIButton edit = find("edit", UIButton.class); if (edit != null) { + edit.bindEnabled(localSelectedServerOnly); edit.subscribe(button -> { AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class); ServerInfo info = visibleList.getSelection(); @@ -337,10 +345,11 @@ private void bindCustomButtons() { UIButton removeButton = find("remove", UIButton.class); if (removeButton != null) { + removeButton.bindEnabled(localSelectedServerOnly); removeButton.subscribe(button -> { ServerInfo info = visibleList.getSelection(); if (info != null) { - config.getNetwork().remove(info); + config.getNetwork().removeServerInfo(info); extInfo.remove(info); visibleList.setSelection(null); }