mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-05 01:29:55 +00:00
Added a generic Promise type
I anticipate increasing demand for promises, and since all the libraries I could find suck, we'll stick to our own impl for now.
This commit is contained in:
parent
33eb97da97
commit
624495f4d3
@ -70,7 +70,6 @@ use pocketmine\permission\DefaultPermissions;
|
|||||||
use pocketmine\player\GameMode;
|
use pocketmine\player\GameMode;
|
||||||
use pocketmine\player\OfflinePlayer;
|
use pocketmine\player\OfflinePlayer;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
use pocketmine\player\PlayerCreationPromise;
|
|
||||||
use pocketmine\player\PlayerInfo;
|
use pocketmine\player\PlayerInfo;
|
||||||
use pocketmine\plugin\PharPluginLoader;
|
use pocketmine\plugin\PharPluginLoader;
|
||||||
use pocketmine\plugin\Plugin;
|
use pocketmine\plugin\Plugin;
|
||||||
@ -93,6 +92,7 @@ use pocketmine\utils\Filesystem;
|
|||||||
use pocketmine\utils\Internet;
|
use pocketmine\utils\Internet;
|
||||||
use pocketmine\utils\MainLogger;
|
use pocketmine\utils\MainLogger;
|
||||||
use pocketmine\utils\Process;
|
use pocketmine\utils\Process;
|
||||||
|
use pocketmine\utils\Promise;
|
||||||
use pocketmine\utils\Terminal;
|
use pocketmine\utils\Terminal;
|
||||||
use pocketmine\utils\TextFormat;
|
use pocketmine\utils\TextFormat;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
@ -580,7 +580,10 @@ class Server{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createPlayer(NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated, ?CompoundTag $offlinePlayerData) : PlayerCreationPromise{
|
/**
|
||||||
|
* @phpstan-return Promise<Player>
|
||||||
|
*/
|
||||||
|
public function createPlayer(NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated, ?CompoundTag $offlinePlayerData) : Promise{
|
||||||
$ev = new PlayerCreationEvent($session);
|
$ev = new PlayerCreationEvent($session);
|
||||||
$ev->call();
|
$ev->call();
|
||||||
$class = $ev->getPlayerClass();
|
$class = $ev->getPlayerClass();
|
||||||
@ -596,7 +599,7 @@ class Server{
|
|||||||
$playerPos = null;
|
$playerPos = null;
|
||||||
$spawn = $world->getSpawnLocation();
|
$spawn = $world->getSpawnLocation();
|
||||||
}
|
}
|
||||||
$playerPromise = new PlayerCreationPromise();
|
$playerPromise = new Promise();
|
||||||
$world->requestChunkPopulation($spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, null)->onCompletion(
|
$world->requestChunkPopulation($spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, null)->onCompletion(
|
||||||
function() use ($playerPromise, $class, $session, $playerInfo, $authenticated, $world, $playerPos, $spawn, $offlinePlayerData) : void{
|
function() use ($playerPromise, $class, $session, $playerInfo, $authenticated, $world, $playerPos, $spawn, $offlinePlayerData) : void{
|
||||||
if(!$session->isConnected()){
|
if(!$session->isConnected()){
|
||||||
|
@ -21,14 +21,17 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace pocketmine\player;
|
namespace pocketmine\utils;
|
||||||
|
|
||||||
use function spl_object_id;
|
use function spl_object_id;
|
||||||
|
|
||||||
final class PlayerCreationPromise{
|
/**
|
||||||
|
* @phpstan-template TValue
|
||||||
|
*/
|
||||||
|
final class Promise{
|
||||||
/**
|
/**
|
||||||
* @var \Closure[]
|
* @var \Closure[]
|
||||||
* @phpstan-var array<int, \Closure(Player) : void>
|
* @phpstan-var array<int, \Closure(TValue) : void>
|
||||||
*/
|
*/
|
||||||
private array $onSuccess = [];
|
private array $onSuccess = [];
|
||||||
|
|
||||||
@ -39,10 +42,15 @@ final class PlayerCreationPromise{
|
|||||||
private array $onFailure = [];
|
private array $onFailure = [];
|
||||||
|
|
||||||
private bool $resolved = false;
|
private bool $resolved = false;
|
||||||
private ?Player $result = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @phpstan-param \Closure(Player) : void $onSuccess
|
* @var mixed
|
||||||
|
* @phpstan-var TValue|null
|
||||||
|
*/
|
||||||
|
private $result = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param \Closure(TValue) : void $onSuccess
|
||||||
* @phpstan-param \Closure() : void $onFailure
|
* @phpstan-param \Closure() : void $onFailure
|
||||||
*/
|
*/
|
||||||
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
||||||
@ -54,17 +62,27 @@ final class PlayerCreationPromise{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolve(Player $player) : void{
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @phpstan-param TValue $value
|
||||||
|
*/
|
||||||
|
public function resolve($value) : void{
|
||||||
|
if($this->resolved){
|
||||||
|
throw new \InvalidStateException("Promise has already been resolved/rejected");
|
||||||
|
}
|
||||||
$this->resolved = true;
|
$this->resolved = true;
|
||||||
$this->result = $player;
|
$this->result = $value;
|
||||||
foreach($this->onSuccess as $c){
|
foreach($this->onSuccess as $c){
|
||||||
$c($player);
|
$c($value);
|
||||||
}
|
}
|
||||||
$this->onSuccess = [];
|
$this->onSuccess = [];
|
||||||
$this->onFailure = [];
|
$this->onFailure = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reject() : void{
|
public function reject() : void{
|
||||||
|
if($this->resolved){
|
||||||
|
throw new \InvalidStateException("Promise has already been resolved/rejected");
|
||||||
|
}
|
||||||
$this->resolved = true;
|
$this->resolved = true;
|
||||||
foreach($this->onFailure as $c){
|
foreach($this->onFailure as $c){
|
||||||
$c();
|
$c();
|
@ -1,76 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* ____ _ _ __ __ _ __ __ ____
|
|
||||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
||||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
||||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
||||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* @author PocketMine Team
|
|
||||||
* @link http://www.pocketmine.net/
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace pocketmine\world;
|
|
||||||
|
|
||||||
use function spl_object_id;
|
|
||||||
|
|
||||||
final class ChunkPopulationPromise{
|
|
||||||
/**
|
|
||||||
* @var \Closure[]
|
|
||||||
* @phpstan-var array<int, \Closure() : void>
|
|
||||||
*/
|
|
||||||
private array $onSuccess = [];
|
|
||||||
/**
|
|
||||||
* @var \Closure[]
|
|
||||||
* @phpstan-var array<int, \Closure() : void>
|
|
||||||
*/
|
|
||||||
private array $onFailure = [];
|
|
||||||
|
|
||||||
private ?bool $success = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @phpstan-param \Closure() : void $onSuccess
|
|
||||||
* @phpstan-param \Closure() : void $onFailure
|
|
||||||
*/
|
|
||||||
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
|
||||||
if($this->success !== null){
|
|
||||||
$this->success ? $onSuccess() : $onFailure();
|
|
||||||
}else{
|
|
||||||
$this->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
|
|
||||||
$this->onFailure[spl_object_id($onFailure)] = $onFailure;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function resolve() : void{
|
|
||||||
$this->success = true;
|
|
||||||
foreach($this->onSuccess as $callback){
|
|
||||||
$callback();
|
|
||||||
}
|
|
||||||
$this->onSuccess = [];
|
|
||||||
$this->onFailure = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function reject() : void{
|
|
||||||
$this->success = false;
|
|
||||||
foreach($this->onFailure as $callback){
|
|
||||||
$callback();
|
|
||||||
}
|
|
||||||
$this->onSuccess = [];
|
|
||||||
$this->onFailure = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isCompleted() : bool{
|
|
||||||
return $this->success !== null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -68,6 +68,7 @@ use pocketmine\Server;
|
|||||||
use pocketmine\timings\Timings;
|
use pocketmine\timings\Timings;
|
||||||
use pocketmine\utils\AssumptionFailedError;
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
use pocketmine\utils\Limits;
|
use pocketmine\utils\Limits;
|
||||||
|
use pocketmine\utils\Promise;
|
||||||
use pocketmine\utils\ReversePriorityQueue;
|
use pocketmine\utils\ReversePriorityQueue;
|
||||||
use pocketmine\world\biome\Biome;
|
use pocketmine\world\biome\Biome;
|
||||||
use pocketmine\world\biome\BiomeRegistry;
|
use pocketmine\world\biome\BiomeRegistry;
|
||||||
@ -240,8 +241,8 @@ class World implements ChunkManager{
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
private $maxConcurrentChunkPopulationTasks = 2;
|
private $maxConcurrentChunkPopulationTasks = 2;
|
||||||
/**
|
/**
|
||||||
* @var ChunkPopulationPromise[] chunkHash => promise
|
* @var Promise[] chunkHash => promise
|
||||||
* @phpstan-var array<int, ChunkPopulationPromise>
|
* @phpstan-var array<int, Promise<Chunk>>
|
||||||
*/
|
*/
|
||||||
private array $chunkPopulationRequestMap = [];
|
private array $chunkPopulationRequestMap = [];
|
||||||
/**
|
/**
|
||||||
@ -2110,7 +2111,7 @@ class World implements ChunkManager{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($this->activeChunkPopulationTasks[$index]);
|
unset($this->activeChunkPopulationTasks[$index]);
|
||||||
$this->chunkPopulationRequestMap[$index]->resolve();
|
$this->chunkPopulationRequestMap[$index]->resolve($chunk);
|
||||||
unset($this->chunkPopulationRequestMap[$index]);
|
unset($this->chunkPopulationRequestMap[$index]);
|
||||||
|
|
||||||
$this->drainPopulationRequestQueue();
|
$this->drainPopulationRequestQueue();
|
||||||
@ -2698,10 +2699,13 @@ class World implements ChunkManager{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function enqueuePopulationRequest(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
/**
|
||||||
|
* @phpstan-return Promise<Chunk>
|
||||||
|
*/
|
||||||
|
private function enqueuePopulationRequest(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||||
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
||||||
$this->chunkPopulationRequestQueue->enqueue($chunkHash);
|
$this->chunkPopulationRequestQueue->enqueue($chunkHash);
|
||||||
$promise = $this->chunkPopulationRequestMap[$chunkHash] = new ChunkPopulationPromise();
|
$promise = $this->chunkPopulationRequestMap[$chunkHash] = new Promise();
|
||||||
if($associatedChunkLoader === null){
|
if($associatedChunkLoader === null){
|
||||||
$temporaryLoader = new class implements ChunkLoader{};
|
$temporaryLoader = new class implements ChunkLoader{};
|
||||||
$this->registerChunkLoader($temporaryLoader, $chunkX, $chunkZ);
|
$this->registerChunkLoader($temporaryLoader, $chunkX, $chunkZ);
|
||||||
@ -2721,8 +2725,10 @@ class World implements ChunkManager{
|
|||||||
* A ChunkLoader can be associated with the generation request to ensure that the generation request is cancelled if
|
* A ChunkLoader can be associated with the generation request to ensure that the generation request is cancelled if
|
||||||
* no loaders are attached to the target chunk. If no loader is provided, one will be assigned (and automatically
|
* no loaders are attached to the target chunk. If no loader is provided, one will be assigned (and automatically
|
||||||
* removed when the generation request completes).
|
* removed when the generation request completes).
|
||||||
|
*
|
||||||
|
* @phpstan-return Promise<Chunk>
|
||||||
*/
|
*/
|
||||||
public function requestChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
public function requestChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||||
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
||||||
$promise = $this->chunkPopulationRequestMap[$chunkHash] ?? null;
|
$promise = $this->chunkPopulationRequestMap[$chunkHash] ?? null;
|
||||||
if($promise !== null && isset($this->activeChunkPopulationTasks[$chunkHash])){
|
if($promise !== null && isset($this->activeChunkPopulationTasks[$chunkHash])){
|
||||||
@ -2743,8 +2749,10 @@ class World implements ChunkManager{
|
|||||||
*
|
*
|
||||||
* If the chunk is currently locked (for example due to another chunk using it for async generation), the request
|
* If the chunk is currently locked (for example due to another chunk using it for async generation), the request
|
||||||
* will be queued and executed at the earliest opportunity.
|
* will be queued and executed at the earliest opportunity.
|
||||||
|
*
|
||||||
|
* @phpstan-return Promise<Chunk>
|
||||||
*/
|
*/
|
||||||
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||||
$index = World::chunkHash($x, $z);
|
$index = World::chunkHash($x, $z);
|
||||||
$promise = $this->chunkPopulationRequestMap[$index] ?? null;
|
$promise = $this->chunkPopulationRequestMap[$index] ?? null;
|
||||||
if($promise !== null && isset($this->activeChunkPopulationTasks[$index])){
|
if($promise !== null && isset($this->activeChunkPopulationTasks[$index])){
|
||||||
@ -2766,7 +2774,7 @@ class World implements ChunkManager{
|
|||||||
|
|
||||||
$this->activeChunkPopulationTasks[$index] = true;
|
$this->activeChunkPopulationTasks[$index] = true;
|
||||||
if($promise === null){
|
if($promise === null){
|
||||||
$promise = new ChunkPopulationPromise();
|
$promise = new Promise();
|
||||||
$this->chunkPopulationRequestMap[$index] = $promise;
|
$this->chunkPopulationRequestMap[$index] = $promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2788,8 +2796,8 @@ class World implements ChunkManager{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned
|
//chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned
|
||||||
$result = new ChunkPopulationPromise();
|
$result = new Promise();
|
||||||
$result->resolve();
|
$result->resolve($chunk);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,16 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/entity/projectile/Projectile.php
|
path: ../../../src/entity/projectile/Projectile.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: "#^Parameter \\#1 \\$ of closure expects TValue, TValue given\\.$#"
|
||||||
|
count: 2
|
||||||
|
path: ../../../src/utils/Promise.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: "#^Property pocketmine\\\\utils\\\\Promise\\<TValue\\>\\:\\:\\$result \\(TValue\\|null\\) does not accept TValue\\.$#"
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/utils/Promise.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Parameter \\#1 \\$ of closure expects TMemberType, TMemberType given\\.$#"
|
message: "#^Parameter \\#1 \\$ of closure expects TMemberType, TMemberType given\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user