Poslání Rcon příkazu na minecraft server pomocí nette Rcon class služby
- JBurdik
- Člen | 10
Čau hele potřeboval bych poradit používám externí kod z gitu na adrese https://gist.github.com/…b7a39a865722
ale nějak mi to nefunguje píše mi to že nemám definovanou proměnnou host mám to zaregistrované jako service (službu)
díky moc za pomoc / nápady
- David Matějka
- Moderator | 6445
ahoj, co to presne hlasi za chybu? predavas pri registraci te sluzby ty parametry host, port a password?
- JBurdik
- Člen | 10
No asi ne ale podle dokumentace jsem to nepochopil jak pri registraci
predavat parametry :)
Config.neon
#
# WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
# https://nette.org/en/security-warning
#
parameters:
application:
errorPresenter: Error
session:
expiration: 14 days
database:
dsn: 'mysql:host=localhost;dbname=quickstart'
user: root
password:
options:
lazy: yes
services:
- App\Model\UserManager
- App\Forms\FormFactory
- App\Forms\SignInFormFactory
- App\Forms\SignUpFormFactory
- App\RouterFactory
Rcon: Rcon
UsersRepository: UsersRepository
router: @App\RouterFactory::createRouter
Backend:MainpagePresenter
namespace BackendModule;
use Nette;
class MainpagePresenter extends BasePresenter
{
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database ) {
$this->database = $database;
}
public function beforeRender() {
parent::beforeRender();
if(!$this->getUser()->isLoggedIn()){
$this->flashMessage('Nejsi přihlášen!', 'warning');
$this->redirect(':Frontend:Sign:in');
}
else if($this->getUser()->isLoggedIn() && $this->getUser()->isInRole("admin") ){
$this->flashMessage('Vítej v administraci!', 'success');
}
else {
$this->flashMessage('Nejsi Administrátor', 'danger');
$this->redirect(':Frontend:Homepage:');
}
}
public function actionDefault() {
$this->rcon = new \Rcon($host, $port, $password, $timeout);
if ($rcon1->connect()){
$rcon1->sendCommand("say rcon enabled!");
$this->template->succ = "nice and connected";
}
}
public function renderDefault()
{
}
}
A jeste rcon.php ve slozce app/modules
<?php
/**
* See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
* more information about Source RCON Packets
*
* PHP Version 7
*
* @copyright 2013-2017 Chris Churchwell
* @author thedudeguy
* @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
*/
class Rcon
{
private $host = 'play.netaxe.cz';
private $port = 11111;
private $password = 'fead9105bd9065f';
private $timeout = 3;
private $socket;
private $authorized = false;
private $lastResponse = '';
const PACKET_AUTHORIZE = 5;
const PACKET_COMMAND = 6;
const SERVERDATA_AUTH = 3;
const SERVERDATA_AUTH_RESPONSE = 2;
const SERVERDATA_EXECCOMMAND = 2;
const SERVERDATA_RESPONSE_VALUE = 0;
/**
* Create a new instance of the Rcon class.
*
* @param string $host
* @param integer $port
* @param string $password
* @param integer $timeout
*/
public function __construct($host, $port, $password, $timeout)
{
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->timeout = $timeout;
}
/**
* Get the latest response from the server.
*
* @return string
*/
public function getResponse()
{
return $this->lastResponse;
}
/**
* Connect to a server.
*
* @return boolean
*/
public function connect()
{
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if (!$this->socket) {
$this->lastResponse = $errstr;
return false;
}
//set timeout
stream_set_timeout($this->socket, 3, 0);
// check authorization
return $this->authorize();
}
/**
* Disconnect from server.
*
* @return void
*/
public function disconnect()
{
if ($this->socket) {
fclose($this->socket);
}
}
/**
* True if socket is connected and authorized.
*
* @return boolean
*/
public function isConnected()
{
return $this->authorized;
}
/**
* Send a command to the connected server.
*
* @param string $command
*
* @return boolean|mixed
*/
public function sendCommand($command)
{
if (!$this->isConnected()) {
return false;
}
// send command packet
$this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
// get response
$response_packet = $this->readPacket();
if ($response_packet['id'] == self::PACKET_COMMAND) {
if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
$this->lastResponse = $response_packet['body'];
return $response_packet['body'];
}
}
return false;
}
Editoval JBurdik (11. 4. 2018 17:24)
- David Matějka
- Moderator | 6445
to sice registrujes (bez parametru), ale tu sluzbu pak vubec nepouzivas a
vytvaris rucne instanci, kde jeste pouzivas neexistujici promenne – nebo kde
si myslis, ze se vezme ta hodnota $host
atd :) ?
v configu predavas parametry nasledovne:
- Rcon(muj.host.cz)
atd.
pripadne muzes pouzit parametry:
parameters:
rcon:
host: muj.host.cz
services:
- Rcon(%rcon.host%)
a pak to predas do presenteru jako zavislost – stejne jako se tam ted
predava Nette\Database\Context
- David Matějka
- Moderator | 6445
v presenteru je pouzivat nemusis, do presenteru si predas nakonfigurovanou instanci Rcon tridy
- David Matějka
- Moderator | 6445
tedka si tam jako zavislost predavas databazi, jen tam staci pridat i ten Rcon, nejak takhle:
/** @var Nette\Database\Context */
private $database;
/** @var \Rcon */
private $rcon;
public function __construct(Nette\Database\Context $database, \Rcon $rcon) {
$this->database = $database;
$this->rcon = $rcon;
}
a v te actionDefault pak budes mit v $this->rcon
dostupnou
nakonfigurovanou sluzbu
- JBurdik
- Člen | 10
no bylo by to dobré potřebuji měnit jen ten port podle toho do jaké konzole to chci poslat ten rcon příkaz potřeboval bych to posílat do více :)
a do té sluzby musím dopsat toto:
/**
*
* @param Rcon $rcon
* @param string $host
* @param string $password
* ...
* ...
* @throws \InvalidArgumentException
*/
zě?
Editoval JBurdik (11. 4. 2018 18:18)