Beispiel #1
0
        /// <summary>
        /// Updates servers to specified version
        /// </summary>
        /// <param name="serverType">The type of server Vanilla/Snapshot/Legacy</param>
        /// <param name="newVersion">The new version to update to</param>
        /// <param name="changeVersionsTXT">Should the version be updated in the text file (if no, serverType is irrelevant)</param>
        public void Update(string serverType, string newVersion)
        {
            ServerType = serverType;
            NewVersion = newVersion;

            if (ServerType == "") // Gets serverType
            {
                Console.Clear();
                Console.WriteLine("What is the server type?");
                Console.WriteLine("Server types include:");
                Funcs.PrettyPrint(new string[] { "vanilla", "snapshot", "both" });
                Console.Write("Server Type [vanilla]: ");
                ServerType = Funcs.NullEmpty("vanilla").ToLower();
            }

            // Gets the new version if empty
            if (String.IsNullOrEmpty(NewVersion))
            {
                string currentVersion = "";
                string latestVersion  = "";
                switch (ServerType.ToLower().First <char>())
                {
                case 'v':     // Vanilla
                    currentVersion = Variables.VV;
                    latestVersion  = Manifest.latest.release;
                    Console.WriteLine($"Latest vanilla version is: {latestVersion}");
                    goto default;

                case 's':     // Snapshot
                    currentVersion = Variables.SV;
                    latestVersion  = Manifest.latest.snapshot;
                    Console.WriteLine($"Latest snapshot version is: {latestVersion}");
                    goto default;

                case 'b':
                    Update("both");
                    return;

                default:
                    Console.WriteLine($"What is the new minecraft version? Current version is: {currentVersion}");
                    Console.Write($"New version [{latestVersion}]: ");
                    NewVersion = Funcs.NullEmpty(latestVersion);
                    break;
                }
                // The lower case `newVersion` is used because we want to know if the passed arg was null
                if (string.IsNullOrEmpty(currentVersion) || newVersion == null)
                {
                    UpdateVersions();
                    Variables.LoadVariables();
                }
            }

            // If the file already exists, don't do anything
            if (File.Exists(LauncherPath))
            {
                Console.WriteLine("File already exists.");
                UpdateVersions();
                Console.ReadLine();
                return;
            }

            Download();
        }
Beispiel #2
0
        /// <summary>
        /// Checks if there is a new version available (for vanilla and snapshot)
        /// </summary>
        public void Update(string serverType)
        {
            ServerType = serverType;
            if (string.IsNullOrEmpty(ServerType))
            {
                ServerType = "both";
            }
            string currentVersion = "";

            switch (ServerType.ToLower().First <char>())
            {
            case 'v':     // Vanilla
                currentVersion = Variables.VV;
                NewVersion     = Manifest.latest.release;
                break;

            case 's':     // Snapshot
                currentVersion = Variables.SV;
                NewVersion     = Manifest.latest.snapshot;
                break;

            case 'b':     // Both
                Update("vanilla");
                Update("snapshot");
                return;

            case 'l':
                currentVersion = Version;
                NewVersion     = currentVersion;
                break;
            }

            if (currentVersion == NewVersion && File.Exists(LauncherPath))
            {
                return;
            }
            else if (!File.Exists(LauncherPath))
            {
                Console.WriteLine($"The version {NewVersion} has not been downloaded, would you like to download it?");
            }
            else
            {
                Console.WriteLine($"There is a new version available, {NewVersion}, would you like to download it?");
            }
            Console.Write("[Y]/N: ");
            bool getNewest = Funcs.ValidateBool(true);

            if (getNewest)
            {
                if (!File.Exists(LauncherPath))
                {
                    Download();
                }
                else
                {
                    UpdateVersions();
                    Console.ReadLine();
                }
                ChangeVersion(NewVersion);
            }
        }
Beispiel #3
0
        public void Start()
        {
            // Makes sure the current directory is the server's directory
            if (Directory.GetCurrentDirectory() != ServerDir)
            {
                Directory.SetCurrentDirectory(ServerDir);
            }

            switch (LaunchType.ToLower().First <char>()) // Decides what to do when starting the server
            {
            case 'b':                                    // backup
                Backup();
                break;

            case 'r':     // restore
                Restore();
                break;

            case 'l':     // legacy
                if (Version == "")
                {
                    // Updates the server's version with the new version
                    ChangeVersion(AskForVersion());     // If legacy is specified, the version is requested (if not specified)
                }
                // If the version has not been specified, ask. Otherwise assume the specified version is valid and continue
                break;

            default:     // default/normal start
                // Everything that needs to happen happened in Launch
                break;
            }

            // Check if the newest version is downloaded
            Update(ServerType);

            if (File.Exists(LaunchJar))
            {
                Process exec = new Process()
                {
                    EnableRaisingEvents = false,
                    StartInfo           = new ProcessStartInfo()
                    {
                        FileName        = "cmd.exe",
                        Arguments       = $"/c java -Xmx2G -jar \"{LaunchJar}\" nogui",
                        UseShellExecute = false,
                    },
                };
                Console.Clear();
                Console.Title = $"Minecraft Server: {ServerName}. Version: {Version}";

                exec.Start();
            }
            else
            {
                Console.WriteLine("Launch file doesn't exist, would you like to download it?");
                Console.Write("Download? Y/[N]: ");
                bool retry = Funcs.ValidateBool(false);
                if (retry)
                {
                    Update(ServerType, Version);
                    Start();
                }
                // Else, close
            }
        }
