forked from pmmp/PocketMine-MP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement PlayerCreation & PlayerDataSave as async events
- Loading branch information
1 parent
7ba0e15
commit c70397d
Showing
7 changed files
with
254 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace pocketmine\event\player; | ||
|
||
use pocketmine\event\AsyncEvent; | ||
use pocketmine\network\mcpe\NetworkSession; | ||
use pocketmine\player\Player; | ||
use pocketmine\utils\Utils; | ||
|
||
/** | ||
* Allows the use of custom Player classes. This enables overriding built-in Player methods to change behaviour that is | ||
* not possible to alter any other way. | ||
* | ||
* You probably don't need this event, and found your way here because you looked at some code in an old plugin that | ||
* abused it (very common). Instead of using custom player classes, you should consider making session classes instead. | ||
* | ||
* @see https://github.com/pmmp/SessionsDemo | ||
* | ||
* This event is a power-user feature, and multiple plugins using it at the same time will conflict and break unless | ||
* they've been designed to work together. This means that it's only usually useful in private plugins. | ||
* | ||
* WARNING: This should NOT be used for adding extra functions or properties. This is intended for **overriding existing | ||
* core behaviour**, and should only be used if you know EXACTLY what you're doing. | ||
* Custom player classes may break in any update without warning. This event isn't much more than glorified reflection. | ||
*/ | ||
class PlayerCreationAsyncEvent extends AsyncEvent{ | ||
/** @phpstan-var class-string<Player> */ | ||
private string $baseClass = Player::class; | ||
/** @phpstan-var class-string<Player> */ | ||
private string $playerClass = Player::class; | ||
|
||
public function __construct(private NetworkSession $session){} | ||
|
||
public function getNetworkSession() : NetworkSession{ | ||
return $this->session; | ||
} | ||
|
||
public function getAddress() : string{ | ||
return $this->session->getIp(); | ||
} | ||
|
||
public function getPort() : int{ | ||
return $this->session->getPort(); | ||
} | ||
|
||
/** | ||
* Returns the base class that the final player class must extend. | ||
* | ||
* @phpstan-return class-string<Player> | ||
*/ | ||
public function getBaseClass() : string{ | ||
return $this->baseClass; | ||
} | ||
|
||
/** | ||
* Sets the class that the final player class must extend. | ||
* The new base class must be a subclass of the current base class. | ||
* This can (perhaps) be used to limit the options for custom player classes provided by other plugins. | ||
* | ||
* @phpstan-param class-string<Player> $class | ||
*/ | ||
public function setBaseClass(string $class) : void{ | ||
if(!is_a($class, $this->baseClass, true)){ | ||
throw new \RuntimeException("Base class $class must extend " . $this->baseClass); | ||
} | ||
|
||
$this->baseClass = $class; | ||
} | ||
|
||
/** | ||
* Returns the class that will be instantiated to create the player after the event. | ||
* | ||
* @phpstan-return class-string<Player> | ||
*/ | ||
public function getPlayerClass() : string{ | ||
return $this->playerClass; | ||
} | ||
|
||
/** | ||
* Sets the class that will be instantiated to create the player after the event. The class must not be abstract, | ||
* and must be an instance of the base class. | ||
* | ||
* @phpstan-param class-string<Player> $class | ||
*/ | ||
public function setPlayerClass(string $class) : void{ | ||
Utils::testValidInstance($class, $this->baseClass); | ||
$this->playerClass = $class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace pocketmine\event\player; | ||
|
||
use pocketmine\event\AsyncEvent; | ||
use pocketmine\event\Cancellable; | ||
use pocketmine\event\CancellableTrait; | ||
use pocketmine\nbt\tag\CompoundTag; | ||
use pocketmine\player\Player; | ||
|
||
class PlayerDataSaveAsyncEvent extends AsyncEvent implements Cancellable{ | ||
use CancellableTrait; | ||
|
||
public function __construct( | ||
protected CompoundTag $data, | ||
protected string $playerName, | ||
private ?Player $player | ||
){} | ||
|
||
/** | ||
* Returns the data to be written to disk as a CompoundTag | ||
*/ | ||
public function getSaveData() : CompoundTag{ | ||
return $this->data; | ||
} | ||
|
||
public function setSaveData(CompoundTag $data) : void{ | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Returns the username of the player whose data is being saved. This is not necessarily an online player. | ||
*/ | ||
public function getPlayerName() : string{ | ||
return $this->playerName; | ||
} | ||
|
||
/** | ||
* Returns the player whose data is being saved, if online. | ||
* If null, this data is for an offline player (possibly just disconnected). | ||
*/ | ||
public function getPlayer() : ?Player{ | ||
return $this->player; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters