Search in sources :

Example 1 with Server

use of org.terasology.engine.network.Server in project Terasology by MovingBlocks.

the class JoinServer method step.

@Override
public boolean step() {
    if (applyModuleThread != null) {
        if (!applyModuleThread.isAlive()) {
            if (oldEnvironment != null) {
                oldEnvironment.close();
            }
            return true;
        }
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
        Server server = networkSystem.getServer();
        ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
        // If no GameName, use Server IP Address
        if (serverInfo.getGameName().length() > 0) {
            gameManifest.setTitle(serverInfo.getGameName());
        } else {
            gameManifest.setTitle(server.getRemoteAddress());
        }
        for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
            gameManifest.addWorld(worldInfo);
        }
        Map<String, Short> blockMap = Maps.newHashMap();
        for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
            String name = entry.getValue();
            short id = entry.getKey().shortValue();
            Short oldId = blockMap.put(name, id);
            if (oldId != null && oldId != id) {
                logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
            }
        }
        gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
        gameManifest.setBlockIdMap(blockMap);
        gameManifest.setTime(networkSystem.getServer().getInfo().getTime());
        ModuleManager moduleManager = context.get(ModuleManager.class);
        Set<Module> moduleSet = Sets.newLinkedHashSet();
        for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
            Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
            if (module == null) {
                StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
                context.get(GameEngine.class).changeState(mainMenu);
                return false;
            } else {
                logger.info("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
                gameManifest.addModule(module.getId(), module.getVersion());
                moduleSet.add(module);
            }
        }
        oldEnvironment = moduleManager.getEnvironment();
        moduleManager.loadEnvironment(moduleSet, true);
        context.get(Game.class).load(gameManifest);
        EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
        applyModuleThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
        applyModuleThread.start();
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
        StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
        context.get(GameEngine.class).changeState(mainMenu);
        networkSystem.shutdown();
    }
    return false;
}
Also used : Set(java.util.Set) Server(org.terasology.engine.network.Server) NameVersion(org.terasology.gestalt.naming.NameVersion) GameEngine(org.terasology.engine.core.GameEngine) ModuleManager(org.terasology.engine.core.module.ModuleManager) Entry(java.util.Map.Entry) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) WorldInfo(org.terasology.engine.world.internal.WorldInfo) ServerInfoMessage(org.terasology.engine.network.ServerInfoMessage) Module(org.terasology.gestalt.module.Module) Map(java.util.Map) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 2 with Server

use of org.terasology.engine.network.Server in project Terasology by MovingBlocks.

the class CoreCommands method ping.

/**
 * Your ping to the server
 *
 * @param sender Sender of command
 * @return String containing ping or error message
 */
@Command(shortDescription = "Your ping to the server", helpText = "The time it takes the packet " + "to reach the server and back", requiredPermission = PermissionManager.NO_PERMISSION)
public String ping(@Sender EntityRef sender) {
    Server server = networkSystem.getServer();
    if (server == null) {
        // TODO: i18n
        if (networkSystem.getMode().isServer()) {
            return "Your player is running on the server";
        } else {
            return "Please make sure you are connected to an online server (singleplayer doesn't count)";
        }
    }
    String[] remoteAddress = server.getRemoteAddress().split("-");
    String address = remoteAddress[1];
    int port = Integer.parseInt(remoteAddress[2]);
    try {
        PingService pingService = new PingService(address, port);
        long delay = pingService.call();
        return String.format("%d ms", delay);
    } catch (UnknownHostException e) {
        return String.format("Error: Unknown host \"%s\" at %s:%s -- %s", remoteAddress[0], remoteAddress[1], remoteAddress[2], e);
    } catch (IOException e) {
        return String.format("Error: Failed to ping server \"%s\" at %s:%s -- %s", remoteAddress[0], remoteAddress[1], remoteAddress[2], e);
    }
}
Also used : Server(org.terasology.engine.network.Server) UnknownHostException(java.net.UnknownHostException) PingService(org.terasology.engine.network.PingService) IOException(java.io.IOException) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Aggregations

Server (org.terasology.engine.network.Server)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 GameEngine (org.terasology.engine.core.GameEngine)1 EnvironmentSwitchHandler (org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)1 StateMainMenu (org.terasology.engine.core.modes.StateMainMenu)1 ModuleManager (org.terasology.engine.core.module.ModuleManager)1 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)1 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)1 PingService (org.terasology.engine.network.PingService)1 ServerInfoMessage (org.terasology.engine.network.ServerInfoMessage)1 WorldInfo (org.terasology.engine.world.internal.WorldInfo)1 Module (org.terasology.gestalt.module.Module)1 NameVersion (org.terasology.gestalt.naming.NameVersion)1