From 77ecdb8727b7687504d1a982076dea14e6bba1e3 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 27 Apr 2013 00:15:01 +0200 Subject: [PATCH] Event test in ConsoleAPI --- src/API/ConsoleAPI.php | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 770745bdb..f7fe4d485 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -36,8 +36,15 @@ class ConsoleAPI{ } public function init(){ - $this->event = $this->server->event("server.tick", array($this, "handle")); - $this->loop = new ConsoleLoop(); + if(HAS_EVENT){ + $this->event = new EventBase(); + $event = new Event($this->event, STDIN, Event::READ | Event::PERSIST, array($this, "readLine")); + $event->add(); + $this->event->loop(); + }else{ + $this->event = $this->server->event("server.tick", array($this, "handle")); + $this->loop = new ConsoleLoop(); + } $this->register("help", "[page|command name]", array($this, "defaultCommands")); $this->register("status", "", array($this, "defaultCommands")); $this->register("difficulty", "<0|1|2>", array($this, "defaultCommands")); @@ -47,12 +54,29 @@ class ConsoleAPI{ $this->register("defaultgamemode", "", array($this, "defaultCommands")); $this->server->api->ban->cmdWhitelist("help"); } + + public function readLine($fd, $events){ + $line = trim(fgets($fd)); + if($line != ""){ + $output = $this->run($line, "console"); + if($output != ""){ + $mes = explode("\n", trim($output)); + foreach($mes as $m){ + console("[CMD] ".$m); + } + } + } + } function __destruct(){ - $this->server->deleteEvent($this->event); - $this->loop->stop = true; - $this->loop->notify(); - //$this->loop->join(); + if(HAS_EVENT){ + $this->loop->stop(); + }else{ + $this->server->deleteEvent($this->event); + $this->loop->stop = true; + $this->loop->notify(); + //$this->loop->join(); + } } public function defaultCommands($cmd, $params, $issuer, $alias){