From 2b0ad36e14599b0a3047807fa125953da8d6d274 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 23 Jul 2023 11:31:33 +0300 Subject: [PATCH] Explosion performance improvements --- src/API/BlockAPI.php | 2 +- src/Player.php | 3 ++- src/PocketMinecraftServer.php | 7 ++++-- src/material/StaticBlock.php | 8 ++++++- src/world/Explosion.php | 42 +++++++++++++++++++---------------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index a466803c..9a19cd73 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -564,7 +564,7 @@ class BlockAPI{ $this->blockUpdate($pos->getSide(5), $type); } } - + public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ $type = (int) $type; if($delay < 0){ diff --git a/src/Player.php b/src/Player.php index 381249c9..5d4dbbea 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1278,7 +1278,6 @@ class Player{ if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ return; } - switch($packet->pid()){ case 0x01: break; @@ -1527,9 +1526,11 @@ class Player{ } break; case ProtocolInfo::MOVE_PLAYER_PACKET: + if($this->spawned === false){ break; } + if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index aceab1a9..518bed5c 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -74,7 +74,6 @@ class PocketMinecraftServer{ if(!defined("NO_THREADS")){ $this->asyncThread = new AsyncMultipleQueue(); } - console("[INFO] Loading extra.properties..."); $this->extraprops = new Config(DATA_PATH . "extra.properties", CONFIG_PROPERTIES, [ "version" => "5", @@ -220,7 +219,11 @@ class PocketMinecraftServer{ $this->interface->close(); if(!defined("NO_THREADS")){ - @$this->asyncThread->stop = true; + $this->asyncThread->synchronized(function ($t){ + $t->stop = true; + $t->notify(); + }, $this->asyncThread); + //@$this->asyncThread->stop = true; } } } diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index 51b0e7b2..d60dfc28 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -5,6 +5,7 @@ class StaticBlock { const DEFAULT_SLIPPERINESS = 0.6; + const DEFAULT_HARDNESS = 0; private static $NULL_BOUNDS; public static $isSolid = []; public static $isTransparent = []; @@ -15,6 +16,7 @@ class StaticBlock public static $isLiquid = []; public static $isFullBlock = []; + public static $hardness = []; public static $slipperiness = []; public static $boundingBoxes = []; public static function init(){ @@ -31,9 +33,9 @@ class StaticBlock self::$hasPhysics[$b->getID()] = $b->hasPhysics; self::$isLiquid[$b->getID()] = $b->isLiquid; self::$isFullBlock[$b->getID()] = $b->isFullBlock; - self::$slipperiness[$b->getID()] = $b->slipperiness; self::$boundingBoxes[$b->getID()] = $b->boundingBox; + self::$hardness[$b->getID()] = $b->getHardness(); } } @@ -41,6 +43,10 @@ class StaticBlock return nullsafe(Block::$class[$id], Block::$class[0]); } + public static function getHardness($id){ + return nullsafe(self::$hardness[$id], StaticBlock::DEFAULT_HARDNESS); + } + public static function getBoundingBoxForBlockCoords($id, $x, $y, $z){ /**@var AxisAlignedBB $bb*/ $bb = nullsafe(self::$boundingBoxes[$id], false); diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 443b6aeb..e329204f 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -68,21 +68,24 @@ class Explosion{ $blockID = $BIDM[0]; $blockMeta = $BIDM[1]; if($blockID > 0){ - $block = BlockAPI::get($blockID, $blockMeta); - $block->x = $vBlock->x; - $block->y = $vBlock->y; - $block->z = $vBlock->z; - $block->level = $this->level; - $index = ($block->x << 15) + ($block->z << 7) + $block->y; - if($block instanceof LiquidBlock && !isset($visited[$index])){ - ServerAPI::request()->api->block->scheduleBlockUpdate($block, 5, BLOCK_UPDATE_NORMAL); + $index = ($vBlock->x << 15) + ($vBlock->z << 7) + $vBlock->y; + + if(StaticBlock::getIsLiquid($blockID) && !isset($visited[$index])){ + ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($vBlock->x, $vBlock->y, $vBlock->z, $this->level), 5, BLOCK_UPDATE_NORMAL); $visited[$index] = true; } - $blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen; + + $blastForce -= (StaticBlock::getHardness($blockID) / 5 + 0.3) * $this->stepLen; if($blastForce > 0){ - $index = ($block->x << 15) + ($block->z << 7) + $block->y; + $index = ($vBlock->x << 15) + ($vBlock->z << 7) + $vBlock->y; if(!isset($this->affectedBlocks[$index])){ - $this->affectedBlocks[$index] = $block; + $this->affectedBlocks[$index] = [ + "x" => $vBlock->x, + "y" => $vBlock->y, + "z" => $vBlock->z, + "id" => $blockID, + "meta" => $blockMeta + ]; } } } @@ -101,27 +104,28 @@ class Explosion{ $entity->harm($damage, "explosion"); } - foreach($this->affectedBlocks as $block){ - if($block instanceof TNTBlock){ + foreach($this->affectedBlocks as $blockA){ + if($blockA["id"] === TNT){ $data = [ - "x" => $block->x + 0.5, - "y" => $block->y + 0.5, - "z" => $block->z + 0.5, + "x" => $blockA["x"] + 0.5, + "y" => $blockA["y"] + 0.5, + "z" => $blockA["z"] + 0.5, "power" => 4, "fuse" => mt_rand(10, 30), //0.5 to 1.5 seconds ]; $e = $server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data); $server->api->entity->spawnToAll($e); }elseif(mt_rand(0, 10000) < ((1 / $this->size) * 10000)){ + $block = BlockAPI::get($blockA["id"], $blockA["meta"], new Position($blockA["x"], $blockA["y"], $blockA["z"], $this->level)); $dropz = $block->getDrops($this->air, $this->nullPlayer); if(is_array($dropz)){ foreach($dropz as $drop){ - $server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), BlockAPI::getItem($drop[0], $drop[1], $drop[2])); //id, meta, count + $server->api->entity->drop(new Position($blockA["x"] + 0.5, $blockA["y"], $blockA["z"] + 0.5, $this->level), BlockAPI::getItem($drop[0], $drop[1], $drop[2])); //id, meta, count } } } - $this->level->setBlock($block, new AirBlock(), true); - $send[] = $block->subtract($source); + $this->level->fastSetBlockUpdate($blockA["x"], $blockA["y"], $blockA["z"], 0, 0); + $send[] = new Vector3($blockA["x"] - $source->x, $blockA["y"] - $source->y, $blockA["z"] - $source->z); } $pk = new ExplodePacket; $pk->x = $this->source->x;