From 933b92f41e95fb9788129cc0ff40ac9a8740e7ec Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 12 Apr 2025 18:49:22 +0300 Subject: [PATCH 1/3] falling sand will schedule update onplace --- src/entity/object/FallingSand.php | 59 +++++++++++++++-------------- src/material/block/GenericBlock.php | 40 ++++++++++++------- 2 files changed, 57 insertions(+), 42 deletions(-) diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 3271929c..517d4ca7 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -23,36 +23,37 @@ class FallingSand extends Entity{ public function update($now){ if($this->closed) return; - if( $this->data["Tile"] == AIR) $this->close(); - else { - $this->lastX = $this->x; - $this->lastY = $this->y; - $this->lastZ = $this->z; - ++$this->fallTime; - $this->speedY -= 0.04; - $this->move($this->speedX, $this->speedY, $this->speedZ); - - $this->speedX *= 0.98; - $this->speedY *= 0.98; - $this->speedZ *= 0.98; - - $x = floor($this->x); - $y = floor($this->y); - $z = floor($this->z); - - if($this->onGround){ - $this->speedX *= 0.7; - $this->speedZ *= 0.7; - $this->speedY *= -0.5; + if( $this->data["Tile"] == AIR) { + $this->close(); + return; + } + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + ++$this->fallTime; + $this->speedY -= 0.04; + $this->move($this->speedX, $this->speedY, $this->speedZ); + + $this->speedX *= 0.98; + $this->speedY *= 0.98; + $this->speedZ *= 0.98; + + $x = floor($this->x); + $y = floor($this->y); + $z = floor($this->z); + + if($this->onGround){ + $this->speedX *= 0.7; + $this->speedZ *= 0.7; + $this->speedY *= -0.5; + $this->close(); + //TODO vanilla-like checking? + $this->level->fastSetBlockUpdate($x, $y, $z, $this->data["Tile"], 0, true); //TODO add metadata + }elseif(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ + //TODO vanilla-like checking? + if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ $this->close(); - //TODO vanilla-like checking? - $this->level->fastSetBlockUpdate($x, $y, $z, $this->data["Tile"], 0, true); //TODO add metadata - }elseif(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ - //TODO vanilla-like checking? - if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ - $this->close(); - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata - } + ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata } } } diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index a4867017..e0780dd1 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -47,6 +47,32 @@ class GenericBlock extends Block{ return $this->level->setBlock($this, new AirBlock(), true, false, true); } + public static function onPlace(Level $level, $x, $y, $z){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if(StaticBlock::getHasPhysics($id)){ + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, 2); //tick delay == 2 + }else{ + parent::onPlace($level, $x, $y, $z); + } + } + + public static function onUpdate(Level $level, $x, $y, $z, $type){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + $down = $level->level->getBlockID($x, $y - 1, $z); + if($down == AIR || StaticBlock::getIsLiquid($down)){ + $data = array( + "x" => $x + 0.5, + "y" => $y - 0.5, + "z" => $z + 0.5, + "Tile" => $id, + ); + $server = ServerAPI::request(); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); + $server->api->entity->spawnToAll($e); + } + } + /** * @param integer $type * @@ -55,19 +81,7 @@ class GenericBlock extends Block{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ [$id, $meta] = $level->level->getBlock($x, $y, $z); if(StaticBlock::getHasPhysics($id)){ //TODO move from here to different class - $down = $level->level->getBlockID($x, $y - 1, $z); - if($down == AIR || StaticBlock::getIsLiquid($down)){ - $data = array( - "x" => $x + 0.5, - "y" => $y - 0.5, - "z" => $z + 0.5, - "Tile" => $id, - ); - $server = ServerAPI::request(); - $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); - $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); - $server->api->entity->spawnToAll($e); - } + static::onUpdate($level, $x, $y, $z, BLOCK_UPDATE_NORMAL); } } From 0b7fd741754ed83656f00a967b36636535d1b305 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 12 Apr 2025 21:14:09 +0300 Subject: [PATCH 2/3] better body placement checks --- src/API/BlockAPI.php | 13 ++++++++++--- src/material/block/nonfull/FenceBlock.php | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 42a067ce..97551224 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -501,9 +501,16 @@ class BlockAPI{ $hand->position($block); //$face = -1; } - - if($hand->isSolid === true and $block->boundingBox->intersectsWith($player->entity->boundingBox) && ($hand->getID() != BED_BLOCK && $hand->getID() != CARPET)){ - return $this->cancelAction($block, $player, false); //Entity in block + + if($hand->isSolid === true && ($hand->getID() != BED_BLOCK && $hand->getID() != CARPET)){ + $aabb = $hand->getAABB($block->level, $block->x, $block->y, $block->z); + $playerbb = $player->entity->boundingBox; + //$aabb->intersectsWith($playerbb) is not good enough for this case + //minY+0.21 is used to stop sneaking from triggering action cancelling + if(($aabb->maxX > $playerbb->minX && $aabb->minX < $playerbb->maxX) && ($aabb->maxY > ($playerbb->minY+0.21) && $aabb->minY < $playerbb->maxY) && ($aabb->maxZ > $playerbb->minZ && $aabb->minZ < $playerbb->maxZ)){ + return $this->cancelAction($block, $player, false); //Entity in block + } + } if($this->server->api->dhandle("player.block.place", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ diff --git a/src/material/block/nonfull/FenceBlock.php b/src/material/block/nonfull/FenceBlock.php index b9ceb18b..98442208 100644 --- a/src/material/block/nonfull/FenceBlock.php +++ b/src/material/block/nonfull/FenceBlock.php @@ -17,6 +17,19 @@ class FenceBlock extends TransparentBlock{ return true; } + public static function getAABB(Level $level, $x, $y, $z){ + $v9 = self::canConnectTo($level, $x, $y, $z - 1); + $v10 = self::canConnectTo($level, $x, $y, $z + 1); + $v11 = self::canConnectTo($level, $x - 1, $y, $z); + $v12 = self::canConnectTo($level, $x + 1, $y, $z); + $minX = $v11 ? 0 : 0.375; + $minZ = $v9 ? 0 : 0.375; + $maxZ = $v10 ? 1 : 0.625; + $maxX = !$v12 ? 0.625 : 1; + StaticBlock::setBlockBounds(static::$blockID, $minX, 0, $minZ, $maxX, 1.5, $maxZ); + return parent::getAABB($level, $x, $y, $z); + } + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ $v8 = self::canConnectTo($level, $x, $y, $z - 1); $v9 = self::canConnectTo($level, $x, $y, $z + 1); From 24269bc35926ae1692e28f43576647d7058631b2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 13 Apr 2025 02:44:51 +0300 Subject: [PATCH 3/3] sendBuffer will add packet to recoveryQueue which should help to prevent the loss of some packets --- src/Player.php | 12 +++--------- src/pmf/PMFLevel.php | 13 +++++++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Player.php b/src/Player.php index 6bda217f..87120d29 100755 --- a/src/Player.php +++ b/src/Player.php @@ -420,6 +420,8 @@ class Player{ public function sendBuffer(){ if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket){ $this->buffer->seqNumber = $this->counter[0]++; + $this->recoveryQueue[$this->buffer->seqNumber] = $this->buffer; + $this->buffer->sendtime = microtime(true); $this->send($this->buffer); } $this->bufferLen = 0; @@ -963,15 +965,6 @@ class Player{ } if($m !== ""){ - - /*$pk = new LevelEventPacket(); - $pk->evid = LevelEventPacket::EVENT_OPEN_DOOR_SOUND; - $pk->x = $this->entity->x; - $pk->y = $this->entity->y; - $pk->z = $this->entity->z; - $pk->data = 0; - $this->dataPacket($pk);*/ - $pk = new MessagePacket; $pk->source = ($author instanceof Player) ? $author->username : $author; $pk->message = TextFormat::clean($m); //Colors not implemented :( @@ -2800,6 +2793,7 @@ class Player{ switch($packet->pid()){ case RakNetInfo::NACK: foreach($packet->packets as $count){ + if(isset($this->recoveryQueue[$count])){ $this->resendQueue[$count] =& $this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 6f8bd200..f00388e6 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -353,13 +353,14 @@ class PMFLevel extends PMF{ } public function getBlockID($x, $y, $z){ + if($y > 127 || $y < 0){ + return 0; + } + if($x < 0 || $x > 255 || $z < 0 || $z > 255){ return INVISIBLE_BEDROCK; } - if($y > 127 || $y < 0){ - return 0; - } $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; @@ -460,12 +461,12 @@ class PMFLevel extends PMF{ } public function getBlock($x, $y, $z){ - if($x < 0 || $x > 255 || $z < 0 || $z > 255){ - return [INVISIBLE_BEDROCK, 0]; - } if($y < 0 || $y > 127){ return [AIR, 0]; } + if($x < 0 || $x > 255 || $z < 0 || $z > 255){ + return [INVISIBLE_BEDROCK, 0]; + } $X = $x >> 4; $Z = $z >> 4;