Beispiel #4
0
        /// <summary>
        /// Launches the server
        /// </summary>
        public void Launch()
        {
            // Gets the name of the Server
            while (ServerName == "")
            {
                Console.Clear();
                Console.WriteLine("----------");
                IEnumerable <string> folders = Directory.GetDirectories(ParentDir).Select(i => Path.GetFileName(i));
                Funcs.PrettyPrint(folders);
                Console.WriteLine("\nWhich server would you like to start?");
                Console.Write("Server Name: ");
                ServerName = Console.ReadLine();
                if (string.IsNullOrEmpty(ServerName))
                {
                    Console.WriteLine("You must specify a name.");
                    Console.ReadLine();
                }
            }

            // If the directory exists, launch
            if (Directory.Exists(ServerDir))
            {
                // Sets current directory to that of the selected server
                Directory.SetCurrentDirectory(ServerDir);

                // Asks for the StartType
                if (LaunchType == "")
                {
                    Console.Clear();
                    Console.WriteLine("----------");
                    string[] allFiles = Directory.GetFiles(ServerDir);
                    // Prints all files with .type extension in the server's directory (backup, restore, default, legacy)
                    List <string> files = new List <string>();
                    foreach (string file in allFiles)
                    {
                        if (Path.GetExtension(file) == ".type")
                        {
                            files.Add(Path.GetFileNameWithoutExtension(file));
                        }
                    }
                    files.Add("legacy (for specific version)");
                    Funcs.PrettyPrint(files);
                    Console.WriteLine("Which start type would you like to use?");
                    Console.Write("Start Type [default]: ");
                    LaunchType = Funcs.NullEmpty("default");

                    if (LaunchType.ToLower().First <char>() != 'l') // If the launch file isn't legacy
                    {
                        IEnumerable <string> fileLines = new string[] { "vanilla" };
                        if (File.Exists(Path.Combine(ServerDir, "default.type")))
                        {
                            fileLines = File.ReadLines(Path.Combine(ServerDir, "default.type"));
                        }
                        switch (fileLines.DefaultIfEmpty("vanilla").FirstOrDefault().ToLower().First <char>())
                        {
                        case 'v':     // vanilla
                            ServerType = "vanilla";
                            Version    = Variables.VV;
                            break;

                        case 's':     // snapshot
                            ServerType = "snapshot";
                            Version    = Variables.SV;
                            break;

                        case 'c':                  // custom
                            ServerType = "legacy"; // Version will be set before starting server
                            break;

                        default:     // custom (predefined): i.e. 1.8.9
                            ServerType = "legacy";
                            Version    = fileLines.First();
                            break;
                        }
                    }
                }
            }
            // If the server doesn't exist, ask if we should create it
            else
            {
                Console.WriteLine($"The server {ServerName}, does not exist. Would you like to create it?");
                Console.Write("Create? Y/[N]: ");
                bool input = Funcs.ValidateBool(false);
                // Create the server, or exit program
                if (input)
                {
                    Create();
                }
                else
                {
                    return;
                }
            }
            // Start the server
            Start();
        }
