Search in sources :

Example 6 with ServerInfo

use of org.terasology.config.ServerInfo in project Terasology by MovingBlocks.

the class JoinGameScreen method bindCustomButtons.

private void bindCustomButtons() {
    UIList<?> customServerList = find("customServerList", UIList.class);
    ReadOnlyBinding<Boolean> localSelectedServerOnly = new ReadOnlyBinding<Boolean>() {

        @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 -> {
                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();
            popup.setServerInfo(info);
            // editing invalidates the currently known info, so query it again
            popup.onSuccess(item -> extInfo.put(item, infoService.requestInfo(item.getAddress(), item.getPort())));
        });
    }
    UIButton removeButton = find("remove", UIButton.class);
    if (removeButton != null) {
        removeButton.bindEnabled(localSelectedServerOnly);
        removeButton.subscribe(button -> {
            ServerInfo info = visibleList.getSelection();
            if (info != null) {
                config.getNetwork().removeServerInfo(info);
                extInfo.remove(info);
                visibleList.setSelection(null);
            }
        });
    }
    UILabel downloadLabel = find("download", UILabel.class);
    if (downloadLabel != null) {
        downloadLabel.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                return translationSystem.translate(downloader.getStatus());
            }
        });
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) ServerInfo(org.terasology.config.ServerInfo) UIButton(org.terasology.rendering.nui.widgets.UIButton)

Example 7 with ServerInfo

use of org.terasology.config.ServerInfo in project Terasology by MovingBlocks.

the class JoinGameScreen method bindInfoLabels.

private void bindInfoLabels() {
    final ReadOnlyBinding<ServerInfo> infoBinding = new ReadOnlyBinding<ServerInfo>() {

        @Override
        public ServerInfo get() {
            return visibleList.getSelection();
        }
    };
    UILabel name = find("name", UILabel.class);
    if (name != null) {
        name.bindText(BindHelper.bindBoundBeanProperty("name", infoBinding, ServerInfo.class, String.class));
    }
    UILabel owner = find("owner", UILabel.class);
    if (owner != null) {
        owner.bindText(BindHelper.bindBoundBeanProperty("owner", infoBinding, ServerInfo.class, String.class));
    }
    UILabel address = find("address", UILabel.class);
    if (address != null) {
        address.bindText(BindHelper.bindBoundBeanProperty("address", infoBinding, ServerInfo.class, String.class));
    }
    UILabel port = find("port", UILabel.class);
    if (port != null) {
        port.bindText(new IntToStringBinding(BindHelper.bindBoundBeanProperty("port", infoBinding, ServerInfo.class, int.class)));
    }
    UILabel onlinePlayers = find("onlinePlayers", UILabel.class);
    onlinePlayers.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
            if (info != null) {
                if (info.isDone()) {
                    return getOnlinePlayersText(info);
                } else {
                    return translationSystem.translate("${engine:menu#join-server-requested}");
                }
            }
            return null;
        }
    });
    UILabel modules = find("modules", UILabel.class);
    modules.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
            if (info != null) {
                if (info.isDone()) {
                    return getModulesText(info);
                } else {
                    return translationSystem.translate("${engine:menu#join-server-requested}");
                }
            }
            return null;
        }
    });
    UILabel worlds = find("worlds", UILabel.class);
    worlds.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
            if (info != null) {
                if (info.isDone()) {
                    return getWorldText(info);
                } else {
                    return translationSystem.translate("${engine:menu#join-server-requested}");
                }
            }
            return null;
        }
    });
    UIButton joinButton = find("join", UIButton.class);
    if (joinButton != null) {
        joinButton.bindEnabled(new ReadOnlyBinding<Boolean>() {

            @Override
            public Boolean get() {
                return infoBinding.get() != null;
            }
        });
        joinButton.subscribe(button -> {
            config.save();
            ServerInfo item = infoBinding.get();
            if (item != null) {
                join(item.getAddress(), item.getPort());
            }
        });
    }
    UIButton refreshButton = find("refresh", UIButton.class);
    if (refreshButton != null) {
        refreshButton.bindEnabled(new ReadOnlyBinding<Boolean>() {

            @Override
            public Boolean get() {
                return visibleList.getSelection() != null;
            }
        });
        refreshButton.subscribe(button -> {
            refresh();
        });
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) IntToStringBinding(org.terasology.rendering.nui.databinding.IntToStringBinding) ServerInfo(org.terasology.config.ServerInfo) UIButton(org.terasology.rendering.nui.widgets.UIButton) Future(java.util.concurrent.Future)

Example 8 with ServerInfo

use of org.terasology.config.ServerInfo in project Terasology by MovingBlocks.

the class ServerListDownloader method download.

private void download(String address) throws IOException {
    status = "${engine:menu#downloading-server-list}";
    URL url = new URL("http", address, "/servers/list");
    try (Reader reader = new InputStreamReader(url.openStream(), charset);
        JsonReader jsonReader = new JsonReader(reader)) {
        status = "${engine:menu#parsing-content}";
        jsonReader.beginArray();
        TypeAdapter<ServerInfo> adapter = GSON.getAdapter(ServerInfo.class);
        while (jsonReader.hasNext()) {
            ServerInfo entry = adapter.read(jsonReader);
            servers.add(entry);
            logger.info("Retrieved game server {}", entry);
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
            // ignore - this is just to create an animation anyway
            }
        }
        jsonReader.endArray();
        if (servers.size() == 0) {
            status = String.format("Server Error!");
        } else {
            status = String.format("${engine:menu#server-list-complete}");
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ServerInfo(org.terasology.config.ServerInfo) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonReader(com.google.gson.stream.JsonReader) JsonReader(com.google.gson.stream.JsonReader) URL(java.net.URL)

Aggregations

ServerInfo (org.terasology.config.ServerInfo)8 UIButton (org.terasology.rendering.nui.widgets.UIButton)4 UILabel (org.terasology.rendering.nui.widgets.UILabel)4 URL (java.net.URL)3 Future (java.util.concurrent.Future)2 TelemetryConfig (org.terasology.config.TelemetryConfig)2 ReadOnlyBinding (org.terasology.rendering.nui.databinding.ReadOnlyBinding)2 AddServerPopup (org.terasology.rendering.nui.layers.mainMenu.AddServerPopup)2 Joiner (com.google.common.base.Joiner)1 Predicate (com.google.common.base.Predicate)1 Collections2 (com.google.common.collect.Collections2)1 JsonReader (com.google.gson.stream.JsonReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1