PHP code example of dev-lancer / mc-pack

1. Go to this page and download the library: Download dev-lancer/mc-pack library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dev-lancer / mc-pack example snippets



    use DevLancer\MCPack\ConsoleRcon;
    use DevLancer\MinecraftStatus\Query;

    $info = new Query("some.minecraftserver.com", 25565);
    $info->connect();
    
    $console = new ConsoleRcon("some.minecraftserver.com", 25575, "pass", 3);
    $console->connect();

    $players = $info->getCountPlayers();
    echo $players . "/" . $info->getMaxPlayers();

    $console->sendCommand("bc MCPack");


    use DevLancer\MCPack\ConsoleRcon;
    use DevLancer\MCPack\Manager\ServerManager;
    use DevLancer\MCPack\Sftp\Sftp;

    $host = "some.minecraftserver.com";
    $sftp = new Sftp($host);
    $sftp->login("username", "password");

    $server = new ServerManager($sftp, 25565);

    $path = "path/to/minecraft/server.jar";
    if(!$server->isRunning()) {
        if ($server->run(["-Xmx1G"], $path))
            echo "server started";
    }


    use DevLancer\MCPack\Logs;
    use DevLancer\MCPack\Sftp\Sftp;

    $host = "some.minecraftserver.com";
    $sftp = new Sftp($host);
    $sftp->login("username", "password");

    $path = "path/to/minecraft/logs/latest.log";
    $logs = new Logs($sftp, $path);
    echo implode("<br />", $logs->getLogs(true));


    use DevLancer\MCPack\Manager\PropertiesManager;
    use DevLancer\MCPack\Sftp\Sftp;

    $sftp = new Sftp("some.minecraftserver.com");
    $sftp->login("username", "password");
    
    $manager = new PropertiesManager("path/to/minecraft/server.properties", $sftp);
    $properties = $manager->getProperties();
    $properties->setRconPassword("new-password");
    $manager->saveProperties($properties);


    use DevLancer\MCPack\Motd;
    use DevLancer\MinecraftStatus\Ping;

    $host = "some.minecraftserver.com";
    $info = new Ping($host, 25565);
    $info->connect();
    
    $motd = new Motd($info);
    $motd->sendRequest();
    
    echo $motd->getResponse(Motd::RESPONSE_HTML);