diff --git a/src/classes/Player.class.php b/src/classes/Player.class.php index 79dad095b..958e6b152 100644 --- a/src/classes/Player.class.php +++ b/src/classes/Player.class.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class Player{ - private $server, $timeout, $connected, $evid, $queue, $buffer; + private $server, $timeout, $connected, $queue, $buffer; var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth, $CID, $MTU, $spawned, $equipment; function __construct($server, $clientID, $ip, $port, $MTU){ $this->queue = array(); @@ -42,11 +42,10 @@ class Player{ $this->entity = false; $this->port = $port; $this->timeout = microtime(true) + 25; - $this->evid = array(); $this->equipment = array(1, 0); $this->spawned = false; - $this->evid[] = $this->server->event("server.tick", array($this, "onTick")); - $this->evid[] = $this->server->event("server.close", array($this, "close")); + $this->server->event("server.tick", array($this, "onTick")); + $this->server->event("server.close", array($this, "close")); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); $this->connected = true; $this->auth = false; @@ -95,9 +94,6 @@ class Player{ if($this->connected === true){ $reason = $reason == "" ? "server stop":$reason; $this->save(); - foreach($this->evid as $ev){ - $this->server->deleteEvent($ev); - } $this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat"); $this->dataPacket(MC_LOGIN_STATUS, array( "status" => 1, @@ -326,17 +322,17 @@ class Player{ $this->entity->data["clientID"] = $this->clientID; $this->server->api->entity->spawnAll($this); $this->server->api->entity->spawnToAll($this->eid); - $this->evid[] = $this->server->event("server.time.change", array($this, "eventHandler")); - $this->evid[] = $this->server->event("server.chat", array($this, "eventHandler")); - $this->evid[] = $this->server->event("entity.remove", array($this, "eventHandler")); - $this->evid[] = $this->server->event("entity.move", array($this, "eventHandler")); - $this->evid[] = $this->server->event("entity.animate", array($this, "eventHandler")); - $this->evid[] = $this->server->event("player.equipment.change", array($this, "eventHandler")); - $this->evid[] = $this->server->event("player.item.pick", array($this, "eventHandler")); - $this->evid[] = $this->server->event("world.block.change", array($this, "eventHandler")); + $this->server->event("server.time.change", array($this, "eventHandler")); + $this->server->event("server.chat", array($this, "eventHandler")); + $this->server->event("entity.remove", array($this, "eventHandler")); + $this->server->event("entity.move", array($this, "eventHandler")); + $this->server->event("entity.animate", array($this, "eventHandler")); + $this->server->event("player.equipment.change", array($this, "eventHandler")); + $this->server->event("player.item.pick", array($this, "eventHandler")); + $this->server->event("world.block.change", array($this, "eventHandler")); console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2); - $this->eventHandler($this->server->motd, "server.chat"); + $this->eventHandler(new Container($this->server->motd), "server.chat"); if($this->MTU <= 548){ $this->eventHandler("Your connection is bad, you may experience lag and slow map loading.", "server.chat"); } diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 2bdb277cf..78b3d51d8 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -26,7 +26,7 @@ the Free Software Foundation, either version 3 of the License, or */ class PocketMinecraftServer{ - var $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $levelData, $tileEntities; + var $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $spawn, $entities, $mapDir, $mapName, $map, $levelData, $tileEntities; private $database, $interface, $evCnt, $handCnt, $events, $handlers, $serverType, $lastTick, $ticker; private function load(){ @@ -118,8 +118,8 @@ class PocketMinecraftServer{ $this->query("CREATE TABLE events (ID INTEGER PRIMARY KEY, name TEXT);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); //$this->query("PRAGMA synchronous = OFF;"); - $this->preparedSQL->selectHandlers = $this->database->prepare("SELECT ID FROM handlers WHERE name = :name ORDER BY priority DESC;"); - $this->preparedSQL->selectEvents = $this->database->prepare("SELECT ID FROM events WHERE name = :name;"); + $this->preparedSQL->selectHandlers = $this->database->prepare("SELECT DISTINCT ID FROM handlers WHERE name = :name ORDER BY priority DESC;"); + $this->preparedSQL->selectEvents = $this->database->prepare("SELECT DISTINCT ID FROM events WHERE name = :name;"); $this->preparedSQL->selectActions = $this->database->prepare("SELECT ID,code,repeat FROM actions WHERE last <= (:time - interval);"); $this->preparedSQL->updateActions = $this->database->prepare("UPDATE actions SET last = :time WHERE last <= (:time - interval);"); } @@ -203,19 +203,28 @@ class PocketMinecraftServer{ $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $handlers = $this->preparedSQL->selectHandlers->execute(); $result = true; - console("[INTERNAL] Handling ".$event, true, true, 3); if($handlers !== false and $handlers !== true){ - while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){ - $handler = $this->handlers[(int) $hn["ID"]]; - if(is_array($handler)){ - $method = $handler[1]; - $result = $handler[0]->$method($data, $event); + console("[INTERNAL] Handling ".$event, true, true, 3); + $call = array(); + while(($hn = $handlers->fetchArray(SQLITE3_ASSOC)) !== false){ + $call[(int) $hn["ID"]] = true; + } + $handlers->finalize(); + foreach($call as $hnid => $boolean){ + if($result !== false){ + $called[$hnid] = true; + $handler = $this->handlers[$hnid]; + if(is_array($handler)){ + $method = $handler[1]; + $result = $handler[0]->$method($data, $event); + }else{ + $result = $handler($data, $event); + } }else{ - $result = $handler($data, $event); + break; } } } - $handlers->finalize(); if($result !== false){ $this->trigger($event, $data); } @@ -502,26 +511,26 @@ class PocketMinecraftServer{ if($events === false or $events === true){ return; } - while(false !== ($evn = $events->fetchArray(SQLITE3_ASSOC))){ - $ev = $this->events[(int) $evn["ID"]]; - if(is_array($ev)){ - $method = $ev[1]; - $this->responses[(int) $evn["ID"]] = $ev[0]->$method($data, $event); - }else{ - $this->responses[(int) $evn["ID"]] = $ev($data, $event); - } + $call = array(); + while(($evn = $events->fetchArray(SQLITE3_ASSOC)) !== false){ + $call[(int) $evn["ID"]] = true; } $events->finalize(); - return true; - } - - public function response($eid){ - if(isset($this->responses[$eid])){ - $res = $this->responses[$eid]; - unset($this->responses[$eid]); - return $res; + foreach($call as $evid => $boolean){ + $ev = $this->events[$evid]; + if(!is_callable($ev)){ + $this->deleteEvent($evid); + continue; + } + if(is_array($ev)){ + $method = $ev[1]; + $ev[0]->$method($data, $event); + }else{ + $ev($data, $event); + } } - return false; + + return true; } public function schedule($ticks, $callback, $data = array(), $repeat = false, $eventName = "server.schedule"){