Implemented Respawn Anchor (#6646)

PlayerRespawnAnchorUseEvent is also added with options SET_SPAWN and EXPLODE, which allows plugins to customise the outcome of using the anchor in PM, which currently doesn't support dimensions. The event is also cancellable.
This commit is contained in:
Adam
2025-05-28 02:57:28 +06:00
committed by GitHub
parent 059f4ee7bf
commit bf33a625c9
19 changed files with 711 additions and 14 deletions

View File

@ -26,6 +26,7 @@ namespace pocketmine\player;
use pocketmine\block\BaseSign;
use pocketmine\block\Bed;
use pocketmine\block\BlockTypeTags;
use pocketmine\block\RespawnAnchor;
use pocketmine\block\UnknownBlock;
use pocketmine\block\VanillaBlocks;
use pocketmine\command\CommandSender;
@ -136,6 +137,7 @@ use pocketmine\world\sound\EntityAttackNoDamageSound;
use pocketmine\world\sound\EntityAttackSound;
use pocketmine\world\sound\FireExtinguishSound;
use pocketmine\world\sound\ItemBreakSound;
use pocketmine\world\sound\RespawnAnchorDepleteSound;
use pocketmine\world\sound\Sound;
use pocketmine\world\World;
use pocketmine\YmlServerProperties;
@ -2538,6 +2540,21 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
}
$this->logger->debug("Respawn position located, completing respawn");
$ev = new PlayerRespawnEvent($this, $safeSpawn);
$spawnPosition = $ev->getRespawnPosition();
$spawnBlock = $spawnPosition->getWorld()->getBlock($spawnPosition);
if($spawnBlock instanceof RespawnAnchor){
if($spawnBlock->getCharges() > 0){
$spawnPosition->getWorld()->setBlock($spawnPosition, $spawnBlock->setCharges($spawnBlock->getCharges() - 1));
$spawnPosition->getWorld()->addSound($spawnPosition, new RespawnAnchorDepleteSound());
}else{
$defaultSpawn = $this->server->getWorldManager()->getDefaultWorld()?->getSpawnLocation();
if($defaultSpawn !== null){
$this->setSpawn($defaultSpawn);
$ev->setRespawnPosition($defaultSpawn);
$this->sendMessage(KnownTranslationFactory::tile_respawn_anchor_notValid()->prefix(TextFormat::GRAY));
}
}
}
$ev->call();
$realSpawn = Position::fromObject($ev->getRespawnPosition()->add(0.5, 0, 0.5), $ev->getRespawnPosition()->getWorld());