diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 07204bbcd..1f46c5057 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -42,7 +42,6 @@ class ConsoleAPI{ $this->register("status", "", array($this, "defaultCommands")); $this->register("difficulty", "<0|1|2>", array($this, "defaultCommands")); $this->register("invisible", "", array($this, "defaultCommands")); - $this->register("save-all", "", array($this, "defaultCommands")); $this->register("stop", "", array($this, "defaultCommands")); $this->register("defaultgamemode", "", array($this, "defaultCommands")); $this->server->api->ban->cmdWhitelist("help"); @@ -126,10 +125,6 @@ class ConsoleAPI{ $this->server->api->setProperty("difficulty", (int) $s); $output .= "Difficulty changed to ".$this->server->difficulty."\n"; break; - case "save-all": - $this->server->save(); - break; - case "?": if($issuer !== "console" and $issuer !== "rcon"){ break; @@ -202,8 +197,7 @@ class ConsoleAPI{ $params = (string) substr($line, $end + 1); if(isset($this->alias[$cmd])){ - $this->run($this->alias[$cmd] . " " .$params, $issuer, $cmd); - return; + return $this->run($this->alias[$cmd] . " " .$params, $issuer, $cmd); } if($issuer instanceof Player){ console("[DEBUG] \x1b[33m".$issuer->username."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); @@ -227,10 +221,11 @@ class ConsoleAPI{ $offsetshift += strlen($selector[0]) - strlen($p) + 1; break; case "all": + $output = ""; foreach($this->server->api->player->getAll() as $p){ - $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); + $output .= $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); } - return; + return $output; case "random": $l = array(); foreach($this->server->api->player->getAll() as $p){ diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index bf049cd2b..9539ffa77 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -45,6 +45,9 @@ class LevelAPI{ public function init(){ $this->server->api->console->register("seed", "[world]", array($this, "commandHandler")); + $this->server->api->console->register("save-all", "", array($this, "commandHandler")); + $this->server->api->console->register("save-on", "", array($this, "commandHandler")); + $this->server->api->console->register("save-off", "", array($this, "commandHandler")); $this->default = $this->server->api->getProperty("level-name"); if($this->loadLevel($this->default) === false){ $this->generateLevel($this->default); @@ -56,6 +59,18 @@ class LevelAPI{ public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ + case "save-all": + $save = $this->server->saveEnabled; + $this->server->saveEnabled = true; + $this->saveAll(); + $this->server->saveEnabled = $save; + break; + case "save-on": + $this->server->saveEnabled = true; + break; + case "save-off": + $this->server->saveEnabled = false; + break; case "seed": if(!isset($params[0]) and ($issuer instanceof Player)){ $output .= "Seed: ".$issuer->level->getSeed()."\n"; @@ -138,6 +153,12 @@ class LevelAPI{ switch($event){ } } + + public function saveAll(){ + foreach($this->levels as $level){ + $level->save(); + } + } public function getSpawn(){ return $this->server->spawn; @@ -156,81 +177,5 @@ class LevelAPI{ $this->spawn = $this->level->getSpawn(); } } - - public function save($final = false){ - if($this->mapName !== false){ - $this->levelData["Time"] = $this->time; - file_put_contents($this->mapDir."level.dat", serialize($this->levelData)); - $this->map->saveMap($final); - $this->trigger("server.save", $final); - if(count($this->entities) > 0){ - $entities = array(); - foreach($this->entities as $entity){ - if($entity->class === ENTITY_MOB){ - $entities[] = array( - "id" => $entity->type, - "Color" => @$entity->data["Color"], - "Sheared" => @$entity->data["Sheared"], - "Health" => $entity->health, - "Pos" => array( - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ), - "Rotation" => array( - 0 => $entity->yaw, - 1 => $entity->pitch, - ), - ); - }elseif($entity->class === ENTITY_OBJECT){ - $entities[] = array( - "id" => $entity->type, - "TileX" => $entity->x, - "TileX" => $entity->y, - "TileX" => $entity->z, - "Health" => $entity->health, - "Motive" => $entity->data["Motive"], - "Pos" => array( - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ), - "Rotation" => array( - 0 => $entity->yaw, - 1 => $entity->pitch, - ), - ); - }elseif($entity->class === ENTITY_ITEM){ - $entities[] = array( - "id" => 64, - "Item" => array( - "id" => $entity->type, - "Damage" => $entity->meta, - "Count" => $entity->stack, - ), - "Health" => $entity->health, - "Pos" => array( - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ), - "Rotation" => array( - 0 => 0, - 1 => 0, - ), - ); - } - } - file_put_contents($this->mapDir."entities.dat", serialize($entities)); - } - if(count($this->tileEntities) > 0){ - $tiles = array(); - foreach($this->tileEntities as $tile){ - $tiles[] = $tile->data; - } - file_put_contents($this->mapDir."tileEntities.dat", serialize($tiles)); - } - } - } } \ No newline at end of file diff --git a/src/Player.php b/src/Player.php index 3bef01c57..b9cb17960 100644 --- a/src/Player.php +++ b/src/Player.php @@ -1039,8 +1039,8 @@ class Player{ if($this->spawned === false){ break; } - if($this->gamemode !== VIEW and $this->blocked === false and isset($this->server->entities[$data["target"]]) and $this->entity->distance($this->server->entities[$data["target"]]) <= 8){ - $target = $this->server->api->entity->get($data["target"]); + $target = $this->server->api->entity->get($data["target"]); + if($this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ $data["targetentity"] = $target; $data["entity"] = $this->entity; if(!($target instanceof Entity)){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 8af0bde8d..497cadd55 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class PocketMinecraftServer{ public $tCnt; - public $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd; + public $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $saveEnabled; private $port, $serverip, $database, $interface, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick; private function load(){ @@ -65,6 +65,7 @@ class PocketMinecraftServer{ $this->whitelist = false; $this->clients = array(); $this->spawn = false; + $this->saveEnabled = true; $this->tickMeasure = array_fill(0, 40, 0); $this->setType("normal"); $this->interface = new MinecraftInterface("255.255.255.255", $this->port, true, false, $this->serverip); diff --git a/src/world/Entity.php b/src/world/Entity.php index 0e1d6d7fa..6162107f2 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -179,7 +179,7 @@ class Entity extends Position{ private function spawnDrops(){ foreach($this->getDrops() as $drop){ - $this->server->api->block->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF), true); + $this->server->api->entity->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF), true); } } diff --git a/src/world/Level.php b/src/world/Level.php index e42e23118..ff5a0ce9b 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -66,19 +66,17 @@ class Level{ if($this->server->api->dhandle("time.change", array("level" => $this, "time" => $time)) !== false){ $this->time = $time; } - - foreach($this->usedChunks as $i => $c){ - if(count($c) === 0){ - unset($this->usedChunks[$i]); - $X = explode(".", $i); - $Z = array_pop($X); - $this->level->unloadChunk((int) array_pop($X), (int) $Z); - } - } - if($this->nextSave < $now){ + if($this->nextSave < $now and $this->server->saveEnabled === true){ + foreach($this->usedChunks as $i => $c){ + if(count($c) === 0){ + unset($this->usedChunks[$i]); + $X = explode(".", $i); + $Z = array_pop($X); + $this->level->unloadChunk((int) array_pop($X), (int) $Z); + } + } $this->save(); - $this->lastSave = $now + 90; } } @@ -88,6 +86,9 @@ class Level{ } public function save(){ + if($this->server->saveEnabled === false){ + return; + } $entities = array(); foreach($this->server->api->entity->getAll($this) as $entity){ if($entity->class === ENTITY_MOB){ @@ -157,6 +158,7 @@ class Level{ $this->level->setData("time", (int) $this->time); $this->level->doSaveRound(); $this->level->saveData(); + $this->lastSave = microtime(true) + 90; } public function getBlock(Vector3 $pos){ diff --git a/src/world/Position.php b/src/world/Position.php index 3f73b0ae4..10693f179 100644 --- a/src/world/Position.php +++ b/src/world/Position.php @@ -34,7 +34,7 @@ class Position extends Vector3{ } public function distance($x = 0, $y = 0, $z = 0){ - if($x instanceof Position and $x->level !== $this->level){ + if(($x instanceof Position) and $x->level !== $this->level){ return PHP_INT_MAX; } return parent::distance($x, $y, $z);