Beispiel #5
0
        /// <summary>
        /// Creates the server.properties file
        /// </summary>
        /// <param name="changes">True to make changes, False to use defaults</param>
        private void ServerProperties(bool changes)
        {
            int    gamemode   = 0;
            int    difficulty = 1;
            bool   hardcore   = false;
            bool   whitelist  = false;
            string motd       = GetMotd("Max\'s Minecraft Server");

            if (changes)
            {
                Console.WriteLine("Gamemodes\n0: Survival, 1: Creative, 2: Adventure, 3: Spectator");
                Console.Write("Gamemode (0-3) [0]: ");
                gamemode = Funcs.GetInt(0, 3, 0);

                Console.WriteLine("Difficulties\n0: Peaceful, 1:Easy, 2: Normal, 3: Hard");
                Console.Write("Difficulty (0-3) [1]: ");
                difficulty = Funcs.GetInt(0, 3, 1);

                Console.Write("Hardcore true/[false]: ");
                hardcore = Funcs.ValidateBool(false);

                Console.Write("Whitelist true/[false]: ");
                whitelist = Funcs.ValidateBool(false);
            }

            string properties = "spawn-protection=0" +
                                "max-tick-time=60000\n" +
                                "generator-settings=\n" +
                                "allow-nether=true\n" +
                                "force-gamemode=false\n" +
                                "enforce-whitelist=false\n" +
                                $"gamemode={gamemode}\n" +
                                "broadcast-console-to-ops=true\n" +
                                "enable-query=true\n" +
                                "player-idle-timeout=5\n" +
                                $"difficulty={difficulty}\n" +
                                "spawn-monsters=true\n" +
                                "op-permission-level=3\n" +
                                "pvp=true\n" +
                                "snooper-enabled=true\n" +
                                "level-type=DEFAULT\n" +
                                $"hardcore={hardcore}\n" +
                                "enable-command-block=true\n" +
                                "max-players=20\n" +
                                "network-compression-threshold=256\n" +
                                "resource-pack-sha1=\n" +
                                "max-world-size=29999984\n" +
                                "server-port=25565\n" +
                                "server-ip=\n" +
                                "spawn-npcs=true\n" +
                                "allow-flight=false\n" +
                                $"level-name={ServerName}\n" +
                                "view-distance=10\n" +
                                "resource-pack=\n" +
                                "spawn-animals=true\n" +
                                $"white-list={whitelist}\n" +
                                "generate-structures=true\n" +
                                "online-mode=true\n" +
                                "max-build-height=256\n" +
                                "level-seed=\n" +
                                "prevent-proxy-connections=false\n" +
                                $"motd={@motd}\n" +
                                "enable-rcon=false";

            Write("server.properties", properties);
        }
Beispiel #6
0
 /// <summary>
 /// Writes text to a file in the server directory
 /// If the file does not exist, a new file is created.
 /// </summary>
 /// <param name="rawPath">The name of the file to write to (serverDir is prepended in this method)</param>
 /// <param name="textToWrite">The string to write to the file</param>
 private void Write(string rawPath, string text) => Funcs.Write(Path.Combine(ServerDir, rawPath), false, text);
Beispiel #7
0
        /// <summary>
        /// Creates the server
        /// </summary>
        public void Create()
        {
            LaunchType = "default";

            // Set ServerName (exits if name is blank)
            while (ServerName == "")
            {
                Console.WriteLine("What is the server name?");
                Console.Write("Name: ");
                ServerName = Console.ReadLine();
                if (string.IsNullOrEmpty(ServerName))
                {
                    Console.WriteLine("You must specify a name.");
                    Console.ReadLine();
                    continue;
                }
                if (Directory.Exists(ServerDir))
                {
                    Console.WriteLine("That server already exists, enter a new server name.");
                    ServerName = "";
                }
            }

            // Create and go to directory
            Directory.CreateDirectory(ServerDir);
            Directory.SetCurrentDirectory(ServerDir);

            // Get text to put in `default.type`
            Console.Clear();
            Console.WriteLine("----------");
            Console.WriteLine("Launch types: ");
            Funcs.PrettyPrint(new string[] { "vanilla", "snapshot", "custom (1.8.9, 1.13.2, etc.)" });
            Console.WriteLine("Launch type [vanilla]: ");
            string launchType = Funcs.NullEmpty("vanilla");

            // Make .type files
            Write("backup.type", "");
            Write("restore.type", "");
            Write("default.type", launchType); // vanilla, snapshot, custom {1.8.9,1.13.1,etc.}

            // The eula has to be accepted before the server can run, automatically accept this
            Write("eula.txt", "eula=true");

            // Op and Whitelist self
            string myUID  = "1b7ffd7a-6c9a-463e-8910-60c7a531b2a4";
            string myName = "maxh76";

            Write("ops.json", $@"[{{ ""uuid"":""{myUID}"", ""name"":""{myName}"", ""level"":4, ""bypassesplayerlimit"":true }}]");
            Write("whitelist.json", $@"[{{ ""uuid"":""{myUID}"", ""name"":""{myName}"" }}]");

            /*
             * These files are created when the server runs for the first time.
             * Since they are empty we can let the server handle that
             * Write("banned-ips.json", false, $"[]");
             * Write("banned-players.json", false, $"[]");
             * Write("usercache.json", false, $"[]");
             */

            // Copy the server icon to the directory (if it exists)
            string iconName = "server-icon.png";
            string iconPath = Path.Combine(Variables.sFiles, iconName);

            if (File.Exists(iconPath))
            {
                File.Copy(iconPath, Path.Combine(ServerDir, iconName), true);
            }
            else
            {
                Console.WriteLine(@"No server icon (""server-icon.png"") exists");
                Console.WriteLine(@"Place an image called server-icon.png in the folder where this executable is stored.");
            }

            // Create/change server.properties and set motd
            Console.WriteLine("Would you like to make settings changes?");
            Console.Write("Changes Y/[N]: ");
            bool changes = Funcs.ValidateBool(false);

            ServerProperties(changes);
        }