From edc487f1f34cc49a2584e3582087fab6d38d12e7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 24 Feb 2024 16:50:10 +0300 Subject: [PATCH] better bed spawnpoint position --- src/Player.php | 10 +++++++++- src/material/block/misc/BedBlock.php | 29 +++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 3471d5f4..1adc22f5 100755 --- a/src/Player.php +++ b/src/Player.php @@ -147,7 +147,15 @@ class Player{ if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } - $this->setSpawn($pos); + + $spawnPoint = BedBlock::findStandUpPosition($this->level, $pos->x, $pos->y, $pos->z); + console($spawnPoint); + if($spawnPoint == null) $spawnPoint = $pos->add(0.5, 1, 0.5); + else{ + $spawnPoint->x += 0.5; + $spawnPoint->z += 0.5; + } + $this->setSpawn($spawnPoint); return true; } diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index 1a12b74a..00627812 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -8,7 +8,12 @@ class BedBlock extends TransparentBlock{ 2 => 2, 3 => 5, ]; - + const HEAD_DIRECTION_OFFSETS = [ + [0, 1], + [-1, 0], + [0, -1], + [1, 0] + ]; public function __construct($type = 0){ parent::__construct(BED_BLOCK, $type, "Bed Block"); $this->isActivable = true; @@ -16,6 +21,28 @@ class BedBlock extends TransparentBlock{ $this->hardness = 1; } + public static function findStandUpPosition(Level $level, $x, $y, $z){ + $blockMeta = $level->level->getBlockDamage($x, $y, $z); + $direction = $blockMeta & 0x3; + for($v7 = 0; $v7 <= 1; ++$v7){ + $minX = $x - self::HEAD_DIRECTION_OFFSETS[$direction][0] * $v7 - 1; + $minZ = $z - self::HEAD_DIRECTION_OFFSETS[$direction][1] * $v7 - 1; + $maxX = $minX + 2; + $maxZ = $minZ + 2; + + for($xCheck = $minX; $xCheck <= $maxX; ++$xCheck){ + for($zCheck = $minZ; $zCheck <= $maxZ; ++$zCheck){ + $idCheck = $level->level->getBlockID($xCheck, $y - 1, $zCheck); + if(!StaticBlock::getIsTransparent($idCheck) && $level->level->getBlockID($xCheck, $y, $zCheck) == 0 && $level->level->getBlockID($xCheck, $y + 1, $zCheck) == 0){ + return new Vector3($xCheck, $y, $zCheck); + } + } + } + } + + return null; + } + public function onActivate(Item $item, Player $player){ if(ServerAPI::request()->api->time->getPhase($player->level) !== "night"){ $pk = new ChatPacket;