From a8bf2f9ce97f2c6b428d0d0af71e41592fad7107 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 16:16:31 +0300 Subject: [PATCH 001/146] fixed dandelions dropping cyan flowers on neighbor change, add /reply(/r) command --- src/API/ChatAPI.php | 72 +++++++++++++-------- src/Player.php | 1 + src/material/block/plant/DandelionBlock.php | 2 +- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 69fade6d..eb29459b 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -3,18 +3,21 @@ class ChatAPI{ private $server; - + public $lastTells = []; function __construct(){ $this->server = ServerAPI::request(); } public function init(){ $this->server->api->console->register("tell", " ", [$this, "commandHandler"]); + $this->server->api->console->register("reply", "", [$this, "commandHandler"]); $this->server->api->console->register("me", "", [$this, "commandHandler"]); $this->server->api->console->register("say", "", [$this, "commandHandler"]); $this->server->api->console->cmdWhitelist("tell"); + $this->server->api->console->cmdWhitelist("reply"); $this->server->api->console->cmdWhitelist("me"); $this->server->api->console->alias("msg", "tell"); + $this->server->api->console->alias("r", "reply"); } /** @@ -26,13 +29,11 @@ class ChatAPI{ * @return string */ public function commandHandler($cmd, $params, $issuer, $alias){ - $output = ""; switch($cmd){ case "say": $s = implode(" ", $params); if(trim($s) == ""){ - $output .= "Usage: /say \n"; - break; + return "Usage: /say "; } $sender = ($issuer instanceof Player) ? "Server" : ucfirst($issuer); if(Utils::hasEmoji($s)) return "Your message contains illegal characters!"; @@ -41,8 +42,7 @@ class ChatAPI{ case "me": $s = implode(" ", $params); if(trim($s) == ""){ - $output .= "Usage: /me \n"; - break; + return "Usage: /me "; } if(!($issuer instanceof Player)){ if($issuer === "rcon"){ @@ -58,42 +58,58 @@ class ChatAPI{ $this->broadcast("* $sender $msg"); break; case "tell": - if(!isset($params[0]) or !isset($params[1])){ - $output .= "Usage: /$cmd \n"; - break; - } - if(!($issuer instanceof Player)){ - $sender = ucfirst($issuer); - }else{ - $sender = $issuer->username; - } + if(!isset($params[0]) or !isset($params[1])) return "Usage: /$cmd \n"; + + if(!($issuer instanceof Player)) $sender = ucfirst($issuer); + else $sender = $issuer->username; + $n = array_shift($params); $target = $this->server->api->player->get($n); if($target instanceof Player){ $target = $target->username; }else{ $target = strtolower($n); - if($target === "server" or $target === "console" or $target === "rcon"){ + if($target === "server" || $target === "console" || $target === "rcon"){ $target = "Console"; }else{ - return "The player is offline."; + return "$target is offline."; } } - if(strtolower($target) === strtolower($issuer)){ - return "You can't send message to yourself."; + if(strtolower($target) === strtolower($sender)) return "You can't send message to yourself."; + + $mes = implode(" ", $params); + if(Utils::hasEmoji($mes)) return "Your message contains illegal characters!"; + + if($target !== "Console" && $target !== "Rcon") $this->sendTo(false, "$sender whispers to you: $mes", $target); + if($target === "Console" || $sender === "Console") console("[INFO] $sender whispers to $target: $mes"); + + + $this->lastTells[strtolower($target)] = strtolower($sender); + $this->lastTells[strtolower($sender)] = strtolower($target); + + return "You're whispering to $target: $mes"; + case "reply": + if(!($issuer instanceof Player)) $sender = ucfirst($issuer); + else $sender = $issuer->username; + + if(!isset($this->lastTells[strtolower($sender)])) return "You have no one to reply to."; + $target = $this->lastTells[strtolower($sender)]; + if($target !== "server" && $target !== "console" && $target !== "rcon"){ + if(!($this->server->api->player->get($target) instanceof Player)){ + return "$target is offline."; + } } $mes = implode(" ", $params); if(Utils::hasEmoji($mes)) return "Your message contains illegal characters!"; - $output .= "You're whispering to " . $target . ": " . $mes . "\n"; - if($target !== "Console" and $target !== "Rcon"){ - $this->sendTo(false, $sender . " whispers to you: " . $mes, $target); - } - if($target === "Console" or $sender === "Console"){ - console("[INFO] " . $sender . " whispers to " . $target . ": " . $mes); - } - break; + + if($target !== "Console" && $target !== "Rcon") $this->sendTo(false, "$sender whispers to you: $mes", $target); + if($target === "Console" || $sender === "Console") console("[INFO] $sender whispers to $target: $mes"); + + $this->lastTells[strtolower($target)] = strtolower($sender); + $this->lastTells[strtolower($sender)] = strtolower($target); + + return "You're whispering to $target: $mes"; } - return $output; } /** diff --git a/src/Player.php b/src/Player.php index 9279f6c0..5a942b95 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1467,6 +1467,7 @@ class Player{ $this->chunkCount = []; $this->craftingItems = []; $this->received = []; + unset($this->server->api->chat->lastTells[$this->iusername]); //$this->entity->close(); } } diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index 57f5ef64..236fa778 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -22,7 +22,7 @@ class DandelionBlock extends FlowableBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $downId = $level->level->getBlockID($x, $y - 1, $z); if(StaticBlock::getIsTransparent($downId) and $downId !== FARMLAND){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(DANDELION)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } From 236e98be23c7f0d0654ae9af40703e4e4e89516a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 18:26:36 +0300 Subject: [PATCH 002/146] mtu changed back to 1100 due to issues some player may have on some servers --- src/Player.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 5a942b95..afc36978 100755 --- a/src/Player.php +++ b/src/Player.php @@ -101,7 +101,7 @@ class Player{ */ public function __construct($clientID, $ip, $port, $MTU){ $this->bigCnt = 0; - $this->MTU = min($MTU, 1492); + $this->MTU = min($MTU, 1100); $this->server = ServerAPI::request(); $this->lastBreak = microtime(true); $this->clientID = $clientID; From 9e9dd532a3ce78d1cc2de5cdf9dea9288a18ec4e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 20:37:03 +0300 Subject: [PATCH 003/146] fixed server not checking crafting results if crafting was done in inventory --- src/Player.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Player.php b/src/Player.php index afc36978..3dd93237 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2195,7 +2195,6 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; $this->craftingType = CraftingRecipes::TYPE_INVENTORY; - $packet->eid = $this->eid; for($i = 0; $i < 4; ++$i){ $s = $packet->slots[$i]; @@ -2205,21 +2204,21 @@ class Player{ $s = BlockAPI::getItem($s + 256, 0, 1); } $slot = $this->armor[$i]; - if($slot->getID() !== AIR and $s->getID() === AIR){ + + if($slot->getID() !== AIR && $s->getID() === AIR){ $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); - }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ + }elseif($s->getID() !== AIR && $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ $item = $this->armor[$i]; $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, $item, false); }else{ $packet->slots[$i] = 255; } - } $this->sendArmor(); if($this->entity->inAction === true){ @@ -2497,6 +2496,7 @@ class Player{ $this->craftingItems = []; $this->craftingType = CraftingRecipes::TYPE_INVENTORY; } + if(!isset($this->windows[$packet->windowid])){ break; } @@ -2731,10 +2731,14 @@ class Player{ $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); - if(!is_array($cc) && $this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ - $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); - if(!is_array($cc)){ - return; //recipe not found + if(!is_array($cc)){ + if($this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ + $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); + if(!is_array($cc)){ + return; //recipe not found + } + }else{ + return; } } From 15740c3e538a6626224de2432d726631083b524b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 11 May 2025 03:15:41 +0300 Subject: [PATCH 004/146] tweaked crafting --- src/Player.php | 7 ++- src/recipes/CraftingRecipes.php | 80 ++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/Player.php b/src/Player.php index 3dd93237..72e5bedc 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2722,15 +2722,18 @@ class Player{ $results = []; foreach($this->toCraft as $i => $slotz){ - $results[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + if(!isset($results[$i])) $results[$i] = []; + $results[$i >> 16][] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } $ingridients = []; foreach($this->craftingItems as $i => $slotz){ - $ingridients[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + if(!isset($ingridients[$i])) $ingridients[$i] = []; + $ingridients[$i >> 16][] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); + if(!is_array($cc)){ if($this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index d4678234..55be3ba5 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -295,14 +295,31 @@ class CraftingRecipes{ $recipeString = ""; $resultString = ""; - foreach($recipeItems as $item){ - $recipeString .= $item[0] . "x" . $item[2] . ","; + $recipeItemsIdCnt = []; + $craftItemsIdCnt = []; + + foreach($recipeItems as $id => $items){ + foreach($items as $meta => $item){ + if(!isset($recipeItemsIdCnt[$item[0]])) $recipeItemsIdCnt[$item[0]] = 0; + $recipeItemsIdCnt[$item[0]] += $item[2]; + } } - foreach($craftItems as $item){ - $resultString .= "{$item[0]}x{$item[2]},"; + foreach($craftItems as $id => $items){ + foreach($items as $meta => $item){ + if(!isset($craftItemsIdCnt[$item[0]])) $craftItemsIdCnt[$item[0]] = 0; + $craftItemsIdCnt[$item[0]] += $item[2]; + } + } + + foreach($recipeItemsIdCnt as $id => $cnt){ + $recipeString .= "{$id}x{$cnt},"; + } + foreach($craftItemsIdCnt as $id => $cnt){ + $resultString .= "{$id}x{$cnt},"; } $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); + $server = ServerAPI::request(); $result = $server->query("SELECT id FROM recipes WHERE type == " . $type . " AND recipe == '" . $recipeString . "';"); if($result instanceof SQLite3Result){ @@ -310,16 +327,36 @@ class CraftingRecipes{ while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ $continue = true; $recipe = CraftingRecipes::$recipes[$r[0]]; + foreach($recipe[0] as $item){ //check ingridients - if(!isset($recipeItems[$item[0]])){ + if(!isset($recipeItems[$item[0]])){ //is item id in recipe $continue = false; break; } $oitem = $recipeItems[$item[0]]; - if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ - $continue = false; - break; + if($item[1] === false){ //recipe allows to use any meta + $totalcnt = 0; + foreach($oitem as $it) $totalcnt += $it[2]; + + if($item[2] !== $totalcnt){ + $continue = false; + break; + } + }else{ //recipe expects specific meta + $matchfound = false; + $matchcnt = 0; + foreach($oitem as $it){ + if($item[1] === $it[1]){ + $matchfound = true; + $matchcnt = $it[2]; + break; + } + } + if(!$matchfound || $item[2] !== $matchcnt){ //meta match not found or count is different + $continue = false; + break; + } } } if($continue === false){ @@ -327,15 +364,34 @@ class CraftingRecipes{ continue; } foreach($recipe[1] as $item){ //check results - if(!isset($craftItems[$item[0]])){ + if(!isset($craftItems[$item[0]])){ //is item id in recipe $continue = false; break; } $oitem = $craftItems[$item[0]]; - if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ - $continue = false; - break; + if($item[1] === false){ //recipe allows to use any meta + $totalcnt = 0; + foreach($oitem as $it) $totalcnt += $it[2]; + + if($item[2] !== $totalcnt){ + $continue = false; + break; + } + }else{ //recipe expects specific meta + $matchfound = false; + $matchcnt = 0; + foreach($oitem as $it){ + if($item[1] === $it[1]){ + $matchfound = true; + $matchcnt = $it[2]; + break; + } + } + if(!$matchfound || $item[2] !== $matchcnt){ //meta match not found or count is different + $continue = false; + break; + } } } if($continue === false){ From c4b7012fe9775287094e00db8aeab75a3f6e4dab Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 11 May 2025 18:04:32 +0300 Subject: [PATCH 005/146] fixed break time of wooden slabs and stairs --- src/material/block/nonfull/WoodSlabBlock.php | 4 +++- src/material/block/nonfull/stairs/WoodStairsBlock.php | 4 +++- src/material/block/solid/DoubleWoodSlabBlock.php | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/material/block/nonfull/WoodSlabBlock.php b/src/material/block/nonfull/WoodSlabBlock.php index d39f0931..776dc334 100644 --- a/src/material/block/nonfull/WoodSlabBlock.php +++ b/src/material/block/nonfull/WoodSlabBlock.php @@ -90,7 +90,9 @@ class WoodSlabBlock extends TransparentBlock{ if(($player->gamemode & 0x01) === 0x01){ return 0.20; } - return match ($item->isAxe()) { + if(!$item->isAxe()) return 3; + + return match ($item->getLevel()) { 5 => 0.4, 4 => 0.5, 3 => 0.75, diff --git a/src/material/block/nonfull/stairs/WoodStairsBlock.php b/src/material/block/nonfull/stairs/WoodStairsBlock.php index 0421dac4..e8b5d35d 100644 --- a/src/material/block/nonfull/stairs/WoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/WoodStairsBlock.php @@ -10,7 +10,9 @@ class WoodStairsBlock extends StairBlock{ if(($player->gamemode & 0x01) === 0x01){ return 0.20; } - return match ($item->isAxe()) { + if(!$item->isAxe()) return 3; + + return match ($item->getLevel()) { 5 => 0.4, 4 => 0.5, 3 => 0.75, diff --git a/src/material/block/solid/DoubleWoodSlabBlock.php b/src/material/block/solid/DoubleWoodSlabBlock.php index 7ff69ef1..74316ede 100644 --- a/src/material/block/solid/DoubleWoodSlabBlock.php +++ b/src/material/block/solid/DoubleWoodSlabBlock.php @@ -18,7 +18,9 @@ class DoubleWoodSlabBlock extends SolidBlock{ if(($player->gamemode & 0x01) === 0x01){ return 0.20; } - return match ($item->isAxe()) { + if(!$item->isAxe()) return 3; + + return match ($item->getLevel()) { 5 => 0.4, 4 => 0.5, 3 => 0.75, From cf25df788fe5c7625f196b3ebb01f47fcf291c70 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 06:22:43 +0300 Subject: [PATCH 006/146] fixed block updates not being unscheduled when level is unloaded --- src/API/BlockAPI.php | 55 +++++++++++++++++--------------------------- src/world/Level.php | 15 ++++++------ 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ddd309d9..9e38ace3 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -430,7 +430,7 @@ class BlockAPI{ $pk->meta = $block->getMetadata(); $player->dataPacket($pk); if($send === true){ - $player->sendInventorySlot($player->slot); + $player->sendInventory(); } return false; } @@ -571,6 +571,18 @@ class BlockAPI{ $this->blockUpdate($pos->getSide(5), $type); } } + + public function removeAllBlockUpdates(Level $level){ + $update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE level = '{$level->getName()}';"); + if($update instanceof SQLite3Result){ + while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){ + $index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"]; + unset($this->scheduledUpdates[$index]); + } + $this->server->query("DELETE FROM blockUpdates WHERE level = '{$level->getName()}';"); + } + } + public function scheduleBlockUpdateXYZ(Level $level, $x, $y, $z, $type = BLOCK_UPDATE_SCHEDULED, $delay = false){ $type = (int) $type; if($delay < 0){ @@ -588,41 +600,9 @@ class BlockAPI{ } public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ - $type = (int) $type; - if($delay < 0){ - return false; - } - - $index = $pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . $type; - $delay = microtime(true) + $delay * 0.05; - if(!isset($this->scheduledUpdates[$index])){ - $this->scheduledUpdates[$index] = $pos; - $this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES (" . $pos->x . ", " . $pos->y . ", " . $pos->z . ", '" . $pos->level->getName() . "', " . $type . ", " . $delay . ");"); - return true; - } - return false; + return $this->scheduleBlockUpdateXYZ($pos->level, $pos->x, $pos->y, $pos->z, $type, $delay); } - public function nextRandomUpdate(Position $pos){ - if(!isset($this->scheduledUpdates[$pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . BLOCK_UPDATE_RANDOM])){ - $time = microtime(true); - $offset = 0; - while(true){ - $t = $offset + Utils::getRandomUpdateTicks() * 0.05; - $update = $this->server->query("SELECT COUNT(*) FROM blockUpdates WHERE level = '" . $pos->level->getName() . "' AND type = " . BLOCK_UPDATE_RANDOM . " AND delay >= " . ($time + $t - 1) . " AND delay <= " . ($time + $t + 1) . ";"); - if($update instanceof SQLite3Result){ - $update = $update->fetchArray(SQLITE3_NUM); - if($update[0] < 3){ - break; - } - }else{ - break; - } - $offset += mt_rand(25, 75); - } - $this->scheduleBlockUpdate($pos, $t / 0.05, BLOCK_UPDATE_RANDOM); - } - } public function blockUpdateTick(){ $time = microtime(true); @@ -644,4 +624,11 @@ class BlockAPI{ } } } + + /** + * @deprecated random updates are handled differently now. + */ + public function nextRandomUpdate(Position $pos){ + + } } diff --git a/src/world/Level.php b/src/world/Level.php index fbec0a71..2f805f98 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -70,7 +70,13 @@ class Level{ } public function close(){ - $this->__destruct(); + if(isset($this->level)){ + $this->save(true, true, true, true); + $this->level->close(); + unset($this->level); + $this->server->api->block->removeAllBlockUpdates($this); + } + unset($this->mobSpawner->level); } public function isTopSolidBlocking($x, $y, $z){ @@ -269,12 +275,7 @@ class Level{ } public function __destruct(){ - if(isset($this->level)){ - $this->save(false, false, false, false); - $this->level->close(); - unset($this->level); - } - unset($this->mobSpawner->level); + $this->close(); } public function isDay(){ From cba572ece699a336e846b2114fd2fa270fb9500b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 06:40:24 +0300 Subject: [PATCH 007/146] force send block if action was cancelled --- src/API/BlockAPI.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 9e38ace3..933190ce 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -467,7 +467,7 @@ class BlockAPI{ if(($player->gamemode & 0x02) === 0x02){ //Adventure mode!! if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ - return $this->cancelAction($block, $player, false); + return $this->cancelAction($block, $player); } } @@ -486,14 +486,14 @@ class BlockAPI{ $hand = $item->getBlock(); $hand->position($block); }elseif($block->getID() === FIRE){ - $player->level->setBlock($block, new AirBlock(), true, false, true); + $player->level->fastSetBlockUpdate($block->x, $block->y, $block->z, 0, 0, true, false); return false; }else{ - return $this->cancelAction($block, $player, false); + return $this->cancelAction($block, $player); } if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){ - return $this->cancelAction($block, $player, false); + return $this->cancelAction($block, $player); } if($target->isReplaceable === true){ @@ -506,7 +506,7 @@ class BlockAPI{ $aabb = $hand->getAABB($block->level, $block->x, $block->y, $block->z); $playerbb = $player->entity->boundingBox; 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 + return $this->cancelAction($block, $player); //Entity in block } } @@ -514,7 +514,7 @@ class BlockAPI{ if($this->server->api->dhandle("player.block.place", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ return $this->cancelAction($block, $player); }elseif($hand->place($item, $player, $block, $target, $face, $fx, $fy, $fz) === false){ - return $this->cancelAction($block, $player, false); + return $this->cancelAction($block, $player); } if($hand->getID() === SIGN_POST or $hand->getID() === WALL_SIGN){ $t = $this->server->api->tile->addSign($player->level, $block->x, $block->y, $block->z); From 8b77b88e84d77109f7a37912c654a2675cb12106 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 23:10:14 +0300 Subject: [PATCH 008/146] crafting checking reworked --- src/API/BlockAPI.php | 10 ++ src/Player.php | 20 +-- src/PocketMinecraftServer.php | 1 - src/recipes/CraftingRecipes.php | 300 ++++++++++++-------------------- 4 files changed, 129 insertions(+), 202 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 933190ce..4748f7a1 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -341,6 +341,16 @@ class BlockAPI{ return $output; } + public static function blockIDFromString($str){ + if(defined(strtoupper($str))){ + $ida = explode(":", constant(strtoupper($str))); + if(count($ida) === 2){} + return (int) $ida[0]; + }else if(is_numeric($str)){ + return (int) $str; + } + } + public static function fromString($str, $multiple = false){ if($multiple === true){ $blocks = []; diff --git a/src/Player.php b/src/Player.php index 72e5bedc..d8ff8073 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1997,7 +1997,7 @@ class Player{ //nc: check item desync $heldItem = $this->getHeldItem(); if($heldItem->getID() != $packet->item || $heldItem->getMetadata() != $packet->meta){ - ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside and serverside when using item. Resending inventory."); + ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside({$packet->item} {$packet->meta}) and serverside({$heldItem->getID()} {$heldItem->getMetadata()}) when using item. Resending inventory."); $this->sendInventory(); if($packet->face >= 0 && $packet->face <= 5){ $target = $this->level->getBlock($blockVector); @@ -2722,28 +2722,16 @@ class Player{ $results = []; foreach($this->toCraft as $i => $slotz){ - if(!isset($results[$i])) $results[$i] = []; - $results[$i >> 16][] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + $results[$i] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } $ingridients = []; foreach($this->craftingItems as $i => $slotz){ - if(!isset($ingridients[$i])) $ingridients[$i] = []; - $ingridients[$i >> 16][] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + $ingridients[$i] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); - - if(!is_array($cc)){ - if($this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ - $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); - if(!is_array($cc)){ - return; //recipe not found - } - }else{ - return; - } - } + if(!is_array($cc)) return; if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){ $this->toCraft = []; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 9086b01e..de5a4bf9 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -146,7 +146,6 @@ class PocketMinecraftServer{ $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); $this->query("CREATE TABLE blockUpdates (level TEXT, x INTEGER, y INTEGER, z INTEGER, type INTEGER, delay NUMERIC);"); - $this->query("CREATE TABLE recipes (id INTEGER PRIMARY KEY, type NUMERIC, recipe TEXT);"); $this->query("PRAGMA synchronous = OFF;"); $this->preparedSQL->selectHandlers = $this->database->prepare("SELECT DISTINCT ID FROM handlers WHERE name = :name ORDER BY priority DESC;"); $this->preparedSQL->selectActions = $this->database->prepare("SELECT ID,code,repeat FROM actions WHERE last <= (:time - interval);"); diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 55be3ba5..ec2c8b4d 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -186,7 +186,7 @@ class CraftingRecipes{ "MOSSY_STONE:0x6=>STONE_WALL:1x6", "NETHER_BRICK:?x4=>NETHER_BRICKS:0x1", "NETHER_BRICKS:?x6=>NETHER_BRICKS_STAIRS:0x4", - "QUARTZ_BLOCK:0x2=>QUARTZ_BLOCK:2x2", + "QUARTZ_BLOCK:?x2=>QUARTZ_BLOCK:2x2", "QUARTZ_BLOCK:?x3=>SLAB:6x6", "SANDSTONE:0x6=>SANDSTONE_STAIRS:0x4", "SAND:?x4=>SANDSTONE:0x1", @@ -200,211 +200,141 @@ class CraftingRecipes{ "COBBLESTONE:?x6=>COBBLESTONE_STAIRS:0x4", ]; - /** - * Each element structure: - * * 0 - ingridients
- * * 1 - results
- * * 2 - recipe string
- * @var array[] - */ - private static $recipes = []; - + private static $craftingTableRecipes = []; + private static $inventoryRecipes = []; + private static $stoneCutterRecipes = []; + public static function init(){ - $server = ServerAPI::request(); - $id = 1; + foreach(CraftingRecipes::$small as $recipe){ - $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = self::TYPE_INVENTORY; //Type - CraftingRecipes::$recipes[$id] = $recipe; - ++$id; + CraftingRecipes::addRecipe($recipe, self::TYPE_INVENTORY); + CraftingRecipes::addRecipe($recipe, self::TYPE_CRAFTIGTABLE); } + foreach(CraftingRecipes::$big as $recipe){ - $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = self::TYPE_CRAFTIGTABLE; - CraftingRecipes::$recipes[$id] = $recipe; - ++$id; + CraftingRecipes::addRecipe($recipe, self::TYPE_CRAFTIGTABLE); } + foreach(CraftingRecipes::$stone as $recipe){ - $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = self::TYPE_STONECUTTER; - CraftingRecipes::$recipes[$id] = $recipe; - ++$id; - } - - foreach(CraftingRecipes::$recipes as $id => $recipe){ - $server->query("INSERT INTO recipes (id, type, recipe) VALUES (" . $id . ", " . $recipe[3] . ", '" . $recipe[2] . "');"); + CraftingRecipes::addRecipe($recipe, self::TYPE_STONECUTTER); } } - private static function parseRecipe($recipe){ + public static function fromString($str){ + [$idm, $cnt] = explode("x", $str); + [$id, $meta] = explode(":", $idm); + $id = BlockAPI::blockIDFromString($id); + + return [$id, ($meta == "?") ? "?" : ((int)$meta), (int)$cnt]; + } + + public static function addRecipe($recipe, $type){ [$ingridients, $results] = explode("=>", $recipe); - $recipeItems = []; - foreach(explode(",", $ingridients) as $item){ - $item = explode("x", $item); - $id = explode(":", $item[0]); - $meta = array_pop($id); - $id = $id[0]; - - $it = BlockAPI::fromString($id); - if(!isset($recipeItems[$it->getID()])){ - $recipeItems[$it->getID()] = [$it->getID(), $meta === "?" ? false : intval($meta) & 0xFFFF, intval($item[1])]; - }else{ - if($it->getMetadata() !== $recipeItems[$it->getID()][1]){ - $recipeItems[$it->getID()][1] = false; - } - $recipeItems[$it->getID()][2] += $it->count; - } + $results_arr = []; //indexed by id, must be rewritten in case some recipe will craft 2 items with same id but different metadata + foreach(explode(",", $results) as $res){ + [$id, $meta, $cnt] = self::fromString($res); + if($meta === "?") throw new RuntimeException("Unknown metadata in result when trying to add $recipe (type: $type)"); + $results_arr[$id] = "{$id}:{$meta}x{$cnt}"; } - ksort($recipeItems); + ksort($results_arr); + $result_index = implode(",", $results_arr); - $craftItems = []; - foreach(explode(",", $results) as $item){ - $item = explode("x", $item); - $id = explode(":", $item[0]); - $meta = array_pop($id); - $id = $id[0]; - $it = BlockAPI::fromString($id); - if(!isset($craftItems[$it->getID()])){ - $craftItems[$it->getID()] = [$it->getID(), intval($meta) & 0xffff, intval($item[1])]; - }else{ - if($it->getMetadata() !== $craftItems[$it->getID()][1]){ - $craftItems[$it->getID()][1] = false; - } - $craftItems[$it->getID()][2] += $it->count; - } - } - ksort($craftItems); - - $recipeString = ""; - $resultString = ""; - foreach($recipeItems as $item){ - $recipeString .= "{$item[0]}x{$item[2]},"; - } - foreach($craftItems as $item){ - $resultString .= "{$item[0]}x{$item[2]},"; + $ingridients_arr = []; + foreach(explode(",", $ingridients) as $resultstr){ + [$id, $meta, $cnt] = self::fromString($resultstr); + $ingridients_arr[] = [$id, $meta, $cnt]; } - $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); - - return [$recipeItems, $craftItems, $recipeString]; + switch($type){ + case self::TYPE_CRAFTIGTABLE: + $arr = &self::$craftingTableRecipes; + break; + case self::TYPE_INVENTORY: + $arr = &self::$inventoryRecipes; + break; + case self::TYPE_STONECUTTER: + $arr = &self::$stoneCutterRecipes; + break; + default: + throw new RuntimeException("Unknown type: {$type}"); + } + + if(!isset($arr[$result_index])){ + $arr[$result_index] = []; + } + $arr[$result_index][] = $ingridients_arr; } + /** + * Checks can craft some item + * + * @param array $craftItems items that will be crafted + * @param array $recipeItems items that will be consumed + * @param int $type craft type (CraftingRecipes::TYPE_INVENTORY, CraftingRecipes::TYPE_CRAFTIGTABLE, CraftingRecipes::TYPE_STONECUTTER) + * @return array|false recipe that will be used or false + */ public static function canCraft(array $craftItems, array $recipeItems, $type){ - ksort($recipeItems); - ksort($craftItems); - - $recipeString = ""; - $resultString = ""; - $recipeItemsIdCnt = []; - $craftItemsIdCnt = []; - - foreach($recipeItems as $id => $items){ - foreach($items as $meta => $item){ - if(!isset($recipeItemsIdCnt[$item[0]])) $recipeItemsIdCnt[$item[0]] = 0; - $recipeItemsIdCnt[$item[0]] += $item[2]; - } - } - foreach($craftItems as $id => $items){ - foreach($items as $meta => $item){ - if(!isset($craftItemsIdCnt[$item[0]])) $craftItemsIdCnt[$item[0]] = 0; - $craftItemsIdCnt[$item[0]] += $item[2]; - } + $craftIndexArr = []; + foreach($craftItems as $it){ + $craftIndexArr[$it[0]] = "{$it[0]}:{$it[1]}x{$it[2]}"; } + ksort($craftIndexArr); + $craftIndex = implode(",", $craftIndexArr); - foreach($recipeItemsIdCnt as $id => $cnt){ - $recipeString .= "{$id}x{$cnt},"; - } - foreach($craftItemsIdCnt as $id => $cnt){ - $resultString .= "{$id}x{$cnt},"; - } - - $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); - - $server = ServerAPI::request(); - $result = $server->query("SELECT id FROM recipes WHERE type == " . $type . " AND recipe == '" . $recipeString . "';"); - if($result instanceof SQLite3Result){ - $continue = true; - while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ - $continue = true; - $recipe = CraftingRecipes::$recipes[$r[0]]; - - foreach($recipe[0] as $item){ //check ingridients - if(!isset($recipeItems[$item[0]])){ //is item id in recipe - $continue = false; - break; - } - $oitem = $recipeItems[$item[0]]; - - if($item[1] === false){ //recipe allows to use any meta - $totalcnt = 0; - foreach($oitem as $it) $totalcnt += $it[2]; - - if($item[2] !== $totalcnt){ - $continue = false; - break; - } - }else{ //recipe expects specific meta - $matchfound = false; - $matchcnt = 0; - foreach($oitem as $it){ - if($item[1] === $it[1]){ - $matchfound = true; - $matchcnt = $it[2]; - break; - } - } - if(!$matchfound || $item[2] !== $matchcnt){ //meta match not found or count is different - $continue = false; - break; - } - } - } - if($continue === false){ - $continue = false; - continue; - } - foreach($recipe[1] as $item){ //check results - if(!isset($craftItems[$item[0]])){ //is item id in recipe - $continue = false; - break; - } - $oitem = $craftItems[$item[0]]; - - if($item[1] === false){ //recipe allows to use any meta - $totalcnt = 0; - foreach($oitem as $it) $totalcnt += $it[2]; - - if($item[2] !== $totalcnt){ - $continue = false; - break; - } - }else{ //recipe expects specific meta - $matchfound = false; - $matchcnt = 0; - foreach($oitem as $it){ - if($item[1] === $it[1]){ - $matchfound = true; - $matchcnt = $it[2]; - break; - } - } - if(!$matchfound || $item[2] !== $matchcnt){ //meta match not found or count is different - $continue = false; - break; - } - } - } - if($continue === false){ - $continue = false; - continue; - } - $continue = $recipe; + switch($type){ + case self::TYPE_CRAFTIGTABLE: + $arr = &self::$craftingTableRecipes; break; - } - }else{ - return true; + case self::TYPE_INVENTORY: + $arr = &self::$inventoryRecipes; + break; + case self::TYPE_STONECUTTER: + $arr = &self::$stoneCutterRecipes; + break; + default: + ConsoleAPI::error("Tried crafting recipe with unknown type {$type}!"); + return false; } - return $continue; + + if(!isset($arr[$craftIndex])) { + console("WAT $craftIndex"); + return false; //recipe not found + } + + foreach($arr[$craftIndex] as $ingridients){ + foreach($ingridients as $item){ + if($item[1] === "?"){ // any metadata is allowed + $needcnt = $item[2]; + foreach($recipeItems as $idmeta => $it){ + $id = $idmeta >> 16; + if($id != $item[0]) continue; + $needcnt -= $it[2]; + } + if($needcnt != 0) { + console("awa"); + goto skip_recipe; + } + }else{ + $exceptedIndex = ($item[0] << 16) | ($item[1] & 0xffff); + if(!isset($recipeItems[$exceptedIndex])) { + + console("awa1"); + goto skip_recipe; //dont check count if no idmeta pair is in ingridients + } + $it = $recipeItems[$exceptedIndex]; + if($it[0] != $item[0] || $it[1] != $item[1] || $it[2] != $item[2]) { + + console("awa2"); + goto skip_recipe; + } + } + } + //recipe is correct + return [$craftItems, $ingridients]; + + skip_recipe: + } + return false; } } From 9d82e2829737a8af035f8851ce3d82ba5c3b50d0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 23:16:43 +0300 Subject: [PATCH 009/146] fixed tileentities getting resetted when unloading level --- src/API/LevelAPI.php | 13 ------------- src/world/Level.php | 13 ++++++++++++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 3784363a..70a892a2 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -225,19 +225,6 @@ class LevelAPI{ return false; } console("[INFO] Unloading level \"" . $name . "\""); - $level->nextSave = PHP_INT_MAX; - $level->save(); - foreach($this->server->api->player->getAll($level) as $player){ - $player->teleport($this->server->spawn); - } - foreach($this->server->api->entity->getAll($level) as $entity){ - if($entity->class !== ENTITY_PLAYER){ - $entity->close(); - } - } - foreach($this->server->api->tile->getAll($level) as $tile){ - $tile->close(); - } $level->close(); unset($this->levels[$name]); return true; diff --git a/src/world/Level.php b/src/world/Level.php index 2f805f98..cd677076 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -72,9 +72,20 @@ class Level{ public function close(){ if(isset($this->level)){ $this->save(true, true, true, true); + $this->server->api->block->removeAllBlockUpdates($this); + foreach($this->server->api->player->getAll($this) as $player){ + $player->teleport($this->server->spawn); + } + foreach($this->entityList as $entity){ + if($entity->class !== ENTITY_PLAYER){ + $entity->close(); + } + } + foreach($this->server->api->tile->getAll($this) as $tile){ + $tile->close(); + } $this->level->close(); unset($this->level); - $this->server->api->block->removeAllBlockUpdates($this); } unset($this->mobSpawner->level); } From 5458c049835881f9f2e9475be8a9a2fffe3ac013 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 23:54:18 +0300 Subject: [PATCH 010/146] fixed metadata check in useitem --- src/Player.php | 6 ++++-- src/recipes/CraftingRecipes.php | 5 ----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index d8ff8073..e72ead92 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1996,8 +1996,10 @@ class Player{ //nc: check item desync $heldItem = $this->getHeldItem(); - if($heldItem->getID() != $packet->item || $heldItem->getMetadata() != $packet->meta){ - ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside({$packet->item} {$packet->meta}) and serverside({$heldItem->getID()} {$heldItem->getMetadata()}) when using item. Resending inventory."); + $pmeta = ($packet->meta & 0xff); + $hmeta = ($heldItem->getMetadata() & 0xff); //in 0.8.1 useitempacket uses byte for meta + if($heldItem->getID() != $packet->item || $hmeta != $pmeta){ + ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside({$packet->item} {$pmeta}) and serverside({$heldItem->getID()} {$hmeta}) when using item. Resending inventory."); $this->sendInventory(); if($packet->face >= 0 && $packet->face <= 5){ $target = $this->level->getBlock($blockVector); diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index ec2c8b4d..20aefbbd 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -311,20 +311,15 @@ class CraftingRecipes{ $needcnt -= $it[2]; } if($needcnt != 0) { - console("awa"); goto skip_recipe; } }else{ $exceptedIndex = ($item[0] << 16) | ($item[1] & 0xffff); if(!isset($recipeItems[$exceptedIndex])) { - - console("awa1"); goto skip_recipe; //dont check count if no idmeta pair is in ingridients } $it = $recipeItems[$exceptedIndex]; if($it[0] != $item[0] || $it[1] != $item[1] || $it[2] != $item[2]) { - - console("awa2"); goto skip_recipe; } } From fe07ec81eec5ecdb9495940ad5187cbbb87b64fd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 13 May 2025 03:15:49 +0300 Subject: [PATCH 011/146] more packets will be always sent to the client --- src/API/PlayerAPI.php | 2 +- src/Player.php | 82 ++++++++++++++++++---------- src/entity/Creature.php | 2 +- src/entity/Entity.php | 23 +------- src/entity/Projectile.php | 2 +- src/entity/object/Arrow.php | 2 +- src/entity/object/FallingSand.php | 14 +++++ src/entity/object/ItemEntity.php | 2 +- src/entity/object/Minecart.php | 4 +- src/entity/object/Painting.php | 2 +- src/entity/object/PrimedTNT.php | 2 +- src/material/block/misc/BedBlock.php | 12 +--- src/world/Tile.php | 4 +- 13 files changed, 85 insertions(+), 68 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 373f3c70..7d6d0c2f 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -404,7 +404,7 @@ class PlayerAPI{ $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); } } } diff --git a/src/Player.php b/src/Player.php index e72ead92..bb402f38 100755 --- a/src/Player.php +++ b/src/Player.php @@ -130,7 +130,7 @@ class Player{ //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); - console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); + console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); } public function __get($name){ @@ -220,7 +220,7 @@ class Player{ $pk->yaw = 0; $pk->pitch = 0; $pk->bodyYaw = 0; - $e->player->dataPacket($pk); + $e->player->dataPacketAlwaysRecover($pk); $pk = new MovePlayerPacket(); $pk->eid = $e->eid; @@ -230,7 +230,7 @@ class Player{ $pk->yaw = 0; $pk->pitch = 0; $pk->bodyYaw = 0; - $this->dataPacket($pk); + $this->dataPacketAlwaysRecover($pk); }else{ $pk = new MoveEntityPacket_PosRot(); @@ -240,7 +240,7 @@ class Player{ $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $this->dataPacket($pk); + $this->dataPacketAlwaysRecover($pk); } } } @@ -266,7 +266,7 @@ class Player{ $pk->z = $e->z; $pk->yaw = $e->yaw; $pk->pitch = $e->pitch; - $this->dataPacket($pk); + $this->dataPacketAlwaysRecover($pk); } } } @@ -284,7 +284,7 @@ class Player{ $pk->z = $player->entity->z; $pk->yaw = $player->entity->yaw; $pk->pitch = $player->entity->pitch; - $this->dataPacket($pk); + $this->dataPacketAlwaysRecover($pk); $pk = new PlayerEquipmentPacket; $pk->eid = $this->eid; @@ -352,13 +352,8 @@ class Player{ * @return array|bool */ public function dataPacket(RakNetDataPacket $packet){ - if($this->connected === false){ - return false; - } - - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ - return; - } + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return; $packet->encode(); $len = strlen($packet->buffer) + 1; @@ -378,6 +373,30 @@ class Player{ return []; } + public function dataPacketAlwaysRecover(RakNetDataPacket $packet){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return; + + $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $pk->data[] = $packet; + $pk->seqNumber = $this->counter[0]++; + $pk->sendtime = microtime(true); + + $packet->encode(); + $len = strlen($packet->buffer) + 1; + $MTU = $this->MTU - 24; + if($len > $MTU){ + return $this->sendChunkDataPacket($packet); + } + + $packet->messageIndex = $this->counter[3]++; + $packet->reliability = 2; + + $this->send($pk); + $this->packetAlwaysRecoverQueue[$pk->seqNumber] = $pk; + return [$pk->seqNumber]; + } + public function sendChunkDataPacket(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; @@ -617,6 +636,25 @@ class Player{ } } + public $currentChunk = false; + + public function onChunkReceived($blockX, $blockZ){ + $minX = $blockX; + $maxX = $blockX + 15; + $minZ = $blockZ; + $maxZ = $blockZ + 15; + $tiles = $this->server->query("SELECT ID, x, y, z FROM tiles WHERE level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); + $this->lastChunk = false; + if($tiles !== false and $tiles !== true){ + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ + $tile = $this->server->api->tile->getByID($tile["ID"]); + if($tile instanceof Tile){ + $tile->spawn($this); + } + } + } + } + public function getNextChunk($world){ if($this->connected === false or $world != $this->level){ return false; @@ -632,20 +670,8 @@ class Player{ } if(is_array($this->lastChunk)){ - $minX = $this->lastChunk[0]; - $maxX = $this->lastChunk[0] + 15; - $minZ = $this->lastChunk[1]; - $maxZ = $this->lastChunk[1] + 15; - $tiles = $this->server->query("SELECT ID, x, y, z FROM tiles WHERE level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); + $this->onChunkReceived($this->lastChunk[0], $this->lastChunk[1]); $this->lastChunk = false; - if($tiles !== false and $tiles !== true){ - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ - $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile){ - $tile->spawn($this); - } - } - } } $c = key($this->chunksOrder); @@ -681,6 +707,7 @@ class Player{ if($cnt === false){ return false; } + $this->chunkCount = []; foreach($cnt as $i => $count){ $this->chunkCount[$count] = true; @@ -1537,7 +1564,6 @@ class Player{ } public function entityTick(){ if($this->isSleeping) ++$this->sleepingTime; - if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ if($this->entity->health < 20 && $this->entity->health > 0){ $this->entity->setHealth(min(20, $this->entity->health + 1), "regeneration"); @@ -2864,7 +2890,7 @@ class Player{ case RakNetInfo::NACK: foreach($packet->packets as $count){ if(isset($this->packetAlwaysRecoverQueue[$count])){ - $this->packetAlwaysRecoverQueue[$count]->sendtime = 0; //forces resend next time it will be checked + $this->resendQueue[$count] = $this->packetAlwaysRecoverQueue[$count]; }else if(isset($this->recoveryQueue[$count])){ $this->resendQueue[$count] =& $this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; diff --git a/src/entity/Creature.php b/src/entity/Creature.php index a8e97972..d6e50024 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -72,7 +72,7 @@ abstract class Creature extends Living{ $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e4f5383c..0bf3960b 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1125,14 +1125,7 @@ class Entity extends Position $pk->itemID = 0; $pk->itemAuxValue = 0; $pk->metadata = $this->getMetadata(); - $player->directDataPacket($pk, 2, true); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk, 2, true); $pk = new PlayerEquipmentPacket(); $pk->eid = $this->eid; @@ -1142,18 +1135,6 @@ class Entity extends Position $player->dataPacket($pk); $this->player->sendArmor($player); break; - case ENTITY_FALLING: - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $pk->did = -$this->data["Tile"]; - $player->dataPacket($pk); } if($this->linkedEntity != 0 && $this->isRider){ @@ -1563,6 +1544,7 @@ class Entity extends Position "health" => $health, "cause" => $cause )) !== false or $force === true){ + $this->health = min(127, max(- 127, $health)); if($harm === true && $allowHarm){ $pk = new EntityEventPacket; @@ -1578,6 +1560,7 @@ class Entity extends Position } } } + if($this->player instanceof Player){ $pk = new SetHealthPacket(); $pk->health = $this->health; diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index 3c37424b..534d5473 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -210,7 +210,7 @@ abstract class Projectile extends Entity{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $pk->did = 0; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); } } \ No newline at end of file diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index d671113e..216afc49 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -272,7 +272,7 @@ class Arrow extends Entity{ $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); } } } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 81b6fa9b..ce3ddfe7 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -19,6 +19,20 @@ class FallingSand extends Entity{ public function isPickable(){ return !$this->dead; } + public function spawn($player) + { + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = -$this->data["Tile"]; + $player->dataPacketAlwaysRecover($pk); + } public function update($now){ if($this->closed) return; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 9af57bee..d4be3e7f 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -54,7 +54,7 @@ class ItemEntity extends Entity{ $pk->roll = 0; $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); $pk = new SetEntityMotionPacket(); $pk->eid = $this->eid; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index f10ab47e..fa39affa 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -435,8 +435,8 @@ class Minecart extends Vehicle{ $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; - $player->dataPacket($pk); - + $player->dataPacketAlwaysRecover($pk); + $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; $pk->speedX = $this->speedX; diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 9b2aa8b5..0e64a00a 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -205,6 +205,6 @@ class Painting extends Entity{ $pk->z = (int) $this->zPos; $pk->direction = $this->direction; $pk->title = $this->motive; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); } } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index f88adb7e..3bc5f03d 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -76,6 +76,6 @@ class PrimedTNT extends Entity{ $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); } } \ No newline at end of file diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index dcfb3729..d94bbb08 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -76,9 +76,7 @@ class BedBlock extends TransparentBlock{ public function onActivate(Item $item, Player $player){ if(ServerAPI::request()->api->time->getPhase($player->level) !== "night"){ - $pk = new ChatPacket; - $pk->message = "You can only sleep at night"; - $player->dataPacket($pk); + $player->sendChat("You can only sleep at night"); return true; } @@ -98,17 +96,13 @@ class BedBlock extends TransparentBlock{ }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ $b = $blockWest; }else{ - $pk = new ChatPacket; - $pk->message = "This bed is incomplete"; - $player->dataPacket($pk); + $player->sendChat("This bed is incomplete"); return true; } } if($player->sleepOn($b) === false){ - $pk = new ChatPacket; - $pk->message = "This bed is occupied"; - $player->dataPacket($pk); + $player->sendChat("This bed is occupied"); } return true; } diff --git a/src/world/Tile.php b/src/world/Tile.php index e270a68b..e482384c 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -375,7 +375,7 @@ class Tile extends Position{ $pk->y = $this->y; $pk->z = $this->z; $pk->namedtag = $nbt->binary; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); break; case TILE_SIGN: $nbt = new NBT(); @@ -420,7 +420,7 @@ class Tile extends Position{ $pk->y = $this->y; $pk->z = $this->z; $pk->namedtag = $nbt->binary; - $player->dataPacket($pk); + $player->dataPacketAlwaysRecover($pk); break; } } From 87d2fbf884d55a21fb6ca7bb5366db7536a0e9c4 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 May 2025 03:08:05 +0300 Subject: [PATCH 012/146] very serious bug fixed(almost as important as readme.md update in roadrunner) --- src/API/ConsoleAPI.php | 15 ++++++--------- src/PocketMinecraftServer.php | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 540bc739..47433f65 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -202,24 +202,21 @@ class ConsoleAPI{ "v" => VIEW, ]; if(!isset($params[0]) or !isset($gms[strtolower($params[0])])){ - $output .= "Usage: /$cmd \n"; - break; + return "Usage: /$cmd "; } $this->server->api->setProperty("gamemode", $gms[strtolower($params[0])]); $output .= "Default Gamemode is now " . strtoupper($this->server->getGamemode()) . ".\n"; break; case "status": - if(!($issuer instanceof Player) and $issuer === "console"){ - $info = $this->server->debugInfo(true); - }else{ - $info = $this->server->debugInfo(); + $info = $this->server->debugInfo(); + if(!($issuer instanceof Player) && $issuer === "console"){ + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}"; } - return "TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . ")"; + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": self::$loop->stop = true; - $output .= "Stopping the server\n"; $this->server->close(); - break; + return "Stopping the server\n"; case "difficulty": $s = trim(array_shift($params)); if($s === "" or (((int) $s) > 3 and ((int) $s) < 0)){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index de5a4bf9..bd11ed78 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -774,7 +774,7 @@ class PocketMinecraftServer{ $info["garbage"] = gc_collect_cycles(); $this->handle("server.debug", $info); if($console === true){ - console("[INFO] TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . "), Entities: " . $info["entities"] . ", Events: " . $info["events"] . ", Handlers: " . $info["handlers"] . ", Actions: " . $info["actions"] . ", Garbage: " . $info["garbage"], true, true); + console("[INFO] TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}", true, true); } return $info; } From 1cbd62629a3c125bf162067690b04be2fa06cd09 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 May 2025 03:19:10 +0300 Subject: [PATCH 013/146] heal wont send you hurt animation packet --- src/entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0bf3960b..925180b6 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1421,7 +1421,7 @@ class Entity extends Position public function heal($health, $cause = "generic") { - return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause); + return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause, allowHarm: false); } public function stopRiding(){ From a8f504ad88678e2fd189f73b3b14f7bc24ef68f9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 May 2025 04:30:41 +0300 Subject: [PATCH 014/146] better(hopefully) onground check --- src/Player.php | 8 +++-- src/entity/Entity.php | 80 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/src/Player.php b/src/Player.php index bb402f38..bf0f13c9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -729,7 +729,7 @@ class Player{ }else{ $level = $pos->level; } - $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); + $this->spawnPosition = new Position($pos->x, $pos->y + 1, $pos->z, $level); $pk = new SetSpawnPositionPacket; $pk->x = (int) $this->spawnPosition->x; $pk->y = (int) $this->spawnPosition->y; @@ -1731,7 +1731,7 @@ class Player{ $pk = new StartGamePacket; $pk->seed = $this->level->getSeed(); $pk->x = $this->data->get("position")["x"]; - $pk->y = ceil($this->data->get("position")["y"]); + $pk->y = $this->data->get("position")["y"]+1; $pk->z = $this->data->get("position")["z"]; $pk->generator = 0; $pk->gamemode = $this->gamemode & 0x01; @@ -1857,7 +1857,7 @@ class Player{ break; } if($this->isSleeping) break; - if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ + if(($this->entity instanceof Entity) && $packet->messageIndex > $this->lastMovement){ $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); if($this->forceMovement instanceof Vector3){ @@ -1877,6 +1877,8 @@ class Player{ $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } $this->entity->updateAABB(); + }else{ + console("skipping movement packet"); } break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 925180b6..506e4c46 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -787,32 +787,84 @@ class Entity extends Position $bbbottom->minZ = round($this->z, 3) - $this->radius; $bbbottom->maxZ = round($this->z, 3) + $this->radius; - $handleFire = false; - $handleCactus = false; - for($x = floor($this->boundingBox->minX); $x < ceil($this->boundingBox->maxX); ++$x){ - for($z = floor($this->boundingBox->minZ); $z < ceil($this->boundingBox->maxZ); ++$z){ - for($y = floor($this->boundingBox->minY - 1); $y < ceil($this->boundingBox->maxY); ++$y){ - $intersects = 0; + $maxx = $this->x; + $minx = $this->lastX; + $maxy = $this->y; + $miny = $this->lastY; + $maxz = $this->z; + $minz = $this->lastZ; + + if($this->x < $this->lastX){ + $minx = $this->x; + $maxx = $this->lastX; + } + + if($this->y < $this->lastY){ + $miny = $this->y; + $maxy = $this->lastY; + } + + if($this->z < $this->lastZ){ + $minz = $this->z; + $maxz = $this->lastZ; + } + + $useOldGroundCheck = false; + $distBetweenLastAndCurrent = sqrt(($this->lastX-$this->x)*($this->lastX-$this->x) + ($this->lastY-$this->y)*($this->lastY-$this->y) + ($this->lastZ-$this->z)*($this->lastZ-$this->z)); + if($distBetweenLastAndCurrent > 10){ + ConsoleAPI::warn("{$this->player} is moving too fast! (Old: {$this->lastX} {$this->lastY} {$this->lastZ}, New: {$this->x} {$this->y} {$this->z}). Skipping new onground check!"); + $useOldGroundCheck = true; + goto skip_new_onground_check; + } + $bbGround = new AxisAlignedBB($minx - $this->radius, $miny, $minz - $this->radius, $maxx + $this->radius, $maxy, $maxz + $this->radius); + + for($x = floor($bbGround->minX); $x <= ceil($bbGround->maxX); ++$x){ + for($z = floor($bbGround->minZ); $z <= ceil($bbGround->maxZ); ++$z){ + for($y = floor($bbGround->minY-1); $y <= ceil($bbGround->maxY); ++$y){ $id = $this->level->level->getBlockID($x, $y, $z); $bounds = StaticBlock::$prealloc[$id]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); foreach($bounds as $bb){ - if($bbbottom->intersectsWith($bb)){ - ++$intersects; + if($bbGround->intersectsWith($bb)){ + $this->onGround = true; } } + } + } + } + + skip_new_onground_check: + + $handleFire = false; + $handleCactus = false; + + for($x = floor($this->boundingBox->minX); $x < ceil($this->boundingBox->maxX); ++$x){ + for($z = floor($this->boundingBox->minZ); $z < ceil($this->boundingBox->maxZ); ++$z){ + for($y = floor($this->boundingBox->minY - 1); $y < ceil($this->boundingBox->maxY); ++$y){ + [$id, $meta] = $this->level->level->getBlock($x, $y, $z); + + if($useOldGroundCheck){ + $intersects = 0; + $bounds = StaticBlock::$prealloc[$id]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); + foreach($bounds as $bb){ + if($bbbottom->intersectsWith($bb)){ + ++$intersects; + } + } + } + if($id == WATER || $id === STILL_WATER || $id === COBWEB || $id == LAVA || $id === STILL_LAVA){ $this->notOnGroundTicks = 0; } - intersects: - if($y <= floor($this->boundingBox->minY) && !$this->onGround){ - if($intersects > 0) $this->onGround = count($bounds) > 0; + if($useOldGroundCheck){ + intersects: + if($y <= floor($this->boundingBox->minY) && !$this->onGround){ + if($intersects > 0) $this->onGround = count($bounds) > 0; + } } + if($y >= floor($this->boundingBox->minY)){ - $block = $this->level->level->getBlock($x, $y, $z); - $id = $block[0]; - $meta = $block[1]; $handleFire = $handleFire || (($id == FIRE || $id == STILL_LAVA || $id == LAVA) && $x >= $fireMinX && $x < $fireMaxX && $y >= $fireMinY && $y < $fireMaxY && $z >= $fireMinZ && $z < $fireMaxZ); $handleCactus = $handleCactus || ($id == CACTUS && $x >= $fireMinX && $x < $fireMaxX && $y >= $fireMinY && $y < $fireMaxY && $z >= $fireMinZ && $z < $fireMaxZ); From 0c6bf7648f2dd3b33b14e2d0f816733a501d3dd5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 May 2025 23:23:43 +0300 Subject: [PATCH 015/146] fixed rangedattack spawning arrow with wrong y pos --- src/Player.php | 3 +-- src/entity/ai/tasks/TaskRangedAttack.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Player.php b/src/Player.php index bf0f13c9..f898f974 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1877,9 +1877,8 @@ class Player{ $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } $this->entity->updateAABB(); - }else{ - console("skipping movement packet"); } + break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: if($this->spawned === false){ diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php index 04d53630..afba75fe 100644 --- a/src/entity/ai/tasks/TaskRangedAttack.php +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -68,7 +68,7 @@ class TaskRangedAttack extends \TaskBase $arrow = $this->server->api->entity->add($selfEntity->level, ENTITY_OBJECT, OBJECT_ARROW, $d); $arrow->shotByEntity = true; $arrow->shooterEID = $selfEntity->eid; - $posY = ($target->y + $target->getEyeHeight() - 0.1); + $posY = ($selfEntity->y + $selfEntity->getEyeHeight() - 0.1); $diffX = $target->x - $selfEntity->x; $diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY; $diffZ = $target->z - $selfEntity->z; From 75d75e426a8431924643a597d1e7e78f8b68c655 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 16 May 2025 07:11:27 +0300 Subject: [PATCH 016/146] entities will use xorshift128 random --- src/entity/Entity.php | 5 ++- src/utils/XorShift128Random.php | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/utils/XorShift128Random.php diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 506e4c46..99217779 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -76,6 +76,9 @@ class Entity extends Position public $check = true; public $width = 1; public $height = 1; + /** + * @var XorShift128Random + */ public $random; public $radius; public $inAction = false; @@ -113,7 +116,7 @@ class Entity extends Position function __construct(Level $level, $eid, $class, $type = 0, $data = []) { - $this->random = new Random(); + $this->random = new XorShift128Random(); $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables $this->canBeAttacked = false; $this->hasKnockback = false; diff --git a/src/utils/XorShift128Random.php b/src/utils/XorShift128Random.php new file mode 100644 index 00000000..8c874065 --- /dev/null +++ b/src/utils/XorShift128Random.php @@ -0,0 +1,55 @@ +setSeed($seed === null ? microtime(true) : $seed); + } + + public function nextGaussian(){ + if($this->haveNextNextGaussian){ + $this->haveNextNextGaussian = false; + return $this->nextNextGaussian; + }else{ + do{ + $v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0 + $v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0 + $s = $v1 * $v1 + $v2 * $v2; + }while($s >= 1 || $s == 0); + $multiplier = sqrt(-2 * log($s) / $s); + $this->nextNextGaussian = $v2 * $multiplier; + $this->haveNextNextGaussian = true; + return $v1 * $multiplier; + } + } + + public function setSeed($seed){ + $this->x = 123456789 ^ $seed; + $this->y = 362436069 ^ ($seed << 17) | (($seed >> 15) & 0x7fffffff) & 0xffffffff; + $this->z = 521288629 ^ ($seed << 31) | (($seed >> 1 ) & 0x7fffffff) & 0xffffffff; + $this->w = 88675123 ^ ($seed << 18) | (($seed >> 14) & 0x7fffffff) & 0xffffffff; + } + + public function gen(){ + $t = ($this->x ^ ($this->x << 11)) & 0xffffffff; + $this->x = $this->y; + $this->y = $this->z; + $this->z = $this->w; + $this->w = ($this->w ^ (($this->w >> 19) & 0x7fffffff) ^ ($t ^ (($t >> 8) & 0x7fffffff))) & 0xffffffff; + + return $this->w << 32 >> 32; + } + + public function nextInt($bound = null){ + if($bound == null) return $this->gen(); + return (($this->gen() & 0x7fffffff) % $bound); + } + + public function nextFloat(){ + return (($this->gen() & 0x7fffffff) / 0x7fffffff); + } +} From c64d5eedbb28ed396a46ce12dbdda5b19b6c8ba2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 18 May 2025 16:19:51 +0300 Subject: [PATCH 017/146] player will spawn 0.1 blocks above the ground instead of 1 --- src/Player.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Player.php b/src/Player.php index f898f974..1f0e39b5 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1,6 +1,5 @@ seed = $this->level->getSeed(); $pk->x = $this->data->get("position")["x"]; - $pk->y = $this->data->get("position")["y"]+1; + $pk->y = ceil($this->data->get("position")["y"])+1; $pk->z = $this->data->get("position")["z"]; $pk->generator = 0; $pk->gamemode = $this->gamemode & 0x01; @@ -1762,7 +1761,7 @@ class Player{ $this->eid = $this->entity->eid; $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); $this->entity->x = $pk->x; - $this->entity->y = $pk->y; + $this->entity->y = $pk->y-0.9; $this->entity->z = $pk->z; if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); From 659aea440d426d0c5f69bfa94c1a0c472c0887fd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 18 May 2025 16:51:03 +0300 Subject: [PATCH 018/146] item meta overflow fix --- src/Player.php | 1 - src/material/Block.php | 2 +- src/material/Item.php | 5 ++-- src/material/block/solid/WoolBlock.php | 41 ++++++++++++++------------ 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Player.php b/src/Player.php index 1f0e39b5..a37ed96c 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2298,7 +2298,6 @@ class Player{ $this->checkSpawnPosition(); $this->teleport($this->spawnPosition, false, false, true, false); - $pk = new MovePlayerPacket(); $pk->eid = $this->entity->eid; $pk->x = $this->entity->x; diff --git a/src/material/Block.php b/src/material/Block.php index 459c1e91..7fd7cdac 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -233,7 +233,7 @@ abstract class Block extends Position{ public function __construct($id, $meta = 0, $name = "Unknown"){ $this->id = (int) $id; - $this->meta = (int) $meta; + $this->meta = ((int) $meta) & 0xf; $this->name = $name; $this->breakTime = 0.20; $this->hardness = 10; diff --git a/src/material/Item.php b/src/material/Item.php index b7997318..7b92221a 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -144,10 +144,11 @@ class Item{ public function __construct($id, $meta = 0, $count = 1, $name = "Unknown"){ $this->id = (int) $id; - $this->meta = (int) $meta; + $this->meta = ((int) $meta) & 0xffff; $this->count = (int) $count; $this->name = $name; - if(!isset($this->block) and $this->id <= 0xff and isset(Block::$class[$this->id])){ + if(!isset($this->block) && $this->id <= 0xff && isset(Block::$class[$this->id])){ + $this->meta &= 0xf; $this->block = BlockAPI::get($this->id, $this->meta); $this->name = $this->block->getName(); } diff --git a/src/material/block/solid/WoolBlock.php b/src/material/block/solid/WoolBlock.php index 3bfb0de8..8c574f65 100644 --- a/src/material/block/solid/WoolBlock.php +++ b/src/material/block/solid/WoolBlock.php @@ -2,27 +2,30 @@ class WoolBlock extends SolidBlock{ public static $blockID; + + public static $names = [ + 0 => "White Wool", + 1 => "Orange Wool", + 2 => "Magenta Wool", + 3 => "Light Blue Wool", + 4 => "Yellow Wool", + 5 => "Lime Wool", + 6 => "Pink Wool", + 7 => "Gray Wool", + 8 => "Light Gray Wool", + 9 => "Cyan Wool", + 10 => "Purple Wool", + 11 => "Blue Wool", + 12 => "Brown Wool", + 13 => "Green Wool", + 14 => "Red Wool", + 15 => "Black Wool", + ]; + public function __construct($meta = 0){ parent::__construct(WOOL, $meta, "Wool"); - $names = array( - 0 => "White Wool", - 1 => "Orange Wool", - 2 => "Magenta Wool", - 3 => "Light Blue Wool", - 4 => "Yellow Wool", - 5 => "Lime Wool", - 6 => "Pink Wool", - 7 => "Gray Wool", - 8 => "Light Gray Wool", - 9 => "Cyan Wool", - 10 => "Purple Wool", - 11 => "Blue Wool", - 12 => "Brown Wool", - 13 => "Green Wool", - 14 => "Red Wool", - 15 => "Black Wool", - ); - $this->name = $names[$this->meta]; + + $this->name = self::$names[$this->meta] ?? "Illegal Wool"; $this->hardness = 4; } From 992b6143cd98345538ce6199255b687a9ddc41f5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 19 May 2025 09:31:50 +0300 Subject: [PATCH 019/146] grass will try spreading only once per tick --- src/material/block/solid/GrassBlock.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 9b689ff6..d6181801 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -47,17 +47,17 @@ class GrassBlock extends SolidBlock{ if(!StaticBlock::getIsTransparent($level->level->getBlockID($x, $y + 1, $z)) && mt_rand(0, 2) == 1){ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0); }else{ - for($cnt = 0; $cnt < 4; ++$cnt){ - $x = $x + mt_rand(0, 2) - 1; - $y = $y + mt_rand(0, 4) - 3; - $z = $z + mt_rand(0, 2) - 1; - - $blockUp = $level->level->getBlockID($x, $y + 1, $z); - if(StaticBlock::getIsTransparent($blockUp) && !StaticBlock::getIsLiquid($blockUp) && !($blockUp === 60) && $level->level->getBlockID($x, $y, $z) === DIRT){ - $level->fastSetBlockUpdate($x, $y, $z, GRASS, 0); - } - + //for($cnt = 0; $cnt < 4; ++$cnt){ + $x = $x + mt_rand(0, 2) - 1; + $y = $y + mt_rand(0, 4) - 3; + $z = $z + mt_rand(0, 2) - 1; + + $blockUp = $level->level->getBlockID($x, $y + 1, $z); + if(StaticBlock::getIsTransparent($blockUp) && !StaticBlock::getIsLiquid($blockUp) && !($blockUp == FARMLAND) && $level->level->getBlockID($x, $y, $z) === DIRT){ + $level->fastSetBlockUpdate($x, $y, $z, GRASS, 0); } + + //} } } From 75a6b66d299de0048ba89edbaed3542212fac010 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 19 May 2025 10:40:54 +0300 Subject: [PATCH 020/146] sendChat will use different queue to send messages to client, chat messages are now ordered --- src/Player.php | 69 ++++++++++++++++++++++++++++------ src/entity/Entity.php | 1 - src/entity/object/Painting.php | 6 +-- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/Player.php b/src/Player.php index a37ed96c..ccfb1b05 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1,6 +1,7 @@ blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; + $this->chatMessagesQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->chatMessagesQueue->data = []; + //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); @@ -1038,7 +1050,7 @@ class Player{ $pk = new MessagePacket; $pk->source = ($author instanceof Player) ? $author->username : $author; $pk->message = TextFormat::clean($m); //Colors not implemented :( - $this->dataPacket($pk); + $this->sendChatMessagePacket($pk); } } } @@ -1293,6 +1305,10 @@ class Player{ } } + if($this->chatMessagesQueueLength > 0 && $this->server->ticks % 5 == 0){ //send 4 times/second + $this->sendChatBuffer(); + } + if($this->bufferLen > 0){ $this->sendBuffer(); } @@ -1322,6 +1338,20 @@ class Player{ } } } + + public function sendChatBuffer(){ + if($this->chatMessagesQueueLength > 0 && $this->chatMessagesQueue instanceof RakNetPacket){ + $this->chatMessagesQueue->seqNumber = $this->counter[0]++; + $this->packetAlwaysRecoverQueue[$this->chatMessagesQueue->seqNumber] = $this->chatMessagesQueue; + $this->chatMessagesQueue->sendtime = microtime(true); + $this->send($this->chatMessagesQueue); + } + + $this->chatMessagesQueueLength = 0; + $this->chatMessagesQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->chatMessagesQueue->data = []; + } + public function sendEntityMovementUpdateQueue(){ if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){ $this->entityMovementQueue->seqNumber = $this->counter[0]++; @@ -1541,13 +1571,8 @@ class Player{ } public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ - if($this->connected === false){ - return false; - } - - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ - return []; - } + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return []; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); @@ -1561,6 +1586,26 @@ class Player{ $this->send($pk); return [$pk->seqNumber]; } + + public function sendChatMessagePacket(RakNetDataPacket $packet){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; + + $packet->encode(); + $len = strlen($packet->buffer) + 1; + $MTU = $this->MTU - 24; + + if(($this->chatMessagesQueueLength + $len) >= $MTU) $this->sendChatBuffer(); + + $packet->reliability = 3; + $packet->messageIndex = $this->counter[3]++; + $packet->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; + $packet->orderIndex = $this->chatMessagesOrderIndex++; + $this->chatMessagesQueue->data[] = $packet; + $this->chatMessagesQueueLength += 10+$len; //reliability(1)+lenbits(2)+messageindex(3)+orderChannel(1)+orderIndex(3) = 10 + return true; + } + public function entityTick(){ if($this->isSleeping) ++$this->sleepingTime; if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 99217779..68dffc21 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -146,7 +146,6 @@ class Entity extends Position $this->name = ""; $this->gravity = 0.08; $this->state = $this->data["State"] = $this->data["State"] ?? 0; - $this->tickCounter = 0; $this->x = isset($this->data["x"]) ? (float) $this->data["x"] : 0; $this->y = isset($this->data["y"]) ? (float) $this->data["y"] : 0; $this->z = isset($this->data["z"]) ? (float) $this->data["z"] : 0; diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 0e64a00a..6a49d48a 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -41,14 +41,14 @@ class Painting extends Entity{ } } - public $counter = 0; + public $scounter = 0; public function update($now){ if($this->closed === true){ return false; } - if(++$this->counter >= 100){ - $this->counter = 0; + if(++$this->scounter >= 100){ + $this->scounter = 0; if(!$this->survives() && !$this->dead){ //maybe dont check entity collision? $this->makeDead("nosurvive"); } From b0bad174e0b0c5a36adc3a8de81f8ac44b57e19d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 19 May 2025 11:24:41 +0300 Subject: [PATCH 021/146] better craft checking --- src/Player.php | 8 +++++- src/recipes/CraftingRecipes.php | 44 ++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/Player.php b/src/Player.php index ccfb1b05..6dc33aaa 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2803,7 +2803,13 @@ class Player{ $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); - if(!is_array($cc)) return; + if(!is_array($cc)){ + if(!$cc){ + $this->toCraft = []; + $this->craftingItems = []; + } + return; + } if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){ $this->toCraft = []; diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 20aefbbd..6ccbc1e9 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -204,6 +204,12 @@ class CraftingRecipes{ private static $inventoryRecipes = []; private static $stoneCutterRecipes = []; + private static $craftingTablePossibleResults = []; + private static $inventoryPossibleResults = []; + private static $stoneCutterPossibleResults = []; + + + public static function init(){ foreach(CraftingRecipes::$small as $recipe){ @@ -248,21 +254,32 @@ class CraftingRecipes{ switch($type){ case self::TYPE_CRAFTIGTABLE: $arr = &self::$craftingTableRecipes; + $arr_c = &self::$craftingTablePossibleResults; break; case self::TYPE_INVENTORY: $arr = &self::$inventoryRecipes; + $arr_c = &self::$inventoryPossibleResults; break; case self::TYPE_STONECUTTER: $arr = &self::$stoneCutterRecipes; + $arr_c = &self::$stoneCutterPossibleResults; break; default: throw new RuntimeException("Unknown type: {$type}"); } - if(!isset($arr[$result_index])){ - $arr[$result_index] = []; - } + if(!isset($arr[$result_index])) $arr[$result_index] = []; $arr[$result_index][] = $ingridients_arr; + + foreach($results_arr as $resitem){ + if(!isset($arr_c[$resitem])){ + $arr_c[$resitem] = []; + } + foreach($results_arr as $resitem2){ + $arr_c[$resitem][$resitem2] = true; + } + } + } /** @@ -271,7 +288,7 @@ class CraftingRecipes{ * @param array $craftItems items that will be crafted * @param array $recipeItems items that will be consumed * @param int $type craft type (CraftingRecipes::TYPE_INVENTORY, CraftingRecipes::TYPE_CRAFTIGTABLE, CraftingRecipes::TYPE_STONECUTTER) - * @return array|false recipe that will be used or false + * @return array|true|false recipe that will be used or bool. If returned false, crafting will be aborted. If returned true crafting wont be aborted */ public static function canCraft(array $craftItems, array $recipeItems, $type){ $craftIndexArr = []; @@ -280,25 +297,34 @@ class CraftingRecipes{ } ksort($craftIndexArr); $craftIndex = implode(",", $craftIndexArr); - switch($type){ case self::TYPE_CRAFTIGTABLE: $arr = &self::$craftingTableRecipes; + $arr_c = &self::$craftingTablePossibleResults; break; case self::TYPE_INVENTORY: $arr = &self::$inventoryRecipes; + $arr_c = &self::$inventoryPossibleResults; break; case self::TYPE_STONECUTTER: $arr = &self::$stoneCutterRecipes; + $arr_c = &self::$stoneCutterPossibleResults; break; default: ConsoleAPI::error("Tried crafting recipe with unknown type {$type}!"); return false; } - if(!isset($arr[$craftIndex])) { - console("WAT $craftIndex"); - return false; //recipe not found + if(!isset($arr[$craftIndex])) { //recipe for those ingridients was not found + $allcombos = $arr_c[current($craftIndexArr)] ?? []; + foreach($craftIndexArr as $res){ + if(!isset($allcombos[$res])){ + ConsoleAPI::info("Recipe with $craftIndex is not found. Crafting aborted. $res"); + return false; //recipe not found and wont be found + } + } + ConsoleAPI::info("Recipe with $craftIndex is not found but it might be found later. Crafting continued."); + return true; //recipe might be found next time } foreach($arr[$craftIndex] as $ingridients){ @@ -329,7 +355,7 @@ class CraftingRecipes{ skip_recipe: } - return false; + return true; } } From e45b99c7e99b0e5520b1878d1939b38afbb65f8b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 20 May 2025 17:15:40 +0300 Subject: [PATCH 022/146] fixed blocks that have no physcis falling sometimes --- src/material/block/GenericBlock.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index f7a128cb..99abc099 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -58,17 +58,20 @@ class GenericBlock extends Block{ 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, - "z" => $z + 0.5, - "Tile" => $id, - ); - $server = ServerAPI::request(); - $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); - $server->api->entity->spawnToAll($e); + if(StaticBlock::getHasPhysics($id)){ + $down = $level->level->getBlockID($x, $y - 1, $z); + + if($down == AIR || StaticBlock::getIsLiquid($down)){ + $data = array( + "x" => $x + 0.5, + "y" => $y, + "z" => $z + 0.5, + "Tile" => $id, + ); + $server = ServerAPI::request(); + $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); + $server->api->entity->spawnToAll($e); + } } } From 2db7d4e33868facb6d39f55aaab66bbf75d1626b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 21 May 2025 21:23:50 +0300 Subject: [PATCH 023/146] jumping on farmland will update neighbors if block changed to dirt --- src/material/block/nonfull/FarmlandBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index dcf05a39..be09ba6c 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -24,7 +24,7 @@ class FarmlandBlock extends TransparentBlock{ public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){ $rv = lcg_value(); if($rv < ($fallDistance - 0.5)){ - $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0); + $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); } } From 24578f940c8bbb6602385340c7fa8fcb9e4111c8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 21 May 2025 21:51:41 +0300 Subject: [PATCH 024/146] fixed itementity's checkinblock --- src/entity/object/ItemEntity.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index d4be3e7f..d645b43c 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -87,25 +87,26 @@ class ItemEntity extends Entity{ $id = $this->level->level->getBlockID($xFloor, $yFloor, $zFloor); - if(StaticBlock::getIsSolid($id)){ + if(StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id)){ $xDiff = $x - $xFloor; $id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor); - $xNeg = StaticBlock::getIsSolid($id); + $xNeg = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); $id = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor); - $xPos = StaticBlock::getIsSolid($id); + $xPos = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); $id = $this->level->level->getBlockID($xFloor, $yFloor - 1, $zFloor); - $yNeg = StaticBlock::getIsSolid($id); + $yNeg = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); $id = $this->level->level->getBlockID($xFloor, $yFloor + 1, $zFloor); - $yPos = StaticBlock::getIsSolid($id); + $yPos = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); - $id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor - 1); - $zNeg = StaticBlock::getIsSolid($id); + $id = $this->level->level->getBlockID($xFloor, $yFloor, $zFloor - 1); + $zNeg = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); - $zPos = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor + 1); + $id = $this->level->level->getBlockID($xFloor, $yFloor, $zFloor + 1); + $zPos = StaticBlock::getIsSolid($id) && StaticBlock::getIsFullBlock($id); if($xNeg || $xDiff >= 9999.0){ //TODO not needed check? $v15 = 9999.0; @@ -144,7 +145,7 @@ class ItemEntity extends Entity{ $v16 = 4; } - if(!StaticBlock::getIsSolid($zPos) && ((1.0 - $zDiff) < $v15)){ + if(!$zPos && ((1.0 - $zDiff) < $v15)){ $v16 = 5; } From 3c18f85d890d2bfa4135ddf640c8abd7536effed Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 22 May 2025 17:24:27 +0300 Subject: [PATCH 025/146] block updates and chunk data are ordered now, removed 16x16x16 chunk sending --- src/API/BlockAPI.php | 8 +- src/Player.php | 170 +++++++++++++++++++--------------- src/PocketMinecraftServer.php | 6 +- src/world/Level.php | 15 +-- 4 files changed, 109 insertions(+), 90 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 4748f7a1..31aaf2c9 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -432,13 +432,7 @@ class BlockAPI{ } private function cancelAction(Block $block, Player $player, $send = true){ - $pk = new UpdateBlockPacket; - $pk->x = $block->x; - $pk->y = $block->y; - $pk->z = $block->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $player->dataPacket($pk); + $player->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); if($send === true){ $player->sendInventory(); } diff --git a/src/Player.php b/src/Player.php index 6dc33aaa..166640cf 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2,8 +2,10 @@ class Player{ const CHATMESSAGE_ORDER_CHANNEL = 1; + const BLOCKUPDATE_ORDER_CHANNEL = 2; - public static $smallChunks = false, $experimentalHotbar = false; + public static $experimentalHotbar = false; + public static $allowDroppingSingleItems = true; /** @var Config */ public $data; /** @var Entity */ @@ -87,10 +89,12 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + public $blockUpdateQueueOrderIndex = 0; public $chatMessagesQueue; public $chatMessagesQueueLength = 0; public $chatMessagesOrderIndex = 0; + public $chunkDataSent = []; /** * Sent by a client while it is linked to some entity. @@ -397,7 +401,7 @@ class Player{ $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; if($len > $MTU){ - return $this->sendChunkDataPacket($packet); + return $this->sendBigPacketAlwaysRecover($packet); } $packet->messageIndex = $this->counter[3]++; @@ -412,6 +416,48 @@ class Player{ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + $pk->encode(); + $sendtime = microtime(true); + $size = $this->MTU - 34; + if($size <= 0) return false; + $buffer = str_split($pk->buffer, $size); + $bigCnt = $this->bigCnt; + $this->bigCnt = ($this->bigCnt + 1) % 0x10000; + $cnts = []; + $bufCount = count($buffer); + + $orderIndex = $this->blockUpdateQueueOrderIndex++; + foreach($buffer as $i => $buf){ + $cnts[] = $count = $this->counter[0]++; + + $pk = new UnknownPacket; + $pk->packetID = $pk->pid(); + $pk->reliability = 3; + + $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; + $pk->orderIndex = $orderIndex; + + $pk->hasSplit = true; + $pk->splitCount = $bufCount; + $pk->splitID = $bigCnt; + $pk->splitIndex = $i; + $pk->buffer = $buf; + $pk->messageIndex = $this->counter[3]++; + + $rk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $rk->data[] = $pk; + $rk->seqNumber = $count; + $rk->sendtime = $sendtime; + $this->packetAlwaysRecoverQueue[$count] = $rk; + $this->send($rk); + } + return $cnts; + } + + public function sendBigPacketAlwaysRecover(RakNetDataPacket $pk){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + $pk->encode(); $sendtime = microtime(true); $size = $this->MTU - 34; @@ -485,7 +531,11 @@ class Player{ if($this->connected === true){ $packet->ip = $this->ip; $packet->port = $this->port; - $this->bandwidthRaw += $this->server->send($packet); + $len = $this->server->send($packet); + if($len > $this->MTU){ + ConsoleAPI::warn("Packet length is more than {$this->iusername}'s mtu!($len > {$this->MTU})"); + } + $this->bandwidthRaw += $len; } } @@ -571,30 +621,15 @@ class Player{ $this->chunksOrder = []; $this->lastOrderX = $X; $this->lastOrderZ = $Z; - if(self::$smallChunks){ - $Y = ((int)$this->entity->y) >> 4; - $v = new Vector3($X, $Y, $Z); - for($x = 0; $x < 16; ++$x){ - for($z = 0; $z < 16; ++$z){ - for($y = 0; $y < 8; ++$y){ - $dist = $v->distance(new Vector3($x, $y, $z)); - $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])){ - $this->chunksOrder[$d] = $dist; - } - } - } - } - }else{ - $v = new Vector2($X, $Z); - for($x = 0; $x < 16; ++$x){ - for($z = 0; $z < 16; ++$z){ - $dist = $v->distance(new Vector2($x, $z)); - for($y = 0; $y < 8; ++$y){ - $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])){ - $this->chunksOrder[$d] = $dist; - } + + $v = new Vector2($X, $Z); + for($x = 0; $x < 16; ++$x){ + for($z = 0; $z < 16; ++$z){ + $dist = $v->distance(new Vector2($x, $z)); + for($y = 0; $y < 8; ++$y){ + $d = $x . ":" . $y . ":" . $z; + if(!isset($this->chunksLoaded[$d])){ + $this->chunksOrder[$d] = $dist; } } } @@ -630,7 +665,8 @@ class Player{ $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); $cnt = $this->sendChunkDataPacket($pk); - + $this->chunkDataSent["$X:$Z"] = true; + $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); $this->lastChunk = false; if($tiles !== false and $tiles !== true){ @@ -715,6 +751,8 @@ class Player{ $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); $cnt = $this->sendChunkDataPacket($pk); + $this->chunkDataSent["$X:$Z"] = true; + if($cnt === false){ return false; } @@ -1365,7 +1403,7 @@ class Player{ public function sendBlockUpdateQueue(){ if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket){ $this->blockUpdateQueue->seqNumber = $this->counter[0]++; - $this->recoveryQueue[$this->blockUpdateQueue->seqNumber] = $this->blockUpdateQueue; + $this->packetAlwaysRecoverQueue[$this->blockUpdateQueue->seqNumber] = $this->blockUpdateQueue; $this->blockUpdateQueue->sendtime = microtime(true); $this->send($this->blockUpdateQueue); } @@ -1375,6 +1413,12 @@ class Player{ } public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta){ + $xC = $x >> 4; + $zC = $z >> 4; + if(!isset($this->chunkDataSent["$xC:$zC"])){ + ConsoleAPI::debug("Ignoring block update $x $y $z $id $meta: chunk data was not sent."); + return; //dont send updateblockpacket as it will be buffered and updated only after all chunks will load + } $packet = new UpdateBlockPacket(); $packet->x = $x; $packet->y = $y; @@ -1391,9 +1435,11 @@ class Player{ } $packet->messageIndex = $this->counter[3]++; - $packet->reliability = 2; + $packet->reliability = 3; + $packet->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; + $packet->orderIndex = $this->blockUpdateQueueOrderIndex++; @$this->blockUpdateQueue->data[] = $packet; - $this->blockUpdateQueueLength += 6 + $len; + $this->blockUpdateQueueLength += 10 + $len; } public function addEntityMovementUpdateToQueue(Entity $e){ @@ -2027,22 +2073,8 @@ class Player{ if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); - - $pk = new UpdateBlockPacket; - $pk->x = $block->x; - $pk->y = $block->y; - $pk->z = $block->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $this->dataPacket($pk); + $this->addBlockUpdateIntoQueue($target->x, $target->y, $target->z, $target->getID(), $target->getMetadata()); + $this->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); break; } $this->craftingItems = []; @@ -2098,22 +2130,9 @@ class Player{ } $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); - - $pk = new UpdateBlockPacket; - $pk->x = $block->x; - $pk->y = $block->y; - $pk->z = $block->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $this->dataPacket($pk); + + $this->addBlockUpdateIntoQueue($target->x, $target->y, $target->z, $target->getID(), $target->getMetadata()); + $this->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); break; }elseif($packet->face === 0xff){ @@ -2245,15 +2264,8 @@ class Player{ case ProtocolInfo::REMOVE_BLOCK_PACKET: $blockVector = new Vector3($packet->x, $packet->y, $packet->z); if($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8){ - $target = $this->level->getBlock($blockVector); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); + [$id, $meta] = $this->level->level->getBlock($blockVector->x, $blockVector->y, $blockVector->z); + $this->addBlockUpdateIntoQueue($blockVector->x, $blockVector->y, $blockVector->z, $id, $meta); break; } $this->craftingItems = []; @@ -2418,7 +2430,14 @@ class Player{ $packet->eid = $this->eid; $prevItem = $packet->item; - $packet->item = $this->getSlot($this->slot); + $newItem = $this->getSlot($this->slot); + if($newItem->count < $prevItem->count){ + ConsoleAPI::warn("{$this->username} tried dropping too many items(serverside stack has {$newItem->count}, tried dropping {$prevItem->count}."); + $this->sendInventory(); + return; + } + + $packet->item = $newItem; $sendOnDrop = false; if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ @@ -3023,6 +3042,11 @@ class Player{ public function craftItems(array $craft, array $recipe, $type){ return false; } + + /** + * @deprecated 16x16x16 chunk sending was removed + */ + public static $smallChunks = false; } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index bd11ed78..b8590262 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -88,17 +88,17 @@ class PocketMinecraftServer{ "discord-bot-name" => "NostalgiaCore Logger", "despawn-mobs" => true, "mob-despawn-ticks" => 18000, - "16x16x16_chunk_sending" => false, "experimental-mob-ai" => false, "force-20-tps" => false, "enable-mob-pushing" => Living::$entityPushing, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, "use-experimental-hotbar" => Player::$experimentalHotbar, - "keep-items-on-death" => Entity::$keepInventory + "keep-items-on-death" => Entity::$keepInventory, + //"allow-dropping-single-items" => Player::$allowDroppingSingleItems ]); Entity::$keepInventory = $this->extraprops->get("keep-items-on-death"); Player::$experimentalHotbar = $this->extraprops->get("use-experimental-hotbar"); - Player::$smallChunks = $this->extraprops->get("16x16x16_chunk_sending"); + //Player::$allowDroppingSingleItems = $this->extraprops->get("allow-dropping-single-items"); Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); diff --git a/src/world/Level.php b/src/world/Level.php index cd677076..9f743577 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -429,13 +429,14 @@ class Level{ if(count($this->changedBlocks) > 0){ foreach($this->changedBlocks as $i => $blocks){ foreach($blocks as $b){ - $pk = new UpdateBlockPacket; - $pk->x = ($b >> 32) & 0xff; - $pk->y = ($b >> 24) & 0xff; - $pk->z = ($b >> 16) & 0xff; - $pk->block =($b >> 8) & 0xff; - $pk->meta = $b & 0xff; - $this->server->api->player->broadcastPacket($this->players, $pk); + $x = ($b >> 32) & 0xff; + $y = ($b >> 24) & 0xff; + $z = ($b >> 16) & 0xff; + $id =($b >> 8) & 0xff; + $meta = $b & 0xff; + foreach($this->players as $p){ + $p->addBlockUpdateIntoQueue($x, $y, $z, $id, $meta); + } } unset($this->changedBlocks[$i]); } From 82b188168b723fa3fcd2cbc3a937beabf79e70c5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 22 May 2025 20:44:46 +0300 Subject: [PATCH 026/146] fixed ability to place blocks inside other players --- src/API/BlockAPI.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 31aaf2c9..0e111670 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -508,11 +508,14 @@ class BlockAPI{ 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; - 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); //Entity in block + foreach($block->level->players as $pl){ + $playerbb = $pl->entity->boundingBox; + 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); //Entity in block + } } + } if($this->server->api->dhandle("player.block.place", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ From 69d3321e1669825f21cee3a0f5c48be41adfa23f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 23 May 2025 05:39:07 +0300 Subject: [PATCH 027/146] fixed broken findLog cache, fixed dyes with meta > 15 --- src/material/block/solid/LeavesBlock.php | 22 ++++++------ src/material/item/generic/DyeItem.php | 43 +++++++++++++----------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 35391300..8a8c5dc3 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -22,28 +22,30 @@ class LeavesBlock extends TransparentBlock{ } public static function createIndex($x, $y, $z){ - return $x.".".$y.".".$z; + return "$x.$y.$z"; } - public static function findLog(Level $level, $x, $y, $z, array $visited, $distance){ //port from newest pocketmine + public static function findLog(Level $level, $x, $y, $z, array &$visited, $distance){ //port from newest pocketmine $index = self::createIndex($x, $y, $z); if(isset($visited[$index])){ - return false; + return $visited[$index]; } - $visited[$index] = true; + $block = $level->level->getBlockID($x, $y, $z); if($block === WOOD){ //type doesn't matter - return true; + return ($visited[$index] = true); } if($block === LEAVES && $distance <= 4){ - if(self::findLog($level, $x - 1, $y, $z, $visited, $distance + 1)) return true; - if(self::findLog($level, $x + 1, $y, $z, $visited, $distance + 1)) return true; - if(self::findLog($level, $x, $y, $z - 1, $visited, $distance + 1)) return true; - if(self::findLog($level, $x, $y, $z + 1, $visited, $distance + 1)) return true; + if(self::findLog($level, $x - 1, $y, $z, $visited, $distance + 1)) return ($visited[$index] = true); + if(self::findLog($level, $x + 1, $y, $z, $visited, $distance + 1)) return ($visited[$index] = true); + if(self::findLog($level, $x, $y, $z - 1, $visited, $distance + 1)) return ($visited[$index] = true); + if(self::findLog($level, $x, $y, $z + 1, $visited, $distance + 1)) return ($visited[$index] = true); } - return false; + + return ($visited[$index] = false); } + public static function onRandomTick(Level $level, $x, $y, $z){ $b = $level->level->getBlock($x, $y, $z); $id = $b[0]; diff --git a/src/material/item/generic/DyeItem.php b/src/material/item/generic/DyeItem.php index 08e7c3c6..a85a3dce 100755 --- a/src/material/item/generic/DyeItem.php +++ b/src/material/item/generic/DyeItem.php @@ -1,26 +1,29 @@ "Ink Sac", + 1 => "Rose Red", + 2 => "Cactus Green", + 3 => "Cocoa Beans", + 4 => "Lapis Lazuli", + 5 => "Purple Dye", + 6 => "Cyan Dye", + 7 => "Light Gray Dye", + 8 => "Gray Dye", + 9 => "Pink Dye", + 10 => "Lime Dye", + 11 => "Dandelion Yellow", + 12 => "Light Blue Dye", + 13 => "Magenta Dye", + 14 => "Orange Dye", + 15 => "Bone Meal", + ); + public function __construct($meta = 0, $count = 1){ - parent::__construct(DYE, $meta, $count, "Dye"); - $names = array( - 0 => "Ink Sac", - 1 => "Rose Red", - 2 => "Cactus Green", - 3 => "Cocoa Beans", - 4 => "Lapis Lazuli", - 5 => "Purple Dye", - 6 => "Cyan Dye", - 7 => "Light Gray Dye", - 8 => "Gray Dye", - 9 => "Pink Dye", - 10 => "Lime Dye", - 11 => "Dandelion Yellow", - 12 => "Light Blue Dye", - 13 => "Magenta Dye", - 14 => "Orange Dye", - 15 => "Bone Meal", - ); - $this->name = $names[$this->meta]; + parent::__construct(DYE, $meta & 0xf, $count, "Dye"); + + $this->name = self::$names[$this->meta]; } } From ccdea98d280de5acb2098a6f3dd6c5304c82d2aa Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 25 May 2025 20:24:35 +0300 Subject: [PATCH 028/146] added ability to disable emojis on signs --- src/API/ChatAPI.php | 4 ++-- src/Player.php | 4 +++- src/PocketMinecraftServer.php | 4 ++++ src/utils/Utils.php | 4 ++++ src/world/Level.php | 1 + src/world/Tile.php | 7 +++++++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index eb29459b..ff6aaf2a 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -36,7 +36,7 @@ class ChatAPI{ return "Usage: /say "; } $sender = ($issuer instanceof Player) ? "Server" : ucfirst($issuer); - if(Utils::hasEmoji($s)) return "Your message contains illegal characters!"; + if(Player::$disableEmojisInChat && Utils::hasEmoji($s)) return "Your message contains illegal characters!"; $this->server->api->chat->broadcast("[$sender] " . $s); break; case "me": @@ -54,7 +54,7 @@ class ChatAPI{ $sender = $issuer->username; } $msg = implode(" ", $params); - if(Utils::hasEmoji($msg)) return "Your message contains illegal characters!"; + if(Player::$disableEmojisInChat && Utils::hasEmoji($msg)) return "Your message contains illegal characters!"; $this->broadcast("* $sender $msg"); break; case "tell": diff --git a/src/Player.php b/src/Player.php index 166640cf..4435c930 100755 --- a/src/Player.php +++ b/src/Player.php @@ -6,6 +6,7 @@ class Player{ public static $experimentalHotbar = false; public static $allowDroppingSingleItems = true; + public static $disableEmojisInChat = true; /** @var Config */ public $data; /** @var Entity */ @@ -2502,7 +2503,7 @@ class Player{ $this->server->api->console->run(substr($message, 1), $this); }else{ $data = ["player" => $this, "message" => $message]; - if(Utils::hasEmoji($data["message"])){ + if(Player::$disableEmojisInChat && Utils::hasEmoji($data["message"])){ $this->sendChat("Your message contains illegal characters"); break; } @@ -2712,6 +2713,7 @@ class Player{ if($d["id"] !== TILE_SIGN){ $t->spawn($this); }else{ + $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index b8590262..ac306df3 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -94,8 +94,12 @@ class PocketMinecraftServer{ "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, "use-experimental-hotbar" => Player::$experimentalHotbar, "keep-items-on-death" => Entity::$keepInventory, + "disable-emojis-in-chat" => Player::$disableEmojisInChat, + "disable-emojis-on-signs" => Level::$disableEmojisOnSigns, //"allow-dropping-single-items" => Player::$allowDroppingSingleItems ]); + Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); + Level::$disableEmojisOnSigns = $this->extraprops->get("disable-emojis-on-signs"); Entity::$keepInventory = $this->extraprops->get("keep-items-on-death"); Player::$experimentalHotbar = $this->extraprops->get("use-experimental-hotbar"); //Player::$allowDroppingSingleItems = $this->extraprops->get("allow-dropping-single-items"); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index a6f8a08c..2f146cba 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -77,6 +77,10 @@ class Utils{ return preg_match(Utils::emojiRegex, $s); } + public static function replaceEmoji($s){ + return preg_replace(Utils::emojiRegex, " ", $s); + } + public static function getCallableIdentifier(callable $variable){ if(is_array($variable)){ return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1])); diff --git a/src/world/Level.php b/src/world/Level.php index 9f743577..a3a907a2 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -1,6 +1,7 @@ class !== TILE_SIGN){ return false; } + if(Level::$disableEmojisOnSigns){ + $line1 = Utils::replaceEmoji($line1); + $line2 = Utils::replaceEmoji($line2); + $line3 = Utils::replaceEmoji($line3); + $line4 = Utils::replaceEmoji($line4); + + } $this->data["Text1"] = $line1; $this->data["Text2"] = $line2; $this->data["Text3"] = $line3; From a298667be048ec8a805ccd50779cde4dfec3826a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 26 May 2025 04:35:28 +0300 Subject: [PATCH 029/146] nc will send inventory if crafting was aborted --- src/Player.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Player.php b/src/Player.php index 4435c930..53687d73 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2828,6 +2828,7 @@ class Player{ if(!$cc){ $this->toCraft = []; $this->craftingItems = []; + $this->sendInventory(); } return; } From f5c8b05a0850b4e77038c4c3fd9fe6a77dbb9c53 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 28 May 2025 13:10:00 +0300 Subject: [PATCH 030/146] better inventory sync --- src/Player.php | 128 ++++++++++++++++++++++++++++++-------- src/entity/animal/Pig.php | 2 +- src/material/Item.php | 2 +- 3 files changed, 104 insertions(+), 28 deletions(-) diff --git a/src/Player.php b/src/Player.php index 53687d73..f3d83484 100755 --- a/src/Player.php +++ b/src/Player.php @@ -15,6 +15,10 @@ class Player{ public $CID; public $MTU; public $spawned = false; + /** + * Stores player inventory. Should not be accessed directly: use Player::addItem/Player::removeItem/Player::setSlot for setting and Player::getSlot for getting. + * @var array + */ public $inventory; public $slot; public $hotbar; @@ -104,6 +108,16 @@ class Player{ */ public $isJumping, $isSneaking; + /** + * should server send inventory to a client or not. Should not be changed directly: use Player::sendInventory() to change to true. + * @var boolean + */ + public $sendingInventoryRequired = false; + + public $expectedSetSlotPackets = []; + public $expectedSetSlotIndex = -1; + public $lastExpectedSetSlotIndexReceived = -1; + private $lastPing = -1; /** @@ -148,14 +162,34 @@ class Player{ $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); } - + public function __get($name){ if(isset($this->{$name})){ return ($this->{$name}); } return null; } - + + public function addExpectedSetSlotPacket(int $slot, int $id, int $meta, int $count){ + $this->expectedSetSlotPackets[++$this->expectedSetSlotIndex]["$slot $id $meta $count"] = $this->server->ticks; + } + + public function isExpectedSetSlot(int $slot, Item $item){ + $index = "$slot {$item->getID()} {$item->getMetadata()} {$item->count}"; + foreach($this->expectedSetSlotPackets as $i => $stuff){ + $a = key($stuff); + + if($index == $a){ + return $i; + } + } + return false; + } + + public function removeExpectedSetSlot(int $index){ + unset($this->expectedSetSlotPackets[$index]); + } + public function getSpawn(){ return $this->spawnPosition; } @@ -271,7 +305,6 @@ class Player{ $this->level->players[$this->CID] = $this; $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); - //$this->server->api->entity->spawnAll($this); foreach($this->level->entityList as $e){ if($e->eid !== $this->entity->eid){ if(!$e->isPlayer()){ @@ -561,7 +594,7 @@ class Player{ public function getSlot($slot){ return $this->inventory[(int)$slot] ?? BlockAPI::getItem(AIR, 0, 0); } - + /** * @param integer $slot * @param Item $item @@ -570,10 +603,19 @@ class Player{ * @return boolean */ public function setSlot($slot, Item $item, $send = true){ - $this->inventory[(int) $slot] = $item; + $slot = (int) $slot; + $old = $this->getSlot($slot); + + $this->inventory[$slot] = $item; + if($send === true){ - $this->sendInventorySlot((int) $slot); + $this->sendInventorySlot($slot); } + + //if($old->getID() != $item->getID() || $old->getMetadata() != $item->getMetadata() || $old->count != $item->count){ + $this->addExpectedSetSlotPacket($slot, $item->getID(), $item->getMetadata(), $item->count); + //} + return true; } @@ -793,18 +835,7 @@ class Player{ } public function sendInventory(){ - if(($this->gamemode & 0x01) === CREATIVE){ - return; - } - $hotbar = []; - foreach($this->hotbar as $slot){ - $hotbar[] = $slot <= -1 ? -1 : $slot + 9; - } - $pk = new ContainerSetContentPacket; - $pk->windowid = 0; - $pk->slots = $this->inventory; - $pk->hotbar = $hotbar; - $this->dataPacket($pk); + $this->sendingInventoryRequired = true; } /** @@ -924,6 +955,7 @@ class Player{ $pk->eid = 0; $pk->target = $data["entity"]->eid; $this->dataPacket($pk); + if(($this->gamemode & 0x01) === 0x00){ $this->addItem($data["entity"]->itemID, $data["entity"]->meta, $data["entity"]->stack, false); } @@ -1041,6 +1073,7 @@ class Player{ } $item->count += $add; if($send) $this->sendInventorySlot($s); + $this->addExpectedSetSlotPacket($s, $item->getID(), $item->getMetadata(), $item->count); $count -= $add; if($count <= 0) return true; @@ -1051,7 +1084,10 @@ class Player{ if($item->getID() === AIR){ $add = min($toadd->getMaxStackSize(), $count); $this->inventory[$s] = BlockAPI::getItem($type, $damage, $add); + if($send) $this->sendInventorySlot($s); + + $this->addExpectedSetSlotPacket($s, $type, $damage, $add); $count -= $add; if($count <= 0) return true; } @@ -1311,6 +1347,23 @@ class Player{ $this->ackQueue = []; } + + if($this->sendingInventoryRequired){ + if(($this->gamemode & 0x01) !== CREATIVE){ + $hotbar = []; + foreach($this->hotbar as $slot){ + $hotbar[] = $slot <= -1 ? -1 : $slot + 9; + } + $pk = new ContainerSetContentPacket; + $pk->windowid = 0; + $pk->slots = $this->inventory; + $pk->hotbar = $hotbar; + $this->dataPacket($pk); + } + + $this->sendingInventoryRequired = false; + } + if(($receiveCnt = count($this->receiveQueue)) > 0){ ksort($this->receiveQueue); foreach($this->receiveQueue as $count => $packets){ @@ -1661,6 +1714,15 @@ class Player{ } } + $cnt = 0; + foreach($this->expectedSetSlotPackets as $s => $stuff){ + $b = current($stuff); + if(($this->server->ticks - $b) > 10*20){ //keep slot as expected for 10 seconds, no idea how good will it work + ++$cnt; + $this->removeExpectedSetSlot($s); + } + } + if($cnt > 0) ConsoleAPI::debug("$cnt slots were removed from {$this->iusername}'s expected queue due to timeout"); } public function sendPing() { @@ -2244,8 +2306,7 @@ class Player{ if(++$bow->meta >= $bow->getMaxDurability()){ $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); } - $this->removeItem(ARROW, 0, 1, false); - $this->sendInventory(); + $this->removeItem(ARROW, 0, 1, send: true); } } } @@ -2563,11 +2624,21 @@ class Player{ } if($packet->windowid === 0){ //crafting - $slot = $this->getSlot($packet->slot); $citem = $packet->item; + + if(($n = $this->isExpectedSetSlot($packet->slot, $packet->item)) !== false){ + //ConsoleAPI::debug("Expected setslot at $n found {$packet->item}"); + $this->removeExpectedSetSlot($n); + break; + }else{ + //ConsoleAPI::info("unexpected setslot, assuming caused by crafting {$packet->slot} {$packet->item}"); + } + if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ + if($citem->count > $slot->count){ //item added, result + console("{$citem->count} {$slot->count}"); $this->addCraftingResult($packet->slot, $slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); }else if($citem->count < $slot->count){ //item removed, ingridient $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); @@ -2828,7 +2899,7 @@ class Player{ if(!$cc){ $this->toCraft = []; $this->craftingItems = []; - $this->sendInventory(); + return false; } return; } @@ -2899,7 +2970,7 @@ class Player{ if(!isset($this->toCraft[$index][$slot])) $this->toCraft[$index][$slot] = 0; $this->toCraft[$index][$slot] += $count; - //console("Result: $id, $meta, $count into $slot"); + console("Result: $id, $meta, $count into $slot"); if($this->tryCraft() === false){ $this->sendInventory(); } @@ -2935,14 +3006,19 @@ class Player{ foreach($this->inventory as $s => $item){ if($item->getID() === $type and $item->getMetadata() === $damage){ $remove = min($count, $item->count); + if($remove < $item->count){ $item->count -= $remove; + $exid = $item->getID(); + $exmeta = $item->getMetadata(); + $excnt = $item->count; }else{ $this->inventory[$s] = BlockAPI::getItem(AIR, 0, 0); + $exid = $exmeta = $excnt = 0; } - if($send === true){ - $this->sendInventorySlot($s); - } + if($send === true) $this->sendInventorySlot($s); + + $this->addExpectedSetSlotPacket($s, $exid, $exmeta, $excnt); break; } } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index d8bfe219..93aabfb3 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -62,7 +62,7 @@ class Pig extends Animal implements Rideable{ if($slot->getID() === SADDLE){ if(!$this->isSaddled()){ - $e->player->removeItem($slot->getID(), 0, 1); + $e->player->removeItem($slot->getID(), 0, 1, send: true); $this->setSaddled(1); } return true; //avoid further interactions diff --git a/src/material/Item.php b/src/material/Item.php index 7b92221a..f8f535b6 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -295,7 +295,7 @@ class Item{ } public function __toString(){ - return "Item ". $this->name ." (".$this->id.":".$this->meta.")"; + return "Item ". $this->name ." (".$this->id.":".$this->meta."x{$this->count})"; } public function getDamageAgainstOf($e){ From 4880be54521693380c6df9d018278ba293cd64d3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 28 May 2025 18:37:02 +0300 Subject: [PATCH 031/146] removed debug stuff --- src/Player.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index f3d83484..1e089166 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2638,7 +2638,6 @@ class Player{ if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ if($citem->count > $slot->count){ //item added, result - console("{$citem->count} {$slot->count}"); $this->addCraftingResult($packet->slot, $slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); }else if($citem->count < $slot->count){ //item removed, ingridient $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); @@ -2970,7 +2969,7 @@ class Player{ if(!isset($this->toCraft[$index][$slot])) $this->toCraft[$index][$slot] = 0; $this->toCraft[$index][$slot] += $count; - console("Result: $id, $meta, $count into $slot"); + //console("Result: $id, $meta, $count into $slot"); if($this->tryCraft() === false){ $this->sendInventory(); } From 841e12b9eaf8997de941f895a9f4a0f47cbafb0d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 29 May 2025 05:30:33 +0300 Subject: [PATCH 032/146] rewritten how item durability/count is handled on serverside --- src/API/BlockAPI.php | 19 +++------ src/Player.php | 42 ++++++++++++------- src/entity/Animal.php | 4 +- src/entity/Entity.php | 22 ++++++---- src/entity/animal/Cow.php | 2 +- src/entity/animal/Pig.php | 2 +- src/entity/animal/Sheep.php | 11 +---- src/entity/monster/Creeper.php | 8 +--- src/material/Item.php | 33 +++++++++++---- src/material/block/misc/TNTBlock.php | 4 +- src/material/block/plant/BeetrootBlock.php | 4 +- src/material/block/plant/CarrotBlock.php | 4 +- src/material/block/plant/CyanFlowerBlock.php | 4 +- src/material/block/plant/DandelionBlock.php | 4 +- src/material/block/plant/MelonStemBlock.php | 4 +- src/material/block/plant/PotatoBlock.php | 4 +- src/material/block/plant/PumpkinStemBlock.php | 4 +- src/material/block/plant/SaplingBlock.php | 4 +- src/material/block/plant/SugarcaneBlock.php | 4 +- src/material/block/plant/WheatBlock.php | 4 +- src/material/block/solid/DirtBlock.php | 6 +-- src/material/block/solid/GrassBlock.php | 10 +---- src/material/item/base/ItemHoe.php | 9 ---- src/material/item/base/ItemSword.php | 10 +++++ src/material/item/base/ItemTool.php | 25 +++++++---- src/material/item/generic/MinecartItem.php | 4 +- src/material/item/generic/PaintingItem.php | 7 ++-- src/material/item/generic/SpawnEggItem.php | 4 +- src/material/item/tool/FlintSteelItem.php | 10 +---- src/material/item/tool/ShearsItem.php | 8 ++-- 30 files changed, 128 insertions(+), 152 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 0e111670..8d9eef60 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -415,9 +415,8 @@ class BlockAPI{ if($target->onBreak($item, $player) === false){ return $this->cancelAction($target, $player, false); } - if(($player->gamemode & 0x01) === 0 and $item->useOn($target) and $item->getMetadata() >= $item->getMaxDurability()){ - $player->setSlot($player->slot, new Item(AIR, 0, 0), false); - } + + $item->mineBlock($target, $player); }else{ return $this->cancelAction($target, $player, false); } @@ -446,7 +445,7 @@ class BlockAPI{ $target = $player->level->getBlock($vector); $block = $target->getSide($face); - $item = $player->getSlot($player->slot); + $item = $player->getHeldItem(); if($target->getID() === AIR and $this->server->api->dhandle("player.block.place.invalid", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ //If no block exists or not allowed in CREATIVE if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ @@ -479,10 +478,7 @@ class BlockAPI{ return false; } - if($item->isActivable === true and $item->onActivate($player->level, $player, $block, $target, $face, $fx, $fy, $fz) === true){ - if($item->count <= 0){ - $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0), false); - } + if($item->isActivable === true && $item->onActivate($player->level, $player, $block, $target, $face, $fx, $fy, $fz) === true){ return false; } @@ -528,12 +524,7 @@ class BlockAPI{ $t->data["creator"] = $player->username; } - if(($player->gamemode & 0x01) === 0x00){ - --$item->count; - if($item->count <= 0){ - $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0), false); - } - } + $player->consumeSingleItem(); return false; } diff --git a/src/Player.php b/src/Player.php index 1e089166..c6acd15a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -23,6 +23,9 @@ class Player{ public $slot; public $hotbar; public $curHotbarIndex = 0; + /** + * @var Item[] + */ public $armor = []; public $loggedIn = false; public $gamemode; @@ -193,7 +196,7 @@ class Player{ public function getSpawn(){ return $this->spawnPosition; } - + /** * @param Vector3 $pos * @@ -2302,10 +2305,7 @@ class Player{ $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); if(($this->gamemode & 0x01) == 0x0) { - $bow = $this->getSlot($this->slot); - if(++$bow->meta >= $bow->getMaxDurability()){ - $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); - } + $this->getHeldItem()->hurtAndBreak(1, $this); $this->removeItem(ARROW, 0, 1, send: true); } } @@ -2652,7 +2652,6 @@ class Player{ $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count); $this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count); } - }else{ $this->toCraft = []; $this->craftingItems = []; @@ -2998,7 +2997,22 @@ class Player{ } return false; } - + + /** + * Removes a single item from whatever player is holding. + * @param boolean $send Force send inventory to player. + */ + public function consumeSingleItem($send = false){ + $it = $this->getHeldItem(); + if(($this->gamemode & 0x01) === 0x00) --$it->count; + + if($it->count <= 0){ + $this->setSlot($this->slot, BlockAPI::getItem(0, 0, 0), false); + } + + if($send) $this->sendInventory(); + } + public function removeItem($type, $damage, $count, $send = true){ while($count > 0){ $remove = 0; @@ -3089,15 +3103,6 @@ class Player{ } } - public function damageArmorPart($slot, $part){ - $part->useOn($this->entity, true); - if($part->getMetadata() >= $part->getMaxDurability()){ - $this->setArmor($slot, BlockAPI::getItem(AIR, 0, 0), false); - return; - } - $this->setArmor($slot, $part, false); - } - /** * @return string */ @@ -3121,6 +3126,11 @@ class Player{ return false; } + /** + * @deprecated armor is damaged by Entity::hurtArmor and cant be damaged by singular pieces + */ + public function damageArmorPart($slot, $part){} + /** * @deprecated 16x16x16 chunk sending was removed */ diff --git a/src/entity/Animal.php b/src/entity/Animal.php index b21aab0d..1f2bc748 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -140,9 +140,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby() && $this->loveTimeout <= 0){ - if(($e->player->gamemode & 0x01) === SURVIVAL){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1); - } + $e->player->consumeSingleItem(); $this->inLove = 600; return true; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 68dffc21..9176c260 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1047,11 +1047,7 @@ class Entity extends Position $slot = $e->player->getHeldItem(); $damage = $slot->getDamageAgainstOf($this); $this->harm($damage, $e->eid); - if($slot->isTool() && (!$this->isPlayer() || ($this->player->gamemode & 0x01) === 0)){ - if($slot->useOn($this) and $slot->getMetadata() >= $slot->getMaxDurability()){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); - } - } + $slot->hurtEnemy($this, $e->player); //TODO dont try destroying if harm was cancelled? return true; } return false; @@ -1558,16 +1554,28 @@ class Entity extends Position foreach($this->player->armor as $slot => $part){ if($part instanceof ArmorItem){ $pnts += $part->getDamageReduceAmount(); - $this->player->damageArmorPart($slot, $part); } } - $this->player->sendArmor($this->player); } return $pnts; } + + public function hurtArmor($dmg){ + if($this->isPlayer()){ + $v2 = (int)($dmg / 4); + if($v2 < 1) $v2 = 1; + + foreach($this->player->armor as $slot => $part){ + $part->hurtAndBreak($v2, $this->player); + } + $this->player->sendArmor(); + } + } + public function applyArmor($damage, $cause){ if(is_numeric($cause) || $cause === "explosion"){ $var3 = 25 - $this->getArmorValue(); + $this->hurtArmor($damage); //uses damage without armor affection $var4 = $damage * $var3 + $this->carryoverDamage; $damage = $var4 / 25; $this->carryoverDamage = $var4 % 25; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index 441ff126..e8a18386 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -26,7 +26,7 @@ class Cow extends Animal{ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); if($slot->getID() === BUCKET && $slot->getMetadata() === 0){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); //remove only 1 bucket + $e->player->consumeSingleItem(); $e->player->addItem(BUCKET, 1, 1, true); return true; } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 93aabfb3..46cd725b 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -62,7 +62,7 @@ class Pig extends Animal implements Rideable{ if($slot->getID() === SADDLE){ if(!$this->isSaddled()){ - $e->player->removeItem($slot->getID(), 0, 1, send: true); + $e->player->consumeSingleItem(send: true); $this->setSaddled(1); } return true; //avoid further interactions diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index d79c8701..3a7fe09f 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -75,25 +75,18 @@ class Sheep extends Animal{ $slot = $e->player->getHeldItem(); if($slot->getID() === SHEARS){ if(!$this->isSheared() && !$this->isBaby()){ - if($e->player->gamemode != 1) $slot->useOn($this); $this->setSheared(1); $speedX = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1; $speedZ = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1; $speedY = 0.2 + (lcg_value()) * 0.05; $this->server->api->entity->dropRawPos($this->level, $this->x, $this->y + 1, $this->z, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)), $speedX, $speedY, $speedZ); - if($slot->getMetadata() >= $slot->getMaxDurability()){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true); - }else{ - $e->player->setSlot($e->player->slot, $slot); - } + $slot->hurtAndBreak(1, $e->player); } return true; }elseif($slot->getID() === DYE){ $this->setColor($this->switchColorMeta($slot->getMetadata())); - if(($e->player->gamemode & 0x01) === SURVIVAL){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); - } + $e->player->consumeSingleItem(); } } return parent::interactWith($e, $action); diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index b1279f9c..12bd50e6 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -27,8 +27,6 @@ class Creeper extends Monster{ $this->setState($v === null ? !$this->getState() : $v); } - - /** * @return boolean */ @@ -40,11 +38,7 @@ class Creeper extends Monster{ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); if($slot->getID() === FLINT_AND_STEEL && !$this->isIgnited()){ - if($slot->useOn($this) && $slot->getMetadata() >= $slot->getMaxDurability()){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true); - }else{ - $e->player->setSlot($e->player->slot, $slot); - } + $slot->hurtAndBreak(1, $e->player); $this->ignite(); return true; } diff --git a/src/material/Item.php b/src/material/Item.php index f8f535b6..fa194dde 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -220,18 +220,30 @@ class Item{ } - public function useOn($object, $force = false){ - if($force){ - if(($object instanceof Entity) and !$this->isSword()){ - $this->meta += 2; - }else{ - $this->meta++; + /** + * @param int $dmg - damage dealt to item + * @param Player $player - player who used the item + */ + public function hurtAndBreak($dmg, Player $player){ + if(($player->gamemode & 1) !== 0) return; //dont break items in creative + + if($this->getMaxDurability() !== false){ + $this->meta += $dmg; + if($this->meta > $this->getMaxDurability()){ + $player->consumeSingleItem(); } - return true; } + } + + public function hurtEnemy(Entity $target, Player $attacker){ + + } + + public function mineBlock(Block $block, Player $player){ return false; } + public function isTool(){ return false; } @@ -334,4 +346,11 @@ class Item{ }; } + /** + * @deprecated Item damaging changed. Use hurtAndBreak to damage the item and update it in the client's inventory correctly. + */ + public function useOn($object, $force = false){ + return false; + } + } diff --git a/src/material/block/misc/TNTBlock.php b/src/material/block/misc/TNTBlock.php index aeef0412..5fb3da3e 100644 --- a/src/material/block/misc/TNTBlock.php +++ b/src/material/block/misc/TNTBlock.php @@ -10,9 +10,7 @@ class TNTBlock extends SolidBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === FLINT_STEEL){ - if(($player->gamemode & 0x01) === 0){ - $item->useOn($this); - } + $item->hurtAndBreak(1, $player); $data = array( "x" => $this->x + 0.5, "y" => $this->y, diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index c1b51d73..88a22d4d 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -32,9 +32,7 @@ class BeetrootBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index 498399f4..36c3e931 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -25,9 +25,7 @@ class CarrotBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/CyanFlowerBlock.php b/src/material/block/plant/CyanFlowerBlock.php index 8486ede6..16c3c97c 100644 --- a/src/material/block/plant/CyanFlowerBlock.php +++ b/src/material/block/plant/CyanFlowerBlock.php @@ -34,11 +34,9 @@ class CyanFlowerBlock extends FlowableBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } $random = new Random(); self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2); + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index 236fa778..9c61d1bd 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -29,11 +29,9 @@ class DandelionBlock extends FlowableBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } $random = new Random(); self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2); + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index a9e3c542..2022f819 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -58,9 +58,7 @@ class MelonStemBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index 3ab969fe..54ec8cec 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -34,9 +34,7 @@ class PotatoBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index c7492623..a40f9f5f 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -58,9 +58,7 @@ class PumpkinStemBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index a6acde15..86cc8048 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -36,9 +36,7 @@ class SaplingBlock extends FlowableBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal TreeObject::growTree($this->level, $this, new Random(), $this->meta & 0x03); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/SugarcaneBlock.php b/src/material/block/plant/SugarcaneBlock.php index d4928460..f17fd596 100644 --- a/src/material/block/plant/SugarcaneBlock.php +++ b/src/material/block/plant/SugarcaneBlock.php @@ -29,9 +29,7 @@ class SugarcaneBlock extends FlowableBlock{ $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $this->meta, true); } - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 53bf67f4..176a2065 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -25,9 +25,7 @@ class WheatBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index bf96b7c2..46131b49 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -11,11 +11,7 @@ class DirtBlock extends SolidBlock{ public function onActivate(Item $item, Player $player){ if($item->isHoe()){ if($this->getSide(1)->isTransparent === false) return false; - if(($player->gamemode & 0x01) === 0){ - $item->useOn($this); - if($item->getMetadata() >= $item->getMaxDurability()) $player->setSlot($player->slot, new Item(AIR, 0, 0), false); - else $player->setSlot($player->slot, $item, true); - } + $item->hurtAndBreak(1, $player); //TODO move to ItemHoe $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, FARMLAND, 0, true); return true; } diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index d6181801..c598b134 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -16,18 +16,12 @@ class GrassBlock extends SolidBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ - if(($player->gamemode & 0x01) === 0){ - $player->removeItem(DYE,0x0F,1); - } + $player->consumeSingleItem(); TallGrassObject::growGrass($this->level, $this, new Random(), 8, 2); return true; }elseif($item->isHoe()){ if($this->getSide(1)->isTransparent === false) return false; - if(($player->gamemode & 0x01) === 0){ - $item->useOn($this); - if($item->getMetadata() >= $item->getMaxDurability()) $player->setSlot($player->slot, new Item(AIR, 0, 0), false); - else $player->setSlot($player->slot, $item, true); - } + $item->hurtAndBreak(1, $player); //TODO move to ItemHoe $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, FARMLAND, 0, true); $this->seedsDrop(); return true; diff --git a/src/material/item/base/ItemHoe.php b/src/material/item/base/ItemHoe.php index 277b75fb..0c3cb416 100644 --- a/src/material/item/base/ItemHoe.php +++ b/src/material/item/base/ItemHoe.php @@ -5,15 +5,6 @@ abstract class ItemHoe extends ItemTool public function isHoe(){ return true; } - - public function useOn($object, $force = false){ - if(($object instanceof Block) and ($object->getID() === GRASS or $object->getID() === DIRT)){ - $this->meta++; - return true; - }else{ - return parent::useOn($object, $force); - } - } public function getLevel(){ return match ($this->id) { diff --git a/src/material/item/base/ItemSword.php b/src/material/item/base/ItemSword.php index 6bd725e0..14bd259a 100644 --- a/src/material/item/base/ItemSword.php +++ b/src/material/item/base/ItemSword.php @@ -17,5 +17,15 @@ abstract class ItemSword extends ItemTool default => false, }; } + + public function hurtEnemy(Entity $target, Player $attacker){ + $this->hurtAndBreak(1, $attacker); + } + + + public function mineBlock(Block $block, Player $player){ + $this->hurtAndBreak(2, $player); + return true; + } } diff --git a/src/material/item/base/ItemTool.php b/src/material/item/base/ItemTool.php index e5a3af25..061cc065 100644 --- a/src/material/item/base/ItemTool.php +++ b/src/material/item/base/ItemTool.php @@ -11,16 +11,23 @@ abstract class ItemTool extends Item public function isTool(){ return true; } - public function useOn($object, $force = false){ - - if($this->isSword() && !($object instanceof Entity)){ - $this->meta += 2; - }else if(($object instanceof Entity) && !$this->isSword()){ - $this->meta += 2; - }else{ - $this->meta++; + + public function hurtEnemy(Entity $target, Player $attacker){ + if($this->isPickaxe() || $this->isAxe() || $this->isShovel()){ + $this->hurtAndBreak(2, $attacker); + return; } - return true; + + parent::hurtEnemy($target, $attacker); + } + + public function mineBlock(Block $block, Player $player){ + if($this->isPickaxe() || $this->isAxe() || $this->isShovel()){ + $this->hurtAndBreak(1, $player); + return true; + } + + return parent::mineBlock($block, $player); } } diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index b8923748..19475fea 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -18,8 +18,6 @@ class MinecartItem extends Item{ ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); $server->api->entity->spawnToAll($e); - if(($player->gamemode & 0x01) === 0x00){ - $player->removeItem($this->getID(), $this->getMetadata(), 1, false); - } + $player->consumeSingleItem(); } } \ No newline at end of file diff --git a/src/material/item/generic/PaintingItem.php b/src/material/item/generic/PaintingItem.php index 4ce688c8..329e9222 100644 --- a/src/material/item/generic/PaintingItem.php +++ b/src/material/item/generic/PaintingItem.php @@ -38,7 +38,7 @@ class PaintingItem extends Item{ private static $right = array(4, 5, 3, 2); public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - if($target->isTransparent === false and $face > 1 and $block->isSolid === false){ + if($target->isTransparent === false && $face > 1 and $block->isSolid === false){ $server = ServerAPI::request(); if($face < 2 || $face > 5) return; @@ -65,9 +65,8 @@ class PaintingItem extends Item{ $painting->eid = $server->api->entity->getNextEID(); $server->api->entity->addRaw($painting); $server->api->entity->spawnToAll($painting); - if(($player->gamemode & 0x01) === 0x00){ - $player->removeItem($this->getID(), $this->getMetadata(), 1, false); - } + + $player->consumeSingleItem(); return true; } return false; diff --git a/src/material/item/generic/SpawnEggItem.php b/src/material/item/generic/SpawnEggItem.php index f41e9a01..68de416d 100644 --- a/src/material/item/generic/SpawnEggItem.php +++ b/src/material/item/generic/SpawnEggItem.php @@ -17,9 +17,7 @@ class SpawnEggItem extends Item{ ); $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); ServerAPI::request()->api->entity->spawnToAll($e); - if(($player->gamemode & 0x01) === 0){ - -- $this->count; - } + $player->consumeSingleItem(); return true; } } \ No newline at end of file diff --git a/src/material/item/tool/FlintSteelItem.php b/src/material/item/tool/FlintSteelItem.php index 134b160a..b9a9f825 100644 --- a/src/material/item/tool/FlintSteelItem.php +++ b/src/material/item/tool/FlintSteelItem.php @@ -8,19 +8,11 @@ class FlintSteelItem extends ItemTool{ } public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getMetadata() >= $this->getMaxDurability()){ - $player->setSlot($player->slot, new Item(AIR, 0, 0), false); - } - + $this->hurtAndBreak(1, $player); if($block->getID() === AIR && $target->isSolid){ $level->fastSetBlockUpdate($block->x, $block->y, $block->z, FIRE, 0, true); return true; } return false; } - - public function useOn($object, $force = false){ - ++$this->meta; - return true; - } } \ No newline at end of file diff --git a/src/material/item/tool/ShearsItem.php b/src/material/item/tool/ShearsItem.php index 059ede72..db14e68c 100755 --- a/src/material/item/tool/ShearsItem.php +++ b/src/material/item/tool/ShearsItem.php @@ -5,11 +5,11 @@ class ShearsItem extends ItemTool{ parent::__construct(SHEARS, $meta, $count, "Shears"); } - public function useOn($object, $force = false){ - if(($object instanceof Sheep) and $this->id === SHEARS){ - $this->meta++; + public function mineBlock(Block $block, Player $player){ + if($block->getID() === LEAVES || $block->getID() === COBWEB){ + $this->hurtAndBreak(1, $player); return true; } - parent::useOn($object, $force); + return parent::mineBlock($block, $player); } } \ No newline at end of file From a485d7506bb69d63e5f382aa48dd788c2b6d8737 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 29 May 2025 08:53:13 +0300 Subject: [PATCH 033/146] fixed pumpkins dropping themselves with incorrect meta --- src/material/block/solid/PumpkinBlock.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/material/block/solid/PumpkinBlock.php b/src/material/block/solid/PumpkinBlock.php index 25239421..84aff6b3 100644 --- a/src/material/block/solid/PumpkinBlock.php +++ b/src/material/block/solid/PumpkinBlock.php @@ -19,4 +19,9 @@ class PumpkinBlock extends SolidBlock{ return true; } + public function getDrops(Item $item, Player $player){ + return [ + [PUMPKIN, 0, 1] + ]; + } } \ No newline at end of file From 38ced36e167ae14384cea2d4fbfcb46d3a1f9099 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 30 May 2025 02:26:33 +0300 Subject: [PATCH 034/146] ops/console/rcon can view coords and ping of other players now --- src/API/PlayerAPI.php | 52 +++++++++++++++++++++++++++++++++++-------- src/Player.php | 3 ++- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 7d6d0c2f..7e4b6f36 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -123,10 +123,25 @@ class PlayerAPI{ $issuer->teleport($this->server->spawn); return "You were teleported to the spawn."; case "ping": + if(!($issuer instanceof Player)){ - return "Please run this command in-game."; + if(isset($args[0])){ + goto get_ping_of_others; + } + return "Usage: /$cmd ."; } - return "ping " . round($issuer->getLag(), 2) . "ms, packet loss " . round($issuer->getPacketLoss() * 100, 2) . "%, " . round($issuer->getBandwidth() / 1024, 2) . " KB/s\n"; + + if(isset($args[0]) && $this->server->api->ban->isOp($issuer->iusername)){ + get_ping_of_others: + $p = $this->get($args[0]); + if(!($p instanceof Player)){ + return "Player with username '{$args[0]}' is not on server."; + } + }else{ + $p = $issuer; + } + + return "{$p->username}'s ping: " . round($p->getLag(), 2) . "ms, packet loss " . round($p->getPacketLoss() * 100, 2) . "%, " . round($p->getBandwidth() / 1024, 2) . " KB/s\n"; case "gamemode": $player = false; $setgm = false; @@ -228,18 +243,37 @@ class PlayerAPI{ } return substr($output, 0, -2) . "\n"; case "loc": - if(!($issuer instanceof Player)) return "Please run this command in-game."; - $x = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP); - $y = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP); - $z = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP); - $level = $issuer->entity->level->getName(); + if(!($issuer instanceof Player)){ + if(isset($args[0])){ + goto get_coords_of_others; + } + return "Usage: /$cmd ."; + } + + if(isset($args[0]) && $this->server->api->ban->isOp($issuer->iusername)){ + get_coords_of_others: + $p = $this->get($args[0]); + if(!($p instanceof Player)){ + return "Player with username '{$args[0]}' is not on server."; + } + $ent = $p->entity; + $username = $p->username; + }else{ + $ent = $issuer->entity; + $username = $issuer->username; + } + + $x = round($ent->x, 1, PHP_ROUND_HALF_UP); + $y = round($ent->y, 1, PHP_ROUND_HALF_UP); + $z = round($ent->z, 1, PHP_ROUND_HALF_UP); + $level = $ent->level->getName(); $compass = [0 => "X+", 1 => "Z+", 2 => "X-", 3 => "Z-", null => "null"]; - $direction = $compass[$issuer->entity->getDirection()]; + $direction = $compass[$ent->getDirection()]; $xChunk = $x >> 4; $zChunk = $z >> 4; - return "Your coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction"; + return "$username's coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction"; } return $output; } diff --git a/src/Player.php b/src/Player.php index c6acd15a..af513fcd 100755 --- a/src/Player.php +++ b/src/Player.php @@ -56,7 +56,7 @@ class Player{ private $ip; private $port; private $counter = [0, 0, 0, 0]; - private $username; + public $username; public $iusername; private $eid = false; private $startAction = false; @@ -1132,6 +1132,7 @@ class Player{ } } } + public function makeInvisibleForOnePlayer(Player $player){ $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; From 2838640649dcee88f00171cb94fdd617b6ab3538 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 30 May 2025 03:25:03 +0300 Subject: [PATCH 035/146] fixed falling from half blocks causing farmland to disappeat --- src/entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9176c260..9fa7297f 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -964,7 +964,7 @@ class Entity extends Position $bid = $this->level->level->getBlockID($x, $y, $z); if($bid > 0){ $clz = StaticBlock::getBlock($bid); - $clz::fallOn($this->level, $x, $y, $z, $this, ceil($this->fallStart - $this->y)); + $clz::fallOn($this->level, $x, $y, $z, $this, $this->fallStart - $this->y); } $dmg = ceil($this->fallStart - $this->y - 3); From b272c1ec6a4c99ef6cee731e11372d134c0e478f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 30 May 2025 09:27:24 +0300 Subject: [PATCH 036/146] inv wont be sent on successful crafting now --- src/API/BlockAPI.php | 2 +- src/Player.php | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 8d9eef60..0033c89a 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -489,7 +489,7 @@ class BlockAPI{ $player->level->fastSetBlockUpdate($block->x, $block->y, $block->z, 0, 0, true, false); return false; }else{ - return $this->cancelAction($block, $player); + return $this->cancelAction($block, $player, false); } if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){ diff --git a/src/Player.php b/src/Player.php index af513fcd..46214af2 100755 --- a/src/Player.php +++ b/src/Player.php @@ -605,7 +605,7 @@ class Player{ * * @return boolean */ - public function setSlot($slot, Item $item, $send = true){ + public function setSlot($slot, Item $item, $send = true, $addexpected = true){ $slot = (int) $slot; $old = $this->getSlot($slot); @@ -615,9 +615,9 @@ class Player{ $this->sendInventorySlot($slot); } - //if($old->getID() != $item->getID() || $old->getMetadata() != $item->getMetadata() || $old->count != $item->count){ + if($addexpected){ $this->addExpectedSetSlotPacket($slot, $item->getID(), $item->getMetadata(), $item->count); - //} + } return true; } @@ -1065,7 +1065,7 @@ class Player{ * * @return boolean */ - public function addItem($type, $damage, $count, $send = true){ + public function addItem($type, $damage, $count, $send = true, $addexpected = true){ foreach($this->inventory as $s => $item){ //force check the inventory for non-full stacks of this item first if($item->getID() === $type and $item->getMetadata() === $damage){ @@ -1076,7 +1076,7 @@ class Player{ } $item->count += $add; if($send) $this->sendInventorySlot($s); - $this->addExpectedSetSlotPacket($s, $item->getID(), $item->getMetadata(), $item->count); + if($addexpected) $this->addExpectedSetSlotPacket($s, $item->getID(), $item->getMetadata(), $item->count); $count -= $add; if($count <= 0) return true; @@ -1090,7 +1090,7 @@ class Player{ if($send) $this->sendInventorySlot($s); - $this->addExpectedSetSlotPacket($s, $type, $damage, $add); + if($addexpected) $this->addExpectedSetSlotPacket($s, $type, $damage, $add); $count -= $add; if($count <= 0) return true; } @@ -1353,6 +1353,7 @@ class Player{ if($this->sendingInventoryRequired){ + //console("inv resent"); if(($this->gamemode & 0x01) !== CREATIVE){ $hotbar = []; foreach($this->hotbar as $slot){ @@ -2637,7 +2638,6 @@ class Player{ } if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ - if($citem->count > $slot->count){ //item added, result $this->addCraftingResult($packet->slot, $slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); }else if($citem->count < $slot->count){ //item removed, ingridient @@ -2909,6 +2909,7 @@ class Player{ return false; } + //ConsoleAPI::debug("crafted successfully"); foreach($this->craftingItems as $i => $slotz){ $id = $i >> 16; $meta = $i & 0xffff; @@ -2916,7 +2917,7 @@ class Player{ $slt = $this->getSlot($slot); if($slt->getID() == $id && $slt->getMetadata() == $meta){ $slt->count -= $count; - if($slt->count <= 0) $this->setSlot($slot, BlockAPI::getItem(0, 0, 0), false); + if($slt->count <= 0) $this->setSlot($slot, BlockAPI::getItem(0, 0, 0), false, addexpected: false); }else{ ConsoleAPI::warn("{$slt->getID()} != $id && {$slt->getMetadata()} != $meta!!!"); } @@ -2946,7 +2947,7 @@ class Player{ if($slt->getID() == $id && $slt->getMetadata() == $meta){ $slt->count += $count; }else if($slt->getID() == 0){ - $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false); + $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false, addexpected: false); }else{ ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!"); } @@ -2954,7 +2955,6 @@ class Player{ } } - $this->sendInventory(); $this->toCraft = []; $this->craftingItems = []; } @@ -3014,7 +3014,7 @@ class Player{ if($send) $this->sendInventory(); } - public function removeItem($type, $damage, $count, $send = true){ + public function removeItem($type, $damage, $count, $send = true, $addexpected = true){ while($count > 0){ $remove = 0; foreach($this->inventory as $s => $item){ @@ -3032,7 +3032,7 @@ class Player{ } if($send === true) $this->sendInventorySlot($s); - $this->addExpectedSetSlotPacket($s, $exid, $exmeta, $excnt); + if($addexpected) $this->addExpectedSetSlotPacket($s, $exid, $exmeta, $excnt); break; } } From 02a038a7f7ec238032e64ec076e94f6e992d57b7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 30 May 2025 09:43:46 +0300 Subject: [PATCH 037/146] fixed potential loss of resources in hotbar if armor will break, damaging an item will be registred in expected slots --- src/entity/Entity.php | 2 +- src/material/Item.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9fa7297f..43a6183c 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1566,7 +1566,7 @@ class Entity extends Position if($v2 < 1) $v2 = 1; foreach($this->player->armor as $slot => $part){ - $part->hurtAndBreak($v2, $this->player); + $part->hurtAndBreak($v2, $this->player, helditem: false); } $this->player->sendArmor(); } diff --git a/src/material/Item.php b/src/material/Item.php index fa194dde..f67a52be 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -223,14 +223,19 @@ class Item{ /** * @param int $dmg - damage dealt to item * @param Player $player - player who used the item + * @param boolean $helditem - modify player's helditem state or not - should be false if using by non-held item(armor damaging for example) */ - public function hurtAndBreak($dmg, Player $player){ + public function hurtAndBreak($dmg, Player $player, $helditem = true){ if(($player->gamemode & 1) !== 0) return; //dont break items in creative if($this->getMaxDurability() !== false){ $this->meta += $dmg; - if($this->meta > $this->getMaxDurability()){ - $player->consumeSingleItem(); + + if($helditem){ + $player->setSlot($player->slot, $this, send: false); + if($this->meta > $this->getMaxDurability()){ + $player->consumeSingleItem(); + } } } } From 294745fb0963abcffb89dcf6257df4547d543965 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 30 May 2025 10:11:25 +0300 Subject: [PATCH 038/146] armor will actually be removed now --- src/Player.php | 2 +- src/entity/Entity.php | 1 + src/material/Item.php | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 46214af2..503993a6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2355,7 +2355,7 @@ class Player{ $slot = $this->armor[$i]; if($slot->getID() !== AIR && $s->getID() === AIR){ - $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); + $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 43a6183c..bcf59a55 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1567,6 +1567,7 @@ class Entity extends Position foreach($this->player->armor as $slot => $part){ $part->hurtAndBreak($v2, $this->player, helditem: false); + if($part->count <= 0) $this->player->setArmor($slot, BlockAPI::getItem(0, 0, 0), send: false); } $this->player->sendArmor(); } diff --git a/src/material/Item.php b/src/material/Item.php index f67a52be..104be38b 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -236,6 +236,10 @@ class Item{ if($this->meta > $this->getMaxDurability()){ $player->consumeSingleItem(); } + }else{ + if($this->meta > $this->getMaxDurability()){ + --$this->count; + } } } } From e5562d81f4af9863e5715d1e1ec03230499f5b65 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 31 May 2025 02:56:23 +0300 Subject: [PATCH 039/146] added ability to drop single items(atipls when repocket) --- src/Player.php | 21 +++++++++++++++++---- src/PocketMinecraftServer.php | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Player.php b/src/Player.php index 503993a6..14cbf6d0 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2494,14 +2494,24 @@ class Player{ $packet->eid = $this->eid; $prevItem = $packet->item; - $newItem = $this->getSlot($this->slot); + $newItem = $this->getHeldItem(); + //TODO it is possible to drop non-held item in vanilla, but in 99% cases it wouldnt happen + if($newItem->getID() != $prevItem->getID() || $newItem->getMetadata() != $prevItem->getMetadata()){ + ConsoleAPI::debug("{$this->username} tried dropping item from non-held slot or inventory desynchronized."); + $this->sendInventory(); + return; + } + if($newItem->count < $prevItem->count){ ConsoleAPI::warn("{$this->username} tried dropping too many items(serverside stack has {$newItem->count}, tried dropping {$prevItem->count}."); $this->sendInventory(); return; } - - $packet->item = $newItem; + if(Player::$allowDroppingSingleItems){ + $packet->item = BlockAPI::getItem($newItem->getID(), $newItem->getMetadata(), $prevItem->count); + }else{ + $packet->item = $newItem; + } $sendOnDrop = false; if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ @@ -2538,7 +2548,10 @@ class Player{ $sY += ($this->entity->random->nextFloat() - $this->entity->random->nextFloat()) * 0.1; $sZ += sin($f3) * $f1; $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, $packet->item, $sX, $sY, $sZ); - $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), $sendOnDrop); + $newItem->count -= $packet->item->count; + if($newItem->count <= 0){ + $this->setSlot($this->slot, BlockAPI::getItem(0, 0, 0), false); + } }else{ $this->sendInventory(); //send if blocked } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index ac306df3..ded4a551 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -96,13 +96,13 @@ class PocketMinecraftServer{ "keep-items-on-death" => Entity::$keepInventory, "disable-emojis-in-chat" => Player::$disableEmojisInChat, "disable-emojis-on-signs" => Level::$disableEmojisOnSigns, - //"allow-dropping-single-items" => Player::$allowDroppingSingleItems + "allow-dropping-single-items" => Player::$allowDroppingSingleItems ]); Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); Level::$disableEmojisOnSigns = $this->extraprops->get("disable-emojis-on-signs"); Entity::$keepInventory = $this->extraprops->get("keep-items-on-death"); Player::$experimentalHotbar = $this->extraprops->get("use-experimental-hotbar"); - //Player::$allowDroppingSingleItems = $this->extraprops->get("allow-dropping-single-items"); + Player::$allowDroppingSingleItems = $this->extraprops->get("allow-dropping-single-items"); Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); From 0eb5e0ea7d1744e3a1285c6c0936b541fd39bdfe Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 2 Jun 2025 08:42:44 +0300 Subject: [PATCH 040/146] dont create RakNetCodec/RakNetParser instances to encode/decode packet --- src/network/MinecraftInterface.php | 14 +-- src/network/raknet/RakNetCodec.php | 140 ++++++++++-------------- src/network/raknet/RakNetParser.php | 161 ++++++++++++++-------------- 3 files changed, 149 insertions(+), 166 deletions(-) diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index ad91322e..725d99ef 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -41,14 +41,14 @@ class MinecraftInterface{ $pid = ord($buffer[0]); if(RakNetInfo::isValid($pid)){ - $parser = new RakNetParser($buffer); - if($parser->packet !== false){ - $parser->packet->ip = $source; - $parser->packet->port = $port; - if(EventHandler::callEvent(new PacketReceiveEvent($parser->packet)) === BaseEvent::DENY){ + $packet = RakNetParser::parse($buffer); + if($packet !== false){ + $packet->ip = $source; + $packet->port = $port; + if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){ return false; } - return $parser->packet; + return $packet; } return false; }elseif($pid === 0xfe and $buffer[1] === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){ @@ -74,7 +74,7 @@ class MinecraftInterface{ if(EventHandler::callEvent(new PacketSendEvent($packet)) === BaseEvent::DENY){ return 0; }elseif($packet instanceof RakNetPacket){ - $codec = new RakNetCodec($packet); + RakNetCodec::encode($packet); } $write = $this->socket->write($packet->buffer, $packet->ip, $packet->port); $this->bandwidth[1] += $write; diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php index 34393bc6..bbd52589 100644 --- a/src/network/raknet/RakNetCodec.php +++ b/src/network/raknet/RakNetCodec.php @@ -1,46 +1,39 @@ packet = $packet; - $this->buffer = &$this->packet->buffer; - $this->encode(); - } - - private function encode(){ - if($this->packet->buffer != null && strlen($this->packet->buffer) > 0){ + public static function encode(RakNetPacket $packet){ + if($packet->buffer != null && strlen($packet->buffer) > 0){ return; } - $this->buffer .= chr($this->packet->pid()); + $packet->buffer .= chr($packet->pid()); - switch($this->packet->pid()){ + switch($packet->pid()){ case RakNetInfo::OPEN_CONNECTION_REPLY_1: - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); - $this->putByte(0); //server security - $this->putShort($this->packet->mtuSize); + $packet->buffer .= RakNetInfo::MAGIC; + $packet->buffer .= Utils::writeLong($packet->serverID); + $packet->buffer .= chr(0); //server security + $packet->buffer .= Utils::writeShort($packet->mtuSize); break; case RakNetInfo::OPEN_CONNECTION_REPLY_2: - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); - $this->putShort($this->packet->port); - $this->putShort($this->packet->mtuSize); - $this->putByte(0); //Server security + $packet->buffer .= RakNetInfo::MAGIC; + $packet->buffer .= Utils::writeLong($packet->serverID); + $packet->buffer .= Utils::writeShort($packet->port); + $packet->buffer .= Utils::writeShort($packet->mtuSize); + $packet->buffer .= chr(0); //Server security break; case RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION: - $this->putByte(RakNetInfo::STRUCTURE); - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); + $packet->buffer .= chr(RakNetInfo::STRUCTURE); + $packet->buffer .= RakNetInfo::MAGIC; + $packet->buffer .= Utils::writeLong($packet->serverID); break; case RakNetInfo::UNCONNECTED_PONG: case RakNetInfo::ADVERTISE_SYSTEM: - $this->putLong($this->packet->pingID); - $this->putLong($this->packet->serverID); - $this->buffer .= RakNetInfo::MAGIC; - $this->putString($this->packet->serverType); + $packet->buffer .= Utils::writeLong($packet->pingID); + $packet->buffer .= Utils::writeLong($packet->serverID); + $packet->buffer .= RakNetInfo::MAGIC; + + $packet->buffer .= Utils::writeShort(strlen($packet->serverType)); + $packet->buffer .= $packet->serverType; break; case RakNetInfo::DATA_PACKET_0: case RakNetInfo::DATA_PACKET_1: @@ -58,9 +51,9 @@ class RakNetCodec{ case RakNetInfo::DATA_PACKET_D: case RakNetInfo::DATA_PACKET_E: case RakNetInfo::DATA_PACKET_F: - $this->putLTriad($this->packet->seqNumber); - foreach($this->packet->data as $pk){ - $this->encodeDataPacket($pk); + $packet->buffer .= Utils::writeLTriad($packet->seqNumber); + foreach($packet->data as $pk){ + static::encodeDataPacket($packet, $pk); } break; case RakNetInfo::NACK: @@ -68,14 +61,14 @@ class RakNetCodec{ $payload = b""; $records = 0; $pointer = 0; - sort($this->packet->packets, SORT_NUMERIC); - $max = count($this->packet->packets); + sort($packet->packets, SORT_NUMERIC); + $max = count($packet->packets); while($pointer < $max){ $type = true; - $curr = $start = $this->packet->packets[$pointer]; + $curr = $start = $packet->packets[$pointer]; for($i = $start + 1; $i < $max; ++$i){ - $n = $this->packet->packets[$i]; + $n = $packet->packets[$i]; if(($n - $curr) === 1){ $curr = $end = $n; $type = false; @@ -95,73 +88,58 @@ class RakNetCodec{ } ++$records; } - $this->putShort($records); - $this->buffer .= $payload; + $packet->buffer .= Utils::writeShort($records); + $packet->buffer .= $payload; break; default: } - } - protected function putLong($v){ - $this->buffer .= Utils::writeLong($v); - } - - protected function putByte($v){ - $this->buffer .= chr($v); - } - - protected function putShort($v){ - $this->buffer .= Utils::writeShort($v); - } - - protected function putString($v){ - $this->putShort(strlen($v)); - $this->put($v); - } - - protected function put($str){ - $this->buffer .= $str; - } - - protected function putLTriad($v){ - $this->buffer .= Utils::writeLTriad($v); - } - - private function encodeDataPacket(RakNetDataPacket $pk){ - $this->putByte(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0)); - $this->putShort(strlen($pk->buffer) << 3); + public static function encodeDataPacket(RakNetPacket $packet, RakNetDataPacket $pk){ + $packet->buffer .= chr(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0)); + $packet->buffer .= Utils::writeShort(strlen($pk->buffer) << 3); if($pk->reliability === 2 or $pk->reliability === 3 or $pk->reliability === 4 or $pk->reliability === 6 or $pk->reliability === 7){ - $this->putLTriad($pk->messageIndex); + $packet->buffer .= Utils::writeLTriad($pk->messageIndex); } if($pk->reliability === 1 or $pk->reliability === 3 or $pk->reliability === 4 or $pk->reliability === 7){ - $this->putLTriad($pk->orderIndex); - $this->putByte($pk->orderChannel); + $packet->buffer .= Utils::writeLTriad($pk->orderIndex); + $packet->buffer .= chr($pk->orderChannel); } if($pk->hasSplit === true){ - $this->putInt($pk->splitCount); - $this->putShort($pk->splitID); - $this->putInt($pk->splitIndex); + $packet->buffer .= Utils::writeInt($pk->splitCount); + $packet->buffer .= Utils::writeShort($pk->splitID); + $packet->buffer .= Utils::writeInt($pk->splitIndex); } - $this->buffer .= $pk->buffer; + $packet->buffer .= $pk->buffer; } - - protected function putInt($v){ - $this->buffer .= Utils::writeInt($v); - } - - protected function putTriad($v){ - $this->buffer .= Utils::writeTriad($v); + + /** + * @deprecated + */ + public $packet; + /** + * @deprecated + */ + public $buffer; + /** + * @deprecated use RakNetCodec::encode + * @param RakNetPacket $packet + */ + public function __construct(RakNetPacket $packet){ + $this->packet = $packet; + $this->buffer = &$packet->buffer; + static::encode($packet); } + } \ No newline at end of file diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index f5318b9c..764bf64f 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -1,42 +1,35 @@ buffer =& $buffer; - $this->offset = 0; - if(strlen($this->buffer) > 0){ - $this->parse(); - }else{ - $this->packet = false; - } - } - - private function parse(){ - $this->packet = new RakNetPacket(ord($this->get(1))); - $this->packet->buffer =& $this->buffer; - $this->packet->length = strlen($this->buffer); - switch($this->packet->pid()){ + public static function parse(&$buffer){ + $offset = 0; + ++$offset; + $packet = new RakNetPacket(ord(substr($buffer, $offset - 1, 1))); + $packet->buffer = &$buffer; + $packet->length = strlen($buffer); + switch($packet->pid()){ case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: - $this->packet->pingID = $this->getLong(); - $this->offset += 16; //Magic + $offset += 8; + $packet->pingID = Utils::readLong(substr($buffer, $offset - 8, 8), false); + $offset += 16; //Magic break; case RakNetInfo::OPEN_CONNECTION_REQUEST_1: - $this->offset += 16; //Magic - $this->packet->structure = $this->getByte(); - $this->packet->mtuSize = strlen($this->get(true)); + $offset += 16; //Magic + ++$offset; + $packet->structure = ord(substr($buffer, $offset - 1, 1)); + $packet->mtuSize = strlen(substr($buffer, $offset)); break; case RakNetInfo::OPEN_CONNECTION_REQUEST_2: - $this->offset += 16; //Magic - $this->packet->security = $this->get(5); - $this->packet->port = $this->getShort(false); - $this->packet->mtuSize = $this->getShort(false); - $this->packet->clientID = $this->getLong(); + $offset += 16; //Magic + $offset += 5; + $packet->security = Utils::readLong(substr($buffer, $offset - 5, 5), false); + $offset += 2; + $packet->port = Utils::readShort(substr($buffer, $offset - 2, 2), false); + $offset += 2; + $packet->mtuSize = Utils::readShort(substr($buffer, $offset - 2, 2), false); + $offset += 8; + $packet->clientID = Utils::readLong(substr($buffer, $offset - 8, 8), false); break; case RakNetInfo::DATA_PACKET_0: case RakNetInfo::DATA_PACKET_1: @@ -54,36 +47,43 @@ class RakNetParser{ case RakNetInfo::DATA_PACKET_D: case RakNetInfo::DATA_PACKET_E: case RakNetInfo::DATA_PACKET_F: - $this->packet->seqNumber = $this->getLTriad(); - $this->packet->data = []; + $offset += 3; + $packet->seqNumber = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); + $packet->data = []; - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; + while(isset($buffer[$offset]) and ($pk = static::parseDataPacket($offset, $buffer)) instanceof RakNetDataPacket){ + $packet->data[] = $pk; } break; case RakNetInfo::NACK: case RakNetInfo::ACK: - $count = $this->getShort(); - $this->packet->packets = []; - for($i = 0; $i < $count and !$this->feof(); ++$i){ - if($this->getByte() === 0){ - $start = $this->getLTriad(); - $end = $this->getLTriad(); + $offset += 2; + $count = Utils::readShort(substr($buffer, $offset - 2, 2), false); + $packet->packets = []; + for($i = 0; $i < $count && isset($buffer[$offset]); ++$i){ + ++$offset; + if(ord(substr($buffer, $offset - 1, 1)) === 0){ + $offset += 3; + $start = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); + $offset += 3; + $end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); if(($end - $start) > 4096){ $end = $start + 4096; } for($c = $start; $c <= $end; ++$c){ - $this->packet->packets[] = $c; + $packet->packets[] = $c; } }else{ - $this->packet->packets[] = $this->getLTriad(); + $offset += 3; + $packet->packets[] = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); } } break; default: - $this->packet = false; + $packet = false; break; } + return $packet; } private function get($len){ @@ -98,57 +98,50 @@ class RakNetParser{ return substr($this->buffer, $this->offset - $len, $len); } - private function getLong($unsigned = false){ - return Utils::readLong($this->get(8), $unsigned); - } - private function getByte(){ return ord($this->get(1)); } private function getShort($unsigned = false){ - return Utils::readShort($this->get(2), $unsigned); + return ; } private function getLTriad(){ return Utils::readTriad(strrev($this->get(3))); } - private function feof(){ - return !isset($this->buffer[$this->offset]); - } - - private function parseDataPacket(){ - $packetFlags = $this->getByte(); + private static function parseDataPacket(&$offset, &$buffer){ + ++$offset; + $packetFlags = ord(substr($buffer, $offset - 1, 1)); $reliability = ($packetFlags & 0b11100000) >> 5; $hasSplit = ($packetFlags & 0b00010000) > 0; - $length = (int) ceil($this->getShort() / 8); + $offset += 2; + $length = (int) ceil(Utils::readShort(substr($buffer, $offset - 2, 2), false) / 8); - if($reliability === 2 - or $reliability === 3 - or $reliability === 4 - or $reliability === 6 - or $reliability === 7){ - $messageIndex = $this->getLTriad(); + if($reliability == 2 ||$reliability == 3 || $reliability == 4 || $reliability == 6 || $reliability == 7){ + $offset += 3; + $messageIndex = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); }else{ $messageIndex = false; } - if($reliability === 1 - or $reliability === 3 - or $reliability === 4 - or $reliability === 7){ - $orderIndex = $this->getLTriad(); - $orderChannel = $this->getByte(); + if($reliability == 1 ||$reliability == 3 || $reliability == 4 || $reliability == 7){ + $offset += 3; + $orderIndex = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); + ++$offset; + $orderChannel = ord(substr($buffer, $offset - 1, 1)); }else{ $orderIndex = false; $orderChannel = false; } if($hasSplit){ - $splitCount = $this->getInt(); - $splitID = $this->getShort(); - $splitIndex = $this->getInt(); + $offset += 4; + $splitCount = Utils::readInt(substr($buffer, $offset - 4, 4), false); + $offset += 2; + $splitID = Utils::readShort(substr($buffer, $offset - 2, 2), false); + $offset += 4; + $splitIndex = Utils::readInt(substr($buffer, $offset - 4, 4), false); }else{ $splitCount = false; $splitID = false; @@ -160,9 +153,11 @@ class RakNetParser{ or ($hasSplit === true and $splitIndex >= $splitCount)){ return false; }else{ - $pid = $this->getByte(); - $buffer = $this->get($length - 1); - if(strlen($buffer) < ($length - 1)){ + ++$offset; + $pid = ord(substr($buffer, $offset - 1, 1)); + $offset += $length-1; + $buf = substr($buffer, $offset - ($length - 1), ($length - 1)); + if(strlen($buf) < ($length - 1)){ return false; } @@ -233,13 +228,23 @@ class RakNetParser{ $data->splitCount = $splitCount; $data->splitID = $splitID; $data->splitIndex = $splitIndex; - $data->setBuffer($buffer); + $data->setBuffer($buf); } return $data; } - - private function getInt($unsigned = false){ - return Utils::readInt($this->get(4), $unsigned); + + /** + * @deprecated + */ + public $packet; + /** + * @deprecated use RakNetParser::parse + */ + public function __construct(&$buffer){ + if(strlen($buffer) > 0){ + $this->packet = static::parse($buffer); + }else{ + $this->packet = false; + } } - } \ No newline at end of file From c408b622eb93a2feef28945b628ddc969ebbf684 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 3 Jun 2025 14:26:53 +0300 Subject: [PATCH 041/146] mtu size limits added --- src/PocketMinecraftServer.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index ded4a551..0f1bc7d3 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -592,7 +592,10 @@ class PocketMinecraftServer{ if($this->invisible === true){ break; } - + + if($packet->mtuSize > 2048) $packet->mtuSize = 2048; + if($packet->mtuSize <= 512) $packet->mtuSize = 512; + $this->clients[$CID] = $c = new Player($packet->clientID, $packet->ip, $packet->port, $packet->mtuSize); //New Session! $pk = new RakNetPacket(RakNetInfo::OPEN_CONNECTION_REPLY_2); $pk->serverID = $this->serverID; From f572605472c48b15f2298b91327f1d62c609582d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 3 Jun 2025 19:21:16 +0300 Subject: [PATCH 042/146] how did this cause sigsegv on nper server->schedule(2, [$this, "handle"], [], true); if(!defined("NO_THREADS")){ - self::$loop = new ConsoleLoop(); + $this->loop = new ConsoleLoop(); } $this->register("help", "[page|command name]", [$this, "defaultCommands"]); $this->register("status", "", [$this, "defaultCommands"]); @@ -51,8 +51,8 @@ class ConsoleAPI{ function __destruct(){ $this->server->deleteEvent($this->event); if(!defined("NO_THREADS")){ - self::$loop->stop(); - self::$loop->notify(); + $this->loop->stop(); + $this->loop->notify(); usleep(50000); } } @@ -66,10 +66,10 @@ class ConsoleAPI{ if(defined("NO_THREADS")){ return; } - $line = self::$loop->line; + $line = $this->loop->line; if($line !== false){ $line = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", trim($line)); - self::$loop->line = false; + $this->loop->line = false; $output = $this->run($line, "console"); if($output != ""){ $mes = explode("\n", trim($output)); @@ -78,7 +78,7 @@ class ConsoleAPI{ } } }else{ - self::$loop->notify(); + $this->loop->notify(); } } @@ -214,7 +214,7 @@ class ConsoleAPI{ } return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": - self::$loop->stop = true; + $this->loop->stop = true; $this->server->close(); return "Stopping the server\n"; case "difficulty": diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 0f1bc7d3..8abf26d7 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -610,7 +610,6 @@ class PocketMinecraftServer{ public static function clientID($ip, $port){ return crc32($ip . $port) ^ crc32($port . $ip . BOOTUP_RANDOM); - //return $ip . ":" . $port; } public function send(Packet $packet){ From d28dad2a07b6318e3c845f80aa8e47985aed29f2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 4 Jun 2025 17:54:25 +0300 Subject: [PATCH 043/146] fixed mobs sending damage animation to player when they spawn --- src/entity/Creature.php | 2 +- src/entity/animal/Chicken.php | 2 +- src/entity/animal/Cow.php | 2 +- src/entity/animal/Pig.php | 2 +- src/entity/animal/Sheep.php | 2 +- src/entity/monster/Creeper.php | 2 +- src/entity/monster/PigZombie.php | 2 +- src/entity/monster/Skeleton.php | 2 +- src/entity/monster/Spider.php | 2 +- src/entity/monster/Zombie.php | 2 +- src/entity/object/FallingSand.php | 2 +- src/entity/object/ItemEntity.php | 2 +- src/entity/object/Minecart.php | 2 +- src/entity/object/PrimedTNT.php | 2 +- src/entity/object/TripodCamera.php | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index d6e50024..b472686d 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -11,7 +11,7 @@ abstract class Creature extends Living{ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->inPanic = false; //force for now parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 1, "generic"); + $this->setHealth($this->data["Health"] ?? 1, "generic", allowHarm: false); $this->enableAutojump = true; $this->searchForClosestPlayers = true; } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 2b19cd1a..65072669 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -7,7 +7,7 @@ class Chicken extends Animal{ $this->timeUntilEgg = mt_rand(0,6000) + 6000; $this->setSize(0.4, 0.7); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 4, "generic"); + $this->setHealth($this->data["Health"] ?? 4, "generic", allowHarm: false); $this->setName('Chicken'); $this->setSpeed(0.25); diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index e8a18386..5abf467e 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -5,7 +5,7 @@ class Cow extends Animal{ function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.9, 1.3); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 10, "generic"); + $this->setHealth($this->data["Health"] ?? 10, "generic", allowHarm: false); $this->setName("Cow"); $this->setSpeed(0.2); $this->ai->addTask(new TaskRandomWalk(0.8)); diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 46cd725b..f7156bfa 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -5,7 +5,7 @@ class Pig extends Animal implements Rideable{ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.9, 0.9); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 10, "generic"); + $this->setHealth($this->data["Health"] ?? 10, "generic", allowHarm: false); $this->server = ServerAPI::request(); $this->setName("Pig"); $this->setSpeed(0.25); diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 3a7fe09f..4ce3e6ab 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -7,7 +7,7 @@ class Sheep extends Animal{ function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.9, 1.3); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 8, "generic"); + $this->setHealth($this->data["Health"] ?? 8, "generic", allowHarm: false); $this->setName("Sheep"); $this->data["Sheared"] = $this->data["Sheared"] ?? 0; $this->data["Color"] = $this->data["Color"] ?? $this->sheepColor(); diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 12bd50e6..d3da8f38 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -7,7 +7,7 @@ class Creeper extends Monster{ function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.6, 1.7); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 16, "generic"); + $this->setHealth($this->data["Health"] ?? 16, "generic", allowHarm: false); $this->setName("Creeper"); $this->ignited = 0; $this->setSpeed(0.25); diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index 812901ce..8d58edc5 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -4,7 +4,7 @@ class PigZombie extends Monster{ //TODO extends zombie function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.3, 1.95); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 12, "generic"); + $this->setHealth($this->data["Health"] ?? 12, "generic", allowHarm: false); $this->setName("Pigman"); $this->setSpeed(0.25); $this->isImmuneToFire = true; diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 52543a31..246a6bc1 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -4,7 +4,7 @@ class Skeleton extends Monster{ function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.6, 1.99); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 10, "generic"); + $this->setHealth($this->data["Health"] ?? 10, "generic", allowHarm: false); $this->setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); $this->setSpeed(0.25); diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 1752638f..65360334 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -5,7 +5,7 @@ class Spider extends Monster{ function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->setSize(1.4, 0.9); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 8, "generic"); + $this->setHealth($this->data["Health"] ?? 8, "generic", allowHarm: false); $this->setName("Spider"); $this->setSpeed(0.3); $this->ai->addTask(new TaskRandomWalk(1.2)); diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index bc38cf3a..95315008 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -4,7 +4,7 @@ class Zombie extends Monster{ function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->setSize(0.6, 1.85); parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth($this->data["Health"] ?? 12, "generic"); + $this->setHealth($this->data["Health"] ?? 12, "generic", allowHarm: false); $this->setName("Zombie"); $this->setSpeed(0.23); diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index ce3ddfe7..3c040340 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -8,7 +8,7 @@ class FallingSand extends Entity{ public function __construct($level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth(PHP_INT_MAX, "generic"); + $this->setHealth(PHP_INT_MAX, "generic", allowHarm: false); $this->height = 0.98; $this->width = 0.98; $this->yOffset = $this->height / 2; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index d645b43c..2f44ee25 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -22,7 +22,7 @@ class ItemEntity extends Entity{ $this->itemID = (int) $data["itemID"]; } $this->hasGravity = true; - $this->setHealth(5, "generic"); + $this->setHealth(5, "generic", allowHarm: false); $this->gravity = 0.04; $this->delayBeforePickup = 40; //in vanilla it is 0 by default $this->stepHeight = 0; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index fa39affa..ab48a67a 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -62,7 +62,7 @@ class Minecart extends Vehicle{ $this->x = $this->data["TileX"] ?? $this->x; $this->y = $this->data["TileY"] ?? $this->y; $this->z = $this->data["TileZ"] ?? $this->z; - $this->setHealth(1, "generic"); //orig: 3 + $this->setHealth(1, "generic", allowHarm: false); //orig: 3 $this->setSize(0.98, 0.7); $this->yOffset = $this->height / 2; $this->stepHeight = 0; diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 3bc5f03d..75a8825d 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -12,7 +12,7 @@ class PrimedTNT extends Entity{ $this->gravity = 0.04; $this->setSize(0.98, 0.98); $this->yOffset = $this->height / 2; - $this->setHealth(10000000, "generic"); + $this->setHealth(10000000, "generic", allowHarm: false); } public function isPickable(){ diff --git a/src/entity/object/TripodCamera.php b/src/entity/object/TripodCamera.php index 6855cd3c..58344052 100644 --- a/src/entity/object/TripodCamera.php +++ b/src/entity/object/TripodCamera.php @@ -5,7 +5,7 @@ class TripodCamera extends Entity{ function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); - $this->setHealth(PHP_INT_MAX, "generic"); + $this->setHealth(PHP_INT_MAX, "generic", allowHarm: false); } public function isPickable(){ From 5e57b64e345f7858d7aaf3b94396f5b8c8ad4f58 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 4 Jun 2025 23:57:45 +0300 Subject: [PATCH 044/146] fixed entity dismounting --- src/Player.php | 25 ++++++------------- .../protocol/packet/SetEntityLinkPacket.php | 4 ++- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/Player.php b/src/Player.php index 14cbf6d0..b42b529f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1968,7 +1968,7 @@ class Player{ $pos = new Position($this->entity->x, $this->entity->y, $this->entity->z, $this->level); $pData = $this->data->get("position"); - $this->entity->setHealth($this->data->get("health"), "spawn", true, false); + $this->entity->setHealth($this->data->get("health"), "spawn", true, allowHarm: false); $this->spawned = true; $this->teleport($pos, $pData["yaw"] ?? false, $pData["pitch"] ?? false, true, true); $this->server->api->player->spawnAllPlayers($this); @@ -2802,35 +2802,24 @@ class Player{ } } break; + case ProtocolInfo::SET_ENTITY_LINK_PACKET: + if($this->entity->linkedEntity != 0){ + $this->entity->stopRiding(); + } + break; case ProtocolInfo::PLAYER_INPUT_PACKET: $this->isJumping = $packet->isJumping; $this->isSneaking = $packet->isSneaking; $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ - $this->entity->stopRiding(); - break; - } - if($this->entity->linkedEntity != 0){ //TODO better riding + if($this->entity->linkedEntity != 0){ $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; if($e === false) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); $this->entity->stopRiding(); break; } - /*$pk = new SetEntityMotionPacket; - $pk->eid = $e->eid; - $pk->speedX = ($this->entity->x - $e->x)*1.2; - $pk->speedY = ($this->entity->y - $e->y)*1.2; - $pk->speedZ = ($this->entity->z - $e->z)*1.2; - foreach($this->level->players as $p){ - if($p->entity->eid != $this->entity->eid){ - $p->dataPacket(clone $pk); - } - } - console("{$e} {$this->entity}"); - //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1);*/ } break; diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 34de37c1..5dca8516 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -12,7 +12,9 @@ class SetEntityLinkPacket extends RakNetDataPacket{ public $riding; public $type; public function decode() { - + $this->rider = $this->getInt(); + $this->riding = $this->getInt(); + $this->type = $this->getInt(); } public function encode() { $this->reset(); From 4430ab5b6543bb74c308ee4fae78ecc9d4e7833c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Jun 2025 00:11:39 +0300 Subject: [PATCH 045/146] fixed dismounting --- src/entity/Entity.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index bcf59a55..c6df2103 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -170,7 +170,7 @@ class Entity extends Position switch($this->class) { case ENTITY_PLAYER: $this->player = $this->data["player"]; - $this->setHealth($this->health, "generic"); + $this->setHealth($this->health, "generic", allowHarm: false); $this->speedModifer = 1; $this->width = 0.6; $this->height = 1.8; @@ -185,7 +185,7 @@ class Entity extends Position $this->x = $this->data["TileX"] ?? $this->x; $this->y = $this->data["TileY"] ?? $this->y; $this->z = $this->data["TileZ"] ?? $this->z; - $this->setHealth(1, "generic"); + $this->setHealth(1, "generic", allowHarm: false); $this->stepHeight = false; $this->setSize(1, 1); break; @@ -1478,11 +1478,11 @@ class Entity extends Position if(isset($this->level->entityList[$this->linkedEntity])){ $e = $this->level->entityList[$this->linkedEntity]; if(!$e->dead && !$e->closed){ - $this->linkedEntity = 0; - $e->linkedEntity = 0; $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => -1, "type" => 1]); $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => -1, "type" => 1]); $this->isRider = false; + $this->linkedEntity = 0; + $e->linkedEntity = 0; } }else{ $this->linkedEntity = 0; From 60c29e9dd1831bd792914595695b4714c6da1a63 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Jun 2025 01:15:20 +0300 Subject: [PATCH 046/146] all entities will always tick --- src/entity/Entity.php | 33 ++++++++------------------------- src/world/Level.php | 7 +++---- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c6df2103..7ed6fcca 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -18,6 +18,10 @@ class Entity extends Position public $canBeAttacked; public $moveTime, $lookTime, $idleTime, $knockbackTime = 0; public $attackTimeout = 0; + /** + * @deprecated always true + * @var boolean + */ public $needsUpdate = true; public $speedModifer; public $hasGravity; @@ -418,8 +422,6 @@ class Entity extends Position public function environmentUpdate($time) { - $hasUpdate = $this->class === ENTITY_MOB; // force true for mobs - $tickDiff = ($time - $this->lastUpdate) / 0.05; if($this->attackTimeout > 0) $this->attackTimeout -= $tickDiff; if($this->attackTimeout < 0) $this->attackTimeout = 0; @@ -480,8 +482,6 @@ class Entity extends Position if($this->fire <= 0){ $this->updateMetadata(); - } else{ - $hasUpdate = true; } } @@ -495,7 +495,6 @@ class Entity extends Position if($this->isInVoid()){ $this->outOfWorld(); - $hasUpdate = true; } if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix @@ -531,7 +530,7 @@ class Entity extends Position $this->air = $this->maxAir; } } - return $hasUpdate; + return true; } public function moveFlying($strafe, $forward, $speed){ //TODO rename? @@ -744,7 +743,7 @@ class Entity extends Position $this->lastUpdate = $now; return; } - $hasUpdate = $this->environmentUpdate($now); + $this->environmentUpdate($now); if($this->closed === true){ return false; @@ -756,18 +755,6 @@ class Entity extends Position $this->updateLast(); $this->updatePosition(); //TODO shouldnt be called in Entity $this->updateEntityMovement(); - $update = false; - if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ - //$this->server->api->handle("entity.move", $this); - $update = true; - }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { - $update = true; - } - - if($update === true){ - $hasUpdate = true; - } - } else{ $prevGroundState = $this->onGround; $this->onGround = false; @@ -929,8 +916,6 @@ class Entity extends Position if(($oldFire > 0 && $this->fire <= 0) || ($oldFire <= 0 && $this->fire > 0)){ $this->updateMetadata(); //TODO rewrite metadata } - - $hasUpdate = true; } } @@ -945,7 +930,7 @@ class Entity extends Position $this->player->entityTick(); } - $this->needsUpdate = $hasUpdate; + $this->needsUpdate = true; $this->lastUpdate = $now; } public function isOnLadder(){ @@ -1197,9 +1182,7 @@ class Entity extends Position if($this->knockbackTime > 0){ --$this->knockbackTime; } - if($this->delayBeforePickup > 0){ - --$this->delayBeforePickup; - } + if($this->moveTime > 0){ --$this->moveTime; } diff --git a/src/world/Level.php b/src/world/Level.php index a3a907a2..fcd621a1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -574,10 +574,9 @@ class Level{ if($e->class === ENTITY_MOB && !$e->isPlayer()){ ++$this->totalMobsAmount; } - if($e->isPlayer() || $e->needsUpdate){ - $e->update($currentTime); - if(!$e->isPlayer()) $post[] = $k; - } + + $e->update($currentTime); + if(!$e->isPlayer()) $post[] = $k; if($e instanceof Entity){ $newChunkX = (int)$e->x >> 4; From 6ab777e5e983b4f2c3b5b1072b3b013f7596a72c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Jun 2025 14:09:04 +0300 Subject: [PATCH 047/146] bed position is saved in playerdata, bed destruction will teleport you to the world spawn --- src/API/LevelAPI.php | 3 +- src/API/PlayerAPI.php | 3 +- src/Player.php | 45 +++++++++++++++++++++------- src/material/block/misc/BedBlock.php | 2 +- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 70a892a2..a4d77bd0 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -109,8 +109,9 @@ class LevelAPI{ return true; } /** + * Gets a level by name. Returns false if no level with such name exists. * @param string $name - * @return Level|boolean + * @return Level|false */ public function get($name){ if(isset($this->levels[$name])){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 7e4b6f36..95c7302b 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -404,7 +404,8 @@ class PlayerAPI{ "lastIP" => "", "lastID" => 0, "achievements" => [], - "slot-count" => 7 + "slot-count" => 7, + "bed-position" => null, ]; if(!file_exists(DATA_PATH . "players/" . $iname . ".yml")){ diff --git a/src/Player.php b/src/Player.php index b42b529f..881ab6a9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -46,7 +46,6 @@ class Player{ private $receiveCount = -1; private $buffer; private $bufferLen = 0; - private $nextBuffer = 0; private $evid = []; private $lastMovement = 0; private $forceMovement = false; @@ -87,6 +86,7 @@ class Player{ private $ackQueue = []; public $slotCount = 7; + public $bedPosition = null; /** * DataPacket that is used as a buffer for all non-player entity movement packets @@ -197,12 +197,18 @@ class Player{ return $this->spawnPosition; } + public function setBedPosition(?Position $bedPos){ + if($bedPos != null) $this->bedPosition = [$bedPos->level->getName(), (int)$bedPos->x, (int)$bedPos->y, (int)$bedPos->z]; + else $this->bedPosition = null; + $this->data->set("bed-position", $this->bedPosition); + } + /** * @param Vector3 $pos * * @return boolean */ - public function sleepOn(Vector3 $pos){ + public function sleepOn(Position $pos){ foreach($this->server->api->player->getAll($this->level) as $p){ if($p->isSleeping instanceof Vector3){ if($pos->distance($p->isSleeping) <= 0.1){ @@ -210,6 +216,8 @@ class Player{ } } } + + $this->setBedPosition($pos); $this->isSleeping = $pos; $this->sleepingTime = 0; $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level), false, false, false, false); @@ -218,13 +226,6 @@ class Player{ $this->entity->updateMetadata(); } - $spawnPoint = BedBlock::findStandUpPosition($this->level, $pos->x, $pos->y, $pos->z); - 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; } @@ -586,7 +587,6 @@ class Player{ $this->bufferLen = 0; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->nextBuffer = microtime(true) + 0.05; } /** @@ -1916,6 +1916,12 @@ class Player{ $this->data->set("slot-count", $this->slotCount); } + if($this->data->exists("bed-position")){ + $this->bedPosition = $this->data->get("bed-position"); + }else{ + $this->setBedPosition(null); + } + $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, ["player" => $this]); $this->eid = $this->entity->eid; $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); @@ -2418,7 +2424,24 @@ class Player{ $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->checkSpawnPosition(); - $this->teleport($this->spawnPosition, false, false, true, false); + $tpTarget = $this->spawnPosition; + if($this->bedPosition != null){ + [$levelname, $x, $y, $z] = $this->bedPosition; + $level = $this->server->api->level->get($levelname); + if($level === false || $level->level->getBlockID($x, $y, $z) != BED_BLOCK){ + remove_bed: + $this->sendChat("The bed is missing or cannot be accessed."); + $this->setBedPosition(null); + }else{ + $spawnPoint = BedBlock::findStandUpPosition($level, $x, $y, $z); + if($spawnPoint == null) goto remove_bed; + $spawnPoint->x += 0.5; + $spawnPoint->z += 0.5; + $tpTarget = $spawnPoint; + } + } + + $this->teleport($tpTarget, false, false, true, false); $pk = new MovePlayerPacket(); $pk->eid = $this->entity->eid; $pk->x = $this->entity->x; diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index d94bbb08..2d9af20d 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -65,7 +65,7 @@ class BedBlock extends TransparentBlock{ 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 new Position($xCheck, $y, $zCheck, $level); } } } From d735564948fb06629b38b3b070eacf98bde62926 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Jun 2025 14:15:35 +0300 Subject: [PATCH 048/146] reset spawn to whatever world has if bed is missing/obstructed --- src/Player.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Player.php b/src/Player.php index 881ab6a9..46e9fdf6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2432,6 +2432,7 @@ class Player{ remove_bed: $this->sendChat("The bed is missing or cannot be accessed."); $this->setBedPosition(null); + $this->setSpawn($this->level->getSpawn()); }else{ $spawnPoint = BedBlock::findStandUpPosition($level, $x, $y, $z); if($spawnPoint == null) goto remove_bed; From f7fb798c0ffdd4947dbf3b97bf6795a7ab6704d1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Jun 2025 14:23:38 +0300 Subject: [PATCH 049/146] always get level spawn when respawning instead of using spawnposition --- src/Player.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 46e9fdf6..4852797e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2424,7 +2424,7 @@ class Player{ $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->checkSpawnPosition(); - $tpTarget = $this->spawnPosition; + $tpTarget = $this->level->getSpawn(); if($this->bedPosition != null){ [$levelname, $x, $y, $z] = $this->bedPosition; $level = $this->server->api->level->get($levelname); @@ -2432,7 +2432,6 @@ class Player{ remove_bed: $this->sendChat("The bed is missing or cannot be accessed."); $this->setBedPosition(null); - $this->setSpawn($this->level->getSpawn()); }else{ $spawnPoint = BedBlock::findStandUpPosition($level, $x, $y, $z); if($spawnPoint == null) goto remove_bed; From 93501d1bbac7410080c66613afa61fd4f2742ebd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 7 Jun 2025 14:26:49 +0300 Subject: [PATCH 050/146] another very critical bug(almost readme.md bug) fixed --- src/API/PlayerAPI.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 95c7302b..2385b9af 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -326,6 +326,7 @@ class PlayerAPI{ $origin = $this->get($name); if($origin instanceof Player){ $name = $origin->username; + $target = $player->username; return $origin->teleport($player->entity); } } From ecc72308e3f1f5b945de64675eee99253cf00739 Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:16:08 +0300 Subject: [PATCH 051/146] Nostalgia --- src/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.php b/src/config.php index fcaaaea8..385b0a4c 100644 --- a/src/config.php +++ b/src/config.php @@ -104,7 +104,7 @@ ini_set("memory_limit", "256M"); //Default define("LOG", true); define("START_TIME", microtime(true)); define("MAJOR_VERSION", "1.1.0dev"); -define("CODENAME", "懐かしさ (Nostalgia)"); //i'm not very creative - kotyaralih +define("CODENAME", "Nostalgia"); //i'm not very creative - kotyaralih ...yeah, - tema1d define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); define("CURRENT_API_VERSION", "12.2"); define("CURRENT_PHP_VERSION", "8.0"); From a28614faa440b05dc7b26bf8aadfae9e543d0be9 Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:21:08 +0300 Subject: [PATCH 052/146] Update README.md --- README.md => update README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) rename README.md => update README.md (94%) diff --git a/README.md b/update README.md similarity index 94% rename from README.md rename to update README.md index e58c7783..2a998314 100644 --- a/README.md +++ b/update README.md @@ -10,22 +10,24 @@ Supported MCPE versions: 0.8.0, 0.8.1 (maybe 0.8.2)

Why should you use NostalgiaCore to run a Minecraft Pocket Edition 0.8.1 server?

+• PHP 8 Support, PHAR plugins and API Bump to 12.2 + +• Crafting system was rewritten to hopefully have less problems + • Items are dropping normally. -• Dupes, that were in PocketMine by mistake are fixed. - • Player hotbar is saved. +• Working Nether Reactor. + +• Mob AI + +• Dupes, that were in PocketMine by mistake are fixed. + • Extra options (like in-game messages to discord) • Vector2 is fixed. -• Working Nether Reactor. - -• PHAR plugins support - -• Mob AI - ...and etc.

Download Stable NostalgiaCore

From a0ddb50b3edab1c8fdb3cb883848fb487f619901 Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:23:33 +0300 Subject: [PATCH 053/146] ouch... --- update README.md => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename update README.md => README.md (100%) diff --git a/update README.md b/README.md similarity index 100% rename from update README.md rename to README.md From 278af85cd185175d412ecdcdb9bcd9084913ce0e Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:25:52 +0300 Subject: [PATCH 054/146] Update and rename src/config.php to 1.1.0dev -> 1.1.1dev --- src/config.php => 1.1.0dev -> 1.1.1dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/config.php => 1.1.0dev -> 1.1.1dev (98%) diff --git a/src/config.php b/1.1.0dev -> 1.1.1dev similarity index 98% rename from src/config.php rename to 1.1.0dev -> 1.1.1dev index 385b0a4c..8c8c0943 100644 --- a/src/config.php +++ b/1.1.0dev -> 1.1.1dev @@ -103,7 +103,7 @@ set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH); ini_set("memory_limit", "256M"); //Default define("LOG", true); define("START_TIME", microtime(true)); -define("MAJOR_VERSION", "1.1.0dev"); +define("MAJOR_VERSION", "1.1.1dev"); define("CODENAME", "Nostalgia"); //i'm not very creative - kotyaralih ...yeah, - tema1d define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); define("CURRENT_API_VERSION", "12.2"); From 146339a309e165e70494ee6526d2de67146dee35 Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:27:26 +0300 Subject: [PATCH 055/146] Delete 1.1.0dev -> 1.1.1dev --- 1.1.0dev -> 1.1.1dev | 116 ------------------------------------------- 1 file changed, 116 deletions(-) delete mode 100644 1.1.0dev -> 1.1.1dev diff --git a/1.1.0dev -> 1.1.1dev b/1.1.0dev -> 1.1.1dev deleted file mode 100644 index 8c8c0943..00000000 --- a/1.1.0dev -> 1.1.1dev +++ /dev/null @@ -1,116 +0,0 @@ - - * string(7) "Caption" - * [1] => - * string(20) "(UTC+09:30) Adelaide" - * [2] => - * string(0) "" - * } - */ - exec("wmic timezone get Caption", $output); - - $string = trim(implode("\n", $output)); - - //Detect the Time Zone string - preg_match($regex, $string, $matches); - - if(!isset($matches[2]) or ($timezone = parseOffset($matches[2])) === false){ - $timezone = 'UTC'; - } - if(date_default_timezone_set($timezone)){ - ini_set("date.timezone", $timezone); - } - }else{ - exec("date +%s", $t); - $offset = round((intval(trim($t[0])) - time()) / 60) * 60; - $daylight = (int) date("I"); - $d = timezone_name_from_abbr("", $offset, $daylight); - @ini_set("date.timezone", $d); - date_default_timezone_set($d); - } -}else{ - $d = @date_default_timezone_get(); - if(!str_contains($d, "/")){ - $d = timezone_name_from_abbr($d); - @ini_set("date.timezone", $d); - date_default_timezone_set($d); - } -} - -gc_enable(); -error_reporting(E_ALL | E_STRICT); -ini_set("allow_url_fopen", 1); -ini_set("display_errors", 1); -ini_set("display_startup_errors", 1); -ini_set("default_charset", "utf-8"); -if(defined("POCKETMINE_COMPILE") and POCKETMINE_COMPILE === true){ - define("FILE_PATH", realpath(dirname(__FILE__)) . "/"); -}else{ - define("FILE_PATH", realpath(dirname(__FILE__) . "/../") . "/"); -} -set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH); - -ini_set("memory_limit", "256M"); //Default -define("LOG", true); -define("START_TIME", microtime(true)); -define("MAJOR_VERSION", "1.1.1dev"); -define("CODENAME", "Nostalgia"); //i'm not very creative - kotyaralih ...yeah, - tema1d -define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); -define("CURRENT_API_VERSION", "12.2"); -define("CURRENT_PHP_VERSION", "8.0"); -$gitsha1 = false; -if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! - define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); -}else{ //Unknown :( - define("GIT_COMMIT", str_repeat("00", 20)); -} From 18698df8e6772a85d104c839ea7a85b92aeb4877 Mon Sep 17 00:00:00 2001 From: Tema1d <84407490+Tema1d@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:29:40 +0300 Subject: [PATCH 056/146] Add files via upload --- src/config.php | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/config.php diff --git a/src/config.php b/src/config.php new file mode 100644 index 00000000..afc3247b --- /dev/null +++ b/src/config.php @@ -0,0 +1,116 @@ + + * string(7) "Caption" + * [1] => + * string(20) "(UTC+09:30) Adelaide" + * [2] => + * string(0) "" + * } + */ + exec("wmic timezone get Caption", $output); + + $string = trim(implode("\n", $output)); + + //Detect the Time Zone string + preg_match($regex, $string, $matches); + + if(!isset($matches[2]) or ($timezone = parseOffset($matches[2])) === false){ + $timezone = 'UTC'; + } + if(date_default_timezone_set($timezone)){ + ini_set("date.timezone", $timezone); + } + }else{ + exec("date +%s", $t); + $offset = round((intval(trim($t[0])) - time()) / 60) * 60; + $daylight = (int) date("I"); + $d = timezone_name_from_abbr("", $offset, $daylight); + @ini_set("date.timezone", $d); + date_default_timezone_set($d); + } +}else{ + $d = @date_default_timezone_get(); + if(!str_contains($d, "/")){ + $d = timezone_name_from_abbr($d); + @ini_set("date.timezone", $d); + date_default_timezone_set($d); + } +} + +gc_enable(); +error_reporting(E_ALL | E_STRICT); +ini_set("allow_url_fopen", 1); +ini_set("display_errors", 1); +ini_set("display_startup_errors", 1); +ini_set("default_charset", "utf-8"); +if(defined("POCKETMINE_COMPILE") and POCKETMINE_COMPILE === true){ + define("FILE_PATH", realpath(dirname(__FILE__)) . "/"); +}else{ + define("FILE_PATH", realpath(dirname(__FILE__) . "/../") . "/"); +} +set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH); + +ini_set("memory_limit", "256M"); //Default +define("LOG", true); +define("START_TIME", microtime(true)); +define("MAJOR_VERSION", "1.1.1dev"); +define("CODENAME", "Nostalgia"); //i'm not very creative - kotyaralih, yeah - tema1d. +define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); +define("CURRENT_API_VERSION", "12.2"); +define("CURRENT_PHP_VERSION", "8.0"); +$gitsha1 = false; +if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! + define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); +}else{ //Unknown :( + define("GIT_COMMIT", str_repeat("00", 20)); +} From baa4ff0437fe7a149da4afb4353e957c80986c99 Mon Sep 17 00:00:00 2001 From: enirboreHemaG <55945072+GameHerobrine@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:36:55 +0300 Subject: [PATCH 057/146] api bump is not a reason to use nc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a998314..f6e4e54e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Supported MCPE versions: 0.8.0, 0.8.1 (maybe 0.8.2)

Why should you use NostalgiaCore to run a Minecraft Pocket Edition 0.8.1 server?

-• PHP 8 Support, PHAR plugins and API Bump to 12.2 +• PHP 8 Support, PHAR plugins • Crafting system was rewritten to hopefully have less problems From 410d87fa145fd0adb67789e2c7071917af233f2d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 12 Jun 2025 03:53:34 +0300 Subject: [PATCH 058/146] fixed durability(again) --- src/material/Item.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/material/Item.php b/src/material/Item.php index 104be38b..5ed4b2d9 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -262,15 +262,15 @@ class Item{ return false; } - $levels = [ //TODO rewrite(item usage too) - 2 => 33, //GOLD - 1 => 60, //WOODEN - 3 => 132, //STONE - 4 => 251, //IRON - 5 => 1562, //DIAMOND(called EMERALD in disassembled code) - FLINT_STEEL => 65, //lets assume it is correct - SHEARS => 239, //x2 - BOW => 385 //x3 + $levels = [ + 2 => 32, //GOLD + 1 => 59, //WOODEN + 3 => 131, //STONE + 4 => 250, //IRON + 5 => 1561, //DIAMOND(called EMERALD in disassembled code) + FLINT_STEEL => 64, + SHEARS => 238, + BOW => 384 ]; if(($type = $this->getLevel()) === false){ From 71da620aa3af71523332b0aacc34267191ee5c3b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 12 Jun 2025 04:01:29 +0300 Subject: [PATCH 059/146] warning when using kill in console without args fixe --- src/API/PlayerAPI.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 2385b9af..ebea444e 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -222,7 +222,8 @@ class PlayerAPI{ } case "kill": case "suicide": - if(!isset($args[0]) and ($issuer instanceof Player)){ + if(!isset($args[0])){ + if(!($issuer instanceof Player)) return "Usage: /kill "; $player = $issuer; }else{ $player = $this->get($args[0]); From 625e9ca71fe7023b9ec1c63c019a60777177c898 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 14 Jun 2025 07:56:10 +0300 Subject: [PATCH 060/146] better break time checks, added block materials --- src/API/BlockAPI.php | 30 +++++- src/Player.php | 11 +++ src/PocketMinecraftServer.php | 1 + src/constants/BlockIDs.php | 4 +- src/material/Block.php | 25 ++++- src/material/Item.php | 12 ++- src/material/Material.php | 91 +++++++++++++++++-- src/material/StaticBlock.php | 9 +- src/material/block/attachable/LadderBlock.php | 2 + .../block/attachable/SignPostBlock.php | 2 + src/material/block/attachable/TorchBlock.php | 2 + .../block/attachable/TrapdoorBlock.php | 2 + .../block/attachable/WallSignBlock.php | 3 + src/material/block/liquid/LavaBlock.php | 4 +- src/material/block/liquid/StillLavaBlock.php | 2 + src/material/block/liquid/StillWaterBlock.php | 2 + src/material/block/liquid/WaterBlock.php | 2 + src/material/block/misc/AirBlock.php | 1 + src/material/block/misc/BedBlock.php | 2 + src/material/block/misc/FireBlock.php | 2 + .../block/misc/NetherReactorBlock.php | 3 + src/material/block/misc/TNTBlock.php | 2 + src/material/block/nonfull/CakeBlock.php | 2 + src/material/block/nonfull/CarpetBlock.php | 4 +- src/material/block/nonfull/ChestBlock.php | 2 + src/material/block/nonfull/CobwebBlock.php | 4 +- src/material/block/nonfull/FarmlandBlock.php | 2 + src/material/block/nonfull/FenceBlock.php | 2 + src/material/block/nonfull/FenceGateBlock.php | 2 + src/material/block/nonfull/GlassPaneBlock.php | 3 + src/material/block/nonfull/IronBarsBlock.php | 3 + src/material/block/nonfull/IronDoorBlock.php | 2 + .../block/nonfull/PoweredRailBlock.php | 4 +- src/material/block/nonfull/RailBlock.php | 4 +- src/material/block/nonfull/SlabBlock.php | 2 + src/material/block/nonfull/SnowLayerBlock.php | 2 + src/material/block/nonfull/StoneWallBlock.php | 4 +- src/material/block/nonfull/WoodDoorBlock.php | 2 + src/material/block/nonfull/WoodSlabBlock.php | 4 +- .../nonfull/stairs/BirchWoodStairsBlock.php | 3 + .../block/nonfull/stairs/BrickStairsBlock.php | 3 + .../nonfull/stairs/CobblestoneStairsBlock.php | 3 + .../nonfull/stairs/JungleWoodStairsBlock.php | 3 + .../stairs/NetherBricksStairsBlock.php | 3 + .../nonfull/stairs/QuartzStairsBlock.php | 3 + .../nonfull/stairs/SandstoneStairsBlock.php | 3 + .../nonfull/stairs/SpruceWoodStairsBlock.php | 3 + .../nonfull/stairs/StoneBrickStairsBlock.php | 3 + .../block/nonfull/stairs/WoodStairsBlock.php | 2 + src/material/block/ore/CoalOreBlock.php | 2 + src/material/block/ore/DiamondOreBlock.php | 2 + .../block/ore/GlowingRedstoneOreBlock.php | 2 + src/material/block/ore/GoldOreBlock.php | 2 + src/material/block/ore/IronOreBlock.php | 2 + src/material/block/ore/LapisOreBlock.php | 2 + src/material/block/ore/RedstoneOreBlock.php | 2 + src/material/block/plant/BeetrootBlock.php | 2 + .../block/plant/BrownMushroomBlock.php | 2 + src/material/block/plant/CactusBlock.php | 2 + src/material/block/plant/CarrotBlock.php | 2 + src/material/block/plant/CyanFlowerBlock.php | 2 + src/material/block/plant/DandelionBlock.php | 2 + src/material/block/plant/DeadBushBlock.php | 2 + src/material/block/plant/MelonStemBlock.php | 2 + src/material/block/plant/PotatoBlock.php | 2 + src/material/block/plant/PumpkinStemBlock.php | 2 + src/material/block/plant/RedMushroomBlock.php | 2 + src/material/block/plant/SaplingBlock.php | 2 + src/material/block/plant/SugarcaneBlock.php | 2 + src/material/block/plant/TallGrassBlock.php | 2 + src/material/block/plant/WheatBlock.php | 2 + src/material/block/solid/BedrockBlock.php | 2 + src/material/block/solid/BookshelfBlock.php | 2 + src/material/block/solid/BricksBlock.php | 2 + .../block/solid/BurningFurnaceBlock.php | 2 + src/material/block/solid/ClayBlock.php | 2 + src/material/block/solid/CoalBlock.php | 2 + src/material/block/solid/CobblestoneBlock.php | 2 + src/material/block/solid/DiamondBlock.php | 2 + src/material/block/solid/DirtBlock.php | 2 + src/material/block/solid/DoubleSlabBlock.php | 2 + .../block/solid/DoubleWoodSlabBlock.php | 4 +- src/material/block/solid/FurnaceBlock.php | 1 + src/material/block/solid/GlassBlock.php | 2 + .../block/solid/GlowingObsidianBlock.php | 3 + src/material/block/solid/GlowstoneBlock.php | 2 + src/material/block/solid/GoldBlock.php | 2 + src/material/block/solid/GrassBlock.php | 2 + src/material/block/solid/GravelBlock.php | 2 + src/material/block/solid/HayBaleBlock.php | 4 +- src/material/block/solid/IceBlock.php | 2 + src/material/block/solid/InfoUpdate2Block.php | 4 +- src/material/block/solid/InfoUpdateBlock.php | 4 +- .../block/solid/InvisibleBedrockBlock.php | 4 +- src/material/block/solid/IronBlock.php | 2 + src/material/block/solid/LapisBlock.php | 2 + src/material/block/solid/LeavesBlock.php | 2 + src/material/block/solid/LitPumpkinBlock.php | 2 + src/material/block/solid/MelonBlock.php | 2 + src/material/block/solid/MossStoneBlock.php | 2 + .../block/solid/NetherBricksBlock.php | 2 + src/material/block/solid/NetherrackBlock.php | 2 + src/material/block/solid/ObsidianBlock.php | 4 +- src/material/block/solid/PlanksBlock.php | 2 + src/material/block/solid/PumpkinBlock.php | 2 + src/material/block/solid/QuartzBlock.php | 3 + src/material/block/solid/Reserved6Block.php | 2 + src/material/block/solid/SandBlock.php | 2 + src/material/block/solid/SandstoneBlock.php | 2 + src/material/block/solid/SnowBlock.php | 2 + src/material/block/solid/SpongeBlock.php | 2 + src/material/block/solid/StoneBlock.php | 2 + src/material/block/solid/StoneBricksBlock.php | 2 + src/material/block/solid/StonecutterBlock.php | 3 + src/material/block/solid/WoodBlock.php | 2 + src/material/block/solid/WoolBlock.php | 2 + src/material/block/solid/WorkbenchBlock.php | 4 +- src/material/item/armor/ChainBootsItem.php | 2 +- .../item/armor/ChainChestplateItem.php | 2 +- src/material/item/armor/ChainHelmetItem.php | 2 +- src/material/item/armor/ChainLeggingsItem.php | 2 +- src/material/item/armor/DiamondBootsItem.php | 2 +- .../item/armor/DiamondChestplateItem.php | 2 +- src/material/item/armor/DiamondHelmetItem.php | 2 +- .../item/armor/DiamondLeggingsItem.php | 2 +- src/material/item/armor/GoldenBootsItem.php | 2 +- .../item/armor/GoldenChestplateItem.php | 2 +- src/material/item/armor/GoldenHelmetItem.php | 2 +- .../item/armor/GoldenLeggingsItem.php | 2 +- src/material/item/armor/IronBootsItem.php | 2 +- .../item/armor/IronChestplateItem.php | 2 +- src/material/item/armor/IronHelmetItem.php | 2 +- src/material/item/armor/IronLeggingsItem.php | 2 +- src/material/item/armor/LeatherBootsItem.php | 2 +- src/material/item/armor/LeatherCapItem.php | 2 +- src/material/item/armor/LeatherPantsItem.php | 2 +- src/material/item/armor/LeatherTunicItem.php | 2 +- src/material/item/base/ItemAxe.php | 7 ++ src/material/item/base/ItemPickaxe.php | 19 ++++ src/material/item/base/ItemShovel.php | 10 ++ src/material/item/base/ItemSword.php | 7 ++ src/material/item/base/ItemTool.php | 15 +++ src/material/item/tool/ShearsItem.php | 9 ++ 143 files changed, 496 insertions(+), 54 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 0033c89a..ea9cc5f2 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -402,13 +402,37 @@ class BlockAPI{ return $this->cancelAction($target, $player, false); } } - - if((!$target->isBreakable($item, $player) and $this->server->api->dhandle("player.block.break.invalid", ["player" => $player, "target" => $target, "item" => $item]) !== true) or ($player->gamemode & 0x02) === 0x02 or (($player->lastBreak - $player->getLag() / 1000) + $target->getBreakTime($item, $player) - 0.2) >= microtime(true)){ + + $currentTime = microtime(true); + $isIndestructible = (!$target->isBreakable($item, $player) && $this->server->api->dhandle("player.block.break.invalid", ["player" => $player, "target" => $target, "item" => $item]) !== true); + $isAdventure = ($player->gamemode & 0x02) === 0x02; + $progress = $target->getDestroyProgress($player); + + $tooFast = false; + if($progress == 0 || is_nan($progress)){ + $exceptedTimeMS = INF; + $tooFast = true; + }else{ + $exceptedTimeMS = ceil(1 / $progress)*0.05; + + } + + if($player->gamemode == 1){ //creative + $exceptedTimeMS = 0.05; //1 block/tick + $tooFast = false; + } + + $exceptedTimeAfter = $player->lastBreak + $exceptedTimeMS; + if($currentTime < $exceptedTimeAfter) $tooFast = true; + if($isIndestructible || $isAdventure || $tooFast){ if($this->server->api->dhandle("player.block.break.bypass", ["player" => $player, "target" => $target, "item" => $item]) !== true){ + if($tooFast){ + ConsoleAPI::warn("{$player->username} tried destroying block({$target->getID()} {$target->getMetadata()}) too fast($currentTime < $exceptedTimeAfter)"); + } return $this->cancelAction($target, $player, false); } } - $player->lastBreak = microtime(true); + $player->lastBreak = $currentTime; if($this->server->api->dhandle("player.block.break", ["player" => $player, "target" => $target, "item" => $item]) !== false){ $drops = $target->getDrops($item, $player); diff --git a/src/Player.php b/src/Player.php index 4852797e..37607591 100755 --- a/src/Player.php +++ b/src/Player.php @@ -731,6 +731,17 @@ class Player{ public $currentChunk = false; + public function getDestroySpeed($id, $meta){ + return $this->getHeldItem()->getDestroySpeed($id, $meta); + } + + public function canDestroy($id, $meta){ + $mat = StaticBlock::getMaterial($id); + if($mat->alwaysDestroyable) return true; + + return $this->getHeldItem()->canDestroySpecial($id, $meta); + } + public function onChunkReceived($blockX, $blockZ){ $minX = $blockX; $maxX = $blockX + 15; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 8abf26d7..b779896a 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -34,6 +34,7 @@ class PocketMinecraftServer{ @cli_set_process_title("NostalgiaCore ".MAJOR_VERSION); }*/ console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port); + Material::init(); EntityRegistry::registerEntities(); PlayerNull::$INSTANCE = new PlayerNull(); Feature::init(); diff --git a/src/constants/BlockIDs.php b/src/constants/BlockIDs.php index 4cd937e9..52e352a2 100755 --- a/src/constants/BlockIDs.php +++ b/src/constants/BlockIDs.php @@ -105,7 +105,7 @@ define("SUGARCANE_BLOCK", 83); define("FENCE", 85); define("PUMPKIN", 86); define("NETHERRACK", 87); -define("SOUL_SAND", 88); +define("SOUL_SAND", 88); //doesnt exist define("GLOWSTONE", 89); define("GLOWSTONE_BLOCK", 89); @@ -136,7 +136,7 @@ define("NETHER_BRICKS", 112); define("NETHER_BRICK_BLOCK", 112); define("NETHER_BRICKS_STAIRS", 114); - +//TODO missing tile: 126 dt 0.500000 h 2.500000 b CakeTile n activatorRail, mat cake define("SANDSTONE_STAIRS", 128); define("SPRUCE_WOOD_STAIRS", 134); diff --git a/src/material/Block.php b/src/material/Block.php index 7fd7cdac..49e1af41 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -133,12 +133,18 @@ abstract class Block extends Position{ INFO_UPDATE2 => "InfoUpdate2Block", RESERVED6 => "Reserved6Block", ); + + /** + * @var Material + */ + public $material; + public $id; public $meta; public $name; - public $breakTime; public $boundingBox; public $hardness; + public $breakTime = 0; public $isActivable = false; public $breakable = true; public $isFlowable = false; @@ -259,6 +265,17 @@ abstract class Block extends Position{ return $this->meta & 0x0F; } + public function getDestroyProgress(Player $player){ + if($this->breakTime < 0) return 0; + $id = $this->getID(); + $meta = $this->getMetadata(); + if($player->canDestroy($id, $meta)){ + return $player->getDestroySpeed($id, $meta) / $this->breakTime / 30; + } + if($this->breakTime == 0) return INF; //instabreak + return (1 / $this->breakTime) / 100; + } + final public function position(Position $v){ $this->level = $v->level; $this->x = (int) $v->x; @@ -278,9 +295,11 @@ abstract class Block extends Position{ } public function getBreakTime(Item $item, Player $player){ + if($this->hardness < 0) return INF; if(($player->gamemode & 0x01) === 0x01){ return 0.15; } + if($player->canDestroy($this->getID(), $this->getMetadata())) return $this->breakTime; } @@ -296,7 +315,9 @@ abstract class Block extends Position{ return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; } - abstract function isBreakable(Item $item, Player $player); + public function isBreakable(Item $item, Player $player){ + return $this->hardness >= 0; + } abstract function onBreak(Item $item, Player $player); diff --git a/src/material/Item.php b/src/material/Item.php index 5ed4b2d9..ca202716 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -157,6 +157,14 @@ class Item{ } } + public function canDestroySpecial($id, $meta){ + return false; + } + + public function getDestroySpeed($id, $meta){ + return 1.0; + } + public function isPickaxe(){ return false; } @@ -323,10 +331,6 @@ class Item{ return Item::DEF_DAMAGE; } - public function getDestroySpeed(Block $block, Player $player){ - return 1; - } - public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ return false; } diff --git a/src/material/Material.php b/src/material/Material.php index 674ce90b..5332b046 100644 --- a/src/material/Material.php +++ b/src/material/Material.php @@ -1,9 +1,84 @@ alwaysDestroyable = true; + $this->flammable = false; + $this->translucent = false; + $this->replaceable = false; + + $this->blocksLight = true; + $this->blocksMotion = true; + $this->isLiquid = false; + $this->isSolid = true; + } + + public function isSolidBlocking(){ + if($this->translucent) return false; + return $this->blocksMotion; + } + public function letsWaterThrough(){ + if($this->isLiquid) return false; + return !$this->isSolid; + } + + public static function init(){ + self::$air = new GasMaterial(); + self::$dirt = new Material(); + self::$wood = new Material(); + self::$wood->flammable = true; + self::$stone = new Material(); + self::$stone->alwaysDestroyable = false; + self::$metal = new Material(); + self::$metal->alwaysDestroyable = false; + self::$water = new LiquidMaterial(); + self::$lava = new LiquidMaterial(); + self::$leaves = new Material(); + self::$leaves->flammable = true; + self::$leaves->translucent = true; + self::$plant = new DecorationMaterial(); + self::$replaceable_plant = new DecorationMaterial(); + self::$replaceable_plant->replaceable = true; + self::$replaceable_plant->flammable = true; + self::$sponge = new Material(); + self::$cloth = new Material(); + self::$cloth->flammable = true; + self::$fire = new GasMaterial(); + self::$fire->replaceable = true; + self::$sand = new Material(); + self::$decoration = new DecorationMaterial(); + self::$glass = new Material(); + self::$glass->translucent = true; + self::$explosive = new Material(); + self::$explosive->flammable = true; + self::$explosive->translucent = true; + self::$coral = new Material(); + self::$ice = new Material(); + self::$ice->translucent = true; + self::$topSnow = new DecorationMaterial(); + self::$topSnow->translucent = true; + self::$topSnow->replaceable = true; + self::$topSnow->alwaysDestroyable = false; + self::$snow = new Material(); + self::$snow->alwaysDestroyable = false; + self::$cactus = new Material(); + self::$cactus->translucent = true; + self::$clay = new Material(); + self::$vegetable = new Material(); + self::$portal = new Material(); + self::$cake = new Material(); + self::$web = new Material(); + self::$web->alwaysDestroyable = false; + self::$web->blocksMotion = false; + } +} + diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index 15269ab5..73082ecd 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -17,8 +17,9 @@ class StaticBlock public static $hasPhysics = []; public static $isLiquid = []; public static $isFullBlock = []; - + public static $material = []; public static $hardness = []; + public static $destroyTime = []; public static $slipperiness = []; public static $boundingBoxes = []; public static $minXs = [], $minYs = [], $minZs = [], $maxXs = [], $maxYs = [], $maxZs = []; @@ -40,6 +41,8 @@ class StaticBlock self::$slipperiness[$b->getID()] = $b->slipperiness; self::$boundingBoxes[$b->getID()] = $b->boundingBox; self::$hardness[$b->getID()] = $b->getHardness(); + self::$destroyTime[$b->getID()] = $b->breakTime; + self::$material[$b->getID()] = $b->material; self::$prealloc[$b->getID()] = $b; $b::$blockID = $b->getID(); FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0); @@ -102,6 +105,10 @@ class StaticBlock FireBlock::setFlammabilityAndCatchingChance(SPONGE, 30, 60); } + public static function getMaterial($blockID){ + return self::$material[$blockID] ?? Material::$air; + } + public static function setBlockBounds($blockID, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ self::$maxXs[$blockID] = $maxX; self::$maxYs[$blockID] = $maxY; diff --git a/src/material/block/attachable/LadderBlock.php b/src/material/block/attachable/LadderBlock.php index 82ef14b3..d4ae8a6f 100644 --- a/src/material/block/attachable/LadderBlock.php +++ b/src/material/block/attachable/LadderBlock.php @@ -28,6 +28,8 @@ class LadderBlock extends TransparentBlock{ $this->isSolid = false; $this->isFullBlock = false; $this->hardness = 2; + $this->breakTime = 0.4; + $this->material = Material::$decoration; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if($face === 0 || $face === 1){ diff --git a/src/material/block/attachable/SignPostBlock.php b/src/material/block/attachable/SignPostBlock.php index a635eccc..8a1db4e2 100644 --- a/src/material/block/attachable/SignPostBlock.php +++ b/src/material/block/attachable/SignPostBlock.php @@ -16,6 +16,8 @@ class SignPostBlock extends TransparentBlock{ $this->isSolid = false; $this->isFullBlock = false; $this->hardness = 5; + $this->breakTime = 1; + $this->material = Material::$wood; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/attachable/TorchBlock.php b/src/material/block/attachable/TorchBlock.php index 761ba9d7..87d7cc36 100644 --- a/src/material/block/attachable/TorchBlock.php +++ b/src/material/block/attachable/TorchBlock.php @@ -5,6 +5,8 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(TORCH, $meta, "Torch"); $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$decoration; } public function getMaxLightValue(){ diff --git a/src/material/block/attachable/TrapdoorBlock.php b/src/material/block/attachable/TrapdoorBlock.php index 6e932bde..3af17929 100644 --- a/src/material/block/attachable/TrapdoorBlock.php +++ b/src/material/block/attachable/TrapdoorBlock.php @@ -11,6 +11,8 @@ class TrapdoorBlock extends TransparentBlock{ $this->isFullBlock = true; } $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$wood; } public function canAttachTo(Block $target){ diff --git a/src/material/block/attachable/WallSignBlock.php b/src/material/block/attachable/WallSignBlock.php index 02ca1aa5..5fe7179d 100644 --- a/src/material/block/attachable/WallSignBlock.php +++ b/src/material/block/attachable/WallSignBlock.php @@ -9,6 +9,9 @@ class WallSignBlock extends SignPostBlock{ public function __construct($meta = 0){ TransparentBlock::__construct(WALL_SIGN, $meta, "Wall Sign"); $this->isSolid = false; + $this->breakTime = 1; + $this->hardness = 5; + $this->material = Material::$wood; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/liquid/LavaBlock.php b/src/material/block/liquid/LavaBlock.php index 9329d736..3fbe9c65 100644 --- a/src/material/block/liquid/LavaBlock.php +++ b/src/material/block/liquid/LavaBlock.php @@ -5,7 +5,9 @@ class LavaBlock extends LiquidBlockDynamic implements LightingBlock{ public static $blockID = LAVA; public function __construct($meta = 0){ parent::__construct(LAVA, $meta, "Lava"); - $this->hardness = 500; + $this->hardness = 500; //0 in vanilla + $this->breakTime = -1; //in vanilla it is 0, but you cant interact with the block so it is not possible to destroy it + $this->material = Material::$lava; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/liquid/StillLavaBlock.php b/src/material/block/liquid/StillLavaBlock.php index 2a8e89cf..b4dff404 100644 --- a/src/material/block/liquid/StillLavaBlock.php +++ b/src/material/block/liquid/StillLavaBlock.php @@ -6,6 +6,8 @@ class StillLavaBlock extends LiquidBlockStatic{ public function __construct($meta = 0){ parent::__construct(STILL_LAVA, $meta, "Still Lava"); $this->hardness = 500; + $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it + $this->material = Material::$lava; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/StillWaterBlock.php b/src/material/block/liquid/StillWaterBlock.php index 632177a3..5072cd5b 100644 --- a/src/material/block/liquid/StillWaterBlock.php +++ b/src/material/block/liquid/StillWaterBlock.php @@ -12,6 +12,8 @@ class StillWaterBlock extends LiquidBlockStatic{ public function __construct($meta = 0){ LiquidBlock::__construct(STILL_WATER, $meta, "Still Water"); $this->hardness = 500; + $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it + $this->material = Material::$water; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/WaterBlock.php b/src/material/block/liquid/WaterBlock.php index c9e5c3e9..1b090e21 100644 --- a/src/material/block/liquid/WaterBlock.php +++ b/src/material/block/liquid/WaterBlock.php @@ -7,6 +7,8 @@ class WaterBlock extends LiquidBlockDynamic{ public function __construct($meta = 0){ parent::__construct(WATER, $meta, "Water"); $this->hardness = 500; + $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it + $this->material = Material::$water; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/misc/AirBlock.php b/src/material/block/misc/AirBlock.php index 9d6545b9..07499700 100644 --- a/src/material/block/misc/AirBlock.php +++ b/src/material/block/misc/AirBlock.php @@ -15,6 +15,7 @@ class AirBlock extends TransparentBlock{ $this->isSolid = false; $this->isFullBlock = true; $this->hardness = 0; + $this->material = Material::$air; } diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index 2d9af20d..58fb6c2b 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -19,6 +19,8 @@ class BedBlock extends TransparentBlock{ $this->isActivable = true; $this->isFullBlock = false; $this->hardness = 1; + $this->breakTime = 0.2; + $this->material = Material::$cloth; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index 8e3b9365..9e04dd86 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -11,6 +11,8 @@ class FireBlock extends FlowableBlock implements LightingBlock{ $this->breakable = false; $this->isFullBlock = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$fire; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 8c1ff0d0..1d62a1ff 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -6,6 +6,9 @@ class NetherReactorBlock extends SolidBlock{ public function __construct($meta = 0){ parent::__construct(NETHER_REACTOR, $meta, "Nether Reactor"); $this->isActivable = true; + $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$metal; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/misc/TNTBlock.php b/src/material/block/misc/TNTBlock.php index 5fb3da3e..3afbb6ab 100644 --- a/src/material/block/misc/TNTBlock.php +++ b/src/material/block/misc/TNTBlock.php @@ -5,7 +5,9 @@ class TNTBlock extends SolidBlock{ public function __construct(){ parent::__construct(TNT, 0, "TNT"); $this->hardness = 0; + $this->breakTime = 0; $this->isActivable = true; + $this->material = Material::$explosive; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/nonfull/CakeBlock.php b/src/material/block/nonfull/CakeBlock.php index f7eded78..fe276310 100644 --- a/src/material/block/nonfull/CakeBlock.php +++ b/src/material/block/nonfull/CakeBlock.php @@ -8,6 +8,8 @@ class CakeBlock extends TransparentBlock{ $this->isActivable = true; $this->meta = $meta & 0x07; $this->hardness = 2.5; + $this->breakTime = 0.5; + $this->material = Material::$cake; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/nonfull/CarpetBlock.php b/src/material/block/nonfull/CarpetBlock.php index 60b93a98..6128c357 100644 --- a/src/material/block/nonfull/CarpetBlock.php +++ b/src/material/block/nonfull/CarpetBlock.php @@ -23,9 +23,11 @@ class CarpetBlock extends FlowableBlock{ 15 => "Black Carpet", ); $this->name = $names[$this->meta]; - $this->hardness = 0; + $this->hardness = 0.5; + $this->breakTime = 0.1; $this->isFullBlock = false; $this->isSolid = true; + $this->material = Material::$cloth; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [static::getAABB($level, $x, $y, $z)]; diff --git a/src/material/block/nonfull/ChestBlock.php b/src/material/block/nonfull/ChestBlock.php index 344daff4..cdc68777 100644 --- a/src/material/block/nonfull/ChestBlock.php +++ b/src/material/block/nonfull/ChestBlock.php @@ -7,6 +7,8 @@ class ChestBlock extends TransparentBlock{ $this->isActivable = true; $this->isFullBlock = false; $this->hardness = 15; + $this->breakTime = 2.5; + $this->material = Material::$wood; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x + 0.025, $y, $z + 0.025, $x + 0.975, $y + 0.95, $z + 0.975)]; diff --git a/src/material/block/nonfull/CobwebBlock.php b/src/material/block/nonfull/CobwebBlock.php index 4b6a9226..133bceee 100644 --- a/src/material/block/nonfull/CobwebBlock.php +++ b/src/material/block/nonfull/CobwebBlock.php @@ -6,7 +6,9 @@ class CobwebBlock extends FlowableBlock{ parent::__construct(COBWEB, 0, "Cobweb"); $this->isSolid = true; $this->isFullBlock = false; - $this->hardness = 25; + $this->hardness = 30; + $this->breakTime = 4; + $this->material = Material::$web; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index be09ba6c..b431bb01 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -5,6 +5,8 @@ class FarmlandBlock extends TransparentBlock{ public function __construct($meta = 0){ parent::__construct(FARMLAND, $meta, "Farmland"); $this->hardness = 3; + $this->breakTime = 0.6; + $this->material = Material::$dirt; } public function getDrops(Item $item, Player $player){ return array( diff --git a/src/material/block/nonfull/FenceBlock.php b/src/material/block/nonfull/FenceBlock.php index 98442208..2af3da8c 100644 --- a/src/material/block/nonfull/FenceBlock.php +++ b/src/material/block/nonfull/FenceBlock.php @@ -6,6 +6,8 @@ class FenceBlock extends TransparentBlock{ parent::__construct(FENCE, 0, "Fence"); $this->isFullBlock = false; $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } public static function canConnectTo(Level $level, $x, $y, $z){ diff --git a/src/material/block/nonfull/FenceGateBlock.php b/src/material/block/nonfull/FenceGateBlock.php index be286bf9..977f346c 100644 --- a/src/material/block/nonfull/FenceGateBlock.php +++ b/src/material/block/nonfull/FenceGateBlock.php @@ -31,6 +31,8 @@ class FenceGateBlock extends TransparentBlock{ $this->isFullBlock = false; } $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $faces = array( diff --git a/src/material/block/nonfull/GlassPaneBlock.php b/src/material/block/nonfull/GlassPaneBlock.php index 0af71ac4..810be468 100644 --- a/src/material/block/nonfull/GlassPaneBlock.php +++ b/src/material/block/nonfull/GlassPaneBlock.php @@ -6,6 +6,9 @@ class GlassPaneBlock extends TransparentBlock{ parent::__construct(GLASS_PANE, 0, "Glass Pane"); $this->isFullBlock = false; $this->isSolid = false; + $this->breakTime = 0.3; + $this->hardness = 0.3*5; + $this->material = Material::$glass; } public static function updateShape(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); diff --git a/src/material/block/nonfull/IronBarsBlock.php b/src/material/block/nonfull/IronBarsBlock.php index feeca1cc..3d15b857 100644 --- a/src/material/block/nonfull/IronBarsBlock.php +++ b/src/material/block/nonfull/IronBarsBlock.php @@ -6,6 +6,9 @@ class IronBarsBlock extends TransparentBlock{ parent::__construct(IRON_BARS, 0, "Iron Bars"); $this->isFullBlock = false; $this->isSolid = false; + $this->breakTime = 5; + $this->hardness = 30; + $this->material = Material::$metal; } public static function updateShape(Level $level, $x, $y, $z){ diff --git a/src/material/block/nonfull/IronDoorBlock.php b/src/material/block/nonfull/IronDoorBlock.php index 5ac59aae..26676986 100644 --- a/src/material/block/nonfull/IronDoorBlock.php +++ b/src/material/block/nonfull/IronDoorBlock.php @@ -6,6 +6,8 @@ class IronDoorBlock extends DoorBlock{ parent::__construct(IRON_DOOR_BLOCK, $meta, "Iron Door Block"); //$this->isActivable = true; $this->hardness = 25; + $this->breakTime = 5; + $this->material = Material::$metal; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/nonfull/PoweredRailBlock.php b/src/material/block/nonfull/PoweredRailBlock.php index 0fd8a32a..e7bb2b35 100644 --- a/src/material/block/nonfull/PoweredRailBlock.php +++ b/src/material/block/nonfull/PoweredRailBlock.php @@ -4,9 +4,11 @@ class PoweredRailBlock extends RailBaseBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(POWERED_RAIL, 0, "PoweredRailBlock"); - $this->hardness = 0.7; $this->isFullBlock = false; $this->isSolid = false; + $this->hardness = 3.5; + $this->breakTime = 0.7; + $this->material = Material::$decoration; } } \ No newline at end of file diff --git a/src/material/block/nonfull/RailBlock.php b/src/material/block/nonfull/RailBlock.php index 9d63e231..b6b18594 100644 --- a/src/material/block/nonfull/RailBlock.php +++ b/src/material/block/nonfull/RailBlock.php @@ -4,9 +4,11 @@ class RailBlock extends RailBaseBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(RAIL, $meta, "Rail"); - $this->hardness = 0.7; + $this->hardness = 0.7*5; + $this->breakTime = 0.7; $this->isFullBlock = false; $this->isSolid = false; + $this->material = Material::$decoration; } public function updateState(){ diff --git a/src/material/block/nonfull/SlabBlock.php b/src/material/block/nonfull/SlabBlock.php index 3319bb7b..da167514 100644 --- a/src/material/block/nonfull/SlabBlock.php +++ b/src/material/block/nonfull/SlabBlock.php @@ -22,6 +22,8 @@ class SlabBlock extends TransparentBlock{ $this->isFullBlock = false; } $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public static function updateShape(Level $level, $x, $y, $z){ diff --git a/src/material/block/nonfull/SnowLayerBlock.php b/src/material/block/nonfull/SnowLayerBlock.php index 3e9961a3..9d03ff58 100644 --- a/src/material/block/nonfull/SnowLayerBlock.php +++ b/src/material/block/nonfull/SnowLayerBlock.php @@ -8,6 +8,8 @@ class SnowLayerBlock extends FlowableBlock{ $this->isSolid = false; $this->isFullBlock = false; $this->hardness = 0.5; + $this->breakTime = 0.1; + $this->material = Material::$topSnow; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/nonfull/StoneWallBlock.php b/src/material/block/nonfull/StoneWallBlock.php index 730bc480..f70c5526 100644 --- a/src/material/block/nonfull/StoneWallBlock.php +++ b/src/material/block/nonfull/StoneWallBlock.php @@ -7,7 +7,9 @@ class StoneWallBlock extends TransparentBlock{ parent::__construct(STONE_WALL, $meta & 0x01, $meta === 1 ? "Mossy Cobblestone Wall" : "Cobblestone Wall"); $this->isFullBlock = false; $this->isSolid = false; - $this->hardness = 30; + $this->hardness = 50; + $this->breakTime = 10; + $this->material = Material::$stone; } public function getDrops(Item $item, Player $player){ if($item->isPickaxe()){ diff --git a/src/material/block/nonfull/WoodDoorBlock.php b/src/material/block/nonfull/WoodDoorBlock.php index 7109a6b6..91bbce49 100644 --- a/src/material/block/nonfull/WoodDoorBlock.php +++ b/src/material/block/nonfull/WoodDoorBlock.php @@ -6,6 +6,8 @@ class WoodDoorBlock extends DoorBlock{ parent::__construct(WOOD_DOOR_BLOCK, $meta, "Wood Door Block"); $this->isActivable = true; $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$wood; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/nonfull/WoodSlabBlock.php b/src/material/block/nonfull/WoodSlabBlock.php index 776dc334..64ced90a 100644 --- a/src/material/block/nonfull/WoodSlabBlock.php +++ b/src/material/block/nonfull/WoodSlabBlock.php @@ -16,7 +16,9 @@ class WoodSlabBlock extends TransparentBlock{ }else{ $this->isFullBlock = false; } - $this->hardness = 15; + $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$wood; } public static function updateShape(Level $level, $x, $y, $z){ diff --git a/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php b/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php index 619208b7..21979c8f 100644 --- a/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php @@ -4,6 +4,9 @@ class BirchWoodStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(BIRCH_WOOD_STAIRS, $meta, "Birch Wood Stairs"); + $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/nonfull/stairs/BrickStairsBlock.php b/src/material/block/nonfull/stairs/BrickStairsBlock.php index 133a5f0b..2d22e1fa 100644 --- a/src/material/block/nonfull/stairs/BrickStairsBlock.php +++ b/src/material/block/nonfull/stairs/BrickStairsBlock.php @@ -4,6 +4,9 @@ class BrickStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(BRICK_STAIRS, $meta, "Brick Stairs"); + $this->breakTime = 2; + $this->hardness = 30; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php b/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php index 54d1710a..80095c91 100644 --- a/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php +++ b/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php @@ -4,6 +4,9 @@ class CobblestoneStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(COBBLESTONE_STAIRS, $meta, "Cobblestone Stairs"); + $this->breakTime = 2; + $this->hardness = 30; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php b/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php index 76c4bc68..179948b1 100644 --- a/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php @@ -4,6 +4,9 @@ class JungleWoodStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(JUNGLE_WOOD_STAIRS, $meta, "Jungle Wood Stairs"); + $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php b/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php index 0b984590..520506ad 100644 --- a/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php +++ b/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php @@ -4,6 +4,9 @@ class NetherBricksStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(NETHER_BRICKS_STAIRS, $meta, "Nether Bricks Stairs"); + $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/QuartzStairsBlock.php b/src/material/block/nonfull/stairs/QuartzStairsBlock.php index 062b60d0..136ded10 100644 --- a/src/material/block/nonfull/stairs/QuartzStairsBlock.php +++ b/src/material/block/nonfull/stairs/QuartzStairsBlock.php @@ -4,6 +4,9 @@ class QuartzStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(QUARTZ_STAIRS, $meta, "Quartz Stairs"); + $this->breakTime = 0.8; + $this->hardness = 4; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/SandstoneStairsBlock.php b/src/material/block/nonfull/stairs/SandstoneStairsBlock.php index 8512e628..7b374f2b 100644 --- a/src/material/block/nonfull/stairs/SandstoneStairsBlock.php +++ b/src/material/block/nonfull/stairs/SandstoneStairsBlock.php @@ -4,6 +4,9 @@ class SandstoneStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(SANDSTONE_STAIRS, $meta, "Sandstone Stairs"); + $this->hardness = 0.8*5; + $this->breakTime = 0.8; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php b/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php index f001a888..98f65fb4 100644 --- a/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php @@ -4,6 +4,9 @@ class SpruceWoodStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(SPRUCE_WOOD_STAIRS, $meta, "Spruce Wood Stairs"); + $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php b/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php index b43efbb7..40575ca3 100644 --- a/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php +++ b/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php @@ -4,6 +4,9 @@ class StoneBrickStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(STONE_BRICK_STAIRS, $meta, "Stone Brick Stairs"); + $this->hardness = 30; + $this->breakTime = 1.5; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/WoodStairsBlock.php b/src/material/block/nonfull/stairs/WoodStairsBlock.php index e8b5d35d..e507e102 100644 --- a/src/material/block/nonfull/stairs/WoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/WoodStairsBlock.php @@ -4,6 +4,8 @@ class WoodStairsBlock extends StairBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(WOOD_STAIRS, $meta, "Wood Stairs"); + $this->breakTime = 2; + $this->material = Material::$wood; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/CoalOreBlock.php b/src/material/block/ore/CoalOreBlock.php index 12e5c262..1831be50 100644 --- a/src/material/block/ore/CoalOreBlock.php +++ b/src/material/block/ore/CoalOreBlock.php @@ -5,6 +5,8 @@ class CoalOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(COAL_ORE, 0, "Coal Ore"); $this->hardness = 15; + $this->breakTime = 3.0; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/DiamondOreBlock.php b/src/material/block/ore/DiamondOreBlock.php index dec08e78..42d73d60 100644 --- a/src/material/block/ore/DiamondOreBlock.php +++ b/src/material/block/ore/DiamondOreBlock.php @@ -5,6 +5,8 @@ class DiamondOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(DIAMOND_ORE, 0, "Diamond Ore"); $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/GlowingRedstoneOreBlock.php b/src/material/block/ore/GlowingRedstoneOreBlock.php index 33dd8e0c..6a415480 100644 --- a/src/material/block/ore/GlowingRedstoneOreBlock.php +++ b/src/material/block/ore/GlowingRedstoneOreBlock.php @@ -5,6 +5,8 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ public function __construct(){ parent::__construct(GLOWING_REDSTONE_ORE, 0, "Glowing Redstone Ore"); $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$stone; } public static function onRandomTick(Level $level, $x, $y, $z){ diff --git a/src/material/block/ore/GoldOreBlock.php b/src/material/block/ore/GoldOreBlock.php index c835dbc1..f494bc4b 100644 --- a/src/material/block/ore/GoldOreBlock.php +++ b/src/material/block/ore/GoldOreBlock.php @@ -5,6 +5,8 @@ class GoldOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(GOLD_ORE, 0, "Gold Ore"); $this->hardness = 15; + $this->breakTime = 3.0; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/IronOreBlock.php b/src/material/block/ore/IronOreBlock.php index d8d001ff..693ca0ca 100644 --- a/src/material/block/ore/IronOreBlock.php +++ b/src/material/block/ore/IronOreBlock.php @@ -5,6 +5,8 @@ class IronOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(IRON_ORE, 0, "Iron Ore"); $this->hardness = 15; + $this->breakTime = 3.0; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/LapisOreBlock.php b/src/material/block/ore/LapisOreBlock.php index ecb3e83f..10c61e8a 100644 --- a/src/material/block/ore/LapisOreBlock.php +++ b/src/material/block/ore/LapisOreBlock.php @@ -5,6 +5,8 @@ class LapisOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(LAPIS_ORE, 0, "Lapis Ore"); $this->hardness = 15; + $this->breakTime = 3.0; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/RedstoneOreBlock.php b/src/material/block/ore/RedstoneOreBlock.php index 471f87ae..962651a5 100644 --- a/src/material/block/ore/RedstoneOreBlock.php +++ b/src/material/block/ore/RedstoneOreBlock.php @@ -5,6 +5,8 @@ class RedstoneOreBlock extends SolidBlock{ public function __construct(){ parent::__construct(REDSTONE_ORE, 0, "Redstone Ore"); $this->hardness = 15; + $this->breakTime = 3; + $this->material = Material::$stone; } public static function interact(Level $level, $x, $y, $z, Player $player){ diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index 88a22d4d..05d2c646 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -6,6 +6,8 @@ class BeetrootBlock extends FlowableBlock{ parent::__construct(BEETROOT_BLOCK, $meta, "Beetroot Block"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index 495d8a73..eb2edf11 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -5,6 +5,8 @@ class BrownMushroomBlock extends FlowableBlock{ public function __construct(){ parent::__construct(BROWN_MUSHROOM, 0, "Brown Mushroom"); $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/plant/CactusBlock.php b/src/material/block/plant/CactusBlock.php index 4dd97196..b218d8b9 100644 --- a/src/material/block/plant/CactusBlock.php +++ b/src/material/block/plant/CactusBlock.php @@ -6,6 +6,8 @@ class CactusBlock extends TransparentBlock{ parent::__construct(CACTUS, $meta, "Cactus"); $this->isFullBlock = false; $this->hardness = 2; + $this->breakTime = 0.4; + $this->material = Material::$cactus; } public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index 36c3e931..98717892 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -6,6 +6,8 @@ class CarrotBlock extends FlowableBlock{ parent::__construct(CARROT_BLOCK, $meta, "Carrot Block"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/CyanFlowerBlock.php b/src/material/block/plant/CyanFlowerBlock.php index 16c3c97c..c48704e0 100644 --- a/src/material/block/plant/CyanFlowerBlock.php +++ b/src/material/block/plant/CyanFlowerBlock.php @@ -6,6 +6,8 @@ class CyanFlowerBlock extends FlowableBlock{ parent::__construct(CYAN_FLOWER, 0, "Cyan Flower"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index 9c61d1bd..bc4d56f2 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -6,6 +6,8 @@ class DandelionBlock extends FlowableBlock{ parent::__construct(DANDELION, 0, "Dandelion"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/plant/DeadBushBlock.php b/src/material/block/plant/DeadBushBlock.php index 4f7d49b7..f1f51e38 100644 --- a/src/material/block/plant/DeadBushBlock.php +++ b/src/material/block/plant/DeadBushBlock.php @@ -6,6 +6,8 @@ class DeadBushBlock extends FlowableBlock{ parent::__construct(DEAD_BUSH, 0, "Dead Bush"); //$this->isReplaceable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$replaceable_plant; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index 2022f819..10500032 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -6,6 +6,8 @@ class MelonStemBlock extends FlowableBlock{ parent::__construct(MELON_STEM, $meta, "Melon Stem"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index 54ec8cec..d8f86f38 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -6,6 +6,8 @@ class PotatoBlock extends FlowableBlock{ parent::__construct(POTATO_BLOCK, $meta, "Potato Block"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index a40f9f5f..ac34ed7d 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -6,6 +6,8 @@ class PumpkinStemBlock extends FlowableBlock{ parent::__construct(PUMPKIN_STEM, $meta, "Pumpkin Stem"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); diff --git a/src/material/block/plant/RedMushroomBlock.php b/src/material/block/plant/RedMushroomBlock.php index 29bdc225..a3507380 100644 --- a/src/material/block/plant/RedMushroomBlock.php +++ b/src/material/block/plant/RedMushroomBlock.php @@ -5,6 +5,8 @@ class RedMushroomBlock extends FlowableBlock{ public function __construct(){ parent::__construct(RED_MUSHROOM, 0, "Red Mushroom"); $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index 86cc8048..bd5635d0 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -19,6 +19,8 @@ class SaplingBlock extends FlowableBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/plant/SugarcaneBlock.php b/src/material/block/plant/SugarcaneBlock.php index f17fd596..279952e2 100644 --- a/src/material/block/plant/SugarcaneBlock.php +++ b/src/material/block/plant/SugarcaneBlock.php @@ -6,6 +6,8 @@ class SugarcaneBlock extends FlowableBlock{ parent::__construct(SUGARCANE_BLOCK, $meta, "Sugarcane"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/plant/TallGrassBlock.php b/src/material/block/plant/TallGrassBlock.php index df8ce39f..02f8b4bd 100644 --- a/src/material/block/plant/TallGrassBlock.php +++ b/src/material/block/plant/TallGrassBlock.php @@ -12,6 +12,8 @@ class TallGrassBlock extends FlowableBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$replaceable_plant; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 176a2065..97f3d39f 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -6,6 +6,8 @@ class WheatBlock extends FlowableBlock{ parent::__construct(WHEAT_BLOCK, $meta, "Wheat Block"); $this->isActivable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/BedrockBlock.php b/src/material/block/solid/BedrockBlock.php index 8974deae..4799553b 100644 --- a/src/material/block/solid/BedrockBlock.php +++ b/src/material/block/solid/BedrockBlock.php @@ -6,6 +6,8 @@ class BedrockBlock extends SolidBlock{ parent::__construct(BEDROCK, 0, "Bedrock"); $this->breakable = false; $this->hardness = 18000000; + $this->breakTime = -1; + $this->material = Material::$stone; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/BookshelfBlock.php b/src/material/block/solid/BookshelfBlock.php index 8f19b6fe..80955d9e 100644 --- a/src/material/block/solid/BookshelfBlock.php +++ b/src/material/block/solid/BookshelfBlock.php @@ -5,6 +5,8 @@ class BookshelfBlock extends SolidBlock{ public function __construct(){ parent::__construct(BOOKSHELF, 0, "Bookshelf"); $this->hardness = 7.5; + $this->breakTime = 1.5; + $this->material = Material::$wood; } } \ No newline at end of file diff --git a/src/material/block/solid/BricksBlock.php b/src/material/block/solid/BricksBlock.php index 97f448b2..cea18904 100644 --- a/src/material/block/solid/BricksBlock.php +++ b/src/material/block/solid/BricksBlock.php @@ -5,6 +5,8 @@ class BricksBlock extends SolidBlock{ public function __construct(){ parent::__construct(BRICKS_BLOCK, 0, "Bricks"); $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/BurningFurnaceBlock.php b/src/material/block/solid/BurningFurnaceBlock.php index 0adb007c..50944933 100644 --- a/src/material/block/solid/BurningFurnaceBlock.php +++ b/src/material/block/solid/BurningFurnaceBlock.php @@ -6,6 +6,8 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ parent::__construct(BURNING_FURNACE, $meta, "Burning Furnace"); $this->isActivable = true; $this->hardness = 17.5; + $this->breakTime = 3.5; + $this->material = Material::$stone; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/ClayBlock.php b/src/material/block/solid/ClayBlock.php index 3ca8b2a4..e1c88681 100644 --- a/src/material/block/solid/ClayBlock.php +++ b/src/material/block/solid/ClayBlock.php @@ -5,6 +5,8 @@ class ClayBlock extends SolidBlock{ public function __construct(){ parent::__construct(CLAY_BLOCK, 0, "Clay Block"); $this->hardness = 3; + $this->breakTime = 0.6; + $this->material = Material::$clay; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/CoalBlock.php b/src/material/block/solid/CoalBlock.php index cf7a252e..6c7a11e2 100644 --- a/src/material/block/solid/CoalBlock.php +++ b/src/material/block/solid/CoalBlock.php @@ -5,6 +5,8 @@ class CoalBlock extends SolidBlock{ public function __construct(){ parent::__construct(COAL_BLOCK, 0, "Coal Block"); $this->hardness = 30; + $this->breakTime = 5; + $this->material = Material::$metal; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/CobblestoneBlock.php b/src/material/block/solid/CobblestoneBlock.php index 895953d1..96bc7172 100644 --- a/src/material/block/solid/CobblestoneBlock.php +++ b/src/material/block/solid/CobblestoneBlock.php @@ -5,6 +5,8 @@ class CobblestoneBlock extends SolidBlock{ public function __construct(){ parent::__construct(COBBLESTONE, 0, "Cobblestone"); $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DiamondBlock.php b/src/material/block/solid/DiamondBlock.php index 2ffeed39..9fc92a18 100644 --- a/src/material/block/solid/DiamondBlock.php +++ b/src/material/block/solid/DiamondBlock.php @@ -5,6 +5,8 @@ class DiamondBlock extends SolidBlock{ public function __construct(){ parent::__construct(DIAMOND_BLOCK, 0, "Diamond Block"); $this->hardness = 30; + $this->breakTime = 5; + $this->material = Material::$metal; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index 46131b49..fef766f0 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -6,6 +6,8 @@ class DirtBlock extends SolidBlock{ parent::__construct(DIRT, 0, "Dirt"); $this->isActivable = true; $this->hardness = 2.5; + $this->breakTime = 0.5; + $this->material = Material::$dirt; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleSlabBlock.php b/src/material/block/solid/DoubleSlabBlock.php index 4b6f28db..c455099d 100755 --- a/src/material/block/solid/DoubleSlabBlock.php +++ b/src/material/block/solid/DoubleSlabBlock.php @@ -16,6 +16,8 @@ class DoubleSlabBlock extends SolidBlock{ ); $this->name = "Double " . $names[$this->meta & 0x07] . " Slab"; $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleWoodSlabBlock.php b/src/material/block/solid/DoubleWoodSlabBlock.php index 74316ede..62aa3c40 100644 --- a/src/material/block/solid/DoubleWoodSlabBlock.php +++ b/src/material/block/solid/DoubleWoodSlabBlock.php @@ -11,7 +11,9 @@ class DoubleWoodSlabBlock extends SolidBlock{ 3 => "Jungle", ); $this->name = "Double " . $names[$this->meta & 0x07] . " Wooden Slab"; - $this->hardness = 15; + $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$wood; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/FurnaceBlock.php b/src/material/block/solid/FurnaceBlock.php index 06f2db28..3f8253e2 100644 --- a/src/material/block/solid/FurnaceBlock.php +++ b/src/material/block/solid/FurnaceBlock.php @@ -12,5 +12,6 @@ class FurnaceBlock extends BurningFurnaceBlock{ $this->id = FURNACE; $this->name = "Furnace"; $this->isActivable = true; + $this->material = Material::$stone; } } \ No newline at end of file diff --git a/src/material/block/solid/GlassBlock.php b/src/material/block/solid/GlassBlock.php index a6149c59..f3c50569 100644 --- a/src/material/block/solid/GlassBlock.php +++ b/src/material/block/solid/GlassBlock.php @@ -5,6 +5,8 @@ class GlassBlock extends TransparentBlock{ public function __construct(){ parent::__construct(GLASS, 0, "Glass"); $this->hardness = 1.5; + $this->breakTime = 0.3; + $this->material = Material::$glass; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/GlowingObsidianBlock.php b/src/material/block/solid/GlowingObsidianBlock.php index ea88b779..cefcb5c5 100644 --- a/src/material/block/solid/GlowingObsidianBlock.php +++ b/src/material/block/solid/GlowingObsidianBlock.php @@ -4,6 +4,9 @@ class GlowingObsidianBlock extends SolidBlock implements LightingBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(GLOWING_OBSIDIAN, $meta, "Glowing Obsidian"); + $this->hardness = 6000; + $this->breakTime = 10; + $this->material = Material::$stone; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GlowstoneBlock.php b/src/material/block/solid/GlowstoneBlock.php index 177cbb3f..f746e6c4 100644 --- a/src/material/block/solid/GlowstoneBlock.php +++ b/src/material/block/solid/GlowstoneBlock.php @@ -5,6 +5,8 @@ class GlowstoneBlock extends TransparentBlock implements LightingBlock{ public function __construct(){ parent::__construct(GLOWSTONE_BLOCK, 0, "Glowstone"); $this->hardness = 1.5; + $this->breakTime = 0.3; + $this->material = Material::$glass; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GoldBlock.php b/src/material/block/solid/GoldBlock.php index 80006170..6c623e93 100644 --- a/src/material/block/solid/GoldBlock.php +++ b/src/material/block/solid/GoldBlock.php @@ -5,6 +5,8 @@ class GoldBlock extends SolidBlock{ public function __construct(){ parent::__construct(GOLD_BLOCK, 0, "Gold Block"); $this->hardness = 30; + $this->breakTime = 3; + $this->material = Material::$metal; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index c598b134..10945978 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -6,6 +6,8 @@ class GrassBlock extends SolidBlock{ parent::__construct(GRASS, 0, "Grass"); $this->isActivable = true; $this->hardness = 3; + $this->breakTime = 0.6; + $this->material = Material::$dirt; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/GravelBlock.php b/src/material/block/solid/GravelBlock.php index f35adb53..4842e512 100644 --- a/src/material/block/solid/GravelBlock.php +++ b/src/material/block/solid/GravelBlock.php @@ -5,6 +5,8 @@ class GravelBlock extends FallableBlock{ public function __construct(){ parent::__construct(GRAVEL, 0, "Gravel"); $this->hardness = 3; + $this->breakTime = 0.6; + $this->material = Material::$sand; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/HayBaleBlock.php b/src/material/block/solid/HayBaleBlock.php index 869eebe5..eb748749 100644 --- a/src/material/block/solid/HayBaleBlock.php +++ b/src/material/block/solid/HayBaleBlock.php @@ -4,7 +4,9 @@ class HayBaleBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(HAY_BALE, $meta, "Hay Bale"); - $this->hardness = 10; + $this->hardness = 2.5; + $this->breakTime = 0.5; + $this->material = Material::$dirt; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index e5e7860d..81d6f82f 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -5,7 +5,9 @@ class IceBlock extends TransparentBlock{ public function __construct(){ parent::__construct(ICE, 0, "Ice"); $this->hardness = 2.5; + $this->breakTime = 0.5; $this->slipperiness = 0.98; + $this->material = Material::$ice; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/InfoUpdate2Block.php b/src/material/block/solid/InfoUpdate2Block.php index 0497b861..0bc5cafd 100755 --- a/src/material/block/solid/InfoUpdate2Block.php +++ b/src/material/block/solid/InfoUpdate2Block.php @@ -5,7 +5,9 @@ class InfoUpdate2Block extends SolidBlock{ public function __construct(){ parent::__construct(INFO_UPDATE2, 0, "tile.info_update2.name<"); $this->breakable = true; - $this->hardness = 0; + $this->hardness = 5; + $this->breakTime = 1; + $this->material = Material::$dirt; } } \ No newline at end of file diff --git a/src/material/block/solid/InfoUpdateBlock.php b/src/material/block/solid/InfoUpdateBlock.php index 8b088190..cb7f2b8c 100755 --- a/src/material/block/solid/InfoUpdateBlock.php +++ b/src/material/block/solid/InfoUpdateBlock.php @@ -5,7 +5,9 @@ class InfoUpdateBlock extends SolidBlock{ public function __construct(){ parent::__construct(INFO_UPDATE, 0, "tile.info_update.name<"); $this->breakable = true; - $this->hardness = 0; + $this->hardness = 5; + $this->breakTime = 1; + $this->material = Material::$dirt; } } \ No newline at end of file diff --git a/src/material/block/solid/InvisibleBedrockBlock.php b/src/material/block/solid/InvisibleBedrockBlock.php index dede980e..02724952 100755 --- a/src/material/block/solid/InvisibleBedrockBlock.php +++ b/src/material/block/solid/InvisibleBedrockBlock.php @@ -5,7 +5,9 @@ class InvisibleBedrockBlock extends SolidBlock{ public function __construct(){ parent::__construct(INVISIBLE_BEDROCK, 0, ".name<"); $this->breakable = false; - $this->hardness = 3600000; + $this->hardness = 18000000; + $this->breakTime = -1; + $this->material = Material::$stone; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/IronBlock.php b/src/material/block/solid/IronBlock.php index 504b94a1..2d14ffc4 100644 --- a/src/material/block/solid/IronBlock.php +++ b/src/material/block/solid/IronBlock.php @@ -5,6 +5,8 @@ class IronBlock extends SolidBlock{ public function __construct(){ parent::__construct(IRON_BLOCK, 0, "Iron Block"); $this->hardness = 30; + $this->breakTime = 5; + $this->material = Material::$metal; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LapisBlock.php b/src/material/block/solid/LapisBlock.php index 1d4c4dc6..21fe55cb 100644 --- a/src/material/block/solid/LapisBlock.php +++ b/src/material/block/solid/LapisBlock.php @@ -5,6 +5,8 @@ class LapisBlock extends SolidBlock{ public function __construct(){ parent::__construct(LAPIS_BLOCK, 0, "Lapis Block"); $this->hardness = 15; + $this->breakTime = 3.0; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 8a8c5dc3..1f6e2480 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -15,6 +15,8 @@ class LeavesBlock extends TransparentBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 1; + $this->breakTime = 0.2; + $this->material = Material::$leaves; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/LitPumpkinBlock.php b/src/material/block/solid/LitPumpkinBlock.php index 6775794a..642caf8d 100644 --- a/src/material/block/solid/LitPumpkinBlock.php +++ b/src/material/block/solid/LitPumpkinBlock.php @@ -13,6 +13,8 @@ class LitPumpkinBlock extends SolidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(LIT_PUMPKIN, $meta, "Jack o'Lantern"); $this->hardness = 5; + $this->breakTime = 1.0; + $this->material = Material::$vegetable; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/MelonBlock.php b/src/material/block/solid/MelonBlock.php index 4b46e393..690b43c2 100644 --- a/src/material/block/solid/MelonBlock.php +++ b/src/material/block/solid/MelonBlock.php @@ -5,6 +5,8 @@ class MelonBlock extends TransparentBlock{ public function __construct(){ parent::__construct(MELON_BLOCK, 0, "Melon Block"); $this->hardness = 5; + $this->breakTime = 1; + $this->material = Material::$vegetable; } public function getDrops(Item $item, Player $player){ return array( diff --git a/src/material/block/solid/MossStoneBlock.php b/src/material/block/solid/MossStoneBlock.php index 495de40d..181ee302 100644 --- a/src/material/block/solid/MossStoneBlock.php +++ b/src/material/block/solid/MossStoneBlock.php @@ -5,6 +5,8 @@ class MossStoneBlock extends SolidBlock{ public function __construct(){ parent::__construct(MOSS_STONE, 0, "Moss Stone"); $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherBricksBlock.php b/src/material/block/solid/NetherBricksBlock.php index bf1c7947..f58002d7 100644 --- a/src/material/block/solid/NetherBricksBlock.php +++ b/src/material/block/solid/NetherBricksBlock.php @@ -5,6 +5,8 @@ class NetherBricksBlock extends SolidBlock{ public function __construct(){ parent::__construct(NETHER_BRICKS, 0, "Nether Bricks"); $this->hardness = 30; + $this->breakTime = 2; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherrackBlock.php b/src/material/block/solid/NetherrackBlock.php index 4700a083..203e7b4f 100644 --- a/src/material/block/solid/NetherrackBlock.php +++ b/src/material/block/solid/NetherrackBlock.php @@ -5,6 +5,8 @@ class NetherrackBlock extends SolidBlock{ public function __construct(){ parent::__construct(NETHERRACK, 0, "Netherrack"); $this->hardness = 2; + $this->breakTime = 0.4; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/ObsidianBlock.php b/src/material/block/solid/ObsidianBlock.php index dc0ff733..466201ab 100644 --- a/src/material/block/solid/ObsidianBlock.php +++ b/src/material/block/solid/ObsidianBlock.php @@ -4,7 +4,9 @@ class ObsidianBlock extends SolidBlock{ public static $blockID; public function __construct(){ parent::__construct(OBSIDIAN, 0, "Obsidian"); - $this->hardness = 6000; + $this->hardness = 6000; + $this->breakTime = 10; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/PlanksBlock.php b/src/material/block/solid/PlanksBlock.php index ed0ba041..c4cc531e 100644 --- a/src/material/block/solid/PlanksBlock.php +++ b/src/material/block/solid/PlanksBlock.php @@ -12,6 +12,8 @@ class PlanksBlock extends SolidBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 15; + $this->breakTime = 2; + $this->material = Material::$wood; } } \ No newline at end of file diff --git a/src/material/block/solid/PumpkinBlock.php b/src/material/block/solid/PumpkinBlock.php index 84aff6b3..92002b14 100644 --- a/src/material/block/solid/PumpkinBlock.php +++ b/src/material/block/solid/PumpkinBlock.php @@ -5,6 +5,8 @@ class PumpkinBlock extends SolidBlock{ public function __construct($meta = 0){ parent::__construct(PUMPKIN, $meta, "Pumpkin"); $this->hardness = 5; + $this->breakTime = 1; + $this->material = Material::$vegetable; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/QuartzBlock.php b/src/material/block/solid/QuartzBlock.php index 1c7bcfb7..3640f18b 100644 --- a/src/material/block/solid/QuartzBlock.php +++ b/src/material/block/solid/QuartzBlock.php @@ -11,6 +11,9 @@ class QuartzBlock extends SolidBlock{ 3 => "Quartz Pillar", ); $this->name = $names[$this->meta & 0x03]; + $this->breakTime = 0.8; + $this->hardness = 4; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/Reserved6Block.php b/src/material/block/solid/Reserved6Block.php index 1a9d6bd6..3c86f0a0 100644 --- a/src/material/block/solid/Reserved6Block.php +++ b/src/material/block/solid/Reserved6Block.php @@ -6,6 +6,8 @@ class Reserved6Block extends SolidBlock{ parent::__construct(RESERVED6, 0, ".name<"); $this->breakable = true; $this->hardness = 0; + $this->breakTime = 0; + $this->material = Material::$dirt; } } \ No newline at end of file diff --git a/src/material/block/solid/SandBlock.php b/src/material/block/solid/SandBlock.php index fa41be00..4c9ee6d1 100644 --- a/src/material/block/solid/SandBlock.php +++ b/src/material/block/solid/SandBlock.php @@ -5,6 +5,8 @@ class SandBlock extends FallableBlock{ public function __construct(){ parent::__construct(SAND, 0, "Sand"); $this->hardness = 2.5; + $this->breakTime = 0.5; + $this->material = Material::$sand; } } \ No newline at end of file diff --git a/src/material/block/solid/SandstoneBlock.php b/src/material/block/solid/SandstoneBlock.php index c6533998..1a0d4bab 100644 --- a/src/material/block/solid/SandstoneBlock.php +++ b/src/material/block/solid/SandstoneBlock.php @@ -11,6 +11,8 @@ class SandstoneBlock extends SolidBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 4; + $this->breakTime = 0.8; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/SnowBlock.php b/src/material/block/solid/SnowBlock.php index 2e024c8f..d41d2148 100644 --- a/src/material/block/solid/SnowBlock.php +++ b/src/material/block/solid/SnowBlock.php @@ -5,6 +5,8 @@ class SnowBlock extends SolidBlock{ public function __construct(){ parent::__construct(SNOW_BLOCK, 0, "Snow Block"); $this->hardness = 1; + $this->breakTime = 0.2; + $this->material = Material::$snow; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/SpongeBlock.php b/src/material/block/solid/SpongeBlock.php index 39b4c237..58702087 100644 --- a/src/material/block/solid/SpongeBlock.php +++ b/src/material/block/solid/SpongeBlock.php @@ -5,6 +5,8 @@ class SpongeBlock extends SolidBlock{ public function __construct(){ parent::__construct(SPONGE, 0, "Sponge"); $this->hardness = 3; + $this->breakTime = 0.6; + $this->material = Material::$dirt; } } diff --git a/src/material/block/solid/StoneBlock.php b/src/material/block/solid/StoneBlock.php index 51504b66..55ee212c 100644 --- a/src/material/block/solid/StoneBlock.php +++ b/src/material/block/solid/StoneBlock.php @@ -5,6 +5,8 @@ class StoneBlock extends SolidBlock{ public function __construct(){ parent::__construct(STONE, 0, "Stone"); $this->hardness = 30; + $this->breakTime = 1.5; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StoneBricksBlock.php b/src/material/block/solid/StoneBricksBlock.php index 58299703..1b856076 100644 --- a/src/material/block/solid/StoneBricksBlock.php +++ b/src/material/block/solid/StoneBricksBlock.php @@ -12,6 +12,8 @@ class StoneBricksBlock extends SolidBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 30; + $this->breakTime = 1.5; + $this->material = Material::$stone; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index 110747d1..fc307d70 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -5,6 +5,9 @@ class StonecutterBlock extends SolidBlock{ public function __construct($meta = 0){ parent::__construct(STONECUTTER, $meta, "Stonecutter"); $this->isActivable = true; + $this->breakTime = 2.5; + $this->hardness = 2.5*5; + $this->material = Material::$stone; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index e9711141..04a26316 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -17,6 +17,8 @@ class WoodBlock extends SolidBlock{ ); $this->name = $names[$this->meta & 0x03]; $this->hardness = 10; + $this->breakTime = 2.0; + $this->material = Material::$wood; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/WoolBlock.php b/src/material/block/solid/WoolBlock.php index 8c574f65..52b91735 100644 --- a/src/material/block/solid/WoolBlock.php +++ b/src/material/block/solid/WoolBlock.php @@ -27,6 +27,8 @@ class WoolBlock extends SolidBlock{ $this->name = self::$names[$this->meta] ?? "Illegal Wool"; $this->hardness = 4; + $this->breakTime = 0.8; + $this->material = Material::$cloth; } } \ No newline at end of file diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index 080ac13c..5f80c000 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -5,7 +5,9 @@ class WorkbenchBlock extends SolidBlock{ public function __construct($meta = 0){ parent::__construct(WORKBENCH, $meta, "Crafting Table"); $this->isActivable = true; - $this->hardness = 15; + $this->hardness = 12.5; + $this->breakTime = 2.5; + $this->material = Material::$wood; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/item/armor/ChainBootsItem.php b/src/material/item/armor/ChainBootsItem.php index 73083238..64064b4e 100644 --- a/src/material/item/armor/ChainBootsItem.php +++ b/src/material/item/armor/ChainBootsItem.php @@ -7,7 +7,7 @@ class ChainBootsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::CHAIN; + return ArmorMaterial::CHAIN; } public function getBaseDurability(){ diff --git a/src/material/item/armor/ChainChestplateItem.php b/src/material/item/armor/ChainChestplateItem.php index ae28d0c5..ceca50d2 100644 --- a/src/material/item/armor/ChainChestplateItem.php +++ b/src/material/item/armor/ChainChestplateItem.php @@ -7,7 +7,7 @@ class ChainChestplateItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::CHAIN; + return ArmorMaterial::CHAIN; } public function getBaseDurability(){ diff --git a/src/material/item/armor/ChainHelmetItem.php b/src/material/item/armor/ChainHelmetItem.php index df7c83d9..4b13c654 100644 --- a/src/material/item/armor/ChainHelmetItem.php +++ b/src/material/item/armor/ChainHelmetItem.php @@ -7,7 +7,7 @@ class ChainHelmetItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::CHAIN; + return ArmorMaterial::CHAIN; } public function getBaseDurability(){ diff --git a/src/material/item/armor/ChainLeggingsItem.php b/src/material/item/armor/ChainLeggingsItem.php index 9d1800b1..4ca503f5 100644 --- a/src/material/item/armor/ChainLeggingsItem.php +++ b/src/material/item/armor/ChainLeggingsItem.php @@ -7,7 +7,7 @@ class ChainLeggingsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::CHAIN; + return ArmorMaterial::CHAIN; } public function getBaseDurability(){ diff --git a/src/material/item/armor/DiamondBootsItem.php b/src/material/item/armor/DiamondBootsItem.php index 4591f9d8..3a34c7a2 100644 --- a/src/material/item/armor/DiamondBootsItem.php +++ b/src/material/item/armor/DiamondBootsItem.php @@ -7,7 +7,7 @@ class DiamondBootsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::DIAMOND; + return ArmorMaterial::DIAMOND; } public function getBaseDurability(){ diff --git a/src/material/item/armor/DiamondChestplateItem.php b/src/material/item/armor/DiamondChestplateItem.php index fad4edce..ce3ccc10 100644 --- a/src/material/item/armor/DiamondChestplateItem.php +++ b/src/material/item/armor/DiamondChestplateItem.php @@ -7,7 +7,7 @@ class DiamondChestplateItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::DIAMOND; + return ArmorMaterial::DIAMOND; } public function getBaseDurability(){ diff --git a/src/material/item/armor/DiamondHelmetItem.php b/src/material/item/armor/DiamondHelmetItem.php index b0e53cbc..2ba99542 100644 --- a/src/material/item/armor/DiamondHelmetItem.php +++ b/src/material/item/armor/DiamondHelmetItem.php @@ -7,7 +7,7 @@ class DiamondHelmetItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::DIAMOND; + return ArmorMaterial::DIAMOND; } public function getBaseDurability(){ diff --git a/src/material/item/armor/DiamondLeggingsItem.php b/src/material/item/armor/DiamondLeggingsItem.php index ef2e005d..59ea5993 100644 --- a/src/material/item/armor/DiamondLeggingsItem.php +++ b/src/material/item/armor/DiamondLeggingsItem.php @@ -7,7 +7,7 @@ class DiamondLeggingsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::DIAMOND; + return ArmorMaterial::DIAMOND; } public function getBaseDurability(){ diff --git a/src/material/item/armor/GoldenBootsItem.php b/src/material/item/armor/GoldenBootsItem.php index 1ac0f9a4..a31a7b72 100644 --- a/src/material/item/armor/GoldenBootsItem.php +++ b/src/material/item/armor/GoldenBootsItem.php @@ -7,7 +7,7 @@ class GoldenBootsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::GOLD; + return ArmorMaterial::GOLD; } public function getBaseDurability(){ diff --git a/src/material/item/armor/GoldenChestplateItem.php b/src/material/item/armor/GoldenChestplateItem.php index 739617b6..957dfd36 100644 --- a/src/material/item/armor/GoldenChestplateItem.php +++ b/src/material/item/armor/GoldenChestplateItem.php @@ -7,7 +7,7 @@ class GoldenChestplateItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::GOLD; + return ArmorMaterial::GOLD; } public function getBaseDurability(){ diff --git a/src/material/item/armor/GoldenHelmetItem.php b/src/material/item/armor/GoldenHelmetItem.php index c7ee4711..62339bd5 100644 --- a/src/material/item/armor/GoldenHelmetItem.php +++ b/src/material/item/armor/GoldenHelmetItem.php @@ -7,7 +7,7 @@ class GoldenHelmetItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::GOLD; + return ArmorMaterial::GOLD; } public function getBaseDurability(){ diff --git a/src/material/item/armor/GoldenLeggingsItem.php b/src/material/item/armor/GoldenLeggingsItem.php index ee9dd964..28841697 100644 --- a/src/material/item/armor/GoldenLeggingsItem.php +++ b/src/material/item/armor/GoldenLeggingsItem.php @@ -7,7 +7,7 @@ class GoldenLeggingsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::GOLD; + return ArmorMaterial::GOLD; } public function getBaseDurability(){ diff --git a/src/material/item/armor/IronBootsItem.php b/src/material/item/armor/IronBootsItem.php index 7f548bb9..07c01d24 100644 --- a/src/material/item/armor/IronBootsItem.php +++ b/src/material/item/armor/IronBootsItem.php @@ -7,7 +7,7 @@ class IronBootsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::IRON; + return ArmorMaterial::IRON; } public function getBaseDurability(){ diff --git a/src/material/item/armor/IronChestplateItem.php b/src/material/item/armor/IronChestplateItem.php index 05ca1086..499811da 100644 --- a/src/material/item/armor/IronChestplateItem.php +++ b/src/material/item/armor/IronChestplateItem.php @@ -7,7 +7,7 @@ class IronChestplateItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::IRON; + return ArmorMaterial::IRON; } public function getBaseDurability(){ diff --git a/src/material/item/armor/IronHelmetItem.php b/src/material/item/armor/IronHelmetItem.php index 70c85f5e..fc3602c0 100644 --- a/src/material/item/armor/IronHelmetItem.php +++ b/src/material/item/armor/IronHelmetItem.php @@ -7,7 +7,7 @@ class IronHelmetItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::IRON; + return ArmorMaterial::IRON; } public function getBaseDurability(){ diff --git a/src/material/item/armor/IronLeggingsItem.php b/src/material/item/armor/IronLeggingsItem.php index f646899d..8b33465c 100644 --- a/src/material/item/armor/IronLeggingsItem.php +++ b/src/material/item/armor/IronLeggingsItem.php @@ -7,7 +7,7 @@ class IronLeggingsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::IRON; + return ArmorMaterial::IRON; } public function getBaseDurability(){ diff --git a/src/material/item/armor/LeatherBootsItem.php b/src/material/item/armor/LeatherBootsItem.php index 6ec78a24..f18988d5 100644 --- a/src/material/item/armor/LeatherBootsItem.php +++ b/src/material/item/armor/LeatherBootsItem.php @@ -7,7 +7,7 @@ class LeatherBootsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::LEATHER; + return ArmorMaterial::LEATHER; } public function getBaseDurability(){ diff --git a/src/material/item/armor/LeatherCapItem.php b/src/material/item/armor/LeatherCapItem.php index 9a146839..2790bae1 100755 --- a/src/material/item/armor/LeatherCapItem.php +++ b/src/material/item/armor/LeatherCapItem.php @@ -7,7 +7,7 @@ class LeatherCapItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::LEATHER; + return ArmorMaterial::LEATHER; } public function getBaseDurability(){ diff --git a/src/material/item/armor/LeatherPantsItem.php b/src/material/item/armor/LeatherPantsItem.php index 4fc3bc74..d7ebfaf3 100644 --- a/src/material/item/armor/LeatherPantsItem.php +++ b/src/material/item/armor/LeatherPantsItem.php @@ -7,7 +7,7 @@ class LeatherPantsItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::LEATHER; + return ArmorMaterial::LEATHER; } public function getBaseDurability(){ diff --git a/src/material/item/armor/LeatherTunicItem.php b/src/material/item/armor/LeatherTunicItem.php index ceaa939a..fe63643d 100644 --- a/src/material/item/armor/LeatherTunicItem.php +++ b/src/material/item/armor/LeatherTunicItem.php @@ -7,7 +7,7 @@ class LeatherTunicItem extends ArmorItem{ } public function getMaterialDurability(){ - return Material::LEATHER; + return ArmorMaterial::LEATHER; } public function getBaseDurability(){ diff --git a/src/material/item/base/ItemAxe.php b/src/material/item/base/ItemAxe.php index 5efc1d5b..469bb551 100644 --- a/src/material/item/base/ItemAxe.php +++ b/src/material/item/base/ItemAxe.php @@ -16,5 +16,12 @@ abstract class ItemAxe extends ItemTool default => false, }; } + public function getDestroySpeed($id, $meta){ + $effMult = ItemTool::TIER_EFFICIENCY_MULTIPLIER[$this->getLevel()] ?? ItemTool::TIER_EFFICIENCY_MULTIPLIER[0]; + + $mat = StaticBlock::getMaterial($id); + if($mat == Material::$wood) return $effMult; + return parent::getDestroySpeed($id, $meta); + } } diff --git a/src/material/item/base/ItemPickaxe.php b/src/material/item/base/ItemPickaxe.php index 91666c54..9f731f9c 100644 --- a/src/material/item/base/ItemPickaxe.php +++ b/src/material/item/base/ItemPickaxe.php @@ -21,5 +21,24 @@ abstract class ItemPickaxe extends ItemTool default => false, }; } + + public function canDestroySpecial($id, $meta){ + $level = ItemTool::TIER_MINING_LEVEL[$this->getLevel()] ?? 0; + $mat = StaticBlock::getMaterial($id); + if($id == OBSIDIAN || $id == GLOWING_OBSIDIAN) return $level == 3; + if($id == DIAMOND_BLOCK || $id == DIAMOND_ORE || $id == GOLD_BLOCK || $id == GOLD_ORE || $id == REDSTONE_ORE || $id == GLOWING_REDSTONE_ORE) return $level > 1; + if($id == IRON_BLOCK || $id == IRON_ORE || $id == LAPIS_BLOCK || $id == LAPIS_ORE) return $level > 0; + if($mat == Material::$stone) return true; + return $mat == Material::$metal; + + } + + public function getDestroySpeed($id, $meta){ + $effMult = ItemTool::TIER_EFFICIENCY_MULTIPLIER[$this->getLevel()] ?? ItemTool::TIER_EFFICIENCY_MULTIPLIER[0]; + + $mat = StaticBlock::getMaterial($id); + if($mat == Material::$metal || $mat == Material::$stone) return $effMult; + return parent::getDestroySpeed($id, $meta); + } } diff --git a/src/material/item/base/ItemShovel.php b/src/material/item/base/ItemShovel.php index 20b61eba..fce7cba3 100644 --- a/src/material/item/base/ItemShovel.php +++ b/src/material/item/base/ItemShovel.php @@ -16,5 +16,15 @@ class ItemShovel extends ItemTool default => false, }; } + + public function canDestroySpecial($id, $meta){ + return $id == SNOW_LAYER || $id == SNOW_BLOCK; + } + public function getDestroySpeed($id, $meta){ + $effMult = ItemTool::TIER_EFFICIENCY_MULTIPLIER[$this->getLevel()] ?? ItemTool::TIER_EFFICIENCY_MULTIPLIER[0]; + + if($id == GRASS || $id == DIRT || $id == SAND || $id == GRAVEL || $id == SNOW_LAYER || $id == SNOW || $id == CLAY || $id == FARMLAND) return $effMult; + return parent::getDestroySpeed($id, $meta); + } } diff --git a/src/material/item/base/ItemSword.php b/src/material/item/base/ItemSword.php index 14bd259a..90679e37 100644 --- a/src/material/item/base/ItemSword.php +++ b/src/material/item/base/ItemSword.php @@ -18,6 +18,13 @@ abstract class ItemSword extends ItemTool }; } + public function canDestroySpecial($id, $meta){ + return $id == COBWEB; + } + public function getDestroySpeed($id, $meta){ + if($id == COBWEB) return 15; + return 1.5; //in 0.8.1 it is faster than other items + } public function hurtEnemy(Entity $target, Player $attacker){ $this->hurtAndBreak(1, $attacker); } diff --git a/src/material/item/base/ItemTool.php b/src/material/item/base/ItemTool.php index 061cc065..af4be2ce 100644 --- a/src/material/item/base/ItemTool.php +++ b/src/material/item/base/ItemTool.php @@ -8,6 +8,21 @@ abstract class ItemTool extends Item const IRON_LEVEL = 4; const DIAMOND_LEVEL = 5; + public const TIER_MINING_LEVEL = [ + 1 => 0, //wooden + 2 => 0, //golden + 3 => 1, //stone + 4 => 2, //iron + 5 => 3, //diamond + ]; + public const TIER_EFFICIENCY_MULTIPLIER = [ + 1 => 2.0, //wooden + 2 => 12.0, //golden + 3 => 4.0, //stone + 4 => 6.0, //iron + 5 => 8.0, //diamond + ]; + public function isTool(){ return true; } diff --git a/src/material/item/tool/ShearsItem.php b/src/material/item/tool/ShearsItem.php index db14e68c..1cc66972 100755 --- a/src/material/item/tool/ShearsItem.php +++ b/src/material/item/tool/ShearsItem.php @@ -12,4 +12,13 @@ class ShearsItem extends ItemTool{ } return parent::mineBlock($block, $player); } + + public function canDestroySpecial($id, $meta){ + return $id == COBWEB; + } + public function getDestroySpeed($id, $meta){ + if($id == COBWEB || $id == LEAVES) return 15; + if($id == WOOL) return 5; + return parent::getDestroySpeed($id, $meta); + } } \ No newline at end of file From 11c1b77f5edbb9a157346f2d8e1867cf9e630223 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 14 Jun 2025 07:59:24 +0300 Subject: [PATCH 061/146] flammable = false; + $this->translucent = false; + $this->alwaysDestroyable = false; + $this->replaceable = false; + + $this->blocksMotion = false; + $this->blocksLight = false; + $this->isSolid = false; + } +} + diff --git a/src/material/materials/GasMaterial.php b/src/material/materials/GasMaterial.php new file mode 100644 index 00000000..892fac4a --- /dev/null +++ b/src/material/materials/GasMaterial.php @@ -0,0 +1,14 @@ +blocksLight = false; + $this->blocksMotion = false; + $this->isSolid = false; + $this->replaceable = true; + } +} + diff --git a/src/material/materials/LiquidMaterial.php b/src/material/materials/LiquidMaterial.php new file mode 100644 index 00000000..b5775484 --- /dev/null +++ b/src/material/materials/LiquidMaterial.php @@ -0,0 +1,14 @@ +blocksMotion = false; + $this->isLiquid = true; + $this->isSolid = false; + $this->replaceable = true; + } +} + From 95854506440fec12900346b18cd7213b2ee154d2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 17 Jun 2025 09:54:33 +0300 Subject: [PATCH 062/146] better aabb check when placing blocks --- src/API/BlockAPI.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ea9cc5f2..c70c8c09 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -527,11 +527,13 @@ class BlockAPI{ } if($hand->isSolid === true && ($hand->getID() != BED_BLOCK && $hand->getID() != CARPET)){ - $aabb = $hand->getAABB($block->level, $block->x, $block->y, $block->z); foreach($block->level->players as $pl){ - $playerbb = $pl->entity->boundingBox; - 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); //Entity in block + $aabbs = $hand->getCollisionBoundingBoxes($block->level, $block->x, $block->y, $block->z, $pl->entity); + foreach($aabbs as $aabb){ + $playerbb = $pl->entity->boundingBox; + 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); //Entity in block + } } } From 9acbf518eaa03b2c26fac4a36a7e1b96d9333dea Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 17 Jun 2025 21:05:10 +0300 Subject: [PATCH 063/146] fixed incorrect destruction speed of clay block using shovel --- src/material/item/base/ItemShovel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/item/base/ItemShovel.php b/src/material/item/base/ItemShovel.php index fce7cba3..84bbb9cd 100644 --- a/src/material/item/base/ItemShovel.php +++ b/src/material/item/base/ItemShovel.php @@ -23,7 +23,7 @@ class ItemShovel extends ItemTool public function getDestroySpeed($id, $meta){ $effMult = ItemTool::TIER_EFFICIENCY_MULTIPLIER[$this->getLevel()] ?? ItemTool::TIER_EFFICIENCY_MULTIPLIER[0]; - if($id == GRASS || $id == DIRT || $id == SAND || $id == GRAVEL || $id == SNOW_LAYER || $id == SNOW || $id == CLAY || $id == FARMLAND) return $effMult; + if($id == GRASS || $id == DIRT || $id == SAND || $id == GRAVEL || $id == SNOW_LAYER || $id == SNOW || $id == CLAY_BLOCK || $id == FARMLAND) return $effMult; return parent::getDestroySpeed($id, $meta); } } From 359f1d34c9145aee3da4252460d681b5b132ac09 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 20 Jun 2025 14:53:57 +0300 Subject: [PATCH 064/146] ping will show amount of loaded chunks(+ additional stuff for ops) --- src/API/PlayerAPI.php | 17 ++++++++++++++--- src/Player.php | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index ebea444e..f288c661 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -123,7 +123,7 @@ class PlayerAPI{ $issuer->teleport($this->server->spawn); return "You were teleported to the spawn."; case "ping": - + $additional = false; if(!($issuer instanceof Player)){ if(isset($args[0])){ goto get_ping_of_others; @@ -133,6 +133,7 @@ class PlayerAPI{ if(isset($args[0]) && $this->server->api->ban->isOp($issuer->iusername)){ get_ping_of_others: + $additional = true; $p = $this->get($args[0]); if(!($p instanceof Player)){ return "Player with username '{$args[0]}' is not on server."; @@ -140,8 +141,18 @@ class PlayerAPI{ }else{ $p = $issuer; } - - return "{$p->username}'s ping: " . round($p->getLag(), 2) . "ms, packet loss " . round($p->getPacketLoss() * 100, 2) . "%, " . round($p->getBandwidth() / 1024, 2) . " KB/s\n"; + $ping = round($p->getLag(), 2); + $pkLoss = round($p->getPacketLoss() * 100, 2); + $transfer = round($p->getBandwidth() / 1024, 2); + $additionalInfo = ""; + if($additional){ + $arqCnt = count($p->packetAlwaysRecoverQueue); + $rqCnt = count($p->recoveryQueue); + $emps = $p->entityMovementPacketsPerSecond; + $additionalInfo = "ARQ/RQ/EMPS: $arqCnt/$rqCnt/$emps"; + } + $chunksCnt = count($p->chunkDataSent); + return "{$p->username}'s ping: {$ping}ms, packet loss {$pkLoss}%, $transfer KB/s. Chunks: $chunksCnt/256\n$additionalInfo"; case "gamemode": $player = false; $setgm = false; diff --git a/src/Player.php b/src/Player.php index 37607591..24e62794 100755 --- a/src/Player.php +++ b/src/Player.php @@ -80,7 +80,7 @@ class Player{ * @var array */ public $packetAlwaysRecoverQueue = []; - private $recoveryQueue = []; + public $recoveryQueue = []; private $receiveQueue = []; private $resendQueue = []; private $ackQueue = []; @@ -93,14 +93,23 @@ class Player{ * @var RakNetPacket */ public $entityMovementQueue; + public $entityMovementQueuePrevSentCnt = 0; public $entityMovementQueueLength = 0; + /** + * Used only for statistic in ping command. + * @var integer + */ + public $entityMovementPacketsPerSecond = 0; + public $lastEntityMovementPacketsPerSecondRecordedTime = 0; public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + public $blockUpdateQueuePrevSentCnt = 0; public $blockUpdateQueueOrderIndex = 0; public $chatMessagesQueue; public $chatMessagesQueueLength = 0; + public $chatMessagesQueuePrevSentCnt = 0; public $chatMessagesOrderIndex = 0; public $chunkDataSent = []; @@ -1362,6 +1371,10 @@ class Player{ $this->ackQueue = []; } + if($time - $this->lastEntityMovementPacketsPerSecondRecordedTime > 1){ + $this->entityMovementPacketsPerSecond = 0; + $this->lastEntityMovementPacketsPerSecondRecordedTime = $time; + } if($this->sendingInventoryRequired){ //console("inv resent"); @@ -1597,6 +1610,7 @@ class Player{ } $this->entityMovementQueueLength += 6*$packets + $len; + $this->entityMovementPacketsPerSecond += $packets; } /** From fd5b103df749f52b9c7028bbabab075d081fc998 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 22 Jun 2025 19:47:13 +0300 Subject: [PATCH 065/146] isWaterBlocking uses block materials now --- src/material/block/liquid/LiquidBlockDynamic.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/material/block/liquid/LiquidBlockDynamic.php b/src/material/block/liquid/LiquidBlockDynamic.php index c4028f30..d26acfd1 100644 --- a/src/material/block/liquid/LiquidBlockDynamic.php +++ b/src/material/block/liquid/LiquidBlockDynamic.php @@ -33,9 +33,9 @@ class LiquidBlockDynamic extends LiquidBlock{ if($id == CARPET || $id == SNOW_LAYER || $id == RAIL || $id == POWERED_RAIL) return false; //TODO Tile::getThickness() > 0 - //TODO materials - //if(!StaticBlock::getIsFlowable($id)) return true; - return StaticBlock::getIsSolid($id); + $mat = StaticBlock::getMaterial($id); + if($mat->blocksMotion) return true; + return $mat->isSolid; } public static function canSpreadTo(Level $level, $x, $y, $z){ From d1a724c143bb574eb542fb4f43c1ebc1885d464f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jun 2025 01:24:00 +0300 Subject: [PATCH 066/146] block light --- src/API/LevelAPI.php | 1 + src/Player.php | 1 + src/material/Block.php | 4 + src/material/StaticBlock.php | 5 + src/material/block/attachable/TorchBlock.php | 1 + src/material/block/liquid/LavaBlock.php | 2 + src/material/block/liquid/StillLavaBlock.php | 2 + src/material/block/liquid/StillWaterBlock.php | 1 + src/material/block/liquid/WaterBlock.php | 1 + src/material/block/misc/FireBlock.php | 1 + .../block/misc/NetherReactorBlock.php | 1 + src/material/block/misc/TNTBlock.php | 1 + src/material/block/nonfull/CobwebBlock.php | 1 + src/material/block/ore/CoalOreBlock.php | 1 + src/material/block/ore/DiamondOreBlock.php | 1 + .../block/ore/GlowingRedstoneOreBlock.php | 2 + src/material/block/ore/GoldOreBlock.php | 1 + src/material/block/ore/IronOreBlock.php | 1 + src/material/block/ore/LapisOreBlock.php | 1 + src/material/block/ore/RedstoneOreBlock.php | 1 + .../block/plant/BrownMushroomBlock.php | 1 + src/material/block/solid/BedrockBlock.php | 1 + src/material/block/solid/BookshelfBlock.php | 1 + src/material/block/solid/BricksBlock.php | 1 + .../block/solid/BurningFurnaceBlock.php | 2 + src/material/block/solid/ClayBlock.php | 1 + src/material/block/solid/CoalBlock.php | 1 + src/material/block/solid/CobblestoneBlock.php | 1 + src/material/block/solid/DiamondBlock.php | 1 + src/material/block/solid/DirtBlock.php | 1 + src/material/block/solid/DoubleSlabBlock.php | 1 + .../block/solid/DoubleWoodSlabBlock.php | 1 + src/material/block/solid/FurnaceBlock.php | 2 + .../block/solid/GlowingObsidianBlock.php | 2 + src/material/block/solid/GlowstoneBlock.php | 2 + src/material/block/solid/GoldBlock.php | 1 + src/material/block/solid/GrassBlock.php | 1 + src/material/block/solid/GravelBlock.php | 1 + src/material/block/solid/HayBaleBlock.php | 1 + src/material/block/solid/IceBlock.php | 1 + .../block/solid/InvisibleBedrockBlock.php | 1 + src/material/block/solid/IronBlock.php | 1 + src/material/block/solid/LapisBlock.php | 1 + src/material/block/solid/LeavesBlock.php | 1 + src/material/block/solid/LitPumpkinBlock.php | 2 + src/material/block/solid/MelonBlock.php | 1 + src/material/block/solid/MossStoneBlock.php | 1 + .../block/solid/NetherBricksBlock.php | 1 + src/material/block/solid/NetherrackBlock.php | 1 + src/material/block/solid/ObsidianBlock.php | 1 + src/material/block/solid/PlanksBlock.php | 1 + src/material/block/solid/PumpkinBlock.php | 1 + src/material/block/solid/QuartzBlock.php | 1 + src/material/block/solid/Reserved6Block.php | 1 + src/material/block/solid/SandBlock.php | 1 + src/material/block/solid/SandstoneBlock.php | 1 + src/material/block/solid/SnowBlock.php | 1 + src/material/block/solid/SpongeBlock.php | 1 + src/material/block/solid/StoneBlock.php | 1 + src/material/block/solid/StoneBricksBlock.php | 1 + src/material/block/solid/StonecutterBlock.php | 1 + src/material/block/solid/WoodBlock.php | 1 + src/material/block/solid/WoolBlock.php | 1 + src/material/block/solid/WorkbenchBlock.php | 1 + src/pmf/PMFLevel.php | 54 ++++++- src/world/Level.php | 134 ++++++++++++++++++ 66 files changed, 266 insertions(+), 1 deletion(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index a4d77bd0..b9fcb543 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -18,6 +18,7 @@ class LevelAPI{ $this->server->api->console->register("save-on", "", [$this, "commandHandler"]); $this->server->api->console->register("save-off", "", [$this, "commandHandler"]); $this->server->api->console->register("setwspawn", "Set the spawn position for your current world. ", [$this, "commandHandler"]); + $this->default = $this->server->api->getProperty("level-name"); if($this->loadLevel($this->default) === false){ $this->generateLevel($this->default, $this->server->seed); diff --git a/src/Player.php b/src/Player.php index 24e62794..cf6fc528 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1737,6 +1737,7 @@ class Player{ } public function entityTick(){ + if($this->isSleeping) ++$this->sleepingTime; if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ if($this->entity->health < 20 && $this->entity->health > 0){ diff --git a/src/material/Block.php b/src/material/Block.php index 49e1af41..21527a26 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -160,6 +160,10 @@ abstract class Block extends Position{ public $y = 0; public $z = 0; public $slipperiness; + + public $lightEmission = 0; + public $lightBlock = 0; + public static function interact(Level $level, $x, $y, $z, Player $player){} public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){} diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index 73082ecd..30080e17 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -22,10 +22,13 @@ class StaticBlock public static $destroyTime = []; public static $slipperiness = []; public static $boundingBoxes = []; + public static $lightBlock = []; + public static $lightEmission = []; public static $minXs = [], $minYs = [], $minZs = [], $maxXs = [], $maxYs = [], $maxZs = []; public static function init(){ self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0); + for($i = 0; $i < 256; ++$i) self::$lightBlock[$i] = self::$lightEmission[$i] = 0; foreach(Block::$class as $nonstaticname){ /**@var Block $b*/ $b = new $nonstaticname(); @@ -43,6 +46,8 @@ class StaticBlock self::$hardness[$b->getID()] = $b->getHardness(); self::$destroyTime[$b->getID()] = $b->breakTime; self::$material[$b->getID()] = $b->material; + self::$lightBlock[$b->getID()] = $b->lightBlock; + self::$lightEmission[$b->getID()] = $b->lightEmission; self::$prealloc[$b->getID()] = $b; $b::$blockID = $b->getID(); FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0); diff --git a/src/material/block/attachable/TorchBlock.php b/src/material/block/attachable/TorchBlock.php index 87d7cc36..bbd181cc 100644 --- a/src/material/block/attachable/TorchBlock.php +++ b/src/material/block/attachable/TorchBlock.php @@ -7,6 +7,7 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$decoration; + $this->lightEmission = 14; } public function getMaxLightValue(){ diff --git a/src/material/block/liquid/LavaBlock.php b/src/material/block/liquid/LavaBlock.php index 3fbe9c65..7167bae4 100644 --- a/src/material/block/liquid/LavaBlock.php +++ b/src/material/block/liquid/LavaBlock.php @@ -8,6 +8,8 @@ class LavaBlock extends LiquidBlockDynamic implements LightingBlock{ $this->hardness = 500; //0 in vanilla $this->breakTime = -1; //in vanilla it is 0, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$lava; + $this->lightEmission = 15; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/liquid/StillLavaBlock.php b/src/material/block/liquid/StillLavaBlock.php index b4dff404..5fb354ac 100644 --- a/src/material/block/liquid/StillLavaBlock.php +++ b/src/material/block/liquid/StillLavaBlock.php @@ -8,6 +8,8 @@ class StillLavaBlock extends LiquidBlockStatic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$lava; + $this->lightEmission = 15; + $this->lightBlock = 255; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/StillWaterBlock.php b/src/material/block/liquid/StillWaterBlock.php index 5072cd5b..14682827 100644 --- a/src/material/block/liquid/StillWaterBlock.php +++ b/src/material/block/liquid/StillWaterBlock.php @@ -14,6 +14,7 @@ class StillWaterBlock extends LiquidBlockStatic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$water; + $this->lightBlock = 3; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/WaterBlock.php b/src/material/block/liquid/WaterBlock.php index 1b090e21..c55bada5 100644 --- a/src/material/block/liquid/WaterBlock.php +++ b/src/material/block/liquid/WaterBlock.php @@ -9,6 +9,7 @@ class WaterBlock extends LiquidBlockDynamic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$water; + $this->lightBlock = 3; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index 9e04dd86..99fc63cc 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -13,6 +13,7 @@ class FireBlock extends FlowableBlock implements LightingBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$fire; + $this->lightEmission = 15; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 1d62a1ff..f7250d94 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -9,6 +9,7 @@ class NetherReactorBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$metal; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/misc/TNTBlock.php b/src/material/block/misc/TNTBlock.php index 3afbb6ab..19d847f8 100644 --- a/src/material/block/misc/TNTBlock.php +++ b/src/material/block/misc/TNTBlock.php @@ -8,6 +8,7 @@ class TNTBlock extends SolidBlock{ $this->breakTime = 0; $this->isActivable = true; $this->material = Material::$explosive; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/nonfull/CobwebBlock.php b/src/material/block/nonfull/CobwebBlock.php index 133bceee..010b1e37 100644 --- a/src/material/block/nonfull/CobwebBlock.php +++ b/src/material/block/nonfull/CobwebBlock.php @@ -9,6 +9,7 @@ class CobwebBlock extends FlowableBlock{ $this->hardness = 30; $this->breakTime = 4; $this->material = Material::$web; + $this->lightBlock = 1; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/ore/CoalOreBlock.php b/src/material/block/ore/CoalOreBlock.php index 1831be50..5dd90323 100644 --- a/src/material/block/ore/CoalOreBlock.php +++ b/src/material/block/ore/CoalOreBlock.php @@ -7,6 +7,7 @@ class CoalOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/DiamondOreBlock.php b/src/material/block/ore/DiamondOreBlock.php index 42d73d60..29929b78 100644 --- a/src/material/block/ore/DiamondOreBlock.php +++ b/src/material/block/ore/DiamondOreBlock.php @@ -7,6 +7,7 @@ class DiamondOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/GlowingRedstoneOreBlock.php b/src/material/block/ore/GlowingRedstoneOreBlock.php index 6a415480..20a92197 100644 --- a/src/material/block/ore/GlowingRedstoneOreBlock.php +++ b/src/material/block/ore/GlowingRedstoneOreBlock.php @@ -7,6 +7,8 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 9; } public static function onRandomTick(Level $level, $x, $y, $z){ diff --git a/src/material/block/ore/GoldOreBlock.php b/src/material/block/ore/GoldOreBlock.php index f494bc4b..5209b379 100644 --- a/src/material/block/ore/GoldOreBlock.php +++ b/src/material/block/ore/GoldOreBlock.php @@ -7,6 +7,7 @@ class GoldOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/IronOreBlock.php b/src/material/block/ore/IronOreBlock.php index 693ca0ca..020dec82 100644 --- a/src/material/block/ore/IronOreBlock.php +++ b/src/material/block/ore/IronOreBlock.php @@ -7,6 +7,7 @@ class IronOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/LapisOreBlock.php b/src/material/block/ore/LapisOreBlock.php index 10c61e8a..a60ef015 100644 --- a/src/material/block/ore/LapisOreBlock.php +++ b/src/material/block/ore/LapisOreBlock.php @@ -7,6 +7,7 @@ class LapisOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/RedstoneOreBlock.php b/src/material/block/ore/RedstoneOreBlock.php index 962651a5..3f0b260c 100644 --- a/src/material/block/ore/RedstoneOreBlock.php +++ b/src/material/block/ore/RedstoneOreBlock.php @@ -7,6 +7,7 @@ class RedstoneOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; } public static function interact(Level $level, $x, $y, $z, Player $player){ diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index eb2edf11..e252640c 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -7,6 +7,7 @@ class BrownMushroomBlock extends FlowableBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$plant; + $this->lightEmission = 1; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/solid/BedrockBlock.php b/src/material/block/solid/BedrockBlock.php index 4799553b..70d8035c 100644 --- a/src/material/block/solid/BedrockBlock.php +++ b/src/material/block/solid/BedrockBlock.php @@ -8,6 +8,7 @@ class BedrockBlock extends SolidBlock{ $this->hardness = 18000000; $this->breakTime = -1; $this->material = Material::$stone; + $this->lightBlock = 255; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/BookshelfBlock.php b/src/material/block/solid/BookshelfBlock.php index 80955d9e..fdd93bfc 100644 --- a/src/material/block/solid/BookshelfBlock.php +++ b/src/material/block/solid/BookshelfBlock.php @@ -7,6 +7,7 @@ class BookshelfBlock extends SolidBlock{ $this->hardness = 7.5; $this->breakTime = 1.5; $this->material = Material::$wood; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/BricksBlock.php b/src/material/block/solid/BricksBlock.php index cea18904..f0657c6d 100644 --- a/src/material/block/solid/BricksBlock.php +++ b/src/material/block/solid/BricksBlock.php @@ -7,6 +7,7 @@ class BricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/BurningFurnaceBlock.php b/src/material/block/solid/BurningFurnaceBlock.php index 50944933..030b7b63 100644 --- a/src/material/block/solid/BurningFurnaceBlock.php +++ b/src/material/block/solid/BurningFurnaceBlock.php @@ -8,6 +8,8 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ $this->hardness = 17.5; $this->breakTime = 3.5; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 13; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/ClayBlock.php b/src/material/block/solid/ClayBlock.php index e1c88681..a059962a 100644 --- a/src/material/block/solid/ClayBlock.php +++ b/src/material/block/solid/ClayBlock.php @@ -7,6 +7,7 @@ class ClayBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$clay; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/CoalBlock.php b/src/material/block/solid/CoalBlock.php index 6c7a11e2..6a8191ef 100644 --- a/src/material/block/solid/CoalBlock.php +++ b/src/material/block/solid/CoalBlock.php @@ -7,6 +7,7 @@ class CoalBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/CobblestoneBlock.php b/src/material/block/solid/CobblestoneBlock.php index 96bc7172..7ba03d99 100644 --- a/src/material/block/solid/CobblestoneBlock.php +++ b/src/material/block/solid/CobblestoneBlock.php @@ -7,6 +7,7 @@ class CobblestoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DiamondBlock.php b/src/material/block/solid/DiamondBlock.php index 9fc92a18..eaa0131d 100644 --- a/src/material/block/solid/DiamondBlock.php +++ b/src/material/block/solid/DiamondBlock.php @@ -7,6 +7,7 @@ class DiamondBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index fef766f0..87f2eb74 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -8,6 +8,7 @@ class DirtBlock extends SolidBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleSlabBlock.php b/src/material/block/solid/DoubleSlabBlock.php index c455099d..0bac086d 100755 --- a/src/material/block/solid/DoubleSlabBlock.php +++ b/src/material/block/solid/DoubleSlabBlock.php @@ -18,6 +18,7 @@ class DoubleSlabBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleWoodSlabBlock.php b/src/material/block/solid/DoubleWoodSlabBlock.php index 62aa3c40..848c7b87 100644 --- a/src/material/block/solid/DoubleWoodSlabBlock.php +++ b/src/material/block/solid/DoubleWoodSlabBlock.php @@ -14,6 +14,7 @@ class DoubleWoodSlabBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$wood; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/FurnaceBlock.php b/src/material/block/solid/FurnaceBlock.php index 3f8253e2..8fe0cdf9 100644 --- a/src/material/block/solid/FurnaceBlock.php +++ b/src/material/block/solid/FurnaceBlock.php @@ -13,5 +13,7 @@ class FurnaceBlock extends BurningFurnaceBlock{ $this->name = "Furnace"; $this->isActivable = true; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 0; } } \ No newline at end of file diff --git a/src/material/block/solid/GlowingObsidianBlock.php b/src/material/block/solid/GlowingObsidianBlock.php index cefcb5c5..b2e143db 100644 --- a/src/material/block/solid/GlowingObsidianBlock.php +++ b/src/material/block/solid/GlowingObsidianBlock.php @@ -7,6 +7,8 @@ class GlowingObsidianBlock extends SolidBlock implements LightingBlock{ $this->hardness = 6000; $this->breakTime = 10; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 13; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GlowstoneBlock.php b/src/material/block/solid/GlowstoneBlock.php index f746e6c4..e123d491 100644 --- a/src/material/block/solid/GlowstoneBlock.php +++ b/src/material/block/solid/GlowstoneBlock.php @@ -7,6 +7,8 @@ class GlowstoneBlock extends TransparentBlock implements LightingBlock{ $this->hardness = 1.5; $this->breakTime = 0.3; $this->material = Material::$glass; + $this->lightBlock = 255; + $this->lightEmission = 15; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GoldBlock.php b/src/material/block/solid/GoldBlock.php index 6c623e93..eff8f97d 100644 --- a/src/material/block/solid/GoldBlock.php +++ b/src/material/block/solid/GoldBlock.php @@ -7,6 +7,7 @@ class GoldBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 3; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 10945978..f3b49baa 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -8,6 +8,7 @@ class GrassBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/GravelBlock.php b/src/material/block/solid/GravelBlock.php index 4842e512..2ac020a3 100644 --- a/src/material/block/solid/GravelBlock.php +++ b/src/material/block/solid/GravelBlock.php @@ -7,6 +7,7 @@ class GravelBlock extends FallableBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$sand; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/HayBaleBlock.php b/src/material/block/solid/HayBaleBlock.php index eb748749..86dacd51 100644 --- a/src/material/block/solid/HayBaleBlock.php +++ b/src/material/block/solid/HayBaleBlock.php @@ -7,6 +7,7 @@ class HayBaleBlock extends SolidBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 81d6f82f..52079e10 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -8,6 +8,7 @@ class IceBlock extends TransparentBlock{ $this->breakTime = 0.5; $this->slipperiness = 0.98; $this->material = Material::$ice; + $this->lightBlock = 3; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/InvisibleBedrockBlock.php b/src/material/block/solid/InvisibleBedrockBlock.php index 02724952..5ef1f20f 100755 --- a/src/material/block/solid/InvisibleBedrockBlock.php +++ b/src/material/block/solid/InvisibleBedrockBlock.php @@ -8,6 +8,7 @@ class InvisibleBedrockBlock extends SolidBlock{ $this->hardness = 18000000; $this->breakTime = -1; $this->material = Material::$stone; + $this->lightBlock = 255; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/IronBlock.php b/src/material/block/solid/IronBlock.php index 2d14ffc4..1b576ba2 100644 --- a/src/material/block/solid/IronBlock.php +++ b/src/material/block/solid/IronBlock.php @@ -7,6 +7,7 @@ class IronBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LapisBlock.php b/src/material/block/solid/LapisBlock.php index 21fe55cb..07346192 100644 --- a/src/material/block/solid/LapisBlock.php +++ b/src/material/block/solid/LapisBlock.php @@ -7,6 +7,7 @@ class LapisBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 1f6e2480..ee2e6a59 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -17,6 +17,7 @@ class LeavesBlock extends TransparentBlock{ $this->hardness = 1; $this->breakTime = 0.2; $this->material = Material::$leaves; + $this->lightBlock = 1; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/LitPumpkinBlock.php b/src/material/block/solid/LitPumpkinBlock.php index 642caf8d..2303db16 100644 --- a/src/material/block/solid/LitPumpkinBlock.php +++ b/src/material/block/solid/LitPumpkinBlock.php @@ -15,6 +15,8 @@ class LitPumpkinBlock extends SolidBlock implements LightingBlock{ $this->hardness = 5; $this->breakTime = 1.0; $this->material = Material::$vegetable; + $this->lightBlock = 255; + $this->lightEmission = 15; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/MelonBlock.php b/src/material/block/solid/MelonBlock.php index 690b43c2..71596b2a 100644 --- a/src/material/block/solid/MelonBlock.php +++ b/src/material/block/solid/MelonBlock.php @@ -7,6 +7,7 @@ class MelonBlock extends TransparentBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$vegetable; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ return array( diff --git a/src/material/block/solid/MossStoneBlock.php b/src/material/block/solid/MossStoneBlock.php index 181ee302..e1bb1436 100644 --- a/src/material/block/solid/MossStoneBlock.php +++ b/src/material/block/solid/MossStoneBlock.php @@ -7,6 +7,7 @@ class MossStoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherBricksBlock.php b/src/material/block/solid/NetherBricksBlock.php index f58002d7..f2eec4a5 100644 --- a/src/material/block/solid/NetherBricksBlock.php +++ b/src/material/block/solid/NetherBricksBlock.php @@ -7,6 +7,7 @@ class NetherBricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherrackBlock.php b/src/material/block/solid/NetherrackBlock.php index 203e7b4f..ff6b510a 100644 --- a/src/material/block/solid/NetherrackBlock.php +++ b/src/material/block/solid/NetherrackBlock.php @@ -7,6 +7,7 @@ class NetherrackBlock extends SolidBlock{ $this->hardness = 2; $this->breakTime = 0.4; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/ObsidianBlock.php b/src/material/block/solid/ObsidianBlock.php index 466201ab..6a7b369d 100644 --- a/src/material/block/solid/ObsidianBlock.php +++ b/src/material/block/solid/ObsidianBlock.php @@ -7,6 +7,7 @@ class ObsidianBlock extends SolidBlock{ $this->hardness = 6000; $this->breakTime = 10; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/PlanksBlock.php b/src/material/block/solid/PlanksBlock.php index c4cc531e..e321f4c7 100644 --- a/src/material/block/solid/PlanksBlock.php +++ b/src/material/block/solid/PlanksBlock.php @@ -14,6 +14,7 @@ class PlanksBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 2; $this->material = Material::$wood; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/PumpkinBlock.php b/src/material/block/solid/PumpkinBlock.php index 92002b14..56c66e72 100644 --- a/src/material/block/solid/PumpkinBlock.php +++ b/src/material/block/solid/PumpkinBlock.php @@ -7,6 +7,7 @@ class PumpkinBlock extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$vegetable; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/QuartzBlock.php b/src/material/block/solid/QuartzBlock.php index 3640f18b..00fb68b3 100644 --- a/src/material/block/solid/QuartzBlock.php +++ b/src/material/block/solid/QuartzBlock.php @@ -14,6 +14,7 @@ class QuartzBlock extends SolidBlock{ $this->breakTime = 0.8; $this->hardness = 4; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/Reserved6Block.php b/src/material/block/solid/Reserved6Block.php index 3c86f0a0..133f763c 100644 --- a/src/material/block/solid/Reserved6Block.php +++ b/src/material/block/solid/Reserved6Block.php @@ -8,6 +8,7 @@ class Reserved6Block extends SolidBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/SandBlock.php b/src/material/block/solid/SandBlock.php index 4c9ee6d1..16623ef1 100644 --- a/src/material/block/solid/SandBlock.php +++ b/src/material/block/solid/SandBlock.php @@ -7,6 +7,7 @@ class SandBlock extends FallableBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$sand; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/SandstoneBlock.php b/src/material/block/solid/SandstoneBlock.php index 1a0d4bab..33a752b5 100644 --- a/src/material/block/solid/SandstoneBlock.php +++ b/src/material/block/solid/SandstoneBlock.php @@ -13,6 +13,7 @@ class SandstoneBlock extends SolidBlock{ $this->hardness = 4; $this->breakTime = 0.8; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/SnowBlock.php b/src/material/block/solid/SnowBlock.php index d41d2148..92addfae 100644 --- a/src/material/block/solid/SnowBlock.php +++ b/src/material/block/solid/SnowBlock.php @@ -7,6 +7,7 @@ class SnowBlock extends SolidBlock{ $this->hardness = 1; $this->breakTime = 0.2; $this->material = Material::$snow; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/SpongeBlock.php b/src/material/block/solid/SpongeBlock.php index 58702087..ac5275e1 100644 --- a/src/material/block/solid/SpongeBlock.php +++ b/src/material/block/solid/SpongeBlock.php @@ -7,6 +7,7 @@ class SpongeBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$dirt; + $this->lightBlock = 255; } } diff --git a/src/material/block/solid/StoneBlock.php b/src/material/block/solid/StoneBlock.php index 55ee212c..e18b1acb 100644 --- a/src/material/block/solid/StoneBlock.php +++ b/src/material/block/solid/StoneBlock.php @@ -7,6 +7,7 @@ class StoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 1.5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StoneBricksBlock.php b/src/material/block/solid/StoneBricksBlock.php index 1b856076..cc83158f 100644 --- a/src/material/block/solid/StoneBricksBlock.php +++ b/src/material/block/solid/StoneBricksBlock.php @@ -14,6 +14,7 @@ class StoneBricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 1.5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index fc307d70..63a4d296 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -8,6 +8,7 @@ class StonecutterBlock extends SolidBlock{ $this->breakTime = 2.5; $this->hardness = 2.5*5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index 04a26316..c9d2fafa 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -19,6 +19,7 @@ class WoodBlock extends SolidBlock{ $this->hardness = 10; $this->breakTime = 2.0; $this->material = Material::$wood; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/WoolBlock.php b/src/material/block/solid/WoolBlock.php index 52b91735..c0e6e7f2 100644 --- a/src/material/block/solid/WoolBlock.php +++ b/src/material/block/solid/WoolBlock.php @@ -29,6 +29,7 @@ class WoolBlock extends SolidBlock{ $this->hardness = 4; $this->breakTime = 0.8; $this->material = Material::$cloth; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index 5f80c000..3148a223 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -8,6 +8,7 @@ class WorkbenchBlock extends SolidBlock{ $this->hardness = 12.5; $this->breakTime = 2.5; $this->material = Material::$wood; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index f00388e6..0902c409 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -3,7 +3,6 @@ define("PMF_CURRENT_LEVEL_VERSION", 0x00); class PMFLevel extends PMF{ - public $isLoaded = true; private $levelData = []; public $locationTable = []; @@ -298,6 +297,7 @@ class PMFLevel extends PMF{ for($Y = 0; $Y < $this->levelData["height"]; ++$Y){ $t = 1 << $Y; if(($info[0] & $t) === $t){ + //TODO do something with skylight // 4096 + 2048 + 2048, Block Data, Meta, Light if(strlen($this->chunks[$index][$Y] = gzread($chunk, 8192)) < 8192){ console("[NOTICE] Empty corrupt chunk detected [$X,$Z,:$Y], recovering contents", true, true, 2); @@ -397,6 +397,7 @@ class PMFLevel extends PMF{ }else{ $this->chunks[$index][$Y][$bind] = chr($block); if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); + $this->level->updateLight(0, $x, $y, $z, $x, $y, $z); } if(!isset($this->chunkChange[$index][$Y])){ @@ -408,6 +409,56 @@ class PMFLevel extends PMF{ return true; } + public function getBlockLight($x, $y, $z){ + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return 0; + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + $index = self::getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)) return 0; + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; + $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 24 + ($aX << 5) + ($aZ << 9))]); + return $y & 1 ? $m >> 4 : $m & 0x0F; + } + + public function setBlockLight($x, $y, $z, $value){ + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return false; + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + $value &= 0x0F; + + $index = self::getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ + if($this->loadChunk($X, $Z) === false) return false; + }elseif($this->chunks[$index][$Y] === false){ + $this->fillMiniChunk($X, $Z, $Y); + } + + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; + $mindex = (int) (($aY >> 1) + 24 + ($aX << 5) + ($aZ << 9)); + $old_m = ord($this->chunks[$index][$Y][$mindex]); + if(($y & 1) === 0) $m = ($old_m & 0xF0) | $value; + else $m = ($value << 4) | ($old_m & 0x0F); + + if($old_m != $m){ + $this->chunks[$index][$Y][$mindex] = chr($m); + + if(!isset($this->chunkChange[$index][$Y])){ + $this->chunkChange[$index][$Y] = 1; + }else{ + ++$this->chunkChange[$index][$Y]; + } + $this->chunkChange[$index][-1] = true; + return true; + } + return false; + } + public function getBlockDamage($x, $y, $z){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return 0; @@ -531,6 +582,7 @@ class PMFLevel extends PMF{ $this->chunkChange[$index][-1] = true; if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); + $this->level->updateLight(0, $x, $y, $z, $x, $y, $z); return true; } return false; diff --git a/src/world/Level.php b/src/world/Level.php index fcd621a1..990d6782 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -520,7 +520,140 @@ class Level{ } $this->addBlockToSendQueue($x, $y, $z, $id, $meta); } + /** + * Contains light updates. Light update is an integer that contains min/max block coords and light layer + * Format: 0011ffeeddccbbaa + * 00 - unused(8th byte) + * 11 - light layer(currently must be 0(block), future: 15(sky)) + * ff, ee, dd - minX, minY, minZ + * cc, bb, aa - maxX, maxY, maxZ + * @var int[] + */ + public $lightUpdates = []; + public $lightUpdateIndx = -1; + public function handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z <= $maxZ; ++$z){ + for($y = $minY; $y <= $maxY; ++$y){ + $brightness = $this->level->getBlockLight($x, $y, $z); + $blockID = $this->level->getBlockID($x, $y, $z); + $lightBlock = StaticBlock::$lightBlock[$blockID]; + if($lightBlock == 0) $lightBlock = 1; + $lightEmission = StaticBlock::$lightEmission[$blockID]; + $newBrightness = 0; + if($lightBlock <= 14 || $lightEmission != 0){ + $xNegBright = $this->level->getBlockLight($x-1, $y, $z); + $xPosBright = $this->level->getBlockLight($x+1, $y, $z); + $yNegBright = $this->level->getBlockLight($x, $y-1, $z); + $yPosBright = $this->level->getBlockLight($x, $y+1, $z); + $zNegBright = $this->level->getBlockLight($x, $y, $z-1); + $zPosBright = $this->level->getBlockLight($x, $y, $z+1); + + $v15 = $xNegBright; + if($xPosBright > $v15) $v15 = $xPosBright; + if($yNegBright > $v15) $v15 = $yNegBright; + if($yPosBright > $v15) $v15 = $yPosBright; + if($zNegBright > $v15) $v15 = $zNegBright; + if($zPosBright > $v15) $v15 = $zPosBright; + + $newBrightness = $v15 - $lightBlock; + if($newBrightness < 0) $newBrightness = 0; + if($lightEmission > $newBrightness) $newBrightness = $lightEmission; + } + + if($brightness != $newBrightness){ + $this->level->setBlockLight($x, $y, $z, $newBrightness); + $v4 = $newBrightness - 1; + if($v4 < 0) $v4 = 0; + $this->updateLightIfOtherThan(0, $x-1, $y, $z, $v4); + $this->updateLightIfOtherThan(0, $x, $y-1, $z, $v4); + $this->updateLightIfOtherThan(0, $x, $y, $z-1, $v4); + if($x+1 >= $maxX) $this->updateLightIfOtherThan(0, $x+1, $y, $z, $v4); + if($y+1 >= $maxY) $this->updateLightIfOtherThan(0, $x, $y+1, $z, $v4); + if($z+1 >= $maxZ) $this->updateLightIfOtherThan(0, $x, $y, $z+1, $v4); + } + } + } + } + } + public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + /*$max = 5; TODO: expand lightupdate + $sz = $this->lightUpdateIndx; + if($sz < $max) $max = $sz; + for($i = 0; $i < $max; ++$i){ + $update = $this->lightUpdates[$i]; + if($update->layer == $layer && ($r = $update->expand($minX, $minY, $minZ, $maxX, $maxY, $maxZ))){ + --$this->lightUpdatesCount; + } + }*/ + + if($maxX < $minX){ + $s = $minX; + $minX = $maxX; + $maxX = $s; + } + if($maxY < $minY){ + $s = $minY; + $minY = $maxY; + $maxY = $s; + } + if($maxZ < $minZ){ + $s = $minZ; + $minZ = $maxZ; + $maxZ = $s; + } + if($minX < 0) $minX = 0; + if($minX > 255) $minX = 255; + if($minY < 0) $minY = 0; + if($minY > 127) $minY = 127; + if($minZ < 0) $minZ = 0; + if($minZ > 255) $minZ = 255; + + if($maxX < 0) $minX = 0; + if($maxX > 255) $minX = 255; + if($maxY < 0) $minY = 0; + if($maxY > 127) $minY = 127; + if($maxZ < 0) $maxZ = 0; + if($maxZ > 255) $maxZ = 255; + + $update = ($layer << 48) | ($minX << 40) | ($minY << 32) | ($minZ << 24) | ($maxX << 16) | ($maxY << 8) | ($maxZ); + $this->lightUpdates[++$this->lightUpdateIndx] = $update; + } + + public function updateLightIfOtherThan($layer, $x, $y, $z, $level){ + if($y < 0 || $y > 127 || $x < 0 || $x > 255 || $z < 0 || $z > 255) return; + if($layer == 0){ + $blockID = $this->level->getBlockID($x, $y, $z); + $emission = StaticBlock::$lightEmission[$blockID]; + if($emission > $level) $level = $emission; + + if($this->level->getBlockLight($x, $y, $z) != $level){ + $this->updateLight($layer, $x, $y, $z, $x, $y, $z); + } + } + } + public function updateLights(){ + if($this->lightUpdateIndx < 0) return false; + $limit = 500; + do{ + $upd = $this->lightUpdates[$this->lightUpdateIndx]; + unset($this->lightUpdates[$this->lightUpdateIndx]); + --$this->lightUpdateIndx; + $maxZ = $upd & 0xff; + $maxY = ($upd >> 8) & 0xff; + $maxX = ($upd >> 16) & 0xff; + $minZ = ($upd >> 24) & 0xff; + $minY = ($upd >> 32) & 0xff; + $minX = ($upd >> 40) & 0xff; + $layer = $upd >> 48; + if($layer == 0) $this->handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ); + else console("wha"); + if(!--$limit) return true; + }while($this->lightUpdateIndx >= 0); + if(count($this->lightUpdates) > 0) return true; + return false; + } public function onTick(PocketMinecraftServer $server, $currentTime){ if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ @@ -647,6 +780,7 @@ class Level{ } $this->queuedBlockUpdates = []; + $this->updateLights(); } public function isBoundingBoxOnFire(AxisAlignedBB $bb){ From cd07581d494a15eb16b1f245d157127a465ad475 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jun 2025 07:02:31 +0300 Subject: [PATCH 067/146] mobs wont spawn if brightness is too high, aditional file with world data for storing additional information --- src/API/ConsoleAPI.php | 6 ++- src/pmf/PMFLevel.php | 102 ++++++++++++++++++++++++++++++++++++++- src/world/Level.php | 73 +++++++++++++++++++++++++--- src/world/MobSpawner.php | 7 ++- 4 files changed, 175 insertions(+), 13 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index c8ac2212..b32e415c 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -210,7 +210,11 @@ class ConsoleAPI{ case "status": $info = $this->server->debugInfo(); if(!($issuer instanceof Player) && $issuer === "console"){ - return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}"; + $lightUpdates = 0; + foreach($this->server->api->level->levels as $level){ + $lightUpdates += count($level->lightUpdates); + } + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]} Light updates: {$lightUpdates}"; } return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 0902c409..af6e0be3 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -3,13 +3,18 @@ define("PMF_CURRENT_LEVEL_VERSION", 0x00); class PMFLevel extends PMF{ + const NC_DATA_VERSION = 1; public $isLoaded = true; private $levelData = []; + public $hasLight = ""; public $locationTable = []; private $log = 4; //must be 4 or else rip world private $payloadOffset = 0; public $chunks = []; public $chunkChange = []; + /** + * @var Level + */ public $level; public function __construct($file, $blank = false){ if(is_array($blank)){ @@ -81,8 +86,83 @@ class PMFLevel extends PMF{ if($locationTable !== false){ $this->writeLocationTable(); } + + $this->writeNCData(); + } + + public function writeNCData(){ + $dir = dirname($this->file); + try{ + $file = fopen("$dir/ncleveldata.pmf", "wb"); + fwrite($file, chr(self::NC_DATA_VERSION), 1); + fwrite($file, $this->hasLight, 16*16); + }finally{ + fclose($file); + } } + public function readNCData(){ + $this->hasLight = str_repeat("\x00", 16*16); + + $dir = dirname($this->file); + $path = "$dir/ncleveldata.pmf"; + if(!is_file($path)){ + ConsoleAPI::notice("No NC Level data found! Old world?"); + return; + } + try{ + $file = fopen($path, "rb"); + $ncdv = ord(fread($file, 1)); + $v = self::NC_DATA_VERSION; + if($ncdv !== $v){ + ConsoleAPI::notice("NC level data version on server($v) does not match the world($ncdv)! Creating NC level data backup."); + $newpath = "$path.bak.".microtime(true); + $f = copy($path, $newpath); + if($f === false) ConsoleAPI::error("NC level data backup creation failed."); + else ConsoleAPI::notice("NC level data backup created. (path: $newpath)"); + } + $this->hasLight = fread($file, 16*16); + }finally{ + fclose($file); + } + } + + /** + * Checks was skylight generated for chunk + * @param int $X - chunk x + * @param int $Z - chunk z + */ + public function hasSkylight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + return (ord($this->hasLight[$index]) & 2) > 0; + } + /** + * Checks was blocklight generated for chunk + * @param int $X - chunk x + * @param int $Z - chunk z + */ + public function hasBlocklight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + return (ord($this->hasLight[$index]) & 1) > 0; + } + + public function markHasBlocklight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + $o = ord($this->hasLight[$index]); + $this->hasLight[$index] = chr($o | 1); + return true; + } + public function markHasSkylight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + $o = ord($this->hasLight[$index]); + $this->hasLight[$index] = chr($o | 2); + return true; + } + private function writeLocationTable(){ $cnt = pow($this->levelData["width"], 2); @ftruncate($this->fp, $this->payloadOffset); @@ -159,6 +239,7 @@ class PMFLevel extends PMF{ }else{ $this->payloadOffset = ftell($this->fp); } + $this->readNCData(); return $this->readLocationTable(); } @@ -274,13 +355,12 @@ class PMFLevel extends PMF{ return $this->chunks[$index][$Y]; } + public $checkLight = []; public function loadChunk($X, $Z){ - $index = self::getIndex($X, $Z); if($this->isChunkLoaded($X, $Z)){ return true; - }elseif(!isset($this->locationTable[$index])){ return false; } @@ -307,9 +387,27 @@ class PMFLevel extends PMF{ $this->chunks[$index][$Y] = false; } } + $this->checkLight[$index] = [$X, $Z]; + @gzclose($chunk); return true; } + + public function forceLightUpdatesIfNeeded(){ + foreach($this->checkLight as $index => [$X, $Z]){ + $update = false; + if(!$this->hasBlocklight($X, $Z)){ + ConsoleAPI::notice("Chunk $X $Z (Level: {$this->level->getName()} has no blocklight! Forcing light update(it might take a while)."); + $this->level->updateLight(0, $X*16, 0, $Z*16, $X*16+15, 127, $Z*16+15); + $update = true; + $this->markHasBlocklight($X, $Z); + } + + if($update) while($this->level->updateLights()); + unset($this->checkLight[$index]); + } + + } protected function fillMiniChunk($X, $Z, $Y){ if($this->isChunkLoaded($X, $Z) === false){ diff --git a/src/world/Level.php b/src/world/Level.php index 990d6782..6536dfc6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -577,18 +577,71 @@ class Level{ } } + /** + * Gets block brightness based on skylight, blocklight and subtracts skyDarken. + * @param int $x + * @param int $y + * @param int $z + */ + public function getRawBrightness(int $x, int $y, int $z){ + //TODO special way to do it for some blocks + + $bl = $this->level->getBlockLight($x, $y, $z); + $l = 0; ///*$this->level->getBrightness(LIGHTLAYER_SKY, $x, $y, $z)*/ (15 - $this->skyDarken); no skylight is implemnted + if($bl > $l) $l = $bl; + if($l < 0) $l = 0; + return $l; + } + + /** + * Schedules light update. Make sure to check that coordinates are in range. + * @param int $layer - light layer(0 - block, 15 - sky(not implemented as of now)). + * @param int $minX - must be in range (0, 255). Must be less than maxX. + * @param int $minY - must be in range (0, 127). Must be less than maxY. + * @param int $minZ - must be in range (0, 255). Must be less than maxZ. + * @param int $maxX - must be in range (0, 255). Must be more than minX. + * @param int $maxY - must be in range (0, 127). Must be more than minY. + * @param int $maxZ - must be in range (0, 255). Must be more than minZ. + */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ - /*$max = 5; TODO: expand lightupdate + /*$max = 5; no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; for($i = 0; $i < $max; ++$i){ - $update = $this->lightUpdates[$i]; - if($update->layer == $layer && ($r = $update->expand($minX, $minY, $minZ, $maxX, $maxY, $maxZ))){ - --$this->lightUpdatesCount; + $upd = $this->lightUpdates[$i]; + $llayer = $upd >> 48; + + if($layer == $llayer){ + $maZ = $upd & 0xff; + $maY = ($upd >> 8) & 0xff; + $maX = ($upd >> 16) & 0xff; + $miZ = ($upd >> 24) & 0xff; + $miY = ($upd >> 32) & 0xff; + $miX = ($upd >> 40) & 0xff; + if($miX <= $minX && $miY <= $minY && $miZ <= $minZ && $maX >= $maxX && $maY >= $maxY && $maZ >= $maxZ){ + console("light happen is scheduled to happen already"); + return; + } + if($miX-1 > $minX || $miY-1 > $minY || $miZ-1 > $minZ || $maX+1 < $maxX || $maY+1 < $maxY || $maZ+1 < $maxZ){ + continue; + } + if($miX < $minX) $minX = $miX; + if($miY < $minY) $minY = $miY; + if($miZ < $minZ) $minZ = $miZ; + if($maX > $maxX) $maxX = $maX; + if($maY > $maxY) $maxY = $maY; + if($maZ > $maxZ) $maxZ = $maZ; + + if((($maxZ-$minZ)*($maX-$minY)*($maxX-$minX) - ($maX-$miX)*($maZ-$miZ)*($maY-$miY)) > 2){ + continue; + } + + $indx = $i; + goto update_light_in_array; } }*/ - - if($maxX < $minX){ + $indx = ++$this->lightUpdateIndx; + /*if($maxX < $minX){ $s = $minX; $minX = $maxX; $maxX = $s; @@ -603,6 +656,8 @@ class Level{ $minZ = $maxZ; $maxZ = $s; } + + update_light_in_array: if($minX < 0) $minX = 0; if($minX > 255) $minX = 255; if($minY < 0) $minY = 0; @@ -615,10 +670,10 @@ class Level{ if($maxY < 0) $minY = 0; if($maxY > 127) $minY = 127; if($maxZ < 0) $maxZ = 0; - if($maxZ > 255) $maxZ = 255; + if($maxZ > 255) $maxZ = 255;*/ $update = ($layer << 48) | ($minX << 40) | ($minY << 32) | ($minZ << 24) | ($maxX << 16) | ($maxY << 8) | ($maxZ); - $this->lightUpdates[++$this->lightUpdateIndx] = $update; + $this->lightUpdates[$indx] = $update; } public function updateLightIfOtherThan($layer, $x, $y, $z, $level){ @@ -655,6 +710,8 @@ class Level{ return false; } public function onTick(PocketMinecraftServer $server, $currentTime){ + $this->level->forceLightUpdatesIfNeeded(); + if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index 48d63306..ff8de7ae 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -88,7 +88,7 @@ class MobSpawner{ $zMob = $z + mt_rand(-3, 3); - $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35); + $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35, $type >= 32 && $type <= 36); if(!$y || $y < 0){ continue; } @@ -125,7 +125,7 @@ class MobSpawner{ ]; } - protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox + protected function getSafeY($x, $z, $grassOnly = false, $highMob = false, $isMonster = false){ //first safe block //TODO check boundingbox $allowed = []; for($y = 0; $y < 128; ++$y){ $b = $this->level->level->getBlockID($x, $y, $z); @@ -136,6 +136,9 @@ class MobSpawner{ (StaticBlock::getIsSolid($b1) && (!$grassOnly || $b1 === GRASS) && (!$highMob || !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2))) ){ + if($isMonster && ($rb = $this->level->getRawBrightness($x, $y, $z)) > 8){ //dont spawn if too bright + continue; + } $allowed[] = $y; } } From 54ef3882b9531a0144bd946698aa095a78ef7a40 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 13:05:19 +0300 Subject: [PATCH 068/146] small changes --- src/API/ConsoleAPI.php | 2 +- src/world/Level.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index b32e415c..574e42e7 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -214,7 +214,7 @@ class ConsoleAPI{ foreach($this->server->api->level->levels as $level){ $lightUpdates += count($level->lightUpdates); } - return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]} Light updates: {$lightUpdates}"; + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}, Light updates: {$lightUpdates}"; } return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": diff --git a/src/world/Level.php b/src/world/Level.php index 6536dfc6..868290c6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -604,7 +604,7 @@ class Level{ * @param int $maxZ - must be in range (0, 255). Must be more than minZ. */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ - /*$max = 5; no idea is it needed for block light + /*$max = 5; //no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; for($i = 0; $i < $max; ++$i){ @@ -619,7 +619,7 @@ class Level{ $miY = ($upd >> 32) & 0xff; $miX = ($upd >> 40) & 0xff; if($miX <= $minX && $miY <= $minY && $miZ <= $minZ && $maX >= $maxX && $maY >= $maxY && $maZ >= $maxZ){ - console("light happen is scheduled to happen already"); + //console("light happen is scheduled to happen already"); return; } if($miX-1 > $minX || $miY-1 > $minY || $miZ-1 > $minZ || $maX+1 < $maxX || $maY+1 < $maxY || $maZ+1 < $maxZ){ From 34d26d8463432a088fd146f256c7c2b92f2b108a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 13:16:50 +0300 Subject: [PATCH 069/146] door fixes --- src/material/block/DoorBlock.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/material/block/DoorBlock.php b/src/material/block/DoorBlock.php index abfe20a6..dfdc7bce 100644 --- a/src/material/block/DoorBlock.php +++ b/src/material/block/DoorBlock.php @@ -107,13 +107,16 @@ class DoorBlock extends TransparentBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ - if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method - $level->fastSetBlockUpdate($x, $y, $z, 0, 0); - $id = $level->level->getBlockID($x, $y, $z); + if($level->level->getBlockID($x, $y - 1, $z) == AIR){ + + [$id, $meta] = $level->level->getBlock($x, $y, $z); + + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + if(($meta & 8) == 0){ + if($id == DOOR_BLOCK) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(WOODEN_DOOR, 0, 1)); + elseif($id == IRON_DOOR_BLOCK) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(IRON_DOOR, 0, 1)); + } - if($id == 64) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(324, 0, 1)); - elseif($id == 71) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(330, 0, 1)); - $top = $level->level->getBlockID($x, $y + 1, $z); if($top == IRON_DOOR_BLOCK || $top == DOOR_BLOCK){ $level->fastSetBlockUpdate($x, $y + 1, $z, 0, 0); From 207dd0ffcdae669c4ba8d500b945c6eec2801d81 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 13:18:09 +0300 Subject: [PATCH 070/146] fixed update blocks not having lightblock value --- src/material/block/solid/InfoUpdate2Block.php | 1 + src/material/block/solid/InfoUpdateBlock.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/material/block/solid/InfoUpdate2Block.php b/src/material/block/solid/InfoUpdate2Block.php index 0bc5cafd..a8a29de6 100755 --- a/src/material/block/solid/InfoUpdate2Block.php +++ b/src/material/block/solid/InfoUpdate2Block.php @@ -8,6 +8,7 @@ class InfoUpdate2Block extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/InfoUpdateBlock.php b/src/material/block/solid/InfoUpdateBlock.php index cb7f2b8c..cad299b5 100755 --- a/src/material/block/solid/InfoUpdateBlock.php +++ b/src/material/block/solid/InfoUpdateBlock.php @@ -8,6 +8,7 @@ class InfoUpdateBlock extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file From c2faff0f22962e4f5008a87df6b1bb0765883084 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 16:58:28 +0300 Subject: [PATCH 071/146] added ability to disable light updates --- src/PocketMinecraftServer.php | 9 +++++++-- src/pmf/PMFLevel.php | 8 ++++++-- src/world/Level.php | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index b779896a..2a0afd75 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -12,7 +12,7 @@ class PocketMinecraftServer{ public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public static $FORCE_20_TPS = false, $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100; - + public static $ENABLE_LIGHT_UPDATES = true; function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; @@ -97,7 +97,8 @@ class PocketMinecraftServer{ "keep-items-on-death" => Entity::$keepInventory, "disable-emojis-in-chat" => Player::$disableEmojisInChat, "disable-emojis-on-signs" => Level::$disableEmojisOnSigns, - "allow-dropping-single-items" => Player::$allowDroppingSingleItems + "allow-dropping-single-items" => Player::$allowDroppingSingleItems, + "enable-light-updates" => self::$ENABLE_LIGHT_UPDATES, ]); Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); Level::$disableEmojisOnSigns = $this->extraprops->get("disable-emojis-on-signs"); @@ -109,6 +110,7 @@ class PocketMinecraftServer{ Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); self::$FORCE_20_TPS = $this->extraprops->get("force-20-tps"); self::$KEEP_CHUNKS_LOADED = $this->extraprops->get("keep-chunks-loaded"); + self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); @@ -116,6 +118,9 @@ class PocketMinecraftServer{ if(self::$FORCE_20_TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } + if(!self::$ENABLE_LIGHT_UPDATES){ + ConsoleAPI::warn("Light updates are disabled: this should decrease memory consumption and increase performance, but will prevent all existing light from updating and disable some light-dependant features."); + } if($this->extraprops->get("discord-msg")){ if($this->extraprops->get("discord-webhook-url") !== "none"){ console("[INFO] Discord Logger is enabled."); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index af6e0be3..2e56276c 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -394,6 +394,10 @@ class PMFLevel extends PMF{ } public function forceLightUpdatesIfNeeded(){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) { + $this->checkLight = []; + return; + } foreach($this->checkLight as $index => [$X, $Z]){ $update = false; if(!$this->hasBlocklight($X, $Z)){ @@ -508,7 +512,7 @@ class PMFLevel extends PMF{ } public function getBlockLight($x, $y, $z){ - if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return 0; + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127 || !PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return 0; $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; @@ -522,7 +526,7 @@ class PMFLevel extends PMF{ } public function setBlockLight($x, $y, $z, $value){ - if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return false; + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127 || !PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return false; $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; diff --git a/src/world/Level.php b/src/world/Level.php index 868290c6..ed779bb2 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -532,6 +532,7 @@ class Level{ public $lightUpdates = []; public $lightUpdateIndx = -1; public function handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return; for($x = $minX; $x <= $maxX; ++$x){ for($z = $minZ; $z <= $maxZ; ++$z){ for($y = $minY; $y <= $maxY; ++$y){ @@ -604,6 +605,7 @@ class Level{ * @param int $maxZ - must be in range (0, 255). Must be more than minZ. */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return; /*$max = 5; //no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; From 23028a7db53818f24aa4c6132f3cfa9d915acbb7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 17:14:07 +0300 Subject: [PATCH 072/146] /xyz will display block brightness at player's position --- src/API/PlayerAPI.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f288c661..75dd7055 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -284,8 +284,8 @@ class PlayerAPI{ $xChunk = $x >> 4; $zChunk = $z >> 4; - - return "$username's coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction"; + $brightness = $ent->level->getRawBrightness((int)$x, (int)$y, (int)$z); + return "$username's coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction Brightness: $brightness"; } return $output; } From d4036d8ac922ad0fc2ff0960d8d6f87510b65f3c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 25 Jun 2025 03:22:24 +0300 Subject: [PATCH 073/146] tile entity related packets use block update queue, cleaned up unused code, some phpdocs --- src/API/AchievementAPI.php | 29 +++- src/API/BanAPI.php | 38 ++-- src/Player.php | 164 ++++++++++-------- src/material/block/plant/BeetrootBlock.php | 1 - src/material/block/plant/CactusBlock.php | 1 - src/material/block/plant/CarrotBlock.php | 1 - src/material/block/plant/MelonStemBlock.php | 1 - src/material/block/plant/PotatoBlock.php | 1 - src/material/block/plant/PumpkinStemBlock.php | 1 - src/material/block/plant/SaplingBlock.php | 1 - src/material/block/plant/WheatBlock.php | 1 - src/material/block/solid/IceBlock.php | 1 - .../protocol/packet/EntityEventPacket.php | 4 + src/world/Explosion.php | 18 +- src/world/Tile.php | 15 +- 15 files changed, 165 insertions(+), 112 deletions(-) diff --git a/src/API/AchievementAPI.php b/src/API/AchievementAPI.php index beabe308..9f5d6e0d 100644 --- a/src/API/AchievementAPI.php +++ b/src/API/AchievementAPI.php @@ -81,7 +81,7 @@ class AchievementAPI{ ], ]; - + /** @var PocketMinecraftServer */ private $server; function __construct(){ @@ -91,9 +91,9 @@ class AchievementAPI{ /** * Add an achievement * @param string $achievementId - * @param string $achievementName - * @param array $requires - * @return boolean + * @param string $achievementName display name + * @param array $requires a list of achievement ids, optional + * @return boolean true if the achievement was added. false if the achievement with this id already exists. */ public static function addAchievement($achievementId, $achievementName, array $requires = []){ if(!isset(self::$achievements[$achievementId])){ @@ -106,6 +106,12 @@ class AchievementAPI{ return false; } + /** + * Gives the achievement to player + * @param Player $player + * @param string $achievementId + * @return boolean + */ public static function grantAchievement(Player $player, $achievementId){ if(isset(self::$achievements[$achievementId]) and !self::hasAchievement($player, $achievementId)){ foreach(self::$achievements[$achievementId]["requires"] as $requerimentId){ @@ -124,6 +130,11 @@ class AchievementAPI{ return false; } + /** + * @param Player $player + * @param string $achievementId + * @return boolean + */ public static function hasAchievement(Player $player, $achievementId){ if(!isset(self::$achievements[$achievementId]) or !isset($player->achievements)){ $player->achievements = []; @@ -136,6 +147,11 @@ class AchievementAPI{ return true; } + /** + * @param Player $player + * @param string $achievementId + * @return boolean + */ public static function broadcastAchievement(Player $player, $achievementId){ if(isset(self::$achievements[$achievementId])){ $result = ServerAPI::request()->api->dhandle("achievement.broadcast", ["player" => $player, "achievementId" => $achievementId]); @@ -151,6 +167,11 @@ class AchievementAPI{ return false; } + /** + * Removes achievement from player. + * @param Player $player + * @param string $achievementId + */ public static function removeAchievement(Player $player, $achievementId){ if(self::hasAchievement($player, $achievementId)){ $player->achievements[$achievementId] = false; diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 7ab128b6..ecd2f7be 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -1,11 +1,8 @@ server = ServerAPI::request(); } @@ -56,7 +57,6 @@ class BanAPI{ /** * @param mixed $data * @param string $event - * * @return boolean */ public function permissionsCheck($data, $event){ @@ -106,11 +106,11 @@ class BanAPI{ } /** + * Checks is player op or not * @param string $username - * * @return boolean */ - public function isOp($username){//Is a player op? + public function isOp($username){ $username = strtolower($username); if($this->server->api->dhandle("op.check", $username) === true){ return true; @@ -121,6 +121,7 @@ class BanAPI{ } /** + * Bans a player. * @param string $username */ public function ban($username){ @@ -316,14 +317,16 @@ class BanAPI{ } /** - * @param string $username - * @param string $reason + * Kicks a player + * @param string $username - player's username + * @param string $reason - kick reason, optional */ public function kick($username, $reason = "No Reason"){ $this->commandHandler("kick", [$username, $reason], "console", ""); } /** + * Unbans a player * @param string $username */ public function pardon($username){ @@ -331,6 +334,7 @@ class BanAPI{ } /** + * Bans some ip * @param string $ip */ public function banIP($ip){ @@ -338,12 +342,16 @@ class BanAPI{ } /** + * Unbans some ip * @param string $ip */ public function pardonIP($ip){ $this->commandHandler("banip", ["pardon", $ip], "console", ""); } + /** + * Rereads ban list, kick list and banip list + */ public function reload(){ $this->commandHandler("ban", ["reload"], "console", ""); $this->commandHandler("banip", ["reload"], "console", ""); @@ -351,8 +359,8 @@ class BanAPI{ } /** + * Checks is ip banned or not * @param string $ip - * * @return boolean */ public function isIPBanned($ip){ @@ -366,8 +374,8 @@ class BanAPI{ } /** + * Checks is username banned or not. * @param string $username - * * @return boolean */ public function isBanned($username){ @@ -382,8 +390,8 @@ class BanAPI{ } /** + * Checks is username in the whitelist or not * @param string $username - * * @return boolean */ public function inWhitelist($username){ diff --git a/src/Player.php b/src/Player.php index cf6fc528..25738e78 100755 --- a/src/Player.php +++ b/src/Player.php @@ -38,7 +38,6 @@ class Player{ public $lastCorrect; public $craftingItems = []; public $toCraft = []; - public $lastCraft = 0; public $loginData = []; /** @var Level */ public $level; @@ -95,6 +94,7 @@ class Player{ public $entityMovementQueue; public $entityMovementQueuePrevSentCnt = 0; public $entityMovementQueueLength = 0; + /** * Used only for statistic in ping command. * @var integer @@ -104,13 +104,13 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; - public $blockUpdateQueuePrevSentCnt = 0; public $blockUpdateQueueOrderIndex = 0; public $chatMessagesQueue; public $chatMessagesQueueLength = 0; - public $chatMessagesQueuePrevSentCnt = 0; public $chatMessagesOrderIndex = 0; + + public $chunkDataSent = []; /** @@ -408,10 +408,32 @@ class Player{ } /** - * @param integer $id - * @param array $data - * - * @return array|bool + * Sends data packet that wont be buffered. Packets are not ordered. + * @param RakNetDataPacket $packet + * @param int $reliability=0 unused + * @param bool $recover=true can the packet be recovered or not. If true the packet will be recovered only once. Otherwise, it won't be recovered at all. + * @returns int[]|false - array with seqNumber of the packet. false if player is not connected. empty array if cancelled by DataPacketSendEvent. + */ + public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return []; + + $packet->encode(); + $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $pk->data[] = $packet; + $pk->seqNumber = $this->counter[0]++; + $pk->sendtime = microtime(true); + if($recover !== false){ + $this->recoveryQueue[$pk->seqNumber] = $pk; + } + + $this->send($pk); + return [$pk->seqNumber]; + } + + /** + * Sends data packet that will be recovered only once. Packet MAY not arrive at all or arrive out of order. + * @param RakNetDataPacket $packet */ public function dataPacket(RakNetDataPacket $packet){ if($this->connected === false) return false; @@ -435,9 +457,13 @@ class Player{ return []; } + /** + * Sends data packet that will always be recovered. Packets MAY arrive out of order. + * @param RakNetDataPacket $packet + */ public function dataPacketAlwaysRecover(RakNetDataPacket $packet){ if($this->connected === false) return false; - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -459,11 +485,7 @@ class Player{ return [$pk->seqNumber]; } - public function sendChunkDataPacket(RakNetDataPacket $pk){ - if($this->connected === false) return false; - if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; - - $pk->encode(); + public function blockQueueDataPacket_big(RakNetDataPacket $pk){ $sendtime = microtime(true); $size = $this->MTU - 34; if($size <= 0) return false; @@ -719,7 +741,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->sendChunkDataPacket($pk); + $cnt = $this->blockQueueDataPacket($pk); $this->chunkDataSent["$X:$Z"] = true; $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); @@ -816,7 +838,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->sendChunkDataPacket($pk); + $cnt = $this->blockQueueDataPacket($pk); $this->chunkDataSent["$X:$Z"] = true; if($cnt === false){ @@ -934,13 +956,13 @@ class Player{ $pk->windowid = $id; $pk->property = 0; //Smelting $pk->value = floor($data->data["CookTime"]); - $this->dataPacket($pk); + $this->blockQueueDataPacket($pk); $pk = new ContainerSetDataPacket; $pk->windowid = $id; $pk->property = 1; //Fire icon $pk->value = $data->data["BurnTicks"]; - $this->dataPacket($pk); + $this->blockQueueDataPacket($pk); } } } @@ -954,7 +976,7 @@ class Player{ $pk->windowid = $id; $pk->slot = $data["slot"] + ($data["offset"] ?? 0); $pk->item = $data["slotdata"]; - $this->dataPacket($pk); + $this->blockQueueDataPacket($pk); } } } @@ -1029,7 +1051,6 @@ class Player{ $this->dataPacket($pk); break; case "entity.animate": - $pk = new AnimatePacket; $pk->eid = $data["eid"]; $pk->action = $data["action"]; //1 swing arm, @@ -1495,12 +1516,48 @@ class Player{ $this->blockUpdateQueue->data = []; } + /** + * Sends packet in block order channel. Packets are always delivered and ordered. + * Used for sending tileentity data, chunk data, updateblock packets. + * @param RakNetPacket $pk + */ + public function blockQueueDataPacket(RakNetDataPacket $pk){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + + $pk->encode(); + + $len = 1 + strlen($pk->buffer); + $MTU = $this->MTU - 24; + if($len > $MTU) return $this->blockQueueDataPacket_big($pk); + + if(($this->blockUpdateQueueLength + $len) >= $MTU){ + $this->sendBlockUpdateQueue(); + } + + $pk->messageIndex = $this->counter[3]++; + $pk->reliability = 3; + $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; + $pk->orderIndex = $this->blockUpdateQueueOrderIndex++; + @$this->blockUpdateQueue->data[] = $pk; + $this->blockUpdateQueueLength += 10 + $len; + } + + /** + * Adds block update into queue. Does nothing if chunk wasnt loaded. + * @param int $x + * @param int $y + * @param int $z + * @param int $id + * @param int $meta + * @return boolean + */ public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta){ $xC = $x >> 4; $zC = $z >> 4; if(!isset($this->chunkDataSent["$xC:$zC"])){ ConsoleAPI::debug("Ignoring block update $x $y $z $id $meta: chunk data was not sent."); - return; //dont send updateblockpacket as it will be buffered and updated only after all chunks will load + return false; //dont send updateblockpacket as it will be buffered and updated only after all chunks will load } $packet = new UpdateBlockPacket(); $packet->x = $x; @@ -1508,21 +1565,7 @@ class Player{ $packet->z = $z; $packet->block = $id; $packet->meta = $meta; - $packet->encode(); - - $len = 1 + strlen($packet->buffer); - $MTU = $this->MTU - 24; - - if(($this->blockUpdateQueueLength + $len) >= $MTU){ - $this->sendBlockUpdateQueue(); - } - - $packet->messageIndex = $this->counter[3]++; - $packet->reliability = 3; - $packet->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; - $packet->orderIndex = $this->blockUpdateQueueOrderIndex++; - @$this->blockUpdateQueue->data[] = $packet; - $this->blockUpdateQueueLength += 10 + $len; + $this->blockQueueDataPacket($packet); } public function addEntityMovementUpdateToQueue(Entity $e){ @@ -1699,28 +1742,12 @@ class Player{ $this->data->set("gamemode", $this->gamemode); } } - - public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ - if($this->connected === false) return false; - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return []; - - $packet->encode(); - $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); - $pk->data[] = $packet; - $pk->seqNumber = $this->counter[0]++; - $pk->sendtime = microtime(true); - if($recover !== false){ - $this->recoveryQueue[$pk->seqNumber] = $pk; - } - - $this->send($pk); - return [$pk->seqNumber]; - } public function sendChatMessagePacket(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; + //TODO big chat messages support $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; @@ -2271,7 +2298,6 @@ class Player{ if($slotItem !== false){ if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); else $slotItem->count -= 1; - //$this->sendInventory(); } } @@ -2516,7 +2542,7 @@ class Player{ if($this->entity->getHealth() < 20 && $foodHeal != 0){ $pk = new EntityEventPacket; $pk->eid = 0; - $pk->event = 9; + $pk->event = EntityEventPacket::ENTITY_COMPLETE_USING_ITEM; $this->dataPacket($pk); $this->entity->heal($foodHeal, "eating"); @@ -2743,18 +2769,18 @@ class Player{ $slot = $tile->getSlot($slotn); if($this->server->api->dhandle("player.container.slot", [ - "tile" => $tile, - "slot" => $packet->slot, - "offset" => $offset, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this - ]) === false){ + "tile" => $tile, + "slot" => $packet->slot, + "offset" => $offset, + "slotdata" => $slot, + "itemdata" => $item, + "player" => $this + ]) === false){ $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; $pk->item = $slot; - $this->dataPacket($pk); + $this->blockQueueDataPacket($pk); break; } @@ -2782,17 +2808,17 @@ class Player{ $slot = $tile->getSlot($packet->slot); if($this->server->api->dhandle("player.container.slot", [ - "tile" => $tile, - "slot" => $packet->slot, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this, - ]) === false){ + "tile" => $tile, + "slot" => $packet->slot, + "slotdata" => $slot, + "itemdata" => $item, + "player" => $this, + ]) === false){ $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; $pk->item = $slot; - $this->dataPacket($pk); + $this->blockQueueDataPacket($pk); break; } diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index 05d2c646..312b63bd 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -14,7 +14,6 @@ class BeetrootBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/CactusBlock.php b/src/material/block/plant/CactusBlock.php index b218d8b9..5c6bdd6b 100644 --- a/src/material/block/plant/CactusBlock.php +++ b/src/material/block/plant/CactusBlock.php @@ -62,7 +62,6 @@ class CactusBlock extends TransparentBlock{ $block3 = $this->getSide(5); if($block0->isFlowable === true and $block1->isFlowable === true and $block2->isFlowable === true and $block3->isFlowable === true){ $this->level->setBlock($this, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); ServerAPI::request()->api->block->scheduleBlockUpdate(clone $this, 10, BLOCK_UPDATE_NORMAL); return true; } diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index 98717892..4bcd86b9 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -14,7 +14,6 @@ class CarrotBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index 10500032..c98d8677 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -13,7 +13,6 @@ class MelonStemBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index d8f86f38..15866171 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -14,7 +14,6 @@ class PotatoBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index ac34ed7d..c662af3a 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -13,7 +13,6 @@ class PumpkinStemBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index bd5635d0..49ae119e 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -29,7 +29,6 @@ class SaplingBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === GRASS or $down->getID() === DIRT or $down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 97f3d39f..3e25d2ea 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -14,7 +14,6 @@ class WheatBlock extends FlowableBlock{ $down = $this->getSide(0); if($down->getID() === FARMLAND){ $this->level->setBlock($block, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } return false; diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 52079e10..3b74cab2 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -17,7 +17,6 @@ class IceBlock extends TransparentBlock{ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return $ret; } diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 05cf7770..44e9b06a 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -6,6 +6,10 @@ class EntityEventPacket extends RakNetDataPacket{ const ENTITY_DAMAGE = 2; const ENTITY_DEAD = 3; + /** + * Works only for player entities + */ + const ENTITY_COMPLETE_USING_ITEM = 9; const ENTITY_ANIM_10 = 10; public function __construct($eid = null, $event = null){ diff --git a/src/world/Explosion.php b/src/world/Explosion.php index e44c7bd3..0a8e041d 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -1,16 +1,10 @@ DIRT, - STONE => COBBLESTONE, - COAL_ORE => COAL, - DIAMOND_ORE => DIAMOND, - REDSTONE_ORE => REDSTONE, - ]; public static $enableExplosions = true; - public $level; //Rays + /** @var Level */ + public $level; + /** @var Position */ public $source; public $size; public $affectedBlocks = []; @@ -222,4 +216,10 @@ class Explosion{ $pk->records = $send; $server->api->player->broadcastPacket($this->level->players, $pk); } + + + /** + * @deprecated unused + */ + public static $specialDrops = []; } diff --git a/src/world/Tile.php b/src/world/Tile.php index 66b20280..5f4714a5 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -283,7 +283,10 @@ class Tile extends Position{ $pk->z = $this->z; $pk->case1 = 1; $pk->case2 = 2; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); + foreach($this->level->players as $player){ + $player->blockQueueDataPacket(clone $pk); + } + for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $this->getSlot($s); if($slot->getID() > AIR and $slot->count > 0){ @@ -297,7 +300,7 @@ class Tile extends Position{ $pk = new ContainerSetContentPacket; $pk->windowid = $id; $pk->slots = $slots; - $player->dataPacket($pk); + $player->blockQueueDataPacket($pk); return true; }elseif($this->class === TILE_FURNACE){ $player->windowCnt++; @@ -311,7 +314,7 @@ class Tile extends Position{ $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; - $player->dataPacket($pk); + $player->blockQueueDataPacket($pk); $slots = []; for($s = 0; $s < FURNACE_SLOTS; ++$s){ @@ -325,7 +328,7 @@ class Tile extends Position{ $pk = new ContainerSetContentPacket; $pk->windowid = $id; $pk->slots = $slots; - $player->dataPacket($pk); + $player->blockQueueDataPacket($pk); return true; } } @@ -375,7 +378,7 @@ class Tile extends Position{ $pk->y = $this->y; $pk->z = $this->z; $pk->namedtag = $nbt->binary; - $player->dataPacketAlwaysRecover($pk); + $player->blockQueueDataPacket($pk); break; case TILE_SIGN: $nbt = new NBT(); @@ -420,7 +423,7 @@ class Tile extends Position{ $pk->y = $this->y; $pk->z = $this->z; $pk->namedtag = $nbt->binary; - $player->dataPacketAlwaysRecover($pk); + $player->blockQueueDataPacket($pk); break; } } From 37e2919f46db18c52622165dc383281be9cb13f9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 26 Jun 2025 04:57:12 +0300 Subject: [PATCH 074/146] entity data packets are sent as ordered reliable packets in entity queue, explosions dont send what blocks they exploded in ExplosionPacket --- src/API/EntityAPI.php | 10 ++ src/API/PlayerAPI.php | 7 +- src/Player.php | 211 +++++++++++++++------------- src/entity/Creature.php | 8 +- src/entity/Entity.php | 17 +-- src/entity/Projectile.php | 4 +- src/entity/object/Arrow.php | 2 +- src/entity/object/FallingSand.php | 2 +- src/entity/object/ItemEntity.php | 4 +- src/entity/object/Minecart.php | 4 +- src/entity/object/Painting.php | 2 +- src/entity/object/PrimedTNT.php | 2 +- src/network/raknet/RakNetPacket.php | 4 +- src/world/Explosion.php | 25 ++-- 14 files changed, 166 insertions(+), 136 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 6d64f453..4c0fe8a6 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -1,6 +1,9 @@ x - $radius)) >> 4; $minChunkZ = ((int)($center->z - $radius)) >> 4; diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 75dd7055..9e74f1ca 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -148,8 +148,7 @@ class PlayerAPI{ if($additional){ $arqCnt = count($p->packetAlwaysRecoverQueue); $rqCnt = count($p->recoveryQueue); - $emps = $p->entityMovementPacketsPerSecond; - $additionalInfo = "ARQ/RQ/EMPS: $arqCnt/$rqCnt/$emps"; + $additionalInfo = "ARQ/RQ/EMPS: $arqCnt/$rqCnt"; } $chunksCnt = count($p->chunkDataSent); return "{$p->username}'s ping: {$ping}ms, packet loss {$pkLoss}%, $transfer KB/s. Chunks: $chunksCnt/256\n$additionalInfo"; @@ -452,7 +451,7 @@ class PlayerAPI{ $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); } } } @@ -489,7 +488,7 @@ class PlayerAPI{ $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $p->dataPacket($pk); + $p->entityQueueDataPacket($pk); } } } diff --git a/src/Player.php b/src/Player.php index 25738e78..9b60e5c9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3,6 +3,7 @@ class Player{ const CHATMESSAGE_ORDER_CHANNEL = 1; const BLOCKUPDATE_ORDER_CHANNEL = 2; + const ENTITY_ORDER_CHANNEL = 3; public static $experimentalHotbar = false; public static $allowDroppingSingleItems = true; @@ -88,19 +89,11 @@ class Player{ public $bedPosition = null; /** - * DataPacket that is used as a buffer for all non-player entity movement packets * @var RakNetPacket */ - public $entityMovementQueue; - public $entityMovementQueuePrevSentCnt = 0; - public $entityMovementQueueLength = 0; - - /** - * Used only for statistic in ping command. - * @var integer - */ - public $entityMovementPacketsPerSecond = 0; - public $lastEntityMovementPacketsPerSecondRecordedTime = 0; + public $entityDataQueue; + public $entityDataQueueLength = 0; + public $entityDataQueueOrderIndex = 0; public $blockUpdateQueue; public $blockUpdateQueueLength = 0; @@ -160,8 +153,8 @@ class Player{ $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); - $this->entityMovementQueue->data = []; + $this->entityDataQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityDataQueue->data = []; $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; @@ -283,7 +276,7 @@ class Player{ $pk->yaw = 0; $pk->pitch = 0; $pk->bodyYaw = 0; - $e->player->dataPacketAlwaysRecover($pk); + $e->player->entityQueueDataPacket($pk); $pk = new MovePlayerPacket(); $pk->eid = $e->eid; @@ -293,7 +286,7 @@ class Player{ $pk->yaw = 0; $pk->pitch = 0; $pk->bodyYaw = 0; - $this->dataPacketAlwaysRecover($pk); + $this->entityQueueDataPacket($pk); }else{ $pk = new MoveEntityPacket_PosRot(); @@ -303,7 +296,7 @@ class Player{ $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $this->dataPacketAlwaysRecover($pk); + $this->entityQueueDataPacket($pk); } } } @@ -328,7 +321,7 @@ class Player{ $pk->z = $e->z; $pk->yaw = $e->yaw; $pk->pitch = $e->pitch; - $this->dataPacketAlwaysRecover($pk); + $this->entityQueueDataPacket($pk); } } } @@ -346,14 +339,14 @@ class Player{ $pk->z = $player->entity->z; $pk->yaw = $player->entity->yaw; $pk->pitch = $player->entity->pitch; - $this->dataPacketAlwaysRecover($pk); + $this->entityQueueDataPacket($pk); $pk = new PlayerEquipmentPacket; $pk->eid = $this->eid; $pk->item = $this->getSlot($this->slot)->getID(); $pk->meta = $this->getSlot($this->slot)->getMetadata(); $pk->slot = 0; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); $this->sendArmor($player); $pk = new PlayerEquipmentPacket; @@ -361,7 +354,7 @@ class Player{ $pk->item = $player->getSlot($player->slot)->getID(); $pk->meta = $player->getSlot($player->slot)->getMetadata(); $pk->slot = 0; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); $player->sendArmor($this); } } @@ -394,7 +387,7 @@ class Player{ $pk->speedX = 0; $pk->speedY = 0; $pk->speedZ = 0; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); $pk = new MovePlayerPacket; $pk->eid = 0; @@ -404,7 +397,7 @@ class Player{ $pk->bodyYaw = $yaw; $pk->pitch = $pitch; $pk->yaw = $yaw; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); } /** @@ -523,6 +516,44 @@ class Player{ return $cnts; } + public function entityQueueDataPacket_big(RakNetDataPacket $pk){ + $sendtime = microtime(true); + $size = $this->MTU - 34; + if($size <= 0) return false; + $buffer = str_split($pk->buffer, $size); + $bigCnt = $this->bigCnt; + $this->bigCnt = ($this->bigCnt + 1) % 0x10000; + $cnts = []; + $bufCount = count($buffer); + + $orderIndex = $this->entityDataQueueOrderIndex++; + foreach($buffer as $i => $buf){ + $cnts[] = $count = $this->counter[0]++; + + $pk = new UnknownPacket; + $pk->packetID = $pk->pid(); + $pk->reliability = 3; + + $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; + $pk->orderIndex = $orderIndex; + + $pk->hasSplit = true; + $pk->splitCount = $bufCount; + $pk->splitID = $bigCnt; + $pk->splitIndex = $i; + $pk->buffer = $buf; + $pk->messageIndex = $this->counter[3]++; + + $rk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $rk->data[] = $pk; + $rk->seqNumber = $count; + $rk->sendtime = $sendtime; + $this->packetAlwaysRecoverQueue[$count] = $rk; + $this->send($rk); + } + return $cnts; + } + public function sendBigPacketAlwaysRecover(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; @@ -675,12 +706,12 @@ class Player{ $pk = new ContainerSetContentPacket; $pk->windowid = 0x78; //Armor window id $pk->slots = $this->armor; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); }else{ $pk = new PlayerArmorEquipmentPacket; $pk->eid = $this->eid; $pk->slots = $data["slots"]; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); } }else{ $this->server->api->dhandle("player.armor", $data); @@ -945,7 +976,7 @@ class Player{ $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; $pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"]; $pk->type = 0; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); break; case "tile.update": if($data->level === $this->level){ @@ -990,7 +1021,7 @@ class Player{ $pk = new PlayerArmorEquipmentPacket; $pk->eid = $data["eid"]; $pk->slots = $data["slots"]; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); } break; case "player.pickup": @@ -999,7 +1030,7 @@ class Player{ $pk = new TakeItemEntityPacket; $pk->eid = 0; $pk->target = $data["entity"]->eid; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); if(($this->gamemode & 0x01) === 0x00){ $this->addItem($data["entity"]->itemID, $data["entity"]->meta, $data["entity"]->stack, false); @@ -1019,7 +1050,7 @@ class Player{ $pk = new TakeItemEntityPacket; $pk->eid = $data["eid"]; $pk->target = $data["entity"]->eid; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); } break; case "player.equipment.change": @@ -1033,7 +1064,7 @@ class Player{ $pk->item = $data["item"]->getID(); $pk->meta = $data["item"]->getMetadata(); $pk->slot = $data["slot"]; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); break; case "entity.motion": @@ -1048,13 +1079,13 @@ class Player{ $pk->speedX = $data->speedX; $pk->speedY = $data->speedY; $pk->speedZ = $data->speedZ; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); break; case "entity.animate": $pk = new AnimatePacket; $pk->eid = $data["eid"]; $pk->action = $data["action"]; //1 swing arm, - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); break; case "entity.metadata": if($data->eid === $this->eid){ @@ -1066,7 +1097,7 @@ class Player{ $pk = new SetEntityDataPacket; $pk->eid = $eid; $pk->metadata = $data->getMetadata(); - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); } break; case "entity.event": @@ -1079,7 +1110,7 @@ class Player{ $pk = new EntityEventPacket; $pk->eid = $eid; $pk->event = $data["event"]; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); } break; case "server.chat": @@ -1175,9 +1206,10 @@ class Player{ } public function makeInvisibleForOnePlayer(Player $player){ + //TODO rewrite how invisibility is handled $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); } public function makeInvisibleForAllPlayers(){ $pk = new RemoveEntityPacket; @@ -1392,11 +1424,6 @@ class Player{ $this->ackQueue = []; } - if($time - $this->lastEntityMovementPacketsPerSecondRecordedTime > 1){ - $this->entityMovementPacketsPerSecond = 0; - $this->lastEntityMovementPacketsPerSecondRecordedTime = $time; - } - if($this->sendingInventoryRequired){ //console("inv resent"); if(($this->gamemode & 0x01) !== CREATIVE){ @@ -1465,7 +1492,8 @@ class Player{ } foreach($this->packetAlwaysRecoverQueue as $cnt => $data){ - if($time - $data->sendtime >= 5){ + $maxDelay = (isset($this->chunkCount[$cnt])) ? 1 : 5; + if($time - $data->sendtime >= $maxDelay){ $this->resendQueue[$cnt] = $data; } } @@ -1495,13 +1523,13 @@ class Player{ } public function sendEntityMovementUpdateQueue(){ - if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){ - $this->entityMovementQueue->seqNumber = $this->counter[0]++; - $this->send($this->entityMovementQueue); + if($this->entityDataQueueLength > 0 && $this->entityDataQueue instanceof RakNetPacket){ + $this->entityDataQueue->seqNumber = $this->counter[0]++; + $this->send($this->entityDataQueue); } - $this->entityMovementQueueLength = 0; - $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); - $this->entityMovementQueue->data = []; + $this->entityDataQueueLength = 0; + $this->entityDataQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityDataQueue->data = []; } public function sendBlockUpdateQueue(){ @@ -1543,6 +1571,32 @@ class Player{ $this->blockUpdateQueueLength += 10 + $len; } + /** + * Sends a packet in entity order channel. Packets are always delivered and ordered. + * + */ + public function entityQueueDataPacket(RakNetDataPacket $pk){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + + $pk->encode(); + + $len = 1 + strlen($pk->buffer); + $MTU = $this->MTU - 24; + if($len > $MTU) return $this->entityQueueDataPacket_big($pk); + + if(($this->entityDataQueueLength + $len) >= $MTU){ + $this->sendEntityMovementUpdateQueue(); + } + + $pk->messageIndex = $this->counter[3]++; + $pk->reliability = 3; + $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; + $pk->orderIndex = $this->entityDataQueueOrderIndex++; + @$this->entityDataQueue->data[] = $pk; + $this->entityDataQueueLength += 10 + $len; + } + /** * Adds block update into queue. Does nothing if chunk wasnt loaded. * @param int $x @@ -1569,11 +1623,6 @@ class Player{ } public function addEntityMovementUpdateToQueue(Entity $e){ - $len = 0; - $packets = 0; - $motionSent = false; - $moveSent = false; - $headSent = false; $svdYSpeed = $e->speedY; if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; @@ -1585,10 +1634,7 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; - $motion->encode(); - $len += 1 + strlen($motion->buffer); - ++$packets; - $motionSent = true; + $this->entityQueueDataPacket($motion); } } if($e->modifySpeedY){ @@ -1606,7 +1652,7 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; - $move->encode(); + $this->entityQueueDataPacket($move); }else{ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; @@ -1615,45 +1661,14 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; - $move->encode(); + $this->entityQueueDataPacket($move); } - - $len += strlen($move->buffer) + 1; - ++$packets; - $moveSent = true; }else if($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; - $headyaw->encode(); - $len += strlen($headyaw->buffer) + 1; - ++$packets; - $headSent = true; + $this->entityQueueDataPacket($headyaw); } - if($packets <= 0) return; - //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); - $MTU = $this->MTU - 24; - if(($this->entityMovementQueueLength + $len) >= $MTU){ - $this->sendEntityMovementUpdateQueue(); - } - if($motionSent){ - $motion->messageIndex = 0; //force 0 cuz reliability 0 - $motion->reliability = 0; - $this->entityMovementQueue->data[] = $motion; - } - if($moveSent){ - $move->messageIndex = 0; - $move->reliability = 0; - $this->entityMovementQueue->data[] = $move; - } - if($headSent){ - $headyaw->messageIndex = 0; - $headyaw->reliability = 0; - $this->entityMovementQueue->data[] = $headyaw; - } - - $this->entityMovementQueueLength += 6*$packets + $len; - $this->entityMovementPacketsPerSecond += $packets; } /** @@ -2007,13 +2022,8 @@ class Player{ $this->lastMeasure = microtime(true); $this->server->schedule(50, [$this, "measureLag"], [], true); - $pk = new SetTimePacket; - $pk->time = $this->level->getTime(); - $pk->started = !$this->level->isTimeStopped(); - $this->dataPacket($pk); - - console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); + console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ") MTU: {$this->MTU}"); break; case ProtocolInfo::READY_PACKET: if($this->loggedIn === false){ @@ -2025,6 +2035,12 @@ class Player{ break; } + $pk = new SetTimePacket; + $pk->time = $this->level->getTime(); + $pk->started = !$this->level->isTimeStopped(); + $this->dataPacketAlwaysRecover($pk); + console($this->level->getTime()); + $pos = new Position($this->entity->x, $this->entity->y, $this->entity->z, $this->level); $pData = $this->data->get("position"); $this->entity->setHealth($this->data->get("health"), "spawn", true, allowHarm: false); @@ -2504,7 +2520,7 @@ class Player{ $pk->bodyYaw = $this->entity->headYaw; foreach($this->entity->level->players as $player){ if($player->entity->eid != $this->entity->eid){ - $player->dataPacket(clone $pk); + $player->entityQueueDataPacket(clone $pk); } } @@ -2543,7 +2559,7 @@ class Player{ $pk = new EntityEventPacket; $pk->eid = 0; $pk->event = EntityEventPacket::ENTITY_COMPLETE_USING_ITEM; - $this->dataPacket($pk); + $this->entityQueueDataPacket($pk); $this->entity->heal($foodHeal, "eating"); --$slot->count; @@ -3144,6 +3160,7 @@ class Player{ foreach($packet->packets as $count){ if(isset($this->packetAlwaysRecoverQueue[$count])){ $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; + unset($this->packetAlwaysRecoverQueue[$count]); unset($this->chunkCount[$count]); }else if(isset($this->recoveryQueue[$count])){ diff --git a/src/entity/Creature.php b/src/entity/Creature.php index b472686d..a3254b6a 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -57,9 +57,6 @@ abstract class Creature extends Living{ return 0; } public function spawn($player){ - if(!($player instanceof Player)){ - $player = $this->server->api->player->get($player); - } if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } @@ -72,18 +69,19 @@ abstract class Creature extends Living{ $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; $pk->metadata = $this->getMetadata(); - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); if($this->linkedEntity != 0 && $this->isRider){ $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); } + return true; } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 7ed6fcca..214b2307 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1132,11 +1132,12 @@ class Entity extends Position $this->server->api->dhandle("entity.metadata", $this); } + /** + * @param Player $player + * @return boolean + */ public function spawn($player) { - if(! ($player instanceof Player)){ - $player = $this->server->api->player->get($player); - } if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } @@ -1160,14 +1161,14 @@ class Entity extends Position $pk->itemID = 0; $pk->itemAuxValue = 0; $pk->metadata = $this->getMetadata(); - $player->dataPacketAlwaysRecover($pk, 2, true); + $player->entityQueueDataPacket($pk, 2, true); $pk = new PlayerEquipmentPacket(); $pk->eid = $this->eid; $pk->item = $this->player->getSlot($this->player->slot)->getID(); $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); $pk->slot = 0; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); $this->player->sendArmor($player); break; } @@ -1600,9 +1601,9 @@ class Entity extends Position if(($p->entity instanceof Entity) && $p->entity->eid == $this->eid){ $pk2 = clone $pk; $pk2->eid = 0; - $p->dataPacket($pk2); + $p->entityQueueDataPacket($pk2); }else{ - $p->dataPacket(clone $pk); + $p->entityQueueDataPacket(clone $pk); } } } @@ -1610,7 +1611,7 @@ class Entity extends Position if($this->player instanceof Player){ $pk = new SetHealthPacket(); $pk->health = $this->health; - $this->player->dataPacket($pk); + $this->player->entityQueueDataPacket($pk); } if($this->health <= 0 and $this->dead === false){ $this->makeDead($cause); diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index 534d5473..7042daa1 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -198,6 +198,7 @@ abstract class Projectile extends Entity{ return $data; } + public function spawn($player) { $pk = new AddEntityPacket(); @@ -210,7 +211,8 @@ abstract class Projectile extends Entity{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $pk->did = 0; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); + return true; } } \ No newline at end of file diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 216afc49..f55a49cd 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -272,7 +272,7 @@ class Arrow extends Entity{ $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); } } } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 3c040340..8481830e 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -31,7 +31,7 @@ class FallingSand extends Entity{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $pk->did = -$this->data["Tile"]; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); } public function update($now){ diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 2f44ee25..e24726fe 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -54,14 +54,14 @@ class ItemEntity extends Entity{ $pk->roll = 0; $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); $pk->metadata = $this->getMetadata(); - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); $pk = new SetEntityMotionPacket(); $pk->eid = $this->eid; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index ab48a67a..326e6adc 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -435,14 +435,14 @@ class Minecart extends Vehicle{ $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); + $player->entityQueueDataPacket($pk); } public function interactWith(Entity $e, $action){ diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 6a49d48a..20bb25e9 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -205,6 +205,6 @@ class Painting extends Entity{ $pk->z = (int) $this->zPos; $pk->direction = $this->direction; $pk->title = $this->motive; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); } } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 75a8825d..2585e917 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -76,6 +76,6 @@ class PrimedTNT extends Entity{ $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; - $player->dataPacketAlwaysRecover($pk); + $player->entityQueueDataPacket($pk); } } \ No newline at end of file diff --git a/src/network/raknet/RakNetPacket.php b/src/network/raknet/RakNetPacket.php index d97f8812..870a2733 100644 --- a/src/network/raknet/RakNetPacket.php +++ b/src/network/raknet/RakNetPacket.php @@ -3,7 +3,9 @@ class RakNetPacket extends Packet{ public $packetID; - + public $seqNumber; + public $sendtime; + public function __construct($packetID){ $this->packetID = (int) $packetID; } diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 0a8e041d..25a05aef 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -85,18 +85,20 @@ class Explosion{ $pk->speedX = $entity->speedX; $pk->speedY = $entity->speedY; $pk->speedZ = $entity->speedZ; - $entity->player->dataPacket($pk); + $entity->player->entityQueueDataPacket($pk); } } } } - $pk = new ExplodePacket; //sound fix + $pk = new ExplodePacket; $pk->x = $this->source->x; $pk->y = $this->source->y; $pk->z = $this->source->z; $pk->radius = $this->size; - $pk->records = []; - $server->api->player->broadcastPacket($this->level->players, $pk); + $pk->records = []; //ExplodePacket doesnt seem to use the records provided by this packet, so no need to send more data + foreach ($this->level->players as $player){ + $player->blockQueueDataPacket($pk); + } return; } if($this->size < 0.1 or $server->api->dhandle("entity.explosion", [ @@ -147,10 +149,7 @@ class Explosion{ $this->sub_expl($i, $mRays, $j, $k); } } - //if($i == 0 or $i == $mRays or $j == 0 or $j == $mRays or $k == 0 or $k == $mRays){ - - $send = []; foreach($server->api->entity->getRadius($this->source, $radius) as $entity){ $distance = $this->source->distance($entity); $distByRad = $distance / $this->size; @@ -173,7 +172,7 @@ class Explosion{ $pk->speedX = $entity->speedX; $pk->speedY = $entity->speedY; $pk->speedZ = $entity->speedZ; - $entity->player->dataPacket($pk); + $entity->player->entityQueueDataPacket($pk); } } } @@ -185,7 +184,7 @@ class Explosion{ $x = $xyz >> 16; $z = $xyz >> 8 & 0xff; $y = $xyz & 0xff; - //console("$x $y $z $id $meta"); + if($id === TNT){ $data = [ "x" => $x + 0.5, @@ -206,15 +205,17 @@ class Explosion{ } } $this->level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); - $send[] = $xyz; } + $pk = new ExplodePacket; $pk->x = $this->source->x; $pk->y = $this->source->y; $pk->z = $this->source->z; $pk->radius = $this->size; - $pk->records = $send; - $server->api->player->broadcastPacket($this->level->players, $pk); + $pk->records = []; //ExplodePacket doesnt seem to use the records provided by this packet, so no need to send more data + foreach ($this->level->players as $player){ + $player->blockQueueDataPacket($pk); + } } From 756c597e47837808c735be564704d92c6c143048 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 26 Jun 2025 08:17:14 +0300 Subject: [PATCH 075/146] rewritten pumpkin and melon growth, fixed new world generation --- src/entity/Entity.php | 3 +- src/material/block/plant/MelonStemBlock.php | 103 +++++++++++----- src/material/block/plant/PumpkinStemBlock.php | 112 ++++++++++++------ src/pmf/PMFLevel.php | 1 + 4 files changed, 149 insertions(+), 70 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 214b2307..f13e36b5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -118,7 +118,7 @@ class Entity extends Position public $moveStrafing, $moveForward; - function __construct(Level $level, $eid, $class, $type = 0, $data = []) + public function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new XorShift128Random(); $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables @@ -202,6 +202,7 @@ class Entity extends Position $this->outOfWorld(); } } + public function handlePrePlayerSearcher(){ } diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index c98d8677..8042d889 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -17,34 +17,79 @@ class MelonStemBlock extends FlowableBlock{ } return false; } + + public static function getGrowthSpeed(Level $level, $x, $y, $z){ + $zneg = $level->level->getBlockID($x, $y, $z-1); + $zpos = $level->level->getBlockID($x, $y, $z+1); + $xneg = $level->level->getBlockID($x-1, $y, $z); + $xpos = $level->level->getBlockID($x+1, $y, $z); + $znxn = $level->level->getBlockID($x-1, $y, $z-1); + $znxp = $level->level->getBlockID($x+1, $y, $z-1); + $zpxp = $level->level->getBlockID($x+1, $y, $z+1); + $zpxn = $level->level->getBlockID($x-1, $y, $z+1); + $xEqual = $xneg == MELON_STEM || $xpos == MELON_STEM; + $zEqual = $zneg == MELON_STEM || $zpos == MELON_STEM; + $xzEqual = $znxn == MELON_STEM || $znxp == MELON_STEM || $zpxn == MELON_STEM || $zpxp == MELON_STEM; + + $speed = 1; + for($xx = $x-1; $xx >= $x+1; ++$xx){ + for($zz = $z-1; $zz >= $z+1; ++$zz){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + $v18 = 0; + if($id == FARMLAND) $v18 = $meta > 0 ? 3 : 1; + if($xx != $x || $zz != $z) $v18 *= 0.25; + $speed += $v18; + } + } + if($xzEqual || ($xEqual && $zEqual)) return $speed*0.5; + return $speed; + } + public static function onRandomTick(Level $level, $x, $y, $z){ - if(mt_rand(0, 2) == 1){ - $block = $level->level->getBlock($x, $y, $z); - if($block[1] < 0x07){ - //++$this->meta; - //$this->level->setBlock($this, $this, true, false, true); - $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); - }else{ - - $position = new AirBlock(); //feke block - $position->x = $x; - $position->y = $y; - $position->z = $z; - $position->level = $level; - for($side = 2; $side <= 5; ++$side){ - $b = $position->getSide($side); - if($b->getID() === MELON_BLOCK){ - return; - } - } - $side = $position->getSide(mt_rand(2,5)); - $d = $side->getSide(0); - if($side->getID() === AIR and ($d->getID() === FARMLAND or $d->getID() === GRASS or $d->getID() === DIRT)){ - $level->setBlock($side, new MelonBlock(), true, false, true); - } + //TODO checkAlive + //if ( Level::getRawBrightness(a2, a3, a4 + 1, a5) > 8 ) TODO - skylight + + $growSpeed = static::getGrowthSpeed($level, $x, $y, $z); + $rand = mt_rand(0, (int)(25/$growSpeed)); + if($rand != 0) return; + + $meta = $level->level->getBlockDamage($x, $y, $z); + if($meta <= 6){ + $level->fastSetBlockUpdateMeta($x, $y, $z, $meta+1); + return; + } + + $xn = $level->level->getBlockID($x-1, $y, $z); + $xp = $level->level->getBlockID($x+1, $y, $z); + $zn = $level->level->getBlockID($x, $y, $z-1); + $zp = $level->level->getBlockID($x, $y, $z+1); + if($xn != MELON_BLOCK && $xp != MELON_BLOCK && $zn != MELON_BLOCK && $zp != MELON_BLOCK){ + $below = $level->level->getBlockID($x-1, $y-1, $z); + if($level->level->getBlockID($x-1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x-1, $y, $z, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x+1, $y-1, $z); + if($level->level->getBlockID($x+1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x+1, $y, $z, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z-1); + if($level->level->getBlockID($x, $y, $z-1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z-1, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z+1); + if($level->level->getBlockID($x, $y, $z+1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z+1, MELON_BLOCK, 0); + return; } } } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); @@ -67,14 +112,8 @@ class MelonStemBlock extends FlowableBlock{ public function getDrops(Item $item, Player $player){ $drops = []; - if($this->meta >= 0x07){ - $drops[] = [MELON_SEEDS, 0, mt_rand(1, 2)]; - } - elseif($this->meta >= 0x01 and $this->meta <= 0x07){ - $drops[] = [MELON_SEEDS, 0, 1]; - } - else{ - $drops[] = [MELON_SEEDS, 0, 0]; + for($i = 0; $i < 3; ++$i){ + if(mt_rand(0, 15) <= $this->meta) $drops[] = [MELON_SEEDS, 0, 1]; } return $drops; } diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index c662af3a..874e1a8e 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -10,38 +10,82 @@ class PumpkinStemBlock extends FlowableBlock{ $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->getID() === FARMLAND){ - $this->level->setBlock($block, $this, true, false, true); - return true; - } + $down = $this->getSide(0); + if($down->getID() === FARMLAND){ + $this->level->setBlock($block, $this, true, false, true); + return true; + } return false; } + + public static function getGrowthSpeed(Level $level, $x, $y, $z){ + $zneg = $level->level->getBlockID($x, $y, $z-1); + $zpos = $level->level->getBlockID($x, $y, $z+1); + $xneg = $level->level->getBlockID($x-1, $y, $z); + $xpos = $level->level->getBlockID($x+1, $y, $z); + $znxn = $level->level->getBlockID($x-1, $y, $z-1); + $znxp = $level->level->getBlockID($x+1, $y, $z-1); + $zpxp = $level->level->getBlockID($x+1, $y, $z+1); + $zpxn = $level->level->getBlockID($x-1, $y, $z+1); + $xEqual = $xneg == PUMPKIN_STEM || $xpos == PUMPKIN_STEM; + $zEqual = $zneg == PUMPKIN_STEM || $zpos == PUMPKIN_STEM; + $xzEqual = $znxn == PUMPKIN_STEM || $znxp == PUMPKIN_STEM || $zpxn == PUMPKIN_STEM || $zpxp == PUMPKIN_STEM; + + $speed = 1; + for($xx = $x-1; $xx >= $x+1; ++$xx){ + for($zz = $z-1; $zz >= $z+1; ++$zz){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if($id == FARMLAND) $v18 = $meta > 0 ? 3 : 1; + else $v18 = 0; + if($xx != $x || $zz != $z) $v18 *= 0.25; + $speed += $v18; + } + } + if($xzEqual || ($xEqual && $zEqual)) return $speed*0.5; + return $speed; + } + public static function onRandomTick(Level $level, $x, $y, $z){ - if(mt_rand(0, 2) == 1){ - $block = $level->level->getBlock($x, $y, $z); - if($block[1] < 0x07){ - //++$this->meta; - //$this->level->setBlock($this, $this, true, false, true); - $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); - }else{ - - $position = new AirBlock(); //feke block - $position->x = $x; - $position->y = $y; - $position->z = $z; - $position->level = $level; - for($side = 2; $side <= 5; ++$side){ - $b = $position->getSide($side); - if($b->getID() === PUMPKIN){ - return; - } - } - $side = $position->getSide(mt_rand(2,5)); - $d = $side->getSide(0); - if($side->getID() === AIR and ($d->getID() === FARMLAND or $d->getID() === GRASS or $d->getID() === DIRT)){ - $level->setBlock($side, new PumpkinBlock(), true, false, true); - } + //TODO checkAlive + //if ( Level::getRawBrightness(a2, a3, a4 + 1, a5) > 8 ) TODO - skylight + + $growSpeed = static::getGrowthSpeed($level, $x, $y, $z); + $rand = mt_rand(0, (int)(25/$growSpeed)); + if($rand != 0) return; + + $meta = $level->level->getBlockDamage($x, $y, $z); + if($meta <= 6){ + $level->fastSetBlockUpdateMeta($x, $y, $z, $meta+1); + return; + } + + $xn = $level->level->getBlockID($x-1, $y, $z); + $xp = $level->level->getBlockID($x+1, $y, $z); + $zn = $level->level->getBlockID($x, $y, $z-1); + $zp = $level->level->getBlockID($x, $y, $z+1); + if($xn != PUMPKIN && $xp != PUMPKIN && $zn != PUMPKIN && $zp != PUMPKIN){ + $below = $level->level->getBlockID($x-1, $y-1, $z); + if($level->level->getBlockID($x-1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x-1, $y, $z, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x+1, $y-1, $z); + if($level->level->getBlockID($x+1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x+1, $y, $z, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z-1); + if($level->level->getBlockID($x, $y, $z-1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z-1, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z+1); + if($level->level->getBlockID($x, $y, $z+1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z+1, PUMPKIN, 0); + return; } } } @@ -67,14 +111,8 @@ class PumpkinStemBlock extends FlowableBlock{ public function getDrops(Item $item, Player $player){ $drops = []; - if($this->meta >= 0x07){ - $drops[] = [PUMPKIN_SEEDS, 0, mt_rand(1, 2)]; - } - elseif($this->meta >= 0x01 and $this->meta <= 0x07){ - $drops[] = [PUMPKIN_SEEDS, 0, 1]; - } - else{ - $drops[] = [PUMPKIN_SEEDS, 0, 0]; + for($i = 0; $i < 3; ++$i){ + if(mt_rand(0, 15) <= $this->meta) $drops[] = [PUMPKIN_SEEDS, 0, 1]; } return $drops; } diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 2e56276c..af575251 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -45,6 +45,7 @@ class PMFLevel extends PMF{ @mkdir($dirname , 0755); } + $this->hasLight = str_repeat("\x00", 16*16); for($index = 0; $index < $cnt; ++$index){ $this->chunks[$index] = false; $this->chunkChange[$index] = false; From 30eae88ebb255952c2f88aaca84b6def6afbb46c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 26 Jun 2025 11:11:35 +0300 Subject: [PATCH 076/146] block update queue and entity data queue will be sent every tick if it contains any packets --- src/Player.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 9b60e5c9..33107ae6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1425,7 +1425,6 @@ class Player{ } if($this->sendingInventoryRequired){ - //console("inv resent"); if(($this->gamemode & 0x01) !== CREATIVE){ $hotbar = []; foreach($this->hotbar as $slot){ @@ -1481,6 +1480,14 @@ class Player{ if($this->bufferLen > 0){ $this->sendBuffer(); } + + if($this->blockUpdateQueueLength > 0){ + $this->sendBlockUpdateQueue(); + } + + if($this->entityDataQueueLength > 0){ + $this->sendEntityMovementUpdateQueue(); + } $limit = $time - 5; //max lag foreach($this->recoveryQueue as $count => $data){ From 52b09f1cf71201ef80e3cd3dca424a71549e35a5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 27 Jun 2025 02:21:15 +0300 Subject: [PATCH 077/146] fixed entity data packets not being added into always recover queue --- src/Player.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Player.php b/src/Player.php index 33107ae6..0460d378 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1532,6 +1532,7 @@ class Player{ public function sendEntityMovementUpdateQueue(){ if($this->entityDataQueueLength > 0 && $this->entityDataQueue instanceof RakNetPacket){ $this->entityDataQueue->seqNumber = $this->counter[0]++; + $this->packetAlwaysRecoverQueue[$this->entityDataQueue->seqNumber] = $this->entityDataQueue; $this->send($this->entityDataQueue); } $this->entityDataQueueLength = 0; From 1ea54bc95b7b24e3d5399c1ad8b005e4fdc8fc73 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 27 Jun 2025 02:37:34 +0300 Subject: [PATCH 078/146] fixed ping displaying incorrect ping --- src/Player.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Player.php b/src/Player.php index 0460d378..6bebe452 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1533,6 +1533,7 @@ class Player{ if($this->entityDataQueueLength > 0 && $this->entityDataQueue instanceof RakNetPacket){ $this->entityDataQueue->seqNumber = $this->counter[0]++; $this->packetAlwaysRecoverQueue[$this->entityDataQueue->seqNumber] = $this->entityDataQueue; + $this->entityDataQueue->sendtime = microtime(true); $this->send($this->entityDataQueue); } $this->entityDataQueueLength = 0; From 565a9420fda2883aed6c609b87f319aa464ba02d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 28 Jun 2025 22:32:47 +0300 Subject: [PATCH 079/146] local entity ids --- src/API/EntityAPI.php | 17 ++- src/Player.php | 165 +++++++++++++++++++------- src/entity/Creature.php | 1 + src/entity/Entity.php | 9 +- src/entity/Projectile.php | 2 + src/entity/object/Arrow.php | 1 + src/entity/object/FallingSand.php | 1 + src/entity/object/ItemEntity.php | 1 + src/entity/object/Minecart.php | 1 + src/entity/object/Painting.php | 1 + src/entity/object/PrimedTNT.php | 1 + src/network/protocol/ProtocolInfo.php | 29 ++++- src/world/Level.php | 1 + 13 files changed, 179 insertions(+), 51 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 4c0fe8a6..5b9d3983 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -184,7 +184,11 @@ class EntityAPI{ public function spawnToAll(Entity $e){ foreach($this->server->api->player->getAll($e->level) as $player){ - if($player->eid !== false and $player->eid !== $e->eid and $e->class !== ENTITY_PLAYER and $e instanceof Entity){ + if($player->eid != false and $player->eid != $e->eid and $e->class != ENTITY_PLAYER and $e instanceof Entity){ + if($e->closed !== false or ($player->level !== $e->level and $e->class !== ENTITY_PLAYER)){ + return false; + } + $e->spawn($player); } } @@ -196,6 +200,7 @@ class EntityAPI{ public function remove($eid){ if(isset($this->entities[$eid])){ + $e = $this->entities[$eid]; $level = $this->entities[$eid]->level; $this->entities[$eid]->closed = true; if($level instanceof Level){ @@ -213,11 +218,17 @@ class EntityAPI{ $pk = new RemovePlayerPacket; $pk->eid = $eid; $pk->clientID = 0; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll(), $pk); + foreach($this->server->api->player->getAll() as $player){ + $player->entityQueueDataPacket(clone $pk); + $player->removeEntity($e); + } }else{ $pk = new RemoveEntityPacket; $pk->eid = $eid; - $this->server->api->player->broadcastPacket($this->entities[$eid]->level->players, $pk); + foreach($this->entities[$eid]->level->players as $player){ + $player->entityQueueDataPacket(clone $pk); + $player->removeEntity($e); + } } $this->server->api->dhandle("entity.remove", $this->entities[$eid]); unset($this->entities[$eid]->level->entityList[$eid]); diff --git a/src/Player.php b/src/Player.php index 6bebe452..83b4e065 100755 --- a/src/Player.php +++ b/src/Player.php @@ -125,6 +125,24 @@ class Player{ private $lastPing = -1; + /** + * Stores local entity ids. Format: global => local. + * Should be modified only by Player::addEntity or Player::removeEntity. + * @var array + */ + public $global2localEID = []; + /** + * Stores global entity ids. Format: local => global. + * Should be modified only by Player::addEntity or Player::removeEntity. + * @var array + */ + public $local2GlobalEID = []; + /** + * Stores last local entity id. Increments every time Player::addEntity is called. + * @var array + */ + public $lastLocalEID = -1; + /** * @param integer $clientID * @param string $ip @@ -168,6 +186,37 @@ class Player{ console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); } + /** + * Gives a new local id for entity. + * @param Entity $e + * @return boolean + */ + public function addEntity(Entity $e){ + $local = ++$this->lastLocalEID; + $this->global2localEID[$e->eid] = $local; + $this->local2GlobalEID[$local] = $e->eid; + ConsoleAPI::info("Adding Entity {$e->eid} => {$local}"); + return true; + } + + public function hasEntity(Entity $e){ + return isset($this->global2localEID[$e->eid]); + } + + /** + * Removes an entity. + * @param Entity $e + * @return boolean + */ + public function removeEntity(Entity $e){ + $local = $this->global2localEID[$e->eid] ?? null; + if($local == null) return false; + unset($this->global2localEID[$e->eid]); + unset($this->local2GlobalEID[$local]); + ConsoleAPI::info("Removing Entity {$e->eid} => {$local}"); + return true; + } + public function __get($name){ if(isset($this->{$name})){ return ($this->{$name}); @@ -289,14 +338,10 @@ class Player{ $this->entityQueueDataPacket($pk); }else{ - $pk = new MoveEntityPacket_PosRot(); + $pk = new RemoveEntityPacket(); $pk->eid = $e->eid; - $pk->x = -256; - $pk->y = 128; - $pk->z = -256; - $pk->yaw = 0; - $pk->pitch = 0; $this->entityQueueDataPacket($pk); + $this->removeEntity($e); } } } @@ -314,14 +359,7 @@ class Player{ foreach($this->level->entityList as $e){ if($e->eid !== $this->entity->eid){ if(!$e->isPlayer()){ - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $e->eid; - $pk->x = $e->x; - $pk->y = $e->y; - $pk->z = $e->z; - $pk->yaw = $e->yaw; - $pk->pitch = $e->pitch; - $this->entityQueueDataPacket($pk); + $e->spawn($this); } } } @@ -383,14 +421,14 @@ class Player{ } $pk = new SetEntityMotionPacket(); - $pk->eid = 0; + $pk->eid = $this->eid; $pk->speedX = 0; $pk->speedY = 0; $pk->speedZ = 0; $this->entityQueueDataPacket($pk); $pk = new MovePlayerPacket; - $pk->eid = 0; + $pk->eid = $this->eid; $pk->x = $pos->x; $pk->y = $pos->y; $pk->z = $pos->z; @@ -410,6 +448,7 @@ class Player{ public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return []; + if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); @@ -431,6 +470,7 @@ class Player{ public function dataPacket(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return; + if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; $packet->encode(); $len = strlen($packet->buffer) + 1; @@ -457,6 +497,7 @@ class Player{ public function dataPacketAlwaysRecover(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; + if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -467,7 +508,7 @@ class Player{ $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; if($len > $MTU){ - return $this->sendBigPacketAlwaysRecover($packet); + return $this->sendBigPacketAlwaysRecover($packet); //TODO fix } $packet->messageIndex = $this->counter[3]++; @@ -557,6 +598,7 @@ class Player{ public function sendBigPacketAlwaysRecover(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); $sendtime = microtime(true); @@ -1026,9 +1068,8 @@ class Player{ break; case "player.pickup": if($data["eid"] === $this->eid){ - $data["eid"] = 0; $pk = new TakeItemEntityPacket; - $pk->eid = 0; + $pk->eid = $data["eid"]; $pk->target = $data["entity"]->eid; $this->entityQueueDataPacket($pk); @@ -1068,7 +1109,7 @@ class Player{ break; case "entity.motion": - if($data->eid === $this->eid or $data->level !== $this->level){ + if($data->eid === $this->eid || $data->level !== $this->level){ break; } if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)){ @@ -1088,11 +1129,7 @@ class Player{ $this->entityQueueDataPacket($pk); break; case "entity.metadata": - if($data->eid === $this->eid){ - $eid = 0; - }else{ - $eid = $data->eid; - } + $eid = $data->eid; if($data->level === $this->level){ $pk = new SetEntityDataPacket; $pk->eid = $eid; @@ -1101,11 +1138,7 @@ class Player{ } break; case "entity.event": - if($data["entity"]->eid === $this->eid){ - $eid = 0; - }else{ - $eid = $data["entity"]->eid; - } + $eid = $data["entity"]->eid; if($data["entity"]->level === $this->level){ $pk = new EntityEventPacket; $pk->eid = $eid; @@ -1561,6 +1594,7 @@ class Player{ public function blockQueueDataPacket(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); @@ -1580,14 +1614,49 @@ class Player{ $this->blockUpdateQueueLength += 10 + $len; } + public function convertToGlobalEIDPacket(RakNetDataPacket $pk){ + $local = $pk->eid; + $pk->eid = $this->local2GlobalEID[$local] ?? false; + if($pk->eid === false){ + PRINT_STACK_TRACE: + ConsoleAPI::warn("Attempting to convert eids to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + return false; + } + if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ + $local = $pk->target; + $pk->target = $this->local2GlobalEID[$local] ?? false; + if($pk->target === false){ + ConsoleAPI::warn("Attempting to convert target eid to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + return false; + } + } + return true; + } + public function convertToLocalEIDPacket(RakNetDataPacket $pk){ + if(!isset($pk->_localeid) || !$pk->_localeid){ + $global = $pk->eid; + $pk->eid = $this->global2localEID[$global] ?? false; + $pk->_localeid = true; + if($pk->eid === false){ + ConsoleAPI::warn("Attempting to convert eids to local in entity packet {$pk->pid()} but local eid is incorrect! (Global: $global, Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + return false; + } + return true; + } + } + /** * Sends a packet in entity order channel. Packets are always delivered and ordered. - * + * @param RakNetDataPacket $pk + * @return void|boolean|boolean|number[] */ public function entityQueueDataPacket(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; - + if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); $len = 1 + strlen($pk->buffer); @@ -1599,6 +1668,7 @@ class Player{ } $pk->messageIndex = $this->counter[3]++; + $pk->reliability = 3; $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; $pk->orderIndex = $this->entityDataQueueOrderIndex++; @@ -1770,6 +1840,7 @@ class Player{ public function sendChatMessagePacket(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; + if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; //TODO big chat messages support $packet->encode(); @@ -1821,6 +1892,7 @@ class Player{ if($this->connected === false){ return; } + if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToGlobalEIDPacket($packet)) return; if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ return; @@ -1962,16 +2034,6 @@ class Player{ $pk = new LoginStatusPacket; $pk->status = 0; $this->dataPacket($pk); - - $pk = new StartGamePacket; - $pk->seed = $this->level->getSeed(); - $pk->x = $this->data->get("position")["x"]; - $pk->y = ceil($this->data->get("position")["y"])+1; - $pk->z = $this->data->get("position")["z"]; - $pk->generator = 0; - $pk->gamemode = $this->gamemode & 0x01; - $pk->eid = 0; - $this->dataPacket($pk); if(($this->gamemode & 0x01) === 0x01){ $this->slot = 0; @@ -2002,9 +2064,24 @@ class Player{ $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, ["player" => $this]); $this->eid = $this->entity->eid; $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); + + $this->addEntity($this->entity); + + $pk = new StartGamePacket; + $pk->seed = $this->level->getSeed(); + $pk->x = $this->data->get("position")["x"]; + $pk->y = ceil($this->data->get("position")["y"])+1; + $pk->z = $this->data->get("position")["z"]; + $pk->generator = 0; + $pk->gamemode = $this->gamemode & 0x01; + $pk->eid = $this->entity->eid; + $this->entity->x = $pk->x; $this->entity->y = $pk->y-0.9; $this->entity->z = $pk->z; + $this->dataPacket($pk); + + if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); @@ -2032,7 +2109,7 @@ class Player{ $this->server->schedule(50, [$this, "measureLag"], [], true); - console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ") MTU: {$this->MTU}"); + console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/{$this->ip}:{$this->port}] logged in with entity id {$this->eid} at ({$this->entity->level->getName()}, " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ") MTU: {$this->MTU}"); break; case ProtocolInfo::READY_PACKET: if($this->loggedIn === false){ @@ -2566,7 +2643,7 @@ class Player{ $foodHeal = Item::getFoodHeal($slot->getID()); if($this->entity->getHealth() < 20 && $foodHeal != 0){ $pk = new EntityEventPacket; - $pk->eid = 0; + $pk->eid = $this->eid; $pk->event = EntityEventPacket::ENTITY_COMPLETE_USING_ITEM; $this->entityQueueDataPacket($pk); diff --git a/src/entity/Creature.php b/src/entity/Creature.php index a3254b6a..f0e1e28d 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -60,6 +60,7 @@ abstract class Creature extends Living{ if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } + $player->addEntity($this); $pk = new AddMobPacket; $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index f13e36b5..c1107ac5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1150,6 +1150,9 @@ class Entity extends Position if($this->player->gamemode === SPECTATOR){ break; } + + $player->addEntity($this); + $pk = new AddPlayerPacket(); $pk->clientID = 0; // $this->player->clientID; $pk->username = $this->player->username; @@ -1162,8 +1165,8 @@ class Entity extends Position $pk->itemID = 0; $pk->itemAuxValue = 0; $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk, 2, true); - + $player->entityQueueDataPacket($pk); + $pk = new PlayerEquipmentPacket(); $pk->eid = $this->eid; $pk->item = $this->player->getSlot($this->player->slot)->getID(); @@ -1517,7 +1520,7 @@ class Entity extends Position public function sendMoveUpdate() { if($this->class === ENTITY_PLAYER){ - $this->player->teleport(new Vector3($this->x, $this->y, $this->z), false, false, true, false); + //$this->player->teleport(new Vector3($this->x, $this->y, $this->z), false, false, true, false); } } diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index 7042daa1..e68ff77e 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -201,6 +201,8 @@ abstract class Projectile extends Entity{ public function spawn($player) { + $player->addEntity($this); + $pk = new AddEntityPacket(); $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index f55a49cd..5915c4cf 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -262,6 +262,7 @@ class Arrow extends Entity{ public function spawn($player){ if($this->type === OBJECT_ARROW){ + $player->addEntity($this); $pk = new AddEntityPacket; $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 8481830e..41c59d8a 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -21,6 +21,7 @@ class FallingSand extends Entity{ } public function spawn($player) { + $player->addEntity($this); $pk = new AddEntityPacket(); $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index e24726fe..43d42808 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -44,6 +44,7 @@ class ItemEntity extends Entity{ public function spawn($player) { + $player->addEntity($this); $pk = new AddItemEntityPacket(); $pk->eid = $this->eid; $pk->x = $this->x; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 326e6adc..cdd29dbb 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -427,6 +427,7 @@ class Minecart extends Vehicle{ } public function spawn($player){ + $player->addEntity($this); $pk = new AddEntityPacket; $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 20bb25e9..8c2794eb 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -198,6 +198,7 @@ class Painting extends Entity{ } public function spawn($player){ + $player->addEntity($this); $pk = new AddPaintingPacket; $pk->eid = $this->eid; $pk->x = (int) $this->xPos; diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 2585e917..0544eb69 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -66,6 +66,7 @@ class PrimedTNT extends Entity{ } public function spawn($player){ + $player->addEntity($this); $pk = new AddEntityPacket; $pk->eid = $this->eid; $pk->type = $this->type; diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index fc6c6b8f..118975fa 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -70,7 +70,34 @@ abstract class ProtocolInfo{ const ADVENTURE_SETTINGS_PACKET = 0xb7; const ENTITY_DATA_PACKET = 0xb8; const PLAYER_INPUT_PACKET = 0xb9; - + + const EID_PACKETS = [ + self::ADD_MOB_PACKET => true, + self::ADD_ITEM_ENTITY_PACKET => true, + self::RESPAWN_PACKET => true, + self::SET_ENTITY_MOTION_PACKET => true, + self::USE_ITEM_PACKET => true, + self::SET_ENTITY_DATA_PACKET => true, + self::PLAYER_ARMOR_EQUIPMENT_PACKET => true, + self::ROTATE_HEAD_PACKET => true, + self::PLAYER_EQUIPMENT_PACKET => true, + self::SEND_INVENTORY_PACKET => true, + self::INTERACT_PACKET => true, + self::ENTITY_EVENT_PACKET => true, + self::PLAYER_ACTION_PACKET => true, + self::REMOVE_ENTITY_PACKET => true, + self::ANIMATE_PACKET => true, + self::ADD_PLAYER_PACKET => true, + self::TAKE_ITEM_ENTITY_PACKET => true, + self::START_GAME_PACKET => true, + self::ADD_PAINTING_PACKET => true, + self::REMOVE_PLAYER_PACKET => true, + self::MOVE_ENTITY_PACKET_POSROT => true, + self::ADD_ENTITY_PACKET => true, + self::REMOVE_BLOCK_PACKET => true, + self::MOVE_PLAYER_PACKET => true, + self::DROP_ITEM_PACKET => true + ]; } /*Unused: * 0xb5 diff --git a/src/world/Level.php b/src/world/Level.php index ed779bb2..d3729af5 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -817,6 +817,7 @@ class Level{ foreach($this->players as $player){ + if(!$player->spawned) continue; foreach($post as $eid){ $e = $this->entityList[$eid] ?? false; if(!($e instanceof Entity)){ From f9646e7e897b7da18d68a91eb3387b6f38f74609 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 Jun 2025 00:10:13 +0300 Subject: [PATCH 080/146] movement queue readded + entity movement is now unreliable --- src/Player.php | 75 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index 83b4e065..76ece8c3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -88,6 +88,10 @@ class Player{ public $slotCount = 7; public $bedPosition = null; + public $entityMovementQueue; + public $entityMovementQueueLength = 0; + public $entityMovementQueueOrderIndex = 0; + /** * @var RakNetPacket */ @@ -174,6 +178,9 @@ class Player{ $this->entityDataQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->entityDataQueue->data = []; + $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityMovementQueue->data = []; + $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; @@ -195,7 +202,7 @@ class Player{ $local = ++$this->lastLocalEID; $this->global2localEID[$e->eid] = $local; $this->local2GlobalEID[$local] = $e->eid; - ConsoleAPI::info("Adding Entity {$e->eid} => {$local}"); + //ConsoleAPI::info("Adding Entity {$e->eid} => {$local}"); return true; } @@ -213,7 +220,7 @@ class Player{ if($local == null) return false; unset($this->global2localEID[$e->eid]); unset($this->local2GlobalEID[$local]); - ConsoleAPI::info("Removing Entity {$e->eid} => {$local}"); + //ConsoleAPI::info("Removing Entity {$e->eid} => {$local}"); return true; } @@ -1519,6 +1526,10 @@ class Player{ } if($this->entityDataQueueLength > 0){ + $this->sendEntityDataQueue(); + } + + if($this->entityMovementQueueLength > 0){ $this->sendEntityMovementUpdateQueue(); } @@ -1563,6 +1574,17 @@ class Player{ } public function sendEntityMovementUpdateQueue(){ + if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){ + $this->entityMovementQueue->seqNumber = $this->counter[0]++; + $this->entityMovementQueue->sendtime = microtime(true); + $this->send($this->entityMovementQueue); + } + $this->entityMovementQueueLength = 0; + $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityMovementQueue->data = []; + } + + public function sendEntityDataQueue(){ if($this->entityDataQueueLength > 0 && $this->entityDataQueue instanceof RakNetPacket){ $this->entityDataQueue->seqNumber = $this->counter[0]++; $this->packetAlwaysRecoverQueue[$this->entityDataQueue->seqNumber] = $this->entityDataQueue; @@ -1664,7 +1686,7 @@ class Player{ if($len > $MTU) return $this->entityQueueDataPacket_big($pk); if(($this->entityDataQueueLength + $len) >= $MTU){ - $this->sendEntityMovementUpdateQueue(); + $this->sendEntityDataQueue(); } $pk->messageIndex = $this->counter[3]++; @@ -1702,6 +1724,11 @@ class Player{ } public function addEntityMovementUpdateToQueue(Entity $e){ + $len = 0; + $packets = 0; + $motionSent = false; + $moveSent = false; + $headSent = false; $svdYSpeed = $e->speedY; if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; @@ -1713,7 +1740,10 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; - $this->entityQueueDataPacket($motion); + $motion->encode(); + $len += 1 + strlen($motion->buffer); + ++$packets; + $motionSent = true; } } if($e->modifySpeedY){ @@ -1731,7 +1761,7 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; - $this->entityQueueDataPacket($move); + $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; @@ -1740,14 +1770,45 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; - $this->entityQueueDataPacket($move); + $move->encode(); } + + $len += strlen($move->buffer) + 1; + ++$packets; + $moveSent = true; }else if($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; - $this->entityQueueDataPacket($headyaw); + $headyaw->encode(); + $len += strlen($headyaw->buffer) + 1; + ++$packets; + $headSent = true; } + if($packets <= 0) return; + //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); + $MTU = $this->MTU - 24; + if(($this->entityMovementQueueLength + $len) >= $MTU){ + $this->sendEntityMovementUpdateQueue(); + } + if($motionSent){ + $motion->messageIndex = 0; //force 0 cuz reliability 0 + $motion->reliability = 0; + $this->entityMovementQueue->data[] = $motion; + } + if($moveSent){ + $move->messageIndex = 0; + $move->reliability = 0; + $this->entityMovementQueue->data[] = $move; + } + if($headSent){ + $headyaw->messageIndex = 0; + $headyaw->reliability = 0; + $this->entityMovementQueue->data[] = $headyaw; + } + + $this->entityMovementQueueLength += 6*$packets + $len; + $this->entityMovementPacketsPerSecond += $packets; } /** From 62819f8b12d1f363ac8f761efc3215c8381cf221 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 Jun 2025 00:22:56 +0300 Subject: [PATCH 081/146] fixes --- src/Player.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Player.php b/src/Player.php index 76ece8c3..cefe1f21 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1729,6 +1729,13 @@ class Player{ $motionSent = false; $moveSent = false; $headSent = false; + $localeid = $this->global2localEID[$e->eid] ?? false; + if($localeid === false){ + ConsoleAPI::warn("Attempting to convert global eid to local failed! (Global: {$e->eid}, Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + return false; + } + $svdYSpeed = $e->speedY; if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; @@ -1736,7 +1743,7 @@ class Player{ if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ $motion = new SetEntityMotionPacket(); - $motion->eid = $e->eid; + $motion->eid = $localeid; $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; @@ -1754,7 +1761,7 @@ class Player{ if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ if($e->headYaw != $e->lastHeadYaw){ $move = new MovePlayerPacket(); - $move->eid = $e->eid; + $move->eid = $localeid; $move->x = $e->x; $move->y = $e->y + $e->yOffset; $move->z = $e->z; @@ -1764,7 +1771,7 @@ class Player{ $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); - $move->eid = $e->eid; + $move->eid = $localeid; $move->x = $e->x; $move->y = $e->y + $e->yOffset; $move->z = $e->z; @@ -1778,7 +1785,7 @@ class Player{ $moveSent = true; }else if($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); - $headyaw->eid = $e->eid; + $headyaw->eid = $localeid; $headyaw->yaw = $e->headYaw; $headyaw->encode(); $len += strlen($headyaw->buffer) + 1; From 21f8796e8963b1d389a961069e9a02c411e5cc45 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 Jun 2025 03:12:24 +0300 Subject: [PATCH 082/146] better(?) way to handle eid translation, local eid 0 is not used now, some additional fixes --- src/Player.php | 43 ++++++------------- src/entity/Entity.php | 12 ++---- src/network/protocol/ProtocolInfo.php | 3 +- .../protocol/packet/AddEntityPacket.php | 9 ++++ .../protocol/packet/AddItemEntityPacket.php | 9 +++- src/network/protocol/packet/AddMobPacket.php | 10 ++++- .../protocol/packet/AddPaintingPacket.php | 10 ++++- .../protocol/packet/AddPlayerPacket.php | 10 ++++- src/network/protocol/packet/AnimatePacket.php | 17 ++++++++ .../protocol/packet/DropItemPacket.php | 10 ++++- .../protocol/packet/EntityEventPacket.php | 18 +++++++- .../protocol/packet/InteractPacket.php | 21 ++++++++- .../packet/MoveEntityPacket_PosRot.php | 16 +++++++ .../protocol/packet/MovePlayerPacket.php | 16 +++++++ .../protocol/packet/PlayerActionPacket.php | 10 ++++- .../packet/PlayerArmorEquipmentPacket.php | 19 +++++++- .../protocol/packet/PlayerEquipmentPacket.php | 19 +++++++- .../protocol/packet/RemoveBlockPacket.php | 8 ++++ .../protocol/packet/RemoveEntityPacket.php | 10 ++++- .../protocol/packet/RemovePlayerPacket.php | 8 ++++ src/network/protocol/packet/RespawnPacket.php | 9 +++- .../protocol/packet/RotateHeadPacket.php | 17 ++++++++ .../protocol/packet/SendInventoryPacket.php | 18 ++++++++ .../protocol/packet/SetEntityDataPacket.php | 8 ++++ .../protocol/packet/SetEntityLinkPacket.php | 19 ++++++++ .../protocol/packet/SetEntityMotionPacket.php | 9 +++- .../protocol/packet/StartGamePacket.php | 10 ++++- .../protocol/packet/TakeItemEntityPacket.php | 10 +++++ src/network/protocol/packet/UseItemPacket.php | 8 ++++ src/network/raknet/RakNetDataPacket.php | 12 +++++- src/network/raknet/RakNetParser.php | 1 + src/world/Level.php | 2 +- 32 files changed, 345 insertions(+), 56 deletions(-) diff --git a/src/Player.php b/src/Player.php index cefe1f21..5b99f7a3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -132,20 +132,21 @@ class Player{ /** * Stores local entity ids. Format: global => local. * Should be modified only by Player::addEntity or Player::removeEntity. + * Entity ID 0 is reserved for non-existent entities. * @var array */ - public $global2localEID = []; + public $global2localEID = [0 => 0]; /** * Stores global entity ids. Format: local => global. * Should be modified only by Player::addEntity or Player::removeEntity. * @var array */ - public $local2GlobalEID = []; + public $local2GlobalEID = [0 => 0]; /** * Stores last local entity id. Increments every time Player::addEntity is called. * @var array */ - public $lastLocalEID = -1; + public $lastLocalEID = 0; /** * @param integer $clientID @@ -1022,8 +1023,8 @@ class Player{ switch($event){ case "entity.link": $pk = new SetEntityLinkPacket(); - $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; - $pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"]; + $pk->rider = $data["rider"]; + $pk->riding = $data["riding"]; $pk->type = 0; $this->entityQueueDataPacket($pk); break; @@ -1136,6 +1137,7 @@ class Player{ $this->entityQueueDataPacket($pk); break; case "entity.metadata": + if(!$this->hasEntity($data)) break; $eid = $data->eid; if($data->level === $this->level){ $pk = new SetEntityDataPacket; @@ -1637,37 +1639,20 @@ class Player{ } public function convertToGlobalEIDPacket(RakNetDataPacket $pk){ - $local = $pk->eid; - $pk->eid = $this->local2GlobalEID[$local] ?? false; - if($pk->eid === false){ - PRINT_STACK_TRACE: - ConsoleAPI::warn("Attempting to convert eids to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: "); + if(!$pk->eidsToGlobal($this)){ + ConsoleAPI::warn("Failed to convert eids to global in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); return false; } - if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ - $local = $pk->target; - $pk->target = $this->local2GlobalEID[$local] ?? false; - if($pk->target === false){ - ConsoleAPI::warn("Attempting to convert target eid to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: "); - foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); - return false; - } - } return true; } public function convertToLocalEIDPacket(RakNetDataPacket $pk){ - if(!isset($pk->_localeid) || !$pk->_localeid){ - $global = $pk->eid; - $pk->eid = $this->global2localEID[$global] ?? false; - $pk->_localeid = true; - if($pk->eid === false){ - ConsoleAPI::warn("Attempting to convert eids to local in entity packet {$pk->pid()} but local eid is incorrect! (Global: $global, Player: {$this->ip}:{$this->port}). Stacktrace: "); - foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); - return false; - } - return true; + if(!$pk->eidsToLocal($this)){ + ConsoleAPI::warn("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + return false; } + return true; } /** diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c1107ac5..975fb90c 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1466,8 +1466,8 @@ class Entity extends Position if(isset($this->level->entityList[$this->linkedEntity])){ $e = $this->level->entityList[$this->linkedEntity]; if(!$e->dead && !$e->closed){ - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => -1, "type" => 1]); - $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => -1, "type" => 1]); + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => 0, "type" => 1]); + $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => 0, "type" => 1]); $this->isRider = false; $this->linkedEntity = 0; $e->linkedEntity = 0; @@ -1602,13 +1602,7 @@ class Entity extends Position $pk->eid = $this->eid; $pk->event = EntityEventPacket::ENTITY_DAMAGE; foreach($this->level->players as $p){ - if(($p->entity instanceof Entity) && $p->entity->eid == $this->eid){ - $pk2 = clone $pk; - $pk2->eid = 0; - $p->entityQueueDataPacket($pk2); - }else{ - $p->entityQueueDataPacket(clone $pk); - } + $p->entityQueueDataPacket(clone $pk); } } diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 118975fa..c2e9ebfb 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -96,7 +96,8 @@ abstract class ProtocolInfo{ self::ADD_ENTITY_PACKET => true, self::REMOVE_BLOCK_PACKET => true, self::MOVE_PLAYER_PACKET => true, - self::DROP_ITEM_PACKET => true + self::DROP_ITEM_PACKET => true, + self::SET_ENTITY_LINK_PACKET => true ]; } /*Unused: diff --git a/src/network/protocol/packet/AddEntityPacket.php b/src/network/protocol/packet/AddEntityPacket.php index ac2c1a75..36d7848b 100644 --- a/src/network/protocol/packet/AddEntityPacket.php +++ b/src/network/protocol/packet/AddEntityPacket.php @@ -33,5 +33,14 @@ class AddEntityPacket extends RakNetDataPacket{ $this->putShort((int)($this->speedZ * 8000)); } } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index fb68f60a..4c250c8d 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -29,5 +29,12 @@ class AddItemEntityPacket extends RakNetDataPacket{ $this->putByte($this->pitch); $this->putByte($this->roll); } - + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddMobPacket.php b/src/network/protocol/packet/AddMobPacket.php index 5b2d9a90..56620be3 100644 --- a/src/network/protocol/packet/AddMobPacket.php +++ b/src/network/protocol/packet/AddMobPacket.php @@ -29,5 +29,13 @@ class AddMobPacket extends RakNetDataPacket{ $this->putByte($this->pitch); $this->put(Utils::writeMetadata($this->metadata)); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddPaintingPacket.php b/src/network/protocol/packet/AddPaintingPacket.php index bd50ab0a..60337c46 100644 --- a/src/network/protocol/packet/AddPaintingPacket.php +++ b/src/network/protocol/packet/AddPaintingPacket.php @@ -25,5 +25,13 @@ class AddPaintingPacket extends RakNetDataPacket{ $this->putInt($this->direction); $this->putString($this->title); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddPlayerPacket.php b/src/network/protocol/packet/AddPlayerPacket.php index 044f119a..166a27e1 100644 --- a/src/network/protocol/packet/AddPlayerPacket.php +++ b/src/network/protocol/packet/AddPlayerPacket.php @@ -46,5 +46,13 @@ class AddPlayerPacket extends RakNetDataPacket{ $this->putShort($this->itemAuxValue); //Example: bow shooting power $this->put(Utils::writeMetadata($this->metadata)); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index a8ea6820..e8ce1225 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -22,4 +22,21 @@ class AnimatePacket extends RakNetDataPacket{ $this->putInt($this->eid); } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 41531160..30c487bf 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -18,5 +18,13 @@ class DropItemPacket extends RakNetDataPacket{ public function encode(){ } - + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 44e9b06a..daa00929 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -31,5 +31,21 @@ class EntityEventPacket extends RakNetDataPacket{ $this->putInt($this->eid); $this->putByte($this->event); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + } } \ No newline at end of file diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 2d33773d..dcc7f53c 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -24,5 +24,24 @@ class InteractPacket extends RakNetDataPacket{ $this->putInt($this->eid); $this->putInt($this->target); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + $this->target = $p->global2localEID[$this->target] ?? false; + if($this->eid === false || $this->target === false) return false; + } + return true; + } + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + $this->target = $p->local2GlobalEID[$this->target] ?? false; + if($this->eid === false || $this->target === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index c2143c18..02930e1f 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -32,4 +32,20 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ $this->putFloat($this->pitch); } + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 5325cf76..dc234b51 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -34,4 +34,20 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->putFloat($this->bodyYaw); } + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index cdcc64ad..743bf674 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -24,5 +24,13 @@ class PlayerActionPacket extends RakNetDataPacket{ public function encode(){ } - + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php index d32be316..c867232c 100644 --- a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php @@ -24,5 +24,22 @@ class PlayerArmorEquipmentPacket extends RakNetDataPacket{ $this->putByte($this->slots[2]); $this->putByte($this->slots[3]); } - + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 06e65fee..62af566e 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -24,5 +24,22 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ $this->putShort($this->meta); $this->putByte($this->slot); } - + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index d3e3a969..c314c20b 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -21,4 +21,12 @@ class RemoveBlockPacket extends RakNetDataPacket{ } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index 27d6c42b..fded3c76 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -15,5 +15,13 @@ class RemoveEntityPacket extends RakNetDataPacket{ $this->reset(); $this->putInt($this->eid); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemovePlayerPacket.php b/src/network/protocol/packet/RemovePlayerPacket.php index f23cd4df..4847aff6 100644 --- a/src/network/protocol/packet/RemovePlayerPacket.php +++ b/src/network/protocol/packet/RemovePlayerPacket.php @@ -18,4 +18,12 @@ class RemovePlayerPacket extends RakNetDataPacket{ $this->putLong($this->clientID); } + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 1d3c208e..7fc4fad6 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -24,5 +24,12 @@ class RespawnPacket extends RakNetDataPacket{ $this->putFloat($this->y); $this->putFloat($this->z); } - + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 21cc7cf0..78157858 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -26,4 +26,21 @@ class RotateHeadPacket extends RakNetDataPacket{ $this->putByte($this->rawYaw ? $this->yaw : ($this->yaw / 360 / 0.0039062)); //wraps 360 angle to 0xff } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 1bddca97..375b7032 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -38,5 +38,23 @@ class SendInventoryPacket extends RakNetDataPacket{ } } } + + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 8626885d..c30f35c9 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -17,5 +17,13 @@ class SetEntityDataPacket extends RakNetDataPacket{ $this->putInt($this->eid); $this->put(Utils::writeMetadata($this->metadata)); } + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 5dca8516..e01fa40a 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -28,4 +28,23 @@ class SetEntityLinkPacket extends RakNetDataPacket{ return ProtocolInfo::SET_ENTITY_LINK_PACKET; } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->rider = $p->local2GlobalEID[$this->rider] ?? false; + $this->riding = $p->local2GlobalEID[$this->riding] ?? false; + if($this->rider === false || $this->riding === false) return false; + } + return true; + } + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->rider = $p->global2localEID[$this->rider] ?? false; + $this->riding = $p->global2localEID[$this->riding] ?? false; + if($this->rider === false || $this->riding === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index e4cf70b9..bc8ff9a1 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -21,5 +21,12 @@ class SetEntityMotionPacket extends RakNetDataPacket{ $this->putShort((int) ($this->speedY * 8000)); $this->putShort((int) ($this->speedZ * 8000)); } - + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/StartGamePacket.php b/src/network/protocol/packet/StartGamePacket.php index a746ea3a..293a1b23 100644 --- a/src/network/protocol/packet/StartGamePacket.php +++ b/src/network/protocol/packet/StartGamePacket.php @@ -27,5 +27,13 @@ class StartGamePacket extends RakNetDataPacket{ $this->putFloat($this->y); $this->putFloat($this->z); } - + + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 85221c38..c64095d9 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -18,4 +18,14 @@ class TakeItemEntityPacket extends RakNetDataPacket{ $this->putInt($this->eid); } + //TODO convert + public function eidsToLocal(Player $p){ + if(!$this->localEids){ + $this->localEids = true; + $this->eid = $p->global2localEID[$this->eid] ?? false; + $this->target = $p->global2localEID[$this->eid] ?? false; + if($this->eid === false || $this->target === false) return false; + } + return true; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 32e2c651..6c2875da 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -38,5 +38,13 @@ class UseItemPacket extends RakNetDataPacket{ public function encode(){ } + public function eidsToGlobal(Player $p){ + if($this->localEids){ + $this->localEids = false; + $this->eid = $p->local2GlobalEID[$this->eid] ?? false; + if($this->eid === false) return false; + } + return true; + } } diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 5f64ac47..25fd2e93 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -13,10 +13,18 @@ abstract class RakNetDataPacket extends stdClass{ public $splitIndex; private $offset = 0; - abstract public function encode(); + public abstract function encode(); - abstract public function decode(); + public abstract function decode(); + public $localEids = false; + public function eidsToLocal(Player $p){ + return true; + } + public function eidsToGlobal(Player $p){ + return true; + } + public function getBuffer(){ return $this->buffer; } diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 764bf64f..900280a4 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -228,6 +228,7 @@ class RakNetParser{ $data->splitCount = $splitCount; $data->splitID = $splitID; $data->splitIndex = $splitIndex; + $data->localEids = true; $data->setBuffer($buf); } return $data; diff --git a/src/world/Level.php b/src/world/Level.php index d3729af5..b07569f1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -395,7 +395,7 @@ class Level{ $this->server->api->time->set("day", $this); } foreach($this->players as $p){ - $p->stopSleep(); + if($p->isSleeping) $p->stopSleep(); } } From 6d10cfd896988c71d10492ea34db75bced068579 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 Jun 2025 11:44:39 +0300 Subject: [PATCH 083/146] replaced more stuff with entity queue, added a limit to how many packets can be resent per tick --- src/Player.php | 11 +++++++++-- src/PocketMinecraftServer.php | 10 ++++++---- src/entity/Entity.php | 16 +++++++++++----- src/entity/ai/tasks/TaskEatTileGoal.php | 4 +++- src/entity/object/Arrow.php | 10 ---------- src/material/block/DoorBlock.php | 2 +- src/material/block/nonfull/FenceGateBlock.php | 2 +- 7 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5b99f7a3..63bfc393 100755 --- a/src/Player.php +++ b/src/Player.php @@ -433,7 +433,7 @@ class Player{ $pk->speedX = 0; $pk->speedY = 0; $pk->speedZ = 0; - $this->entityQueueDataPacket($pk); + $this->dataPacketAlwaysRecover($pk); $pk = new MovePlayerPacket; $pk->eid = $this->eid; @@ -443,7 +443,7 @@ class Player{ $pk->bodyYaw = $yaw; $pk->pitch = $pitch; $pk->yaw = $yaw; - $this->entityQueueDataPacket($pk); + $this->dataPacketAlwaysRecover($pk); } /** @@ -1439,6 +1439,10 @@ class Player{ } public function handlePacketQueues(){ + if($this->server->ticks % 40 == 0){ //2s + $this->sendPing(); + } + if($this->connected === false){ return false; } @@ -1552,7 +1556,9 @@ class Player{ } if(($resendCnt = count($this->resendQueue)) > 0){ + $maxResendCnt = $resendCnt > 25 ? 25 : $resendCnt; foreach($this->resendQueue as $count => $data){ + if(--$maxResendCnt) break; unset($this->resendQueue[$count]); $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; @@ -3296,6 +3302,7 @@ class Player{ break; case RakNetInfo::ACK: + //if(mt_rand(0, 99) > 50) break; foreach($packet->packets as $count){ if(isset($this->packetAlwaysRecoverQueue[$count])){ $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 2a0afd75..39a8523c 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -3,11 +3,16 @@ class PocketMinecraftServer{ public $tCnt, $ticks; - public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; + public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; /** * @var ServerAPI */ public $api; + /** + * @var Player[] + */ + public $clients; + private $serverip, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $memoryStats, $async = [], $asyncID = 0; public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; @@ -631,9 +636,6 @@ class PocketMinecraftServer{ foreach($this->clients as $client){ $client->handlePacketQueues(); - if($this->ticks % 40 == 0){ //2s - $client->sendPing(); - } } foreach($this->api->level->levels as $l){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 975fb90c..36104762 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -999,7 +999,9 @@ class Entity extends Position $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; $pk->bodyYaw = $this->yaw; - $this->server->api->player->broadcastPacket($players, $pk); + foreach($players as $player){ + if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk); + } } } } @@ -1602,14 +1604,14 @@ class Entity extends Position $pk->eid = $this->eid; $pk->event = EntityEventPacket::ENTITY_DAMAGE; foreach($this->level->players as $p){ - $p->entityQueueDataPacket(clone $pk); + if($p->hasEntity($this)) $p->entityQueueDataPacket(clone $pk); } } if($this->player instanceof Player){ $pk = new SetHealthPacket(); $pk->health = $this->health; - $this->player->entityQueueDataPacket($pk); + if($this->player->hasEntity($this)) $this->player->entityQueueDataPacket($pk); } if($this->health <= 0 and $this->dead === false){ $this->makeDead($cause); @@ -1646,12 +1648,16 @@ class Entity extends Position $pk->z = -256; $pk->yaw = 0; $pk->pitch = 0; - $this->server->api->player->broadcastPacket($this->level->players, $pk); + foreach($this->level->players as $player){ + if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk); + } }else{ $pk = new EntityEventPacket; $pk->eid = $this->eid; $pk->event = EntityEventPacket::ENTITY_DEAD; - $this->server->api->player->broadcastPacket($this->level->players, $pk); + foreach($this->level->players as $player){ + if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk); + } } if($this->player instanceof Player){ diff --git a/src/entity/ai/tasks/TaskEatTileGoal.php b/src/entity/ai/tasks/TaskEatTileGoal.php index 5af5d21e..8bc3766e 100644 --- a/src/entity/ai/tasks/TaskEatTileGoal.php +++ b/src/entity/ai/tasks/TaskEatTileGoal.php @@ -5,7 +5,9 @@ class TaskEatTileGoal extends TaskBase public function onStart(EntityAI $ai) { $this->selfCounter = 40; - $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, new EntityEventPacket($ai->entity->eid, EntityEventPacket::ENTITY_ANIM_10)); + foreach($ai->entity->level->players as $player){ + if($player->hasEntity($ai->entity)) $player->entityQueueDataPacket(new EntityEventPacket($ai->entity->eid, EntityEventPacket::ENTITY_ANIM_10)); + } } public function onEnd(EntityAI $ai) diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 5915c4cf..4b26a8a0 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -47,16 +47,6 @@ class Arrow extends Entity{ $data["shotByPlayer"] = $this->shotByPlayer; return $data; } - public function handleUpdate(){ - $pk = new MoveEntityPacket_PosRot; - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3 $f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2); diff --git a/src/material/block/DoorBlock.php b/src/material/block/DoorBlock.php index dfdc7bce..a1634cde 100644 --- a/src/material/block/DoorBlock.php +++ b/src/material/block/DoorBlock.php @@ -208,7 +208,7 @@ class DoorBlock extends TransparentBlock{ $pk->z = $this->z; $pk->evid = 1003; $pk->data = 0; - ServerAPI::request()->api->player->broadcastPacket($players, $pk); + ServerAPI::request()->api->player->broadcastPacket($players, $pk); //TODO use block queue? return true; } return false; diff --git a/src/material/block/nonfull/FenceGateBlock.php b/src/material/block/nonfull/FenceGateBlock.php index 977f346c..74d8c59e 100644 --- a/src/material/block/nonfull/FenceGateBlock.php +++ b/src/material/block/nonfull/FenceGateBlock.php @@ -75,7 +75,7 @@ class FenceGateBlock extends TransparentBlock{ $pk->z = $this->z; $pk->evid = 1003; $pk->data = 0; - ServerAPI::request()->api->player->broadcastPacket($players, $pk); + ServerAPI::request()->api->player->broadcastPacket($players, $pk); //TODO use blok queue? return true; } } From 385f15f1bb5a8cdcb95754883d63065a86eb20db Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 30 Jun 2025 07:45:19 +0300 Subject: [PATCH 084/146] fixed sequenced packets, entity movement uses unrealible_sequenced --- src/Player.php | 34 +++++++++++++++---------- src/network/raknet/RakNetCodec.php | 3 +++ src/network/raknet/RakNetDataPacket.php | 1 + src/network/raknet/RakNetParser.php | 7 +++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Player.php b/src/Player.php index 63bfc393..198df046 100755 --- a/src/Player.php +++ b/src/Player.php @@ -90,7 +90,7 @@ class Player{ public $entityMovementQueue; public $entityMovementQueueLength = 0; - public $entityMovementQueueOrderIndex = 0; + public $entityMovementQueueSeqIndex = 0; /** * @var RakNetPacket @@ -122,7 +122,6 @@ class Player{ * @var boolean */ public $sendingInventoryRequired = false; - public $expectedSetSlotPackets = []; public $expectedSetSlotIndex = -1; public $lastExpectedSetSlotIndexReceived = -1; @@ -1556,9 +1555,7 @@ class Player{ } if(($resendCnt = count($this->resendQueue)) > 0){ - $maxResendCnt = $resendCnt > 25 ? 25 : $resendCnt; foreach($this->resendQueue as $count => $data){ - if(--$maxResendCnt) break; unset($this->resendQueue[$count]); $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; @@ -1654,8 +1651,8 @@ class Player{ } public function convertToLocalEIDPacket(RakNetDataPacket $pk){ if(!$pk->eidsToLocal($this)){ - ConsoleAPI::warn("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); - foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + ConsoleAPI::debug("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); + foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::debug($s); return false; } return true; @@ -1722,7 +1719,7 @@ class Player{ $headSent = false; $localeid = $this->global2localEID[$e->eid] ?? false; if($localeid === false){ - ConsoleAPI::warn("Attempting to convert global eid to local failed! (Global: {$e->eid}, Player: {$this->ip}:{$this->port}). Stacktrace: "); + ConsoleAPI::warn("Attempt to convert global eid to local failed! (Global: {$e->eid}, Player: {$this->ip}:{$this->port}). Stacktrace: "); foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); return false; } @@ -1739,7 +1736,7 @@ class Player{ $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; $motion->encode(); - $len += 1 + strlen($motion->buffer); + $len += 10 + strlen($motion->buffer); ++$packets; $motionSent = true; } @@ -1771,7 +1768,7 @@ class Player{ $move->encode(); } - $len += strlen($move->buffer) + 1; + $len += strlen($move->buffer) + 10; ++$packets; $moveSent = true; }else if($e->headYaw != $e->lastHeadYaw){ @@ -1779,7 +1776,7 @@ class Player{ $headyaw->eid = $localeid; $headyaw->yaw = $e->headYaw; $headyaw->encode(); - $len += strlen($headyaw->buffer) + 1; + $len += strlen($headyaw->buffer) + 10; ++$packets; $headSent = true; } @@ -1791,21 +1788,30 @@ class Player{ } if($motionSent){ $motion->messageIndex = 0; //force 0 cuz reliability 0 - $motion->reliability = 0; + $motion->reliability = 1; + $motion->orderIndex = $this->entityDataQueueOrderIndex; + $motion->orderChannel = self::ENTITY_ORDER_CHANNEL; + $motion->seqIndex = $this->entityMovementQueueSeqIndex++; $this->entityMovementQueue->data[] = $motion; } if($moveSent){ $move->messageIndex = 0; - $move->reliability = 0; + $move->reliability = 1; + $move->orderIndex = $this->entityDataQueueOrderIndex; + $move->orderChannel = self::ENTITY_ORDER_CHANNEL; + $move->seqIndex = $this->entityMovementQueueSeqIndex++; $this->entityMovementQueue->data[] = $move; } if($headSent){ $headyaw->messageIndex = 0; - $headyaw->reliability = 0; + $headyaw->reliability = 1; + $headyaw->orderIndex = $this->entityDataQueueOrderIndex; + $headyaw->orderChannel = self::ENTITY_ORDER_CHANNEL; + $headyaw->seqIndex = $this->entityMovementQueueSeqIndex++; $this->entityMovementQueue->data[] = $headyaw; } - $this->entityMovementQueueLength += 6*$packets + $len; + $this->entityMovementQueueLength += $len; $this->entityMovementPacketsPerSecond += $packets; } diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php index bbd52589..b8e5b55b 100644 --- a/src/network/raknet/RakNetCodec.php +++ b/src/network/raknet/RakNetCodec.php @@ -106,6 +106,9 @@ class RakNetCodec{ or $pk->reliability === 7){ $packet->buffer .= Utils::writeLTriad($pk->messageIndex); } + if($pk->reliability == 1 || $pk->reliability == 4){ + $packet->buffer .= Utils::writeLTriad($pk->seqIndex); + } if($pk->reliability === 1 or $pk->reliability === 3 diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 25fd2e93..8a45a3e5 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -11,6 +11,7 @@ abstract class RakNetDataPacket extends stdClass{ public $splitCount; public $splitID; public $splitIndex; + public $seqIndex; private $offset = 0; public abstract function encode(); diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 900280a4..74bc3708 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -124,6 +124,12 @@ class RakNetParser{ }else{ $messageIndex = false; } + if($reliability == 1 || $reliability == 4){ + $offset += 3; + $seqIndex = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); + }else{ + $seqIndex = false; + } if($reliability == 1 ||$reliability == 3 || $reliability == 4 || $reliability == 7){ $offset += 3; @@ -228,6 +234,7 @@ class RakNetParser{ $data->splitCount = $splitCount; $data->splitID = $splitID; $data->splitIndex = $splitIndex; + $data->seqIndex = $seqIndex; $data->localEids = true; $data->setBuffer($buf); } From dd6db76577cdc2953a673a186eed5fa8a0090252 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 30 Jun 2025 15:06:58 +0300 Subject: [PATCH 085/146] big chat messages support --- src/Player.php | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 198df046..a3d16826 100755 --- a/src/Player.php +++ b/src/Player.php @@ -525,7 +525,43 @@ class Player{ $this->packetAlwaysRecoverQueue[$pk->seqNumber] = $pk; return [$pk->seqNumber]; } - + public function sendChatMessagePacket_big(RakNetDataPacket $pk){ + $sendtime = microtime(true); + $size = $this->MTU - 34; + if($size <= 0) return false; + $buffer = str_split($pk->buffer, $size); + $bigCnt = $this->bigCnt; + $this->bigCnt = ($this->bigCnt + 1) % 0x10000; + $cnts = []; + $bufCount = count($buffer); + + $orderIndex = $this->chatMessagesOrderIndex++; + foreach($buffer as $i => $buf){ + $cnts[] = $count = $this->counter[0]++; + + $pk = new UnknownPacket; + $pk->packetID = $pk->pid(); + $pk->reliability = 3; + + $pk->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; + $pk->orderIndex = $orderIndex; + + $pk->hasSplit = true; + $pk->splitCount = $bufCount; + $pk->splitID = $bigCnt; + $pk->splitIndex = $i; + $pk->buffer = $buf; + $pk->messageIndex = $this->counter[3]++; + + $rk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $rk->data[] = $pk; + $rk->seqNumber = $count; + $rk->sendtime = $sendtime; + $this->packetAlwaysRecoverQueue[$count] = $rk; + $this->send($rk); + } + return $cnts; + } public function blockQueueDataPacket_big(RakNetDataPacket $pk){ $sendtime = microtime(true); $size = $this->MTU - 34; @@ -1907,10 +1943,10 @@ class Player{ if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; - //TODO big chat messages support $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; + if($len > $MTU) return $this->sendChatMessagePacket_big($packet); if(($this->chatMessagesQueueLength + $len) >= $MTU) $this->sendChatBuffer(); @@ -2180,6 +2216,7 @@ class Player{ if($this->loggedIn === false){ break; } + switch($packet->status){ case 1: //Spawn!! if($this->spawned !== false){ From 3eea709f94888ed3bd6a56083bbf0ea4dba0fa83 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 30 Jun 2025 15:47:41 +0300 Subject: [PATCH 086/146] removed some debug output --- src/Player.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index a3d16826..52f279e8 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2096,7 +2096,7 @@ class Player{ } $this->auth = true; - if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ + if(!$this->data->exists("inventory") || ($this->gamemode & 0x01) === 0x01){ if(($this->gamemode & 0x01) === 0x01){ $inv = []; if(($this->gamemode & 0x02) === 0x02){ @@ -2227,7 +2227,6 @@ class Player{ $pk->time = $this->level->getTime(); $pk->started = !$this->level->isTimeStopped(); $this->dataPacketAlwaysRecover($pk); - console($this->level->getTime()); $pos = new Position($this->entity->x, $this->entity->y, $this->entity->z, $this->level); $pData = $this->data->get("position"); From bf42bd0b6d67b264ea1bd7fd6f7deec5ba44bf0f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 30 Jun 2025 17:04:31 +0300 Subject: [PATCH 087/146] fixed minedgs crash exploit --- src/utils/Utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 2f146cba..c4f4a1ee 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -437,7 +437,7 @@ class Utils{ } public static function readTriad($str){ - return strlen($str) < 3 ? false : @unpack("N", "\x00$str")[1]; + return strlen($str) < 3 ? 0 : @unpack("N", "\x00$str")[1]; //make sure this returns 0 on failure instead of false or else raknet parser will fall into infinite loop if start is false } public static function writeDataArray($data){ From 31ae78674852af19043fc2da0c582193b5085cfe Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 07:03:39 +0300 Subject: [PATCH 088/146] invisibility was rewritten --- src/Player.php | 124 ++++++++++++++++++++++++++++-------------- src/entity/Entity.php | 28 ++++++---- 2 files changed, 101 insertions(+), 51 deletions(-) diff --git a/src/Player.php b/src/Player.php index 52f279e8..48c36c97 100755 --- a/src/Player.php +++ b/src/Player.php @@ -147,6 +147,8 @@ class Player{ */ public $lastLocalEID = 0; + public $invisibleFor = []; + /** * @param integer $clientID * @param string $ip @@ -324,26 +326,8 @@ class Player{ foreach($this->level->entityList as $e){ if($e->eid !== $this->entity->eid){ if($e->isPlayer()){ - $pk = new MovePlayerPacket(); - $pk->eid = $this->entity->eid; - $pk->x = -256; - $pk->y = 128; - $pk->z = -256; - $pk->yaw = 0; - $pk->pitch = 0; - $pk->bodyYaw = 0; - $e->player->entityQueueDataPacket($pk); - - $pk = new MovePlayerPacket(); - $pk->eid = $e->eid; - $pk->x = -256; - $pk->y = 128; - $pk->z = -256; - $pk->yaw = 0; - $pk->pitch = 0; - $pk->bodyYaw = 0; - $this->entityQueueDataPacket($pk); - + $this->setInvisibleFor($e->player, true); + $e->player->setInvisibleFor($this, true); }else{ $pk = new RemoveEntityPacket(); $pk->eid = $e->eid; @@ -377,14 +361,8 @@ class Player{ $terrain = true; foreach($this->level->players as $player){ if($player !== $this and $player->entity instanceof Entity){ - $pk = new MoveEntityPacket_PosRot; - $pk->eid = $player->entity->eid; - $pk->x = $player->entity->x; - $pk->y = $player->entity->y; - $pk->z = $player->entity->z; - $pk->yaw = $player->entity->yaw; - $pk->pitch = $player->entity->pitch; - $this->entityQueueDataPacket($pk); + $player->setInvisibleFor($this, false); + $this->setInvisibleFor($player, false); $pk = new PlayerEquipmentPacket; $pk->eid = $this->eid; @@ -1049,9 +1027,19 @@ class Player{ } return true; } + + public function invisibilityHandler(&$data, $event){ + $target = $data["target"]; + if($target == $this){ + $for = $data["for"]; + if($target->level != $for->level || $target->gamemode == SPECTATOR) { + $data["status"] = true; + } + } + } /** - * @param mixed $data + * @param mixed &$data * @param string $event */ public function eventHandler($data, $event){ @@ -1282,16 +1270,54 @@ class Player{ } } - public function makeInvisibleForOnePlayer(Player $player){ - //TODO rewrite how invisibility is handled - $pk = new RemoveEntityPacket; - $pk->eid = $this->entity->eid; - $player->entityQueueDataPacket($pk); + + public function isInvisibleFor(Player $player){ + return isset($this->invisibleFor[$player->CID]); } + + public function setInvisibleFor(Player $player, $b, $send=true){ + $data = [ + "target" => $this, + "for" => $player, + "status" => $b + ]; + + $this->server->handle("player.invisible", $data); + $b = $data["status"]; + + if($b){ + $this->invisibleFor[$player->CID] = $b; + if($send){ + $pk = new MovePlayerPacket(); + $pk->x = -256; + $pk->y = 128; + $pk->z = -256; + $pk->eid = $this->eid; + $pk->yaw = 0; + $pk->pitch = 0; + $pk->headYaw = 0; + $player->entityQueueDataPacket($pk); + } + }else{ + unset($this->invisibleFor[$player->CID]); + if($send){ + $pk = new MovePlayerPacket(); + $pk->eid = $this->entity->eid; + $pk->x = $this->entity->x; + $pk->y = $this->entity->y; + $pk->z = $this->entity->z; + $pk->yaw = $this->entity->yaw; + $pk->pitch = $this->entity->pitch; + $pk->bodyYaw = $this->entity->headYaw; + $player->entityQueueDataPacket($pk); + } + } + } + public function makeInvisibleForAllPlayers(){ - $pk = new RemoveEntityPacket; - $pk->eid = $this->entity->eid; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); + foreach($this->server->api->player->getA as $player){ + if($player->CID != $this->CID) $this->setInvisibleFor($player, true); + } } public function getGamemode(){ switch($this->gamemode){ @@ -1385,10 +1411,14 @@ class Player{ } if($this->gamemode === SPECTATOR){ - $this->makeInvisibleForAllPlayers(); + foreach($this->server->api->player->getAll() as $player){ + if($player->CID != $this->CID) $this->setInvisibleFor($player, true); + } } if($this->gamemode === CREATIVE){ - $this->server->api->player->spawnToAllPlayers($this); + foreach($this->server->api->player->getAll() as $player){ + if($player->CID != $this->CID) $this->setInvisibleFor($player, false); + } } $this->inventory = $inv; @@ -1881,6 +1911,11 @@ class Player{ if($msg === true and $this->username != "" and $this->spawned !== false){ $this->server->api->chat->broadcast($this->username . " left the game: " . $reason); } + + foreach($this->server->api->player->getAll() as $player){ + $player->setInvisibleFor($this, false); + } + $this->spawned = false; console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged out due to " . $reason); $this->windows = []; @@ -2206,6 +2241,7 @@ class Player{ $this->evid[] = $this->server->event("player.pickup", [$this, "eventHandler"]); $this->evid[] = $this->server->event("tile.container.slot", [$this, "eventHandler"]); $this->evid[] = $this->server->event("tile.update", [$this, "eventHandler"]); + $this->server->addHandler("player.invisible", [$this, "invisibilityHandler"]); $this->lastMeasure = microtime(true); $this->server->schedule(50, [$this, "measureLag"], [], true); @@ -2706,7 +2742,7 @@ class Player{ $pk->pitch = $this->entity->pitch; $pk->bodyYaw = $this->entity->headYaw; foreach($this->entity->level->players as $player){ - if($player->entity->eid != $this->entity->eid){ + if($player->entity->eid != $this->entity->eid && !$this->isInvisibleFor($player)){ $player->entityQueueDataPacket(clone $pk); } } @@ -3397,6 +3433,14 @@ class Player{ } + /** + * @deprecated use Player::setInvisibleFor + * @param Player $player + */ + public function makeInvisibleForOnePlayer(Player $player){ + $this->setInvisibleFor($player, true); + } + /** * @deprecated craft system changed * @param array $craft diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 36104762..3121b487 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -998,9 +998,9 @@ class Entity extends Position $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; - $pk->bodyYaw = $this->yaw; + $pk->bodyYaw = $this->headYaw; foreach($players as $player){ - if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk); + if($player->hasEntity($this) && !$this->player->isInvisibleFor($player)) $player->entityQueueDataPacket(clone $pk); } } } @@ -1149,21 +1149,27 @@ class Entity extends Position if($this->player->connected !== true or $this->player->spawned === false){ return false; } - if($this->player->gamemode === SPECTATOR){ - break; - } $player->addEntity($this); - + if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); $pk = new AddPlayerPacket(); $pk->clientID = 0; // $this->player->clientID; $pk->username = $this->player->username; $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = 0; - $pk->pitch = 0; + if($this->player->isInvisibleFor($player)){ + $pk->x = -256; + $pk->y = 128; + $pk->z = -256; + $pk->yaw = 0; + $pk->pitch = 0; + }else{ + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + } + $pk->itemID = 0; $pk->itemAuxValue = 0; $pk->metadata = $this->getMetadata(); From 00c03d8bf05b7670ea9db55d79024685718ae26f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 09:47:04 +0300 Subject: [PATCH 089/146] readded limit to resendQueue, MovePlayerPacket will be sent after AddPlayerPacket to fix AddPlayerPacket bug --- src/Player.php | 2 ++ src/entity/Entity.php | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Player.php b/src/Player.php index 48c36c97..9f71cabe 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1621,7 +1621,9 @@ class Player{ } if(($resendCnt = count($this->resendQueue)) > 0){ + $limit = 25; foreach($this->resendQueue as $count => $data){ + if(!--$limit) break; unset($this->resendQueue[$count]); $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 3121b487..04575501 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1153,27 +1153,33 @@ class Entity extends Position $player->addEntity($this); if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); $pk = new AddPlayerPacket(); + $pkm = new MovePlayerPacket(); $pk->clientID = 0; // $this->player->clientID; $pk->username = $this->player->username; - $pk->eid = $this->eid; + $pkm->eid = $pk->eid = $this->eid; + if($this->player->isInvisibleFor($player)){ - $pk->x = -256; - $pk->y = 128; - $pk->z = -256; - $pk->yaw = 0; - $pk->pitch = 0; + $pkm->x = $pk->x = -256; + $pkm->y = $pk->y = 128; + $pkm->z = $pk->z = -256; + $pkm->yaw = $pk->yaw = 0; + $pkm->pitch = $pk->pitch = 0; + $pkm->bodyYaw = 0; }else{ - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; + $pkm->x = $pk->x = $this->x; + $pkm->y = $pk->y = $this->y; + $pkm->z = $pk->z = $this->z; + $pkm->yaw = $pk->yaw = $this->yaw; + $pkm->pitch = $pk->pitch = $this->pitch; + $pkm->bodyYaw = $this->headYaw; } $pk->itemID = 0; $pk->itemAuxValue = 0; $pk->metadata = $this->getMetadata(); $player->entityQueueDataPacket($pk); + //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) + $player->entityQueueDataPacket($pkm); $pk = new PlayerEquipmentPacket(); $pk->eid = $this->eid; From 26fc61d3c09b6475278dc5ede1f246e142733d52 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 10:37:30 +0300 Subject: [PATCH 090/146] fixed target eid being incorrect in TakeItemPacket after converting --- src/network/protocol/packet/TakeItemEntityPacket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index c64095d9..d3e6b949 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -23,7 +23,7 @@ class TakeItemEntityPacket extends RakNetDataPacket{ if(!$this->localEids){ $this->localEids = true; $this->eid = $p->global2localEID[$this->eid] ?? false; - $this->target = $p->global2localEID[$this->eid] ?? false; + $this->target = $p->global2localEID[$this->target] ?? false; if($this->eid === false || $this->target === false) return false; } return true; From 9a9d67d6078070f9e30a814c00e6414c329c841d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 14:23:05 +0300 Subject: [PATCH 091/146] fixed entity event packet --- src/network/protocol/packet/EntityEventPacket.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index daa00929..767f4b5c 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -47,5 +47,6 @@ class EntityEventPacket extends RakNetDataPacket{ $this->eid = $p->local2GlobalEID[$this->eid] ?? false; if($this->eid === false) return false; } + return true; } -} \ No newline at end of file +} From 2acc93648ea6faca7363b72a2a101cdd4ba0b514 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 15:49:21 +0300 Subject: [PATCH 092/146] some tweaks --- src/Player.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 9f71cabe..aa25083e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1284,7 +1284,7 @@ class Player{ $this->server->handle("player.invisible", $data); $b = $data["status"]; - + if(!($this->entity instanceof Entity)) $send = false; if($b){ $this->invisibleFor[$player->CID] = $b; if($send){ @@ -1710,8 +1710,15 @@ class Player{ } public function convertToGlobalEIDPacket(RakNetDataPacket $pk){ + if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ + $eid = $pk->eid; + $target = $pk->target; + } if(!$pk->eidsToGlobal($this)){ ConsoleAPI::warn("Failed to convert eids to global in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); + if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ + ConsoleAPI::warn("EID: $eid Target: $target"); + } foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); return false; } From 5e613268ab789786bc199993d7663c9192318c71 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 1 Jul 2025 19:42:44 +0300 Subject: [PATCH 093/146] container open packet uses block queue packet --- src/world/Tile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/world/Tile.php b/src/world/Tile.php index 5f4714a5..1497923a 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -254,7 +254,7 @@ class Tile extends Position{ $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; - $player->dataPacket($pk); + $player->blockQueueDataPacket($pk); $slots = []; if(is_array($player->windows[$id])){ From 6c7bcf014be5be8feeec74ca2b9f9b96fa64e66f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 3 Jul 2025 16:55:47 +0300 Subject: [PATCH 094/146] Level::handleMaterialAcceleration uses materials now --- src/entity/Entity.php | 2 +- src/entity/object/ItemEntity.php | 2 +- src/material/block/nonfull/FenceBlock.php | 9 ++++++++- src/world/Level.php | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 04575501..57fd44d4 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -404,7 +404,7 @@ class Entity extends Position } public function handleWaterMovement(){ - if($this->level->handleMaterialAcceleration($this->boundingBox->expand(0, -0.4, 0)->contract(0.001, 0.001, 0.001), 0, $this)){ + if($this->level->handleMaterialAcceleration($this->boundingBox->expand(0, -0.4, 0)->contract(0.001, 0.001, 0.001), Material::$water, $this)){ $this->fallDistance = 0; $this->inWater = true; if($this->fire > 0){ diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 43d42808..825f031c 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -174,7 +174,7 @@ class ItemEntity extends Entity{ } public function handleWaterMovement(){ - return $this->level->handleMaterialAcceleration($this->boundingBox, 0, $this); + return $this->level->handleMaterialAcceleration($this->boundingBox, Material::$water, $this); } public function updateEntityMovement(){ diff --git a/src/material/block/nonfull/FenceBlock.php b/src/material/block/nonfull/FenceBlock.php index 2af3da8c..fddb9c80 100644 --- a/src/material/block/nonfull/FenceBlock.php +++ b/src/material/block/nonfull/FenceBlock.php @@ -12,9 +12,16 @@ class FenceBlock extends TransparentBlock{ public static function canConnectTo(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); + /*if($id == FENCE || $id == FENCE_GATE) return true; + if($id == 0) return false; + $mat = StaticBlock::getMaterial($id); + if($mat->isSolidBlocking() && tile->isCubeShaped()){ //TODO tile->isCubeShaped + return $mat != Material::$vegetable; + } + return false;*/ if($id != FENCE && $id != FENCE_GATE){ - return StaticBlock::getIsSolid($id) && $id != PUMPKIN; //TODO check var6 != null && var6.blockMaterial.isOpaque() && var6.renderAsNormalBlock() ? var6.blockMaterial != Material.pumpkin : false; + return StaticBlock::getIsSolid($id) && $id != PUMPKIN; } return true; } diff --git a/src/world/Level.php b/src/world/Level.php index b07569f1..2f1cc931 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -216,7 +216,7 @@ class Level{ return false; } - public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ + public function handleMaterialAcceleration(AxisAlignedBB $aabb, Material $material, Entity $entity){ $minX = floor($aabb->minX); $maxX = ceil($aabb->maxX); $minY = floor($aabb->minY); @@ -232,7 +232,8 @@ class Level{ for($y = $minY; $y < $maxY; ++$y){ for($z = $minZ; $z < $maxZ; ++$z){ [$block, $meta] = $this->level->getBlock($x, $y, $z); - if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))){ //TODO better material system + $mat = StaticBlock::getMaterial($block); + if($material == $mat){ $v16 = ($y + 1) - LiquidBlock::getPercentAir($meta); if($maxY >= $v16){ $appliedVelocity = true; From a62986a8d636533d9cdb7bd2ca8237a224340d09 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 3 Jul 2025 20:57:00 +0300 Subject: [PATCH 095/146] fixed division by zero when breaking tnt, (hopefully?) fixed empty chests --- src/Player.php | 1 - src/material/Block.php | 3 ++- src/world/Tile.php | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Player.php b/src/Player.php index aa25083e..fef8a973 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1696,7 +1696,6 @@ class Player{ $len = 1 + strlen($pk->buffer); $MTU = $this->MTU - 24; if($len > $MTU) return $this->blockQueueDataPacket_big($pk); - if(($this->blockUpdateQueueLength + $len) >= $MTU){ $this->sendBlockUpdateQueue(); } diff --git a/src/material/Block.php b/src/material/Block.php index 21527a26..34209442 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -271,12 +271,13 @@ abstract class Block extends Position{ public function getDestroyProgress(Player $player){ if($this->breakTime < 0) return 0; + if($this->breakTime == 0) return INF; //instabreak $id = $this->getID(); $meta = $this->getMetadata(); if($player->canDestroy($id, $meta)){ return $player->getDestroySpeed($id, $meta) / $this->breakTime / 30; } - if($this->breakTime == 0) return INF; //instabreak + return (1 / $this->breakTime) / 100; } diff --git a/src/world/Tile.php b/src/world/Tile.php index 1497923a..da471f13 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -266,7 +266,9 @@ class Tile extends Position{ $pk->z = $ob->z; $pk->case1 = 1; $pk->case2 = 2; - $this->server->api->player->broadcastPacket($all, $pk); + foreach($this->level->players as $pl){ + $pl->blockQueueDataPacket(clone $pk); + } for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $ob->getSlot($s); if($slot->getID() > AIR and $slot->count > 0){ @@ -283,8 +285,8 @@ class Tile extends Position{ $pk->z = $this->z; $pk->case1 = 1; $pk->case2 = 2; - foreach($this->level->players as $player){ - $player->blockQueueDataPacket(clone $pk); + foreach($this->level->players as $pl){ + $pl->blockQueueDataPacket(clone $pk); } for($s = 0; $s < CHEST_SLOTS; ++$s){ From 5537f3e197f1e68877c1b71c1f516b2eff560abd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 4 Jul 2025 16:10:49 +0300 Subject: [PATCH 096/146] removed ProtocolInfo::EID_PACKETS, added ability to customize min block breaking progress required to destroy a block + change value from 1 to 0.8 --- src/API/BlockAPI.php | 2 +- src/Player.php | 30 ++++++++++----------------- src/PocketMinecraftServer.php | 6 ++++++ src/network/protocol/ProtocolInfo.php | 29 -------------------------- 4 files changed, 18 insertions(+), 49 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index c70c8c09..ed8d47c1 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -413,7 +413,7 @@ class BlockAPI{ $exceptedTimeMS = INF; $tooFast = true; }else{ - $exceptedTimeMS = ceil(1 / $progress)*0.05; + $exceptedTimeMS = ceil(PocketMinecraftServer::$BLOCK_BREAKING_PROGRESS / $progress)*0.05; } diff --git a/src/Player.php b/src/Player.php index fef8a973..5eaf9b70 100755 --- a/src/Player.php +++ b/src/Player.php @@ -433,7 +433,7 @@ class Player{ public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return []; - if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; + if(!$this->convertToLocalEIDPacket($packet)) return false; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); @@ -455,7 +455,7 @@ class Player{ public function dataPacket(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return; - if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; + if(!$this->convertToLocalEIDPacket($packet)) return false; $packet->encode(); $len = strlen($packet->buffer) + 1; @@ -482,7 +482,7 @@ class Player{ public function dataPacketAlwaysRecover(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; - if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; + if(!$this->convertToLocalEIDPacket($packet)) return false; $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -619,7 +619,6 @@ class Player{ public function sendBigPacketAlwaysRecover(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; - if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); $sendtime = microtime(true); @@ -1689,7 +1688,7 @@ class Player{ public function blockQueueDataPacket(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; - if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; + if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); @@ -1709,24 +1708,17 @@ class Player{ } public function convertToGlobalEIDPacket(RakNetDataPacket $pk){ - if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ - $eid = $pk->eid; - $target = $pk->target; - } if(!$pk->eidsToGlobal($this)){ - ConsoleAPI::warn("Failed to convert eids to global in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); - if($pk->pid() == ProtocolInfo::INTERACT_PACKET){ - ConsoleAPI::warn("EID: $eid Target: $target"); - } - foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); + //ConsoleAPI::warn("Failed to convert eids to global in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); + //foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s); return false; } return true; } public function convertToLocalEIDPacket(RakNetDataPacket $pk){ if(!$pk->eidsToLocal($this)){ - ConsoleAPI::debug("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); - foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::debug($s); + //ConsoleAPI::debug("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: "); + //foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::debug($s); return false; } return true; @@ -1740,7 +1732,7 @@ class Player{ public function entityQueueDataPacket(RakNetDataPacket $pk){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; - if(isset(ProtocolInfo::EID_PACKETS[$pk->pid()])) if(!$this->convertToLocalEIDPacket($pk)) return false; + if(!$this->convertToLocalEIDPacket($pk)) return false; $pk->encode(); $len = 1 + strlen($pk->buffer); @@ -1984,7 +1976,7 @@ class Player{ public function sendChatMessagePacket(RakNetDataPacket $packet){ if($this->connected === false) return false; if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; - if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToLocalEIDPacket($packet)) return false; + if(!$this->convertToLocalEIDPacket($packet)) return false; $packet->encode(); $len = strlen($packet->buffer) + 1; @@ -2036,7 +2028,7 @@ class Player{ if($this->connected === false){ return; } - if(isset(ProtocolInfo::EID_PACKETS[$packet->pid()])) if(!$this->convertToGlobalEIDPacket($packet)) return; + if(!$this->convertToGlobalEIDPacket($packet)) return; if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ return; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 39a8523c..659b32f1 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -17,6 +17,7 @@ class PocketMinecraftServer{ public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public static $FORCE_20_TPS = false, $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100; + public static $BLOCK_BREAKING_PROGRESS = 0.8; public static $ENABLE_LIGHT_UPDATES = true; function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; @@ -104,7 +105,9 @@ class PocketMinecraftServer{ "disable-emojis-on-signs" => Level::$disableEmojisOnSigns, "allow-dropping-single-items" => Player::$allowDroppingSingleItems, "enable-light-updates" => self::$ENABLE_LIGHT_UPDATES, + "min-block-breaking-progress" => self::$BLOCK_BREAKING_PROGRESS, ]); + self::$BLOCK_BREAKING_PROGRESS = $this->extraprops->get("min-block-breaking-progress"); Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); Level::$disableEmojisOnSigns = $this->extraprops->get("disable-emojis-on-signs"); Entity::$keepInventory = $this->extraprops->get("keep-items-on-death"); @@ -123,6 +126,9 @@ class PocketMinecraftServer{ if(self::$FORCE_20_TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } + if(self::$BLOCK_BREAKING_PROGRESS > 1){ + ConsoleAPI::warn("Minimal block breaking progress is more than 1, may cause ghost blocks to appear even for players with extremely low ping!"); + } if(!self::$ENABLE_LIGHT_UPDATES){ ConsoleAPI::warn("Light updates are disabled: this should decrease memory consumption and increase performance, but will prevent all existing light from updating and disable some light-dependant features."); } diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index c2e9ebfb..efa7e47e 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -70,35 +70,6 @@ abstract class ProtocolInfo{ const ADVENTURE_SETTINGS_PACKET = 0xb7; const ENTITY_DATA_PACKET = 0xb8; const PLAYER_INPUT_PACKET = 0xb9; - - const EID_PACKETS = [ - self::ADD_MOB_PACKET => true, - self::ADD_ITEM_ENTITY_PACKET => true, - self::RESPAWN_PACKET => true, - self::SET_ENTITY_MOTION_PACKET => true, - self::USE_ITEM_PACKET => true, - self::SET_ENTITY_DATA_PACKET => true, - self::PLAYER_ARMOR_EQUIPMENT_PACKET => true, - self::ROTATE_HEAD_PACKET => true, - self::PLAYER_EQUIPMENT_PACKET => true, - self::SEND_INVENTORY_PACKET => true, - self::INTERACT_PACKET => true, - self::ENTITY_EVENT_PACKET => true, - self::PLAYER_ACTION_PACKET => true, - self::REMOVE_ENTITY_PACKET => true, - self::ANIMATE_PACKET => true, - self::ADD_PLAYER_PACKET => true, - self::TAKE_ITEM_ENTITY_PACKET => true, - self::START_GAME_PACKET => true, - self::ADD_PAINTING_PACKET => true, - self::REMOVE_PLAYER_PACKET => true, - self::MOVE_ENTITY_PACKET_POSROT => true, - self::ADD_ENTITY_PACKET => true, - self::REMOVE_BLOCK_PACKET => true, - self::MOVE_PLAYER_PACKET => true, - self::DROP_ITEM_PACKET => true, - self::SET_ENTITY_LINK_PACKET => true - ]; } /*Unused: * 0xb5 From 44348c84a402519d41dfab90d6d860ed01e85493 Mon Sep 17 00:00:00 2001 From: catgirl <137518916+ccatgirl@users.noreply.github.com> Date: Tue, 8 Jul 2025 10:00:09 -0500 Subject: [PATCH 097/146] Add reliability enums --- src/Player.php | 26 ++++++++++++------------- src/network/raknet/RakNetCodec.php | 20 +++++++++---------- src/network/raknet/RakNetDataPacket.php | 2 +- src/network/raknet/RakNetInfo.php | 9 +++++++++ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5eaf9b70..5320afdd 100755 --- a/src/Player.php +++ b/src/Player.php @@ -469,7 +469,7 @@ class Player{ } $packet->messageIndex = $this->counter[3]++; - $packet->reliability = 2; + $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE; @$this->buffer->data[] = $packet; $this->bufferLen += 6 + $len; return []; @@ -497,7 +497,7 @@ class Player{ } $packet->messageIndex = $this->counter[3]++; - $packet->reliability = 2; + $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE; $this->send($pk); $this->packetAlwaysRecoverQueue[$pk->seqNumber] = $pk; @@ -519,7 +519,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = 3; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $pk->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -556,7 +556,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = 3; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -594,7 +594,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = 3; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -634,7 +634,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = 2; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE; $pk->hasSplit = true; $pk->splitCount = $bufCount; $pk->splitID = $bigCnt; @@ -671,7 +671,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $packet->pid(); - $pk->reliability = 2; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE; $pk->hasSplit = true; $pk->splitCount = $bufCount; $pk->splitID = $bigCnt; @@ -1700,7 +1700,7 @@ class Player{ } $pk->messageIndex = $this->counter[3]++; - $pk->reliability = 3; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; $pk->orderIndex = $this->blockUpdateQueueOrderIndex++; @$this->blockUpdateQueue->data[] = $pk; @@ -1745,7 +1745,7 @@ class Player{ $pk->messageIndex = $this->counter[3]++; - $pk->reliability = 3; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; $pk->orderIndex = $this->entityDataQueueOrderIndex++; @$this->entityDataQueue->data[] = $pk; @@ -1854,7 +1854,7 @@ class Player{ } if($motionSent){ $motion->messageIndex = 0; //force 0 cuz reliability 0 - $motion->reliability = 1; + $motion->reliability = RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED; $motion->orderIndex = $this->entityDataQueueOrderIndex; $motion->orderChannel = self::ENTITY_ORDER_CHANNEL; $motion->seqIndex = $this->entityMovementQueueSeqIndex++; @@ -1862,7 +1862,7 @@ class Player{ } if($moveSent){ $move->messageIndex = 0; - $move->reliability = 1; + $move->reliability = RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED; $move->orderIndex = $this->entityDataQueueOrderIndex; $move->orderChannel = self::ENTITY_ORDER_CHANNEL; $move->seqIndex = $this->entityMovementQueueSeqIndex++; @@ -1870,7 +1870,7 @@ class Player{ } if($headSent){ $headyaw->messageIndex = 0; - $headyaw->reliability = 1; + $headyaw->reliability = RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED; $headyaw->orderIndex = $this->entityDataQueueOrderIndex; $headyaw->orderChannel = self::ENTITY_ORDER_CHANNEL; $headyaw->seqIndex = $this->entityMovementQueueSeqIndex++; @@ -1985,7 +1985,7 @@ class Player{ if(($this->chatMessagesQueueLength + $len) >= $MTU) $this->sendChatBuffer(); - $packet->reliability = 3; + $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; $packet->messageIndex = $this->counter[3]++; $packet->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; $packet->orderIndex = $this->chatMessagesOrderIndex++; diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php index b8e5b55b..bf001bb0 100644 --- a/src/network/raknet/RakNetCodec.php +++ b/src/network/raknet/RakNetCodec.php @@ -99,21 +99,21 @@ class RakNetCodec{ public static function encodeDataPacket(RakNetPacket $packet, RakNetDataPacket $pk){ $packet->buffer .= chr(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0)); $packet->buffer .= Utils::writeShort(strlen($pk->buffer) << 3); - if($pk->reliability === 2 - or $pk->reliability === 3 - or $pk->reliability === 4 - or $pk->reliability === 6 - or $pk->reliability === 7){ + if($pk->reliability === RakNetInfo::RELIABILITY_RELIABLE + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_WITHACKRECEIPT + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT){ $packet->buffer .= Utils::writeLTriad($pk->messageIndex); } - if($pk->reliability == 1 || $pk->reliability == 4){ + if($pk->reliability == RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED || $pk->reliability == RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED){ $packet->buffer .= Utils::writeLTriad($pk->seqIndex); } - if($pk->reliability === 1 - or $pk->reliability === 3 - or $pk->reliability === 4 - or $pk->reliability === 7){ + if($pk->reliability === RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT){ $packet->buffer .= Utils::writeLTriad($pk->orderIndex); $packet->buffer .= chr($pk->orderChannel); } diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 8a45a3e5..10d11ee8 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -3,7 +3,7 @@ abstract class RakNetDataPacket extends stdClass{ public $buffer = b""; - public $reliability = 0; + public $reliability = RakNetInfo::RELIABILITY_UNRELIABLE; public $hasSplit = false; public $messageIndex; public $orderIndex; diff --git a/src/network/raknet/RakNetInfo.php b/src/network/raknet/RakNetInfo.php index 634e2e76..3554b189 100644 --- a/src/network/raknet/RakNetInfo.php +++ b/src/network/raknet/RakNetInfo.php @@ -37,6 +37,15 @@ abstract class RakNetInfo{ const NACK = 0xa0; const ACK = 0xc0; + const RELIABILITY_UNRELIABLE = 0; + const RELIABILITY_UNRELIABLE_SEQUENCED = 1; + const RELIABILITY_RELIABLE = 2; + const RELIABILITY_RELIABLE_ARRANGED = 3; + const RELIABILITY_RELIABLE_SEQUENCED = 4; + const RELIABILITY_UNRELIABLE_WITHACKRECEIPT = 5; + const RELIABILITY_RELIABLE_WITHACKRECEIPT = 6; + const RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT = 7; + public static function isValid($pid){ return match ((int)$pid) { RakNetInfo::UNCONNECTED_PING, RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS, RakNetInfo::OPEN_CONNECTION_REQUEST_1, RakNetInfo::OPEN_CONNECTION_REPLY_1, RakNetInfo::OPEN_CONNECTION_REQUEST_2, RakNetInfo::OPEN_CONNECTION_REPLY_2, RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION, RakNetInfo::UNCONNECTED_PONG, RakNetInfo::ADVERTISE_SYSTEM, RakNetInfo::DATA_PACKET_0, RakNetInfo::DATA_PACKET_1, RakNetInfo::DATA_PACKET_2, RakNetInfo::DATA_PACKET_3, RakNetInfo::DATA_PACKET_4, RakNetInfo::DATA_PACKET_5, RakNetInfo::DATA_PACKET_6, RakNetInfo::DATA_PACKET_7, RakNetInfo::DATA_PACKET_8, RakNetInfo::DATA_PACKET_9, RakNetInfo::DATA_PACKET_A, RakNetInfo::DATA_PACKET_B, RakNetInfo::DATA_PACKET_C, RakNetInfo::DATA_PACKET_D, RakNetInfo::DATA_PACKET_E, RakNetInfo::DATA_PACKET_F, RakNetInfo::NACK, RakNetInfo::ACK => true, From 421b7b26e13643f43e24ee45db0ffef882dabc5b Mon Sep 17 00:00:00 2001 From: catgirl <137518916+ccatgirl@users.noreply.github.com> Date: Thu, 10 Jul 2025 20:12:47 -0500 Subject: [PATCH 098/146] arranged > ordered --- src/Player.php | 12 ++++++------ src/network/raknet/RakNetCodec.php | 8 ++++---- src/network/raknet/RakNetInfo.php | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5320afdd..fad1a285 100755 --- a/src/Player.php +++ b/src/Player.php @@ -519,7 +519,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $pk->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -556,7 +556,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -594,7 +594,7 @@ class Player{ $pk = new UnknownPacket; $pk->packetID = $pk->pid(); - $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; $pk->orderIndex = $orderIndex; @@ -1700,7 +1700,7 @@ class Player{ } $pk->messageIndex = $this->counter[3]++; - $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $pk->orderChannel = Player::BLOCKUPDATE_ORDER_CHANNEL; $pk->orderIndex = $this->blockUpdateQueueOrderIndex++; @$this->blockUpdateQueue->data[] = $pk; @@ -1745,7 +1745,7 @@ class Player{ $pk->messageIndex = $this->counter[3]++; - $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $pk->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $pk->orderChannel = Player::ENTITY_ORDER_CHANNEL; $pk->orderIndex = $this->entityDataQueueOrderIndex++; @$this->entityDataQueue->data[] = $pk; @@ -1985,7 +1985,7 @@ class Player{ if(($this->chatMessagesQueueLength + $len) >= $MTU) $this->sendChatBuffer(); - $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE_ARRANGED; + $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $packet->messageIndex = $this->counter[3]++; $packet->orderChannel = Player::CHATMESSAGE_ORDER_CHANNEL; $packet->orderIndex = $this->chatMessagesOrderIndex++; diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php index bf001bb0..d8803c0e 100644 --- a/src/network/raknet/RakNetCodec.php +++ b/src/network/raknet/RakNetCodec.php @@ -100,10 +100,10 @@ class RakNetCodec{ $packet->buffer .= chr(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0)); $packet->buffer .= Utils::writeShort(strlen($pk->buffer) << 3); if($pk->reliability === RakNetInfo::RELIABILITY_RELIABLE - or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ORDERED or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_WITHACKRECEIPT - or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT){ + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ORDERED_WITHACKRECEIPT){ $packet->buffer .= Utils::writeLTriad($pk->messageIndex); } if($pk->reliability == RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED || $pk->reliability == RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED){ @@ -111,9 +111,9 @@ class RakNetCodec{ } if($pk->reliability === RakNetInfo::RELIABILITY_UNRELIABLE_SEQUENCED - or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ORDERED or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_SEQUENCED - or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT){ + or $pk->reliability === RakNetInfo::RELIABILITY_RELIABLE_ORDERED_WITHACKRECEIPT){ $packet->buffer .= Utils::writeLTriad($pk->orderIndex); $packet->buffer .= chr($pk->orderChannel); } diff --git a/src/network/raknet/RakNetInfo.php b/src/network/raknet/RakNetInfo.php index 3554b189..4c25743d 100644 --- a/src/network/raknet/RakNetInfo.php +++ b/src/network/raknet/RakNetInfo.php @@ -38,13 +38,13 @@ abstract class RakNetInfo{ const ACK = 0xc0; const RELIABILITY_UNRELIABLE = 0; - const RELIABILITY_UNRELIABLE_SEQUENCED = 1; - const RELIABILITY_RELIABLE = 2; - const RELIABILITY_RELIABLE_ARRANGED = 3; - const RELIABILITY_RELIABLE_SEQUENCED = 4; - const RELIABILITY_UNRELIABLE_WITHACKRECEIPT = 5; - const RELIABILITY_RELIABLE_WITHACKRECEIPT = 6; - const RELIABILITY_RELIABLE_ARRANGED_WITHACKRECEIPT = 7; + const RELIABILITY_UNRELIABLE_SEQUENCED = 1; + const RELIABILITY_RELIABLE = 2; + const RELIABILITY_RELIABLE_ORDERED = 3; + const RELIABILITY_RELIABLE_SEQUENCED = 4; + const RELIABILITY_UNRELIABLE_WITHACKRECEIPT = 5; + const RELIABILITY_RELIABLE_WITHACKRECEIPT = 6; + const RELIABILITY_RELIABLE_ORDERED_WITHACKRECEIPT = 7; public static function isValid($pid){ return match ((int)$pid) { From 9b0879e4a4b8659020d21783c738938bce031893 Mon Sep 17 00:00:00 2001 From: catgirl <137518916+ccatgirl@users.noreply.github.com> Date: Thu, 10 Jul 2025 20:20:10 -0500 Subject: [PATCH 099/146] whitespace changes --- src/network/raknet/RakNetInfo.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/network/raknet/RakNetInfo.php b/src/network/raknet/RakNetInfo.php index 4c25743d..2e5abf75 100644 --- a/src/network/raknet/RakNetInfo.php +++ b/src/network/raknet/RakNetInfo.php @@ -39,12 +39,12 @@ abstract class RakNetInfo{ const RELIABILITY_UNRELIABLE = 0; const RELIABILITY_UNRELIABLE_SEQUENCED = 1; - const RELIABILITY_RELIABLE = 2; - const RELIABILITY_RELIABLE_ORDERED = 3; - const RELIABILITY_RELIABLE_SEQUENCED = 4; - const RELIABILITY_UNRELIABLE_WITHACKRECEIPT = 5; - const RELIABILITY_RELIABLE_WITHACKRECEIPT = 6; - const RELIABILITY_RELIABLE_ORDERED_WITHACKRECEIPT = 7; + const RELIABILITY_RELIABLE = 2; + const RELIABILITY_RELIABLE_ORDERED = 3; + const RELIABILITY_RELIABLE_SEQUENCED = 4; + const RELIABILITY_UNRELIABLE_WITHACKRECEIPT = 5; + const RELIABILITY_RELIABLE_WITHACKRECEIPT = 6; + const RELIABILITY_RELIABLE_ORDERED_WITHACKRECEIPT = 7; public static function isValid($pid){ return match ((int)$pid) { From 401e3fac21033f99e27ed39af6cf286c94e6bbce Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 12 Jul 2025 01:41:00 +0300 Subject: [PATCH 100/146] fixed inability to get players by their full username sometimes using PlayerAPI::get --- src/API/PlayerAPI.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 9e74f1ca..4bbd53dc 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -294,7 +294,7 @@ class PlayerAPI{ if($name === ""){ return false; } - $query = $this->server->query("SELECT ip,port,name FROM players WHERE name " . ($alike === true ? "LIKE '" . $name . "%'" : "= '" . $name . "'") . ";"); + $query = $this->server->query("SELECT ip,port,name FROM players WHERE name = '$name';"); //try getting player by name first $players = []; if($query !== false and $query !== true){ while(($d = $query->fetchArray(SQLITE3_ASSOC)) !== false){ @@ -307,7 +307,21 @@ class PlayerAPI{ } } } - + if($alike === true){ + $query = $this->server->query("SELECT ip,port,name FROM players WHERE name LIKE '$name%';"); //try getting player with non-full name match if none was found + if($query !== false and $query !== true){ + while(($d = $query->fetchArray(SQLITE3_ASSOC)) !== false){ + $CID = PocketMinecraftServer::clientID($d["ip"], $d["port"]); + if(isset($this->server->clients[$CID])){ + $players[$CID] = $this->server->clients[$CID]; + if($multiple === false and $d["name"] === $name){ + return $players[$CID]; + } + } + } + } + } + if($multiple === false){ if(count($players) > 0){ return array_shift($players); From a0f7607d7e1921e7ddbbec5140c5e665c1543671 Mon Sep 17 00:00:00 2001 From: catgirl <137518916+ccatgirl@users.noreply.github.com> Date: Sun, 13 Jul 2025 08:43:33 -0500 Subject: [PATCH 101/146] Encode IP in OPEN_CONNECTION_REPLY_2 for RakNet compatibility NC would crash Jenkins' RakNet clients compiled with assertions. --- src/network/raknet/RakNetCodec.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php index d8803c0e..082cfcf5 100644 --- a/src/network/raknet/RakNetCodec.php +++ b/src/network/raknet/RakNetCodec.php @@ -1,6 +1,12 @@ buffer .= chr(4); + $packet->buffer .= Utils::writeInt($packet->ip); + $packet->buffer .= Utils::writeShort($packet->port); + } + public static function encode(RakNetPacket $packet){ if($packet->buffer != null && strlen($packet->buffer) > 0){ return; @@ -17,7 +23,7 @@ class RakNetCodec{ case RakNetInfo::OPEN_CONNECTION_REPLY_2: $packet->buffer .= RakNetInfo::MAGIC; $packet->buffer .= Utils::writeLong($packet->serverID); - $packet->buffer .= Utils::writeShort($packet->port); + static::encodeIPAddress($packet); $packet->buffer .= Utils::writeShort($packet->mtuSize); $packet->buffer .= chr(0); //Server security break; From c95a8d5cffc584831928b7fac394553f540b9598 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 13 Jul 2025 20:06:00 +0300 Subject: [PATCH 102/146] using prepared statements for PlayerAPI::get --- src/API/PlayerAPI.php | 9 +++++++-- src/PocketMinecraftServer.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 4bbd53dc..d043412f 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -294,7 +294,10 @@ class PlayerAPI{ if($name === ""){ return false; } - $query = $this->server->query("SELECT ip,port,name FROM players WHERE name = '$name';"); //try getting player by name first + $this->server->preparedSQL->player->getEq->reset(); + $this->server->preparedSQL->player->getEq->bindValue(":name", $name); + $query = $this->server->preparedSQL->player->getEq->execute(); + $players = []; if($query !== false and $query !== true){ while(($d = $query->fetchArray(SQLITE3_ASSOC)) !== false){ @@ -308,7 +311,9 @@ class PlayerAPI{ } } if($alike === true){ - $query = $this->server->query("SELECT ip,port,name FROM players WHERE name LIKE '$name%';"); //try getting player with non-full name match if none was found + $this->server->preparedSQL->player->getLike->reset(); + $query = $this->server->preparedSQL->player->getLike->bindValue(":name", "$name%"); + $query = $this->server->preparedSQL->player->getLike->execute(); //try getting player with non-full name match if none was found if($query !== false and $query !== true){ while(($d = $query->fetchArray(SQLITE3_ASSOC)) !== false){ $CID = PocketMinecraftServer::clientID($d["ip"], $d["port"]); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 659b32f1..a9ef9924 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -157,6 +157,8 @@ class PocketMinecraftServer{ public function startDatabase(){ $this->preparedSQL = new stdClass(); $this->preparedSQL->entity = new stdClass(); + $this->preparedSQL->player = new stdClass(); + $this->database = new SQLite3(":memory:"); $this->query("PRAGMA journal_mode = OFF;"); $this->query("PRAGMA encoding = \"UTF-8\";"); @@ -173,6 +175,10 @@ class PocketMinecraftServer{ $this->preparedSQL->updateAction = $this->database->prepare("UPDATE actions SET last = :time WHERE ID = :id;"); $this->preparedSQL->entity->setPosition = $this->database->prepare("UPDATE entities SET x = :x, y = :y, z = :z, pitch = :pitch, yaw = :yaw WHERE EID = :eid ;"); $this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;"); + + $this->preparedSQL->player->getEq = $this->database->prepare("SELECT ip,port,name FROM players WHERE name = :name;"); //'$name' + $this->preparedSQL->player->getLike = $this->database->prepare("SELECT ip,port,name FROM players WHERE name LIKE :name;"); //'$name' + } public function query($sql, $fetch = false){ From d7763f445aeda87bf6cedde99b65d3cd7f6f6fe6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 16 Jul 2025 12:35:12 +0300 Subject: [PATCH 103/146] better entity linking system --- src/API/EntityAPI.php | 35 ++++- src/Player.php | 8 +- src/entity/Creature.php | 47 +++--- src/entity/Entity.php | 245 +++++++++++++++++++----------- src/entity/Projectile.php | 33 ++-- src/entity/ai/MobController.php | 1 + src/entity/animal/Pig.php | 2 +- src/entity/object/Arrow.php | 11 +- src/entity/object/FallingSand.php | 29 ++-- src/entity/object/ItemEntity.php | 45 +++--- src/entity/object/Minecart.php | 67 ++++---- src/entity/object/Painting.php | 25 +-- src/entity/object/PrimedTNT.php | 32 ++-- 13 files changed, 353 insertions(+), 227 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 5b9d3983..049fbbe2 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -53,9 +53,9 @@ class EntityAPI{ if(is_int($args[0])) $type = $args[0]; else $type = $mob[strtolower($args[0])] ?? 0; if($type < 10 || $type > 36){ - return "Unknown mob."; + if($args[0] != "secret") return "Unknown mob."; } - $mobName = ucfirst(array_flip($mob)[$type]); + $mobName = $args[0] == "secret" ? "" : ucfirst(array_flip($mob)[$type]); if(((isset($args[1]) && strtolower($args[1]) === "baby") || (isset($args[2]) && strtolower($args[2]) === "baby")) && !Utils::in_range($type, 10, 13)){ return "$mobName cannot be a baby!"; @@ -67,6 +67,37 @@ class EntityAPI{ $level = $issuer->entity->level; $pos = new Position($x, $y, $z, $level); + if($args[0] == "secret"){ + $data = [ + "x" => $pos->x, + "y" => $pos->y, + "z" => $pos->z + ]; + $e = $this->add($level, ENTITY_MOB, MOB_SPIDER, $data); + $e2 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e3 = $this->add($level, ENTITY_MOB, MOB_ZOMBIE, $data); + $e4 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e5 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e6 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e7 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + + $this->spawnToAll($e7); + $this->spawnToAll($e6); + $this->spawnToAll($e5); + $this->spawnToAll($e4); + $this->spawnToAll($e3); + $this->spawnToAll($e2); + $this->spawnToAll($e); + + $e2->setRiding($e); + $e3->setRiding($e2); + $e4->setRiding($e3); + $e5->setRiding($e4); + $e6->setRiding($e5); + $e7->setRiding($e6); + return "???"; + } + if(count($args) === 1){//summon $this->summon($pos, ENTITY_MOB, $type); return "$mobName spawned in $x, $y, $z."; diff --git a/src/Player.php b/src/Player.php index fad1a285..ad226049 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3117,7 +3117,7 @@ class Player{ } break; case ProtocolInfo::SET_ENTITY_LINK_PACKET: - if($this->entity->linkedEntity != 0){ + if($this->entity->riding != 0){ $this->entity->stopRiding(); } break; @@ -3127,10 +3127,10 @@ class Player{ $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if($this->entity->linkedEntity != 0){ - $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; + if($this->entity->riding != 0){ + $e = $this->entity->level->entityList[$this->entity->riding] ?? false; if($e === false) { - ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); + ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->riding})"); $this->entity->stopRiding(); break; } diff --git a/src/entity/Creature.php b/src/entity/Creature.php index f0e1e28d..d2a83eca 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -56,33 +56,34 @@ abstract class Creature extends Living{ public function getArmorValue(){ return 0; } - public function spawn($player){ + public function spawn(Player $player){ if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } - $player->addEntity($this); - $pk = new AddMobPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket; - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); - - if($this->linkedEntity != 0 && $this->isRider){ - $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + console("ADD MOB"); + $pk = new AddMobPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; } - return true; + return false; } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 57fd44d4..8ef686cd 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -73,10 +73,8 @@ class Entity extends Position public $server; private $isStatic; public $level; - public $isRiding = false; public $lastUpdate; - public $linkedEntity = 0; - public $isRider = false; + public $check = true; public $width = 1; public $height = 1; @@ -118,6 +116,18 @@ class Entity extends Position public $moveStrafing, $moveForward; + + /** + * Stores EID of the entity that is riding on this entity. + * @var integer + */ + public $rider = 0; + /** + * Stores EID of the entity that is being ridden by this entity. + * @var integer + */ + public $riding = 0; + public function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new XorShift128Random(); @@ -727,6 +737,14 @@ class Entity extends Position return false; } + public function getRidingHeight(){ + return $this->yOffset; + } + + public function getRideHeight(){ + return $this->height * 0.75; + } + public function isInVoid(){ return $this->y < -1.6; } @@ -735,6 +753,33 @@ class Entity extends Position } + public function positionRider($dead){ + if($this->rider != 0){ + $e = $this->level->entityList[$this->rider]; + if($e === false) return; + + $v3 = $this->getRideHeight(); + if($dead || $this->dead) $v3 = 0.01; + $v4 = $e->getRidingHeight(); + $e->setPos($this->x, $this->y + $v3+$v4, $this->z); + } + } + + public function handleRiding(){ + if($this->riding != 0){ + $e = $this->server->api->entity->get($this->riding); + if($e instanceof Entity){ + $this->speedX = $this->speedY = $this->speedZ = 0; + + $e->positionRider($e->dead); + + $this->fallDistance = 0; + }else{ + ConsoleAPI::warn("$this is riding invalid entity({$this->riding})"); + } + } + } + public function update($now){ if($this->closed === true){ return false; @@ -751,6 +796,10 @@ class Entity extends Position } ++$this->counter; + if($this->riding != 0){ + $this->handleRiding(); + } + if($this->isStatic === false){ if(!$this->isPlayer()){ $this->updateLast(); @@ -1135,66 +1184,80 @@ class Entity extends Position $this->server->api->dhandle("entity.metadata", $this); } + public function sendLinkPackets(Player $player){ + if($this->rider != 0){ + $e = $this->level->entityList[$this->rider] ?? false; + + if($e != false) { + $e->spawn($player); + $player->eventHandler(["rider" => $this->rider, "riding" => $this->eid, "type" => 0], "entity.link"); + } + } + if($this->riding != 0){ + $e = $this->level->entityList[$this->riding] ?? false; + + if($e != false) { + $e->spawn($player); + $player->eventHandler(["rider" => $this->eid, "riding" => $this->riding, "type" => 0], "entity.link"); + } + } + } + /** * @param Player $player * @return boolean */ - public function spawn($player) + public function spawn(Player $player) { if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } - switch($this->class) { - case ENTITY_PLAYER: - if($this->player->connected !== true or $this->player->spawned === false){ - return false; - } - - $player->addEntity($this); - if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); - $pk = new AddPlayerPacket(); - $pkm = new MovePlayerPacket(); - $pk->clientID = 0; // $this->player->clientID; - $pk->username = $this->player->username; - $pkm->eid = $pk->eid = $this->eid; - - if($this->player->isInvisibleFor($player)){ - $pkm->x = $pk->x = -256; - $pkm->y = $pk->y = 128; - $pkm->z = $pk->z = -256; - $pkm->yaw = $pk->yaw = 0; - $pkm->pitch = $pk->pitch = 0; - $pkm->bodyYaw = 0; - }else{ - $pkm->x = $pk->x = $this->x; - $pkm->y = $pk->y = $this->y; - $pkm->z = $pk->z = $this->z; - $pkm->yaw = $pk->yaw = $this->yaw; - $pkm->pitch = $pk->pitch = $this->pitch; - $pkm->bodyYaw = $this->headYaw; - } - - $pk->itemID = 0; - $pk->itemAuxValue = 0; - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) - $player->entityQueueDataPacket($pkm); - - $pk = new PlayerEquipmentPacket(); - $pk->eid = $this->eid; - $pk->item = $this->player->getSlot($this->player->slot)->getID(); - $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); - $pk->slot = 0; - $player->entityQueueDataPacket($pk); - $this->player->sendArmor($player); - break; + if($this->class != ENTITY_PLAYER) return false; + if($this->player->connected !== true or $this->player->spawned === false) return false; + if(!$player->hasEntity($this)){ + $player->addEntity($this); + if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); + $pk = new AddPlayerPacket(); + $pkm = new MovePlayerPacket(); + $pk->clientID = 0; // $this->player->clientID; + $pk->username = $this->player->username; + $pkm->eid = $pk->eid = $this->eid; + + if($this->player->isInvisibleFor($player)){ + $pkm->x = $pk->x = -256; + $pkm->y = $pk->y = 128; + $pkm->z = $pk->z = -256; + $pkm->yaw = $pk->yaw = 0; + $pkm->pitch = $pk->pitch = 0; + $pkm->bodyYaw = 0; + }else{ + $pkm->x = $pk->x = $this->x; + $pkm->y = $pk->y = $this->y; + $pkm->z = $pk->z = $this->z; + $pkm->yaw = $pk->yaw = $this->yaw; + $pkm->pitch = $pk->pitch = $this->pitch; + $pkm->bodyYaw = $this->headYaw; + } + + $pk->itemID = 0; + $pk->itemAuxValue = 0; + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) + $player->entityQueueDataPacket($pkm); + + $pk = new PlayerEquipmentPacket(); + $pk->eid = $this->eid; + $pk->item = $this->player->getSlot($this->player->slot)->getID(); + $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); + $pk->slot = 0; + $player->entityQueueDataPacket($pk); + $this->player->sendArmor($player); + + $this->sendLinkPackets($player); + return true; } - - if($this->linkedEntity != 0 && $this->isRider){ - $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it - } - + return false; } public function counterUpdate(){ @@ -1222,6 +1285,12 @@ class Entity extends Position { if($this->closed === false){ $this->closed = true; + + + $rider = $this->level->entityList[$this->rider] ?? false; + if($rider != false) $rider->setRiding(null); + $this->setRiding(null); + $this->server->api->entity->remove($this->eid); } } @@ -1476,21 +1545,6 @@ class Entity extends Position return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause, allowHarm: false); } - public function stopRiding(){ - if(isset($this->level->entityList[$this->linkedEntity])){ - $e = $this->level->entityList[$this->linkedEntity]; - if(!$e->dead && !$e->closed){ - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => 0, "type" => 1]); - $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => 0, "type" => 1]); - $this->isRider = false; - $this->linkedEntity = 0; - $e->linkedEntity = 0; - } - }else{ - $this->linkedEntity = 0; - } - } - public function setPos($x, $y, $z){ $this->x = $x; $this->y = $y; @@ -1505,25 +1559,32 @@ class Entity extends Position $this->boundingBox->maxZ = $z + $this->radius; } - public function setRiding(Entity $e, $type = 0) + /** + * Sets entity that is being ridden by this entity. + * @param ?Entity $riding + */ + public function setRiding(?Entity $riding) { - if(!isset($this->level->entityList[$e->eid])){ - ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); - return; - } - - if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ - $this->linkedEntity = 0; - $e->linkedEntity = 0; - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => $type]); + if($riding == null){ + if($this->riding != 0){ + $riding = $this->server->api->entity->get($this->riding); + if($riding instanceof Entity){ + $riding->rider = 0; + }else{ + ConsoleAPI::warn("SR Failed to get entity that is being ridden by $this"); + } + } + $this->riding = 0; }else{ - $this->linkedEntity = $e->eid; - $e->linkedEntity = $this->eid; - $this->isRider = true; - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); + if(!isset($this->level->entityList[$riding->eid])){ + ConsoleAPI::warn("Tried linking $this with $riding that doesnt exist in the world!"); + return; + } + $this->riding = $riding->eid; + $riding->rider = $this->eid; + } - - + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $this->riding, "type" => 0]); } public function isPlayer() @@ -1725,7 +1786,7 @@ class Entity extends Position public function __toString() { - return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; + return "Entity(eid={$this->eid},x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; } /** @@ -1741,7 +1802,7 @@ class Entity extends Position */ /** * - * @deprecated Use {@link getHeightOf} or {@link getWidthOf} instead + * @deprecated Use {@link Entity::getHeightOf} or {@link Entity::getWidthOf} instead * @throws Exception */ public static function getSizeOf($e) @@ -1751,12 +1812,18 @@ class Entity extends Position /** - * - * @deprecated Use {@link getHeight} or {@link getWidth} instead + * @deprecated Use {@link Entity::getHeight} or {@link Entity::getWidth} instead * @throws Exception */ public function getSize() { throw new Exception("Use getHeight or getWidth method instead of this."); } + + /** + * @deprecated use {@link Entity::setRiding} instead + */ + public function stopRiding(){ + $this->setRiding(null); + } } diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index e68ff77e..b6a916b1 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -199,22 +199,25 @@ abstract class Projectile extends Entity{ return $data; } - public function spawn($player) + public function spawn(Player $player) { - $player->addEntity($this); - - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $pk->did = 0; - $player->entityQueueDataPacket($pk); - return true; + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = 0; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } } \ No newline at end of file diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 1e1eec00..de5e27ac 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -92,6 +92,7 @@ class MobController public function movementTick(){ $this->entity->moveForward = 0; + if($this->updateMove){ $this->updateMove = false; $v1 = floor($this->entity->boundingBox->minY + 0.5); diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index f7156bfa..48b0f278 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -34,7 +34,7 @@ class Pig extends Animal implements Rideable{ } public function canRide($e){ - return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; + return $this->isSaddled() && $this->rider == 0; } public function updateEntityMovement(){ diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 4b26a8a0..c98ec521 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -245,13 +245,11 @@ class Arrow extends Entity{ $this->doBlocksCollision(); } - - - } - public function spawn($player){ - if($this->type === OBJECT_ARROW){ + public function spawn(Player $player){ + if(!($this->type == OBJECT_ARROW)) return false; + if(!$player->hasEntity($this)){ $player->addEntity($this); $pk = new AddEntityPacket; $pk->eid = $this->eid; @@ -264,6 +262,9 @@ class Arrow extends Entity{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; } + return false; } } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 41c59d8a..10f93c2d 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -21,18 +21,23 @@ class FallingSand extends Entity{ } public function spawn($player) { - $player->addEntity($this); - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $pk->did = -$this->data["Tile"]; - $player->entityQueueDataPacket($pk); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = -$this->data["Tile"]; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } public function update($now){ diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 825f031c..1c25bf30 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -42,27 +42,32 @@ class ItemEntity extends Entity{ } - public function spawn($player) + public function spawn(Player $player) { - $player->addEntity($this); - $pk = new AddItemEntityPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddItemEntityPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->roll = 0; + $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index cdd29dbb..c7711d73 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -72,6 +72,10 @@ class Minecart extends Vehicle{ return !$this->dead; } + public function getRideHeight(){ + return ($this->height * 0) - 0.3; + } + public function getDrops(){ return [ [MINECART, 0, 1] @@ -166,8 +170,8 @@ class Minecart extends Vehicle{ $this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal; $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; - if($this->linkedEntity != 0 && !$this->isRider){ - $rider = $this->level->entityList[$this->linkedEntity] ?? false; + if($this->rider != 0){ + $rider = $this->level->entityList[$this->rider] ?? false; if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){ if($rider->moveForward > 0){ $v32 = sin($this->yaw * M_PI / 180); @@ -201,7 +205,7 @@ class Minecart extends Vehicle{ $this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z); $dx = $this->speedX; $dz = $this->speedZ; - if($this->linkedEntity != 0 && !$this->isRider){ + if($this->rider != 0){ $dx *= 0.75; $dz *= 0.75; } @@ -283,7 +287,7 @@ class Minecart extends Vehicle{ public function applyNaturalSlowdown(){ - $mult = $this->linkedEntity != 0 && !$this->isRider ? 0.997 : 0.96; + $mult = $this->rider != 0 ? 0.997 : 0.96; $this->speedX *= $mult; $this->speedY = 0; @@ -394,7 +398,7 @@ class Minecart extends Vehicle{ $ind = "$chunkX $chunkZ"; foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ $e = ($this->level->entityList[$entid] ?? null); - if($e instanceof Entity && $e->eid != $this->eid && $e->eid != $this->linkedEntity){ + if($e instanceof Entity && $e->eid != $this->eid && $e->eid != $this->rider){ if($e->isPushable() && $e->boundingBox->intersectsWith($bb)){ if($e->isPlayer()){ $this->applyCollision($e); @@ -412,38 +416,35 @@ class Minecart extends Vehicle{ public function close() { parent::close(); - if($this->linkedEntity != 0){ - $ent = $this->level->entityList[$this->linkedEntity] ?? false; - if($ent instanceof Entity){ - $ent->stopRiding(); - }else{ - ConsoleAPI::warn("$this is being ridden by invalid entity {$this->linkedEntity}"); - } - } } public function isPushable(){ return false; //TODO replace with true } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddEntityPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; //+ $this->yOffset; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket; - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; //+ $this->yOffset; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } public function interactWith(Entity $e, $action){ @@ -451,14 +452,14 @@ class Minecart extends Vehicle{ $e->setRiding($this); return true; } - if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->linkedEntity){ + if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->rider){ return false; //TODO more vanilla way? } parent::interactWith($e, $action); } public function canRide($e) { - return $this->linkedEntity == 0 && $e->linkedEntity == 0; + return $this->rider == 0; } } diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 8c2794eb..86e625ff 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -197,15 +197,20 @@ class Painting extends Entity{ return $data; } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddPaintingPacket; - $pk->eid = $this->eid; - $pk->x = (int) $this->xPos; - $pk->y = (int) $this->yPos; - $pk->z = (int) $this->zPos; - $pk->direction = $this->direction; - $pk->title = $this->motive; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddPaintingPacket; + $pk->eid = $this->eid; + $pk->x = (int) $this->xPos; + $pk->y = (int) $this->yPos; + $pk->z = (int) $this->zPos; + $pk->direction = $this->direction; + $pk->title = $this->motive; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 0544eb69..9714663a 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -65,18 +65,24 @@ class PrimedTNT extends Entity{ $this->lastUpdate = $now; } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddEntityPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->did = 1; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->did = 1; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; + } } \ No newline at end of file From 77a896b18ce42ebd4f12d20bd7ce76cbe15bfd79 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 16 Jul 2025 12:38:29 +0300 Subject: [PATCH 104/146] removed add mob spam --- src/entity/Creature.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index d2a83eca..61ee671b 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -62,7 +62,6 @@ abstract class Creature extends Living{ } if(!$player->hasEntity($this)){ $player->addEntity($this); - console("ADD MOB"); $pk = new AddMobPacket; $pk->eid = $this->eid; $pk->type = $this->type; From fc2e104c418bf149a8d279033ddb1b2002c2ccbd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 16 Jul 2025 14:41:27 +0300 Subject: [PATCH 105/146] hotbar is full of air in spectator mode now --- src/Player.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index ad226049..44a61f57 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1535,7 +1535,7 @@ class Player{ } if($this->sendingInventoryRequired){ - if(($this->gamemode & 0x01) !== CREATIVE){ + if($this->gamemode !== CREATIVE){ $hotbar = []; foreach($this->hotbar as $slot){ $hotbar[] = $slot <= -1 ? -1 : $slot + 9; @@ -2380,7 +2380,12 @@ class Player{ } $data["slot"] = $packet->slot; - + + if($this->gamemode == VIEW && $packet->item != AIR){ + $this->sendInventory(); + break; + } + if($this->server->handle("player.equipment.change", $data) !== false){ if(!Player::$experimentalHotbar) $this->slot = $packet->slot; if(($this->gamemode & 0x01) === SURVIVAL){ From bbe9e0ee13866f0960451b6476ace4e7fd2011c6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 16 Jul 2025 19:16:11 +0300 Subject: [PATCH 106/146] fixed enitites being able to clip through 0 barrier --- src/entity/Entity.php | 8 ++++---- src/math/AxisAlignedBB.php | 13 ++++++++++++- src/world/Level.php | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 8ef686cd..0d3970c0 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -586,7 +586,7 @@ class Entity extends Position } } - + public function move($dx, $dy, $dz){ $movX = $this->x; $movY = $this->y; @@ -610,6 +610,7 @@ class Entity extends Position $aaBBs = $this->level->getCubes($this, $this->boundingBox->addCoord($dx, $dy, $dz)); + foreach($aaBBs as $bb){ $dy = $bb->calculateYOffset($this->boundingBox, $dy); } @@ -618,14 +619,13 @@ class Entity extends Position foreach($aaBBs as $bb){ $dx = $bb->calculateXOffset($this->boundingBox, $dx); } - $this->boundingBox->offset($dx, 0, 0); + $this->boundingBox->offset($dx, 0, 0); foreach($aaBBs as $bb){ $dz = $bb->calculateZOffset($this->boundingBox, $dz); } $this->boundingBox->offset(0, 0, $dz); - $fallingFlag = $this->onGround || $savedDY != $dy && $savedDY < 0; if($this->stepHeight > 0 && $fallingFlag && ($savedDX != $dx || $savedDZ != $dz)){ @@ -666,6 +666,7 @@ class Entity extends Position $this->modifySpeedY = true; $this->modifedSpeedY = 0.5; } + } $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; @@ -677,7 +678,6 @@ class Entity extends Position $this->isCollided = $this->isCollidedHorizontally || $this->isCollidedVertically; $this->updateFallState($this->speedY); - if($savedDX != $dx) $this->speedX = 0; if($savedDY != $dy) $this->speedY = 0; if($savedDZ != $dz) $this->speedZ = 0; diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index 187700ca..a186fa68 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -75,7 +75,14 @@ class AxisAlignedBB{ $this->maxX += $x; $this->maxY += $y; $this->maxZ += $z; - + + if($this->minX < 1e-6 && $this->minX > -1e-6)$this->minX = 0; + if($this->minY < 1e-6 && $this->minY > -1e-6)$this->minY = 0; + if($this->minZ < 1e-6 && $this->minZ > -1e-6)$this->minZ = 0; + if($this->maxX < 1e-6 && $this->maxX > -1e-6)$this->maxX = 0; + if($this->maxY < 1e-6 && $this->maxY > -1e-6)$this->maxY = 0; + if($this->maxZ < 1e-6 && $this->maxZ > -1e-6)$this->maxZ = 0; + return $this; } @@ -170,6 +177,10 @@ class AxisAlignedBB{ return $z; } + public function intersectsWith_(AxisAlignedBB $bb){ + return $bb->maxX > $this->minX && $bb->minX < $this->maxX && $bb->maxY > $this->minY && $bb->minY < $this->maxY && $bb->maxZ > $this->minZ && $bb->minZ < $this->maxZ; + } + public function intersectsWith(AxisAlignedBB $bb){ return $bb->maxX >= $this->minX && $bb->minX <= $this->maxX && $bb->maxZ >= $this->minZ && $bb->minZ <= $this->maxZ && $bb->maxY >= $this->minY && $bb->minY <= $this->maxY; } diff --git a/src/world/Level.php b/src/world/Level.php index 2f1cc931..bd18ffb7 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -277,7 +277,7 @@ class Level{ $blockBounds = StaticBlock::$prealloc[$bid]::getCollisionBoundingBoxes($this, $x, $y, $z, $e); //StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); foreach($blockBounds as $blockBound){ - if($aABB->intersectsWith($blockBound)) $aABBs[] = $blockBound; + if($aABB->intersectsWith_($blockBound)) $aABBs[] = $blockBound; } } } From ba4ccafa32d415421ca8612e9079c9c2b5203a80 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 17 Jul 2025 05:27:15 +0300 Subject: [PATCH 107/146] PlayerAPI::remove removes a player from the table by his clientID, packets wont be handled after the player was kicked due to player already ingame --- src/API/PlayerAPI.php | 6 +++++- src/Player.php | 4 ++-- src/PocketMinecraftServer.php | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index d043412f..4ea9b42a 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -521,7 +521,11 @@ class PlayerAPI{ if($player->username != "" and ($player->data instanceof Config)){ $this->saveOffline($player->data); } - $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); + + $this->server->preparedSQL->player->deleteCID->reset(); + $this->server->preparedSQL->player->deleteCID->bindValue(":CID", $CID); + $this->server->preparedSQL->player->getEq->execute(); + $this->server->api->entity->remove($player->eid); unset($player->level->players[$player->CID]); if($player->entity instanceof Entity){ diff --git a/src/Player.php b/src/Player.php index 44a61f57..2dacbb63 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2115,8 +2115,8 @@ class Player{ } $u = $this->server->api->player->get($this->iusername, false); if($u !== false){ - $u = $this->server->clients[$this->CID]; - $u->close("this player already in game"); + $this->close("this player already in game"); + return; } $this->server->api->player->add($this->CID); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index a9ef9924..0473610d 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -176,6 +176,7 @@ class PocketMinecraftServer{ $this->preparedSQL->entity->setPosition = $this->database->prepare("UPDATE entities SET x = :x, y = :y, z = :z, pitch = :pitch, yaw = :yaw WHERE EID = :eid ;"); $this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;"); + $this->preparedSQL->player->deleteCID = $this->database->prepare("DELETE FROM players WHERE CID = :cid;"); $this->preparedSQL->player->getEq = $this->database->prepare("SELECT ip,port,name FROM players WHERE name = :name;"); //'$name' $this->preparedSQL->player->getLike = $this->database->prepare("SELECT ip,port,name FROM players WHERE name LIKE :name;"); //'$name' From aa39b77156880365678a39253ba419580a6d1a61 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 17 Jul 2025 05:27:48 +0300 Subject: [PATCH 108/146] one small change --- src/PocketMinecraftServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 0473610d..71688810 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -176,7 +176,7 @@ class PocketMinecraftServer{ $this->preparedSQL->entity->setPosition = $this->database->prepare("UPDATE entities SET x = :x, y = :y, z = :z, pitch = :pitch, yaw = :yaw WHERE EID = :eid ;"); $this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;"); - $this->preparedSQL->player->deleteCID = $this->database->prepare("DELETE FROM players WHERE CID = :cid;"); + $this->preparedSQL->player->deleteCID = $this->database->prepare("DELETE FROM players WHERE CID = :CID;"); $this->preparedSQL->player->getEq = $this->database->prepare("SELECT ip,port,name FROM players WHERE name = :name;"); //'$name' $this->preparedSQL->player->getLike = $this->database->prepare("SELECT ip,port,name FROM players WHERE name LIKE :name;"); //'$name' From 26516699c7b38c8121ca458a4d6b71f6dafcfdc5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 17 Jul 2025 13:53:33 +0300 Subject: [PATCH 109/146] fixed some players being invisible if they join while other players are joining --- src/API/EntityAPI.php | 4 ++-- src/API/PlayerAPI.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 049fbbe2..002220a8 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -215,8 +215,8 @@ class EntityAPI{ public function spawnToAll(Entity $e){ foreach($this->server->api->player->getAll($e->level) as $player){ - if($player->eid != false and $player->eid != $e->eid and $e->class != ENTITY_PLAYER and $e instanceof Entity){ - if($e->closed !== false or ($player->level !== $e->level and $e->class !== ENTITY_PLAYER)){ + if($player->spawned && $player->eid != false && $player->eid != $e->eid && $e->class != ENTITY_PLAYER && $e instanceof Entity){ + if($e->closed !== false || ($player->level !== $e->level && $e->class !== ENTITY_PLAYER)){ return false; } diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 4ea9b42a..5ffaa4a1 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -497,7 +497,7 @@ class PlayerAPI{ public function spawnToAllPlayers(Player $player){ foreach($this->getAll() as $p){ - if($p !== $player and ($p->entity instanceof Entity) and ($player->entity instanceof Entity)){ + if($p->spawned && $p !== $player && ($p->entity instanceof Entity) && ($player->entity instanceof Entity)){ $player->entity->spawn($p); if($p->level !== $player->level){ $pk = new MoveEntityPacket_PosRot; From 187a2d0805c6843819c61d197c7acd4f016aab62 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 18 Jul 2025 17:35:49 +0300 Subject: [PATCH 110/146] switching gamemode to 1/3 wont remove items from survival inventory --- src/API/PlayerAPI.php | 1 + src/Player.php | 61 +++++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 5ffaa4a1..f8986212 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -397,6 +397,7 @@ class PlayerAPI{ $player = $this->server->clients[$CID]; $player->data = $this->getOffline($player->username); $player->gamemode = $player->data->get("gamemode"); + $player->saveInventory = ($player->gamemode & 1) == SURVIVAL; if(($player->level = $this->server->api->level->get($player->data->get("position")["level"])) === false){ $player->level = $this->server->api->level->getDefault(); $player->data->set("position", [ diff --git a/src/Player.php b/src/Player.php index 2dacbb63..c194bcf6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -34,6 +34,7 @@ class Player{ public $windowCnt = 2; public $windows = []; public $blocked = true; + public $saveInventory = true; public $achievements = []; public $chunksLoaded = []; public $lastCorrect; @@ -169,6 +170,7 @@ class Player{ $this->inventory = []; $this->armor = []; $this->gamemode = $this->server->gamemode; + $this->saveInventory = ($this->gamemode & 1) == SURVIVAL; //survival, adventure $this->level = $this->server->api->level->getDefault(); $this->slot = 0; $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; @@ -1378,13 +1380,14 @@ class Player{ return false; } + $this->save(); $inv = &$this->inventory; if($gm === VIEW){ $this->armor = []; $this->sendArmor(); } if(($this->gamemode & 0x01) === ($gm & 0x01)){ - if(($gm & 0x01) === 0x01 and ($gm & 0x02) === 0x02){ + if(($gm & 0x01) === 0x01 && ($gm & 0x02) === 0x02){ //gm == VIEW $inv = []; foreach(BlockAPI::$creative as $item){ $inv[] = BlockAPI::getItem(0, 0, 1); @@ -1396,6 +1399,7 @@ class Player{ } } $this->gamemode = $gm; + $this->saveInventory = ($this->gamemode & 1) == SURVIVAL; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ".\n"); }else{ foreach($this->inventory as $slot => $item){ @@ -1404,9 +1408,13 @@ class Player{ $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; $this->lastCorrect = $this->entity->copy(); $this->blocked = true; + if(($this->gamemode & 1) != ($gm & 1)){ + $this->saveInventory = false; + } $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ", you've to do a forced reconnect.\n"); $this->server->schedule(30, [$this, "close"], "gamemode change", false, true); //Forces a kick + } if($this->gamemode === SPECTATOR){ @@ -1948,24 +1956,31 @@ class Player{ "y" => $this->spawnPosition->y, "z" => $this->spawnPosition->z ]); - $inv = []; - foreach($this->inventory as $slot => $item){ - if($item instanceof Item){ - if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)){ - $inv[$slot] = [$item->getID(), $item->getMetadata(), $item->count]; + + if($this->saveInventory){ + $inv = []; + foreach($this->inventory as $slot => $item){ + if($item instanceof Item){ + if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)){ + $inv[$slot] = [$item->getID(), $item->getMetadata(), $item->count]; + } } } + $this->data->set("inventory", $inv); } - $this->data->set("inventory", $inv); + $this->data->set("hotbar", $this->hotbar); - $armor = []; - foreach($this->armor as $slot => $item){ - if($item instanceof Item){ - $armor[$slot] = [$item->getID(), $item->getMetadata()]; + if($this->saveInventory){ + $armor = []; + foreach($this->armor as $slot => $item){ + if($item instanceof Item){ + $armor[$slot] = [$item->getID(), $item->getMetadata()]; + } } + $this->data->set("armor", $armor); } - $this->data->set("armor", $armor); + if($this->entity instanceof Entity){ $this->data->set("health", $this->entity->getHealth()); } @@ -2131,9 +2146,10 @@ class Player{ } $this->auth = true; + + $inv = []; if(!$this->data->exists("inventory") || ($this->gamemode & 0x01) === 0x01){ if(($this->gamemode & 0x01) === 0x01){ - $inv = []; if(($this->gamemode & 0x02) === 0x02){ foreach(BlockAPI::$creative as $item){ $inv[] = [0, 0, 1]; @@ -2144,12 +2160,17 @@ class Player{ } } } - $this->data->set("inventory", $inv); + }else{ + $inv = []; + foreach($this->data->get("inventory") as $slot => $item){ + $inv[$slot] = [$item[0], $item[1], $item[2]]; + } } + $this->achievements = $this->data->get("achievements"); $this->data->set("caseusername", $this->username); $this->inventory = []; - foreach($this->data->get("inventory") as $slot => $item){ + foreach($inv as $slot => $item){ if(!is_array($item) or count($item) < 3){ $item = [AIR, 0, 0]; } @@ -2157,8 +2178,10 @@ class Player{ } $this->armor = []; - foreach($this->data->get("armor") as $slot => $item){ - $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); + if(($this->gamemode & 0x01) == 0){ + foreach($this->data->get("armor") as $slot => $item){ + $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); + } } $this->data->set("lastIP", $this->ip); @@ -3123,7 +3146,7 @@ class Player{ break; case ProtocolInfo::SET_ENTITY_LINK_PACKET: if($this->entity->riding != 0){ - $this->entity->stopRiding(); + $this->entity->setRiding(null); } break; case ProtocolInfo::PLAYER_INPUT_PACKET: @@ -3136,7 +3159,7 @@ class Player{ $e = $this->entity->level->entityList[$this->entity->riding] ?? false; if($e === false) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->riding})"); - $this->entity->stopRiding(); + $this->entity->setRiding(null); break; } } From 92e1411ed3a7bcd7121b9c1e81b31f6ec7db9706 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 18 Jul 2025 17:44:37 +0300 Subject: [PATCH 111/146] show error in console if information about the phar plugin was not found --- src/API/PluginAPI.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 5e98b6ce..100671eb 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -149,6 +149,10 @@ class PluginAPI extends stdClass{ break; } } + if(count($pluginInfo) == 0){ + ConsoleAPI::error("Failed to load PHAR plugin from $file: pluginInfo has no information(PHAR was made for PMMP 1.4+?)"); + continue; + } console("[INFO] Loading PHAR plugin \"".FORMAT_GREEN.$pluginInfo["name"].FORMAT_RESET."\" ".FORMAT_AQUA.$pluginInfo["version"].FORMAT_RESET." by ".FORMAT_AQUA.$pluginInfo["author"].FORMAT_RESET); $aver = CURRENT_API_VERSION; From a61d4936c580f6c77c694a7738cbb8271876cc30 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 19 Jul 2025 14:55:40 +0300 Subject: [PATCH 112/146] hotbar will save in creative mode --- src/API/BlockAPI.php | 29 ++++++++++++++++++++-- src/API/PlayerAPI.php | 30 ++++++++++++++--------- src/Player.php | 56 +++++++++++++++++++++++++++++++------------ 3 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ed8d47c1..dcf00cc1 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -1,9 +1,9 @@ server = ServerAPI::request(); + foreach(static::$creative as $slot => [$id, $meta]){ + $rind = ($id << 24) | ($meta); + static::$reverseCreativeLookup[$rind] = $slot; + } + + self::$creativeHotbarSlots[0] = static::hasCreativeItem(STONE, 0); + self::$creativeHotbarSlots[1] = static::hasCreativeItem(COBBLESTONE, 0); + self::$creativeHotbarSlots[2] = static::hasCreativeItem(DIRT, 0); + self::$creativeHotbarSlots[3] = static::hasCreativeItem(PLANKS, 0); + self::$creativeHotbarSlots[4] = static::hasCreativeItem(PLANKS, 1); + self::$creativeHotbarSlots[5] = static::hasCreativeItem(TORCH, 0); + self::$creativeHotbarSlots[6] = static::hasCreativeItem(BRICK_STAIRS, 0); + self::$creativeHotbarSlots[7] = static::hasCreativeItem(COBBLE_WALL, 0); + self::$creativeHotbarSlots[8] = static::hasCreativeItem(SAPLING, 0); + } public static function get($id, $meta = 0, $v = false){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f8986212..21859b06 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -429,7 +429,6 @@ class PlayerAPI{ "z" => $this->server->spawn->z, ], "inventory" => array_fill(0, PLAYER_SURVIVAL_SLOTS, [AIR, 0, 0]), - "hotbar" => [0, 1, 2, 3, 4, 5, 6, 7, 8], "armor" => array_fill(0, 4, [AIR, 0]), "gamemode" => $this->server->gamemode, "health" => 20, @@ -439,23 +438,32 @@ class PlayerAPI{ "slot-count" => 7, "bed-position" => null, ]; - - if(!file_exists(DATA_PATH . "players/" . $iname . ".yml")){ + $ev = true; + $sav = false; + if(!file_exists(DATA_PATH . "players/$iname.yml")){ if(PocketMinecraftServer::$SAVE_PLAYER_DATA && $create){ - console("[NOTICE] Player data not found for \"" . $iname . "\", creating new profile"); - $data = new Config(DATA_PATH . "players/" . $iname . ".yml", CONFIG_YAML, $default); - $data->save(); + console("[NOTICE] Player data not found for \"$iname\", creating new profile"); + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $sav = true; }else{ - return new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $ev = false; } + }else{ + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); } - - $data = new Config(DATA_PATH . "players/" . $iname . ".yml", CONFIG_YAML, $default); - + if(($data->get("gamemode") & 0x01) === 1){ $data->set("health", 20); } - $this->server->handle("player.offline.get", $data); + if(!$data->exists("hotbar")){ + if(($data->get("gamemode") & 1) == CREATIVE) $hb = BlockAPI::$creativeHotbarSlots; + else $hb = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + $data->set("hotbar", $hb); + } + if($ev) $this->server->handle("player.offline.get", $data); + if($sav) $data->save(); + return $data; } diff --git a/src/Player.php b/src/Player.php index c194bcf6..258b5a99 100755 --- a/src/Player.php +++ b/src/Player.php @@ -30,6 +30,7 @@ class Player{ public $armor = []; public $loggedIn = false; public $gamemode; + public $gmKickSoon = false; public $lastBreak; public $windowCnt = 2; public $windows = []; @@ -1405,7 +1406,7 @@ class Player{ foreach($this->inventory as $slot => $item){ $inv[$slot] = BlockAPI::getItem(AIR, 0, 0); } - $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + $this->hotbar = $gm == CREATIVE ? BlockAPI::$creativeHotbarSlots : [0, 1, 2, 3, 4, 5, 6, 7, 8]; $this->lastCorrect = $this->entity->copy(); $this->blocked = true; if(($this->gamemode & 1) != ($gm & 1)){ @@ -1413,6 +1414,7 @@ class Player{ } $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ", you've to do a forced reconnect.\n"); + $this->gmKickSoon = true; $this->server->schedule(30, [$this, "close"], "gamemode change", false, true); //Forces a kick } @@ -1543,17 +1545,29 @@ class Player{ } if($this->sendingInventoryRequired){ - if($this->gamemode !== CREATIVE){ + + $pk = new ContainerSetContentPacket; + $pk->windowid = 0; + if($this->gmKickSoon){ + $pk->slots = []; + $hotbar = []; + }else if($this->gamemode !== CREATIVE){ $hotbar = []; foreach($this->hotbar as $slot){ $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } - $pk = new ContainerSetContentPacket; - $pk->windowid = 0; $pk->slots = $this->inventory; - $pk->hotbar = $hotbar; - $this->dataPacket($pk); + }else{ + $pk->slots = []; + for($i = 0; $i < 9; ++$i){ + [$id, $meta] = BlockAPI::$creative[$this->hotbar[$i]] ?? [0, 0]; + $pk->slots[$i] = BlockAPI::getItem($id, $meta, 1); + } + $hotbar = [9, 10, 11, 12, 13, 14, 15, 16, 17]; + } + $pk->hotbar = $hotbar; + $this->dataPacket($pk); $this->sendingInventoryRequired = false; } @@ -2194,18 +2208,17 @@ class Player{ $pk->status = 0; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x01){ - $this->slot = 0; - $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ + if($this->data->exists("hotbar")){ $this->hotbar = $this->data->get("hotbar"); - $this->slot = $this->hotbar[0]; }else{ - $this->slot = 0; - $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + if(($this->gamemode & 0x01) === 0x01) $this->hotbar = BlockAPI::$creativeHotbarSlots; + else $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; } + + $this->slot = $this->hotbar[0]; + for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? + if($this->hotbar[$i] > (($this->gamemode & 1) == 0 ? 36 : count(BlockAPI::$creative))) $this->hotbar[$i] = -1; if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; } if($this->data->exists("slot-count")){ @@ -2438,6 +2451,7 @@ class Player{ array_unshift($this->hotbar, $this->slot); } } + if(Player::$experimentalHotbar) $this->sendInventory(); }else{ $this->slot = $packet->slot; @@ -2979,8 +2993,20 @@ class Player{ if($this->spawned === false or $this->blocked === true){ break; } - if($packet->windowid === 0){ //crafting + + if(($this->gamemode & 1) == CREATIVE){ //sets hotbar item - works only in creative(-based) mode + if($this->gamemode == VIEW) break; //TODO maybe send inventory? + if($packet->slot < 0 || $packet->slot > 8) break; + $it = $packet->item; + $yes = BlockAPI::hasCreativeItem($it->getID(), $it->getMetadata()); + if($yes === false){ //TODO maybe send inventory + break; + } + $this->hotbar[$packet->slot] = $yes; + return; + } + $slot = $this->getSlot($packet->slot); $citem = $packet->item; From cc3f333660a14de8bc984cd879f072aabb7da073 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 20 Jul 2025 13:30:49 +0300 Subject: [PATCH 113/146] fixed memory leak during light recalculation, importing the world saves nc level data and uses blocklight from vanilla world --- src/pmf/PMFLevel.php | 3 +-- src/world/Level.php | 1 + src/world/LevelImport.php | 8 ++++++-- src/world/PocketChunkParser.php | 7 +++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index af575251..0d03bd15 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -402,7 +402,7 @@ class PMFLevel extends PMF{ foreach($this->checkLight as $index => [$X, $Z]){ $update = false; if(!$this->hasBlocklight($X, $Z)){ - ConsoleAPI::notice("Chunk $X $Z (Level: {$this->level->getName()} has no blocklight! Forcing light update(it might take a while)."); + ConsoleAPI::notice("Chunk $X $Z (Level: {$this->level->getName()}) has no blocklight! Forcing light update(it might take a while)."); $this->level->updateLight(0, $X*16, 0, $Z*16, $X*16+15, 127, $Z*16+15); $update = true; $this->markHasBlocklight($X, $Z); @@ -550,7 +550,6 @@ class PMFLevel extends PMF{ if($old_m != $m){ $this->chunks[$index][$Y][$mindex] = chr($m); - if(!isset($this->chunkChange[$index][$Y])){ $this->chunkChange[$index][$Y] = 1; }else{ diff --git a/src/world/Level.php b/src/world/Level.php index bd18ffb7..30f15d8a 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -710,6 +710,7 @@ class Level{ if(!--$limit) return true; }while($this->lightUpdateIndx >= 0); if(count($this->lightUpdates) > 0) return true; + $this->lightUpdates = []; //php uses a lot of memory without this thing if it performed a lot of light updates return false; } public function onTick(PocketMinecraftServer $server, $currentTime){ diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index 49f21773..0e41139d 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -66,14 +66,17 @@ class LevelImport{ 6 => "", 7 => "" ]; + $pmf->markHasBlocklight($X, $Z); + for($z = 0; $z < 16; ++$z){ for($x = 0; $x < 16; ++$x){ $block = $chunks->getChunkColumn($X, $Z, $x, $z, 0); $meta = $chunks->getChunkColumn($X, $Z, $x, $z, 1); + $blocklight = $chunks->getChunkColumn($X, $Z, $x, $z, 3); for($Y = 0; $Y < 8; ++$Y){ $chunk[$Y] .= substr($block, $Y << 4, 16); $chunk[$Y] .= substr($meta, $Y << 3, 8); - $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00"; + $chunk[$Y] .= substr($blocklight, $Y << 3, 8); } } } @@ -84,7 +87,8 @@ class LevelImport{ } console("[NOTICE] Importing level " . ceil(($Z + 1) / 0.16) . "%"); } - $chunks->map = null; + $pmf->writeNCData(); + $chunks = null; if(file_exists($this->path . "level.dat")) @unlink($this->path . "level.dat"); if(file_exists($this->path . "level.dat_old")) @unlink($this->path . "level.dat_old"); diff --git a/src/world/PocketChunkParser.php b/src/world/PocketChunkParser.php index 2c85bb4a..1620bf83 100644 --- a/src/world/PocketChunkParser.php +++ b/src/world/PocketChunkParser.php @@ -7,6 +7,13 @@ class PocketChunkParser{ var $map; //21 * $sectorLength private $location, $raw = b"", $file; + public function __destruct(){ + unset($this->location); + unset($this->raw); + unset($this->file); + unset($this->map); + } + public function loadFile($file){ if(file_exists($file . ".gz")){ $this->raw = gzinflate(file_get_contents($file . ".gz")); From 24d5b8f1f52bf013a369cbac23dc8b1387f01e30 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 20 Jul 2025 16:55:05 +0300 Subject: [PATCH 114/146] allow using tools even if their metadata doesnt match on server and client --- src/Player.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 258b5a99..4249ded9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2510,8 +2510,12 @@ class Player{ $pmeta = ($packet->meta & 0xff); $hmeta = ($heldItem->getMetadata() & 0xff); //in 0.8.1 useitempacket uses byte for meta if($heldItem->getID() != $packet->item || $hmeta != $pmeta){ - ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside({$packet->item} {$pmeta}) and serverside({$heldItem->getID()} {$hmeta}) when using item. Resending inventory."); $this->sendInventory(); + if($heldItem->getID() == $packet->item && $heldItem->getMaxDurability() != false){ //id matches, meta is used as durability -> allow to perform whatever action the player wants to do + goto allow_use_item; + } + ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside({$packet->item} {$pmeta}) and serverside({$heldItem->getID()} {$hmeta}) when using item. Resending inventory."); + if($packet->face >= 0 && $packet->face <= 5){ $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -2520,6 +2524,7 @@ class Player{ } break; } + allow_use_item: if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place if($this->entity->inAction === true){ From b05ca0e1b87a8f3f13c092ecdbe928d612df4e45 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 23 Jul 2025 17:24:13 +0300 Subject: [PATCH 115/146] using bonemeal on grass sends inventory --- src/material/block/solid/GrassBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index f3b49baa..252579c2 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -19,7 +19,7 @@ class GrassBlock extends SolidBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); TallGrassObject::growGrass($this->level, $this, new Random(), 8, 2); return true; }elseif($item->isHoe()){ From 21f106f66c91c398a311ce57c03d344632f27990 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 25 Jul 2025 01:34:20 +0300 Subject: [PATCH 116/146] Player::addItem will attempt to drop items, fixed crafting with full inventory --- src/Player.php | 74 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/src/Player.php b/src/Player.php index 4249ded9..89d8a29c 100755 --- a/src/Player.php +++ b/src/Player.php @@ -236,7 +236,14 @@ class Player{ return null; } + /** + * @param int $slot inventory slot. -1 - drop + * @param int $id + * @param int $meta + * @param int $count + */ public function addExpectedSetSlotPacket(int $slot, int $id, int $meta, int $count){ + ConsoleAPI::debug("Adding expected set slot: $slot $id $meta $count"); $this->expectedSetSlotPackets[++$this->expectedSetSlotIndex]["$slot $id $meta $count"] = $this->server->ticks; } @@ -1204,11 +1211,11 @@ class Player{ * * @return boolean */ - public function addItem($type, $damage, $count, $send = true, $addexpected = true){ - + public function addItem($type, $damage, $count, $send = true, $addexpected = true, $drop=true){ + $toadd = BlockAPI::getItem($type, $damage, $count); foreach($this->inventory as $s => $item){ //force check the inventory for non-full stacks of this item first if($item->getID() === $type and $item->getMetadata() === $damage){ - $add = min($item->getMaxStackSize() - $item->count, $count); + $add = min($toadd->getMaxStackSize() - $item->count, $count); if($add <= 0){ continue; @@ -1221,7 +1228,7 @@ class Player{ if($count <= 0) return true; } } - $toadd = BlockAPI::getItem($type, $damage, $count); + foreach($this->inventory as $s => $item){ if($item->getID() === AIR){ $add = min($toadd->getMaxStackSize(), $count); @@ -1235,7 +1242,26 @@ class Player{ } } if($count <= 0) return true; - return false; + if(!$drop) return false; + + while($count > 0){ + $stacksz = min($toadd->getMaxStackSize(), $count); + $count -= $stacksz; + + $f1 = 0.3; + $sX = -sin(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; + $sZ = cos(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; + $sY = -sin(($this->entity->pitch / 180) * M_PI) * $f1 + 0.1; + $f1 = 0.02; + $f3 = $this->entity->random->nextFloat() * M_PI * 2.0; + $f1 *= $this->entity->random->nextFloat(); + $sX += cos($f3) * $f1; + $sY += ($this->entity->random->nextFloat() - $this->entity->random->nextFloat()) * 0.1; + $sZ += sin($f3) * $f1; + $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, BlockAPI::getItem($type, $damage, $stacksz), $sX, $sY, $sZ); + if($addexpected) $this->addExpectedSetSlotPacket(-1, $type, $damage, $stacksz); + } + return true; } /** @@ -2509,6 +2535,7 @@ class Player{ $heldItem = $this->getHeldItem(); $pmeta = ($packet->meta & 0xff); $hmeta = ($heldItem->getMetadata() & 0xff); //in 0.8.1 useitempacket uses byte for meta + if($heldItem->getID() != $packet->item || $hmeta != $pmeta){ $this->sendInventory(); if($heldItem->getID() == $packet->item && $heldItem->getMaxDurability() != false){ //id matches, meta is used as durability -> allow to perform whatever action the player wants to do @@ -2856,11 +2883,28 @@ class Player{ $packet->eid = $this->eid; $prevItem = $packet->item; $newItem = $this->getHeldItem(); - //TODO it is possible to drop non-held item in vanilla, but in 99% cases it wouldnt happen + + if(($n = $this->isExpectedSetSlot(-1, $prevItem))){ + ConsoleAPI::debug("{$this->username}: Expected drop slot dropped"); + $this->removeExpectedSetSlot($n); + return; + } + if($newItem->getID() != $prevItem->getID() || $newItem->getMetadata() != $prevItem->getMetadata()){ + + if(count($this->inventory) >= 36){ + foreach($this->inventory as $slot => $item){ + if($item->getID() == 0) goto inv_desync_on_drop; + } + + $this->addCraftingResult(-1, $prevItem->getID(), $prevItem->getMetadata(), $prevItem->count); + break; + } + inv_desync_on_drop: + ConsoleAPI::debug("{$this->username} tried dropping item from non-held slot or inventory desynchronized."); $this->sendInventory(); - return; + break; } if($newItem->count < $prevItem->count){ @@ -2873,22 +2917,6 @@ class Player{ }else{ $packet->item = $newItem; } - $sendOnDrop = false; - - if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ - if(count($this->inventory) >= 36){ - foreach($this->inventory as $slot => $item){ - if($item->getID() == 0) goto inv_desync_on_drop; - } - - $this->addCraftingResult(-1, $prevItem->getID(), $prevItem->getMetadata(), $prevItem->count); - break; - }else{ - inv_desync_on_drop: - ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); - $sendOnDrop = true; - } - } $this->craftingItems = []; $this->toCraft = []; From e827c1ed1521d63104a80623b6a54eeaf69f43b4 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 7 Aug 2025 20:41:48 +0300 Subject: [PATCH 117/146] close connection if no login packet was sent 20 seconds after the connection was established --- src/Player.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 89d8a29c..0465d181 100755 --- a/src/Player.php +++ b/src/Player.php @@ -31,7 +31,7 @@ class Player{ public $loggedIn = false; public $gamemode; public $gmKickSoon = false; - public $lastBreak; + public $lastBreak, $connectionStartedAt; public $windowCnt = 2; public $windows = []; public $blocked = true; @@ -162,6 +162,7 @@ class Player{ $this->MTU = min($MTU, 1100); $this->server = ServerAPI::request(); $this->lastBreak = microtime(true); + $this->connectionStartedAt = microtime(true); $this->clientID = $clientID; $this->CID = PocketMinecraftServer::clientID($ip, $port); $this->ip = $ip; @@ -1547,6 +1548,12 @@ class Player{ return false; } $time = microtime(true); + + if($this->loggedIn === false && ($time - $this->connectionStartedAt) >= 20){ + $this->close("timeout"); + return false; + } + if($time > $this->timeout){ $this->close("timeout"); return false; From 8046d0e17c20dd13b04edc2254629667379fb0f1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 10 Aug 2025 21:11:42 +0300 Subject: [PATCH 118/146] hopefiully fixed some warning eqozqq has --- src/Player.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 0465d181..7758c880 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1593,7 +1593,8 @@ class Player{ }else{ $pk->slots = []; for($i = 0; $i < 9; ++$i){ - [$id, $meta] = BlockAPI::$creative[$this->hotbar[$i]] ?? [0, 0]; + if(!isset($this->hotbar[$i])) [$id, $meta] = [0, 0]; + else [$id, $meta] = BlockAPI::$creative[$this->hotbar[$i]] ?? [0, 0]; $pk->slots[$i] = BlockAPI::getItem($id, $meta, 1); } $hotbar = [9, 10, 11, 12, 13, 14, 15, 16, 17]; From 2a827c42c4034baa3173312174622a4896925e30 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 12 Sep 2025 12:54:49 +0300 Subject: [PATCH 119/146] added ability to add comments for entries in .properties files, deleted experimental-mob-ai since it does nothing --- src/API/ServerAPI.php | 10 +++++++++ src/PocketMinecraftServer.php | 38 +++++++++++++++++++++++++++++++-- src/entity/ai/MobController.php | 1 - src/utils/Config.php | 25 ++++++++++++++++------ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index b990a8c6..7cc5cab4 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -137,6 +137,16 @@ class ServerAPI{ "auto-save" => true, "enable-mob-ai" => true, "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT + ], comments: [ + "level-type" => [ + "Alowed types:", + "FLAT - flat world", + "DEFAULT - use one of the 4 pregenerated worlds from pocketmine", + "VANILLA - unfinished, attempts to generate vanilla 0.8.1 world. This geneeator is very slow." + ], + "abort-reading-after-N-packets" => [ + "Sets max amount of packets the server can read before forcing tick check." + ] ]); Entity::$allowFly = $this->getProperty("allow-flight", true); if(!Entity::$allowFly){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 71688810..fdbc0ffd 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -95,7 +95,6 @@ class PocketMinecraftServer{ "discord-bot-name" => "NostalgiaCore Logger", "despawn-mobs" => true, "mob-despawn-ticks" => 18000, - "experimental-mob-ai" => false, "force-20-tps" => false, "enable-mob-pushing" => Living::$entityPushing, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, @@ -106,6 +105,42 @@ class PocketMinecraftServer{ "allow-dropping-single-items" => Player::$allowDroppingSingleItems, "enable-light-updates" => self::$ENABLE_LIGHT_UPDATES, "min-block-breaking-progress" => self::$BLOCK_BREAKING_PROGRESS, + ], comments: [ + "min-block-breaking-progress" => [ + "Affects how strict block breaking speed check is.", + "0 - allow blocks to be mined instantly", + "0.8 - allow breaking blocks after 80% of vanilla block break time.", + "The value should be in range from 0 to 1." + ], + "enable-light-updates" => [ + "Enabling light updates may affects performance and will increase world generation time.", + "Disabling light updates will disable all light-related features(like light-based mob spawning)" + ], + "allow-dropping-single-items" => [ + "Allows players to drop any amount of items from stack in the slot.", + "While it is not possible using vanilla client, some custom clients(like Ninecraft) have this feature" + ], + "disable-emojis-in-chat" => [ + "Disallows players to send certain symbols in chat to prevent client lags" + ], + "use-experimental-hotbar" => [ + "Changes serverside hotbar behavior to be more like in 0.8 rather than 0.7(when disabled)", + "May cause more problems than old hotbar" + ], + "enable-mob-pushing" => [ + "Enables or disables serverside mob pushing", + "If enabled it may cause performance issues when there are a lot of mobs in a small enclosed area", + "If disabled it will cause small entity position desync(that should be fixed every time a server sends a new movement packet to the client)" + ], + "force-20-tps" => [ + "Forces server to run at 20 tps on some platforms where sleeping for small amount of time is not possible(hi windows & 16 tps)", + ], + "discord-ru-smiles" => [ + "Replaces cyrillic symbols Ы Ь Ъ Ё with emojis when sending a message using discord webhook(as it is done in some custom clients)" + ], + "keep-chunks-loaded" => [ + "If disabled chunks won't be loaded into memory until the player(or something else) loads them" + ] ]); self::$BLOCK_BREAKING_PROGRESS = $this->extraprops->get("min-block-breaking-progress"); Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); @@ -120,7 +155,6 @@ class PocketMinecraftServer{ self::$KEEP_CHUNKS_LOADED = $this->extraprops->get("keep-chunks-loaded"); self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); - MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); if(self::$FORCE_20_TPS){ diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index de5e27ac..27909a8f 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -2,7 +2,6 @@ class MobController { - public static $ADVANCED = false; public static $AUTOJUMP = false; public static $landed = false; public static $DANGEROUS_BLOCKS = [ diff --git a/src/utils/Config.php b/src/utils/Config.php index 23b3c153..cd38811c 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -35,6 +35,9 @@ class Config{ * @var array */ private $config; + + private $comments; + /** * @var string */ @@ -54,8 +57,8 @@ class Config{ * @param array $default * @param null|boolean $correct */ - public function __construct($file, $type = CONFIG_DETECT, $default = [], &$correct = null){ - $this->load($file, $type, $default); + public function __construct($file, $type = CONFIG_DETECT, $default = [], &$correct = null, $comments = []){ + $this->load($file, $type, $default, comments:$comments); $correct = $this->check(); } @@ -66,13 +69,14 @@ class Config{ * * @return boolean */ - public function load($file, $type = CONFIG_DETECT, $default = []){ + public function load($file, $type = CONFIG_DETECT, $default = [], $comments = []){ $this->correct = true; $this->type = (int) $type; $this->file = $file; if(!is_array($default)){ $default = []; } + $this->comments = $comments; if(!file_exists($file)){ $this->config = $default; $this->save(); @@ -113,9 +117,8 @@ class Config{ if(!is_array($this->config)){ $this->config = $default; } - if($this->fillDefaults($default, $this->config) > 0){ - $this->save(); - } + $this->fillDefaults($default, $this->config); + $this->save(); }else{ return false; } @@ -158,6 +161,16 @@ class Config{ private function writeProperties(){ $content = "#Properties Config file\r\n#" . date("D M j H:i:s T Y") . "\r\n"; foreach($this->config as $k => $v){ + //evil \r\n + if(isset($this->comments[$k])){ + $com = $this->comments[$k]; + $gen = ""; + if(!is_array($com)) $gen .= "#$com\r\n"; + else{ + foreach($com as $lin) $gen .= "#$lin\r\n"; + } + $content .= $gen; + } if(is_bool($v) === true){ $v = $v === true ? "on" : "off"; }elseif(is_array($v)){ From 52d03da9384837b464d8b06f9e3db7ba189f1a70 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 6 Oct 2025 02:02:50 +0300 Subject: [PATCH 120/146] moved php version check into the config.php file --- src/config.php | 118 +++---------------------------------------- src/config_post.php | 116 ++++++++++++++++++++++++++++++++++++++++++ src/dependencies.php | 5 -- 3 files changed, 122 insertions(+), 117 deletions(-) create mode 100644 src/config_post.php diff --git a/src/config.php b/src/config.php index afc3247b..d29cb707 100644 --- a/src/config.php +++ b/src/config.php @@ -1,116 +1,10 @@ 0){ + echo "[ERROR] Use PHP >= 8.0.0 to launch this server software.\n"; + exit(1); } -if(ini_get("date.timezone") == ""){ //No Timezone set - date_default_timezone_set("GMT"); - if(str_contains(" " . strtoupper(php_uname("s")), " WIN")){ - - $regex = '/(UTC)(\+*\-*\d*\d*\:*\d*\d*)/'; - - /* - * wmic timezone get Caption - * Get the timezone offset - * - * Sample Output var_dump - * array(3) { - * [0] => - * string(7) "Caption" - * [1] => - * string(20) "(UTC+09:30) Adelaide" - * [2] => - * string(0) "" - * } - */ - exec("wmic timezone get Caption", $output); - - $string = trim(implode("\n", $output)); - - //Detect the Time Zone string - preg_match($regex, $string, $matches); - - if(!isset($matches[2]) or ($timezone = parseOffset($matches[2])) === false){ - $timezone = 'UTC'; - } - if(date_default_timezone_set($timezone)){ - ini_set("date.timezone", $timezone); - } - }else{ - exec("date +%s", $t); - $offset = round((intval(trim($t[0])) - time()) / 60) * 60; - $daylight = (int) date("I"); - $d = timezone_name_from_abbr("", $offset, $daylight); - @ini_set("date.timezone", $d); - date_default_timezone_set($d); - } -}else{ - $d = @date_default_timezone_get(); - if(!str_contains($d, "/")){ - $d = timezone_name_from_abbr($d); - @ini_set("date.timezone", $d); - date_default_timezone_set($d); - } -} - -gc_enable(); -error_reporting(E_ALL | E_STRICT); -ini_set("allow_url_fopen", 1); -ini_set("display_errors", 1); -ini_set("display_startup_errors", 1); -ini_set("default_charset", "utf-8"); -if(defined("POCKETMINE_COMPILE") and POCKETMINE_COMPILE === true){ - define("FILE_PATH", realpath(dirname(__FILE__)) . "/"); -}else{ - define("FILE_PATH", realpath(dirname(__FILE__) . "/../") . "/"); -} -set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH); - -ini_set("memory_limit", "256M"); //Default -define("LOG", true); -define("START_TIME", microtime(true)); -define("MAJOR_VERSION", "1.1.1dev"); -define("CODENAME", "Nostalgia"); //i'm not very creative - kotyaralih, yeah - tema1d. -define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); -define("CURRENT_API_VERSION", "12.2"); -define("CURRENT_PHP_VERSION", "8.0"); -$gitsha1 = false; -if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! - define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); -}else{ //Unknown :( - define("GIT_COMMIT", str_repeat("00", 20)); -} +require_once(dirname(__FILE__)."/config_post.php"); //that config uses modern php features which may cause errors before php version check has been completed \ No newline at end of file diff --git a/src/config_post.php b/src/config_post.php new file mode 100644 index 00000000..879a941a --- /dev/null +++ b/src/config_post.php @@ -0,0 +1,116 @@ + + * string(7) "Caption" + * [1] => + * string(20) "(UTC+09:30) Adelaide" + * [2] => + * string(0) "" + * } + */ + exec("wmic timezone get Caption", $output); + + $string = trim(implode("\n", $output)); + + //Detect the Time Zone string + preg_match($regex, $string, $matches); + + if(!isset($matches[2]) or ($timezone = parseOffset($matches[2])) === false){ + $timezone = 'UTC'; + } + if(date_default_timezone_set($timezone)){ + ini_set("date.timezone", $timezone); + } + }else{ + exec("date +%s", $t); + $offset = round((intval(trim($t[0])) - time()) / 60) * 60; + $daylight = (int) date("I"); + $d = timezone_name_from_abbr("", $offset, $daylight); + @ini_set("date.timezone", $d); + date_default_timezone_set($d); + } +}else{ + $d = @date_default_timezone_get(); + if(!str_contains($d, "/")){ + $d = timezone_name_from_abbr($d); + @ini_set("date.timezone", $d); + date_default_timezone_set($d); + } +} + +gc_enable(); +error_reporting(E_ALL | E_STRICT); +ini_set("allow_url_fopen", 1); +ini_set("display_errors", 1); +ini_set("display_startup_errors", 1); +ini_set("default_charset", "utf-8"); +if(defined("POCKETMINE_COMPILE") and POCKETMINE_COMPILE === true){ + define("FILE_PATH", realpath(dirname(__FILE__)) . "/"); +}else{ + define("FILE_PATH", realpath(dirname(__FILE__) . "/../") . "/"); +} +set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH); + +ini_set("memory_limit", "256M"); //Default +define("LOG", true); +define("START_TIME", microtime(true)); +define("MAJOR_VERSION", "1.1.1dev"); +define("CODENAME", "Nostalgia"); //i\'m not very creative - kotyaralih, yeah - tema1d. +define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); +define("CURRENT_API_VERSION", "12.2"); +define("CURRENT_PHP_VERSION", "8.0"); +$gitsha1 = false; +if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! + define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); +}else{ //Unknown :( + define("GIT_COMMIT", str_repeat("00", 20)); +} \ No newline at end of file diff --git a/src/dependencies.php b/src/dependencies.php index 5af85a1c..98f74b90 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -17,11 +17,6 @@ set_error_handler("error_handler", E_ALL); $errors = 0; -if(version_compare("8.0.0", PHP_VERSION) > 0){ - console("[ERROR] Use PHP >= 8.0.0", true, true, 0); - ++$errors; -} - if(php_sapi_name() !== "cli"){ console("[ERROR] You must run NostalgiaCore using the CLI.", true, true, 0); ++$errors; From 8362ed4211629c7560e1c99404ab9128936a55a9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 14 Oct 2025 12:05:06 +0300 Subject: [PATCH 121/146] added ticking-mode into server.properties, replaced force-20-tps with nodelay mode --- src/API/ServerAPI.php | 14 +++++++++++++- src/PocketMinecraftServer.php | 20 ++++++++++---------- src/network/UDPSocket.php | 11 +++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 7cc5cab4..169a3598 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -136,7 +136,8 @@ class ServerAPI{ "rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10), "auto-save" => true, "enable-mob-ai" => true, - "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT + "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT, + "ticking-mode" => "legacy", ], comments: [ "level-type" => [ "Alowed types:", @@ -146,6 +147,12 @@ class ServerAPI{ ], "abort-reading-after-N-packets" => [ "Sets max amount of packets the server can read before forcing tick check." + ], + "ticking-mode" => [ + "Sets ticking mode. Allowed modes:", + "legacy - sleep while waiting for the next tick. same ticking as in old PocketMine. The server won't use a lot of cpu, but tps may be not stable(causes 16 TPS on Windows)", + "nodelay - unused time is not used for anything.. May cause to use 100% of CPU.", + "netwait - uses unused time to wait for packets. Should always give max tps. Untested on windows." ] ]); Entity::$allowFly = $this->getProperty("allow-flight", true); @@ -156,6 +163,11 @@ class ServerAPI{ MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50); Entity::$allowedAI = $this->getProperty("enable-mob-ai", true); PocketMinecraftServer::$PACKET_READING_LIMIT = $this->getProperty("abort-reading-after-N-packets", PocketMinecraftServer::$PACKET_READING_LIMIT); + PocketMinecraftServer::$TICKING_MODE = match(strtolower($this->getProperty("ticking-mode"))){ + default => PocketMinecraftServer::TICK_LEGACY, + "nodelay" => PocketMinecraftServer::TICK_F20TPS, + "netwait" => PocketMinecraftServer::TICK_NETWAIT + }; //Load advanced properties define("DEBUG", $this->getProperty("debug", 1)); define("ADVANCED_CACHE", false); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index fdbc0ffd..150e3e90 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,7 +1,9 @@ "NostalgiaCore Logger", "despawn-mobs" => true, "mob-despawn-ticks" => 18000, - "force-20-tps" => false, "enable-mob-pushing" => Living::$entityPushing, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, "use-experimental-hotbar" => Player::$experimentalHotbar, @@ -132,9 +135,6 @@ class PocketMinecraftServer{ "If enabled it may cause performance issues when there are a lot of mobs in a small enclosed area", "If disabled it will cause small entity position desync(that should be fixed every time a server sends a new movement packet to the client)" ], - "force-20-tps" => [ - "Forces server to run at 20 tps on some platforms where sleeping for small amount of time is not possible(hi windows & 16 tps)", - ], "discord-ru-smiles" => [ "Replaces cyrillic symbols Ы Ь Ъ Ё with emojis when sending a message using discord webhook(as it is done in some custom clients)" ], @@ -151,13 +151,13 @@ class PocketMinecraftServer{ Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); - self::$FORCE_20_TPS = $this->extraprops->get("force-20-tps"); + self::$KEEP_CHUNKS_LOADED = $this->extraprops->get("keep-chunks-loaded"); self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); - if(self::$FORCE_20_TPS){ + if(self::$TICKING_MODE == self::TICK_F20TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } if(self::$BLOCK_BREAKING_PROGRESS > 1){ @@ -540,7 +540,7 @@ class PocketMinecraftServer{ public function process() { $lastLoop = 0; - if(self::$FORCE_20_TPS){ + if(self::$TICKING_MODE != self::TICK_LEGACY){ while($this->stop === false){ $packetcnt = 0; while($packet = $this->interface->readPacket()){ diff --git a/src/network/UDPSocket.php b/src/network/UDPSocket.php index 15cf0b3c..260a1d4b 100644 --- a/src/network/UDPSocket.php +++ b/src/network/UDPSocket.php @@ -3,7 +3,6 @@ class UDPSocket{ public $connected, $sock, $server, $port; - function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){ $this->server = $server; $this->port = $port; @@ -42,6 +41,14 @@ class UDPSocket{ if($this->connected === false){ return false; } + if(PocketMinecraftServer::$TICKING_MODE == PocketMinecraftServer::TICK_NETWAIT){ + $read = [$this->sock]; + $next = (ServerAPI::request()->lastTick + 0.05); + $waitTime = $next - microtime(true); + if($waitTime < 0) $waitTime = 0; + $c = socket_select($read, $_, $_, 0, (int)($waitTime*1000000)); + if($c === false || $c <= 0) return false; + } return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port); } @@ -52,4 +59,4 @@ class UDPSocket{ return @socket_sendto($this->sock, $data, strlen($data), 0, $dest, $port); } -} \ No newline at end of file +} From 20298d60fdeacb37dd9b5859964c98a9b95c7b10 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 14 Oct 2025 16:02:31 +0300 Subject: [PATCH 122/146] comments for server-type and generator-settings in server.properties --- src/API/ServerAPI.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 169a3598..9f570ead 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -153,6 +153,31 @@ class ServerAPI{ "legacy - sleep while waiting for the next tick. same ticking as in old PocketMine. The server won't use a lot of cpu, but tps may be not stable(causes 16 TPS on Windows)", "nodelay - unused time is not used for anything.. May cause to use 100% of CPU.", "netwait - uses unused time to wait for packets. Should always give max tps. Untested on windows." + ], + "server-type" => [ + "Changes server type. Does not affect anything in 0.8.1. Allowed types:", + "normal - changes unconnected_pong reply to use MCCPP;Demo;", + "demo - same as normal", + "minecon - changes unconnected_pong reply to use MCCPP;MINECON;", + ], + "generator-settings" => [ + "Used to set level settings when generating a world. Currently affects only FLAT world.", + "FLAT world preset format: VERSION;LAYERS;STARTY;PARAMS", + "The string contains multiple parameters split by ;", + "VERSION - preset version (2)", + "LAYERS - defines layers and their sizes", + " each layer is formatted as x", + " - block id to fill the layer", + " - layer height", + "Multiple layers are separated by ,", + "STARTY - start y level. Must be more than 0", + "PARAMS - additional patamaters", + " spawn(radius= block=) - sets spawn parameters", + " radius= - sets spawn radius to ", + " block= - sets block to fill spawn area with", + " decoration(treecount= grasscount=) - sets amount of decorations", + "Preset example:", + "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)" ] ]); Entity::$allowFly = $this->getProperty("allow-flight", true); From acd55cce964b808898e1fd32ab7732f31f5475a5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 17 Oct 2025 04:11:42 +0300 Subject: [PATCH 123/146] fixed #67 --- src/entity/object/ItemEntity.php | 11 ++--------- src/network/protocol/packet/AddItemEntityPacket.php | 12 ++++++------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 1c25bf30..0d77fbff 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -51,18 +51,11 @@ class ItemEntity extends Entity{ $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; + $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); + $pk->metadata = $this->getMetadata(); $player->entityQueueDataPacket($pk); $this->sendLinkPackets($player); return true; diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index 4c250c8d..131b8079 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -6,9 +6,9 @@ class AddItemEntityPacket extends RakNetDataPacket{ public $x; public $y; public $z; - public $yaw; - public $pitch; - public $roll; + public $speedX; + public $speedY; + public $speedZ; public function pid(){ return ProtocolInfo::ADD_ITEM_ENTITY_PACKET; @@ -25,9 +25,9 @@ class AddItemEntityPacket extends RakNetDataPacket{ $this->putFloat($this->x); $this->putFloat($this->y); $this->putFloat($this->z); - $this->putByte($this->yaw); - $this->putByte($this->pitch); - $this->putByte($this->roll); + $this->putByte((int)($this->speedX * 128)); + $this->putByte((int)($this->speedY * 128)); + $this->putByte((int)($this->speedZ * 128)); } public function eidsToLocal(Player $p){ if(!$this->localEids){ From 42fef5f812f12c2d6b40c274fd8c1f65874f1f6d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 23 Oct 2025 15:58:43 +0300 Subject: [PATCH 124/146] removed useless data when updating tile entity data --- src/world/Tile.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/world/Tile.php b/src/world/Tile.php index da471f13..261c657f 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -347,10 +347,6 @@ class Tile extends Position{ $nbt = new NBT(); $nbt->write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("id"); - $nbt->writeTAG_String($this->class); - $nbt->write(chr(NBT::TAG_INT)); $nbt->writeTAG_String("x"); $nbt->writeTAG_Int((int) $this->x); @@ -388,23 +384,19 @@ class Tile extends Position{ $nbt->write(chr(NBT::TAG_STRING)); $nbt->writeTAG_String("Text1"); - $nbt->writeTAG_String($this->data["Text1"]); + $nbt->writeTAG_String(mb_substr($this->data["Text1"], 0, 15)); $nbt->write(chr(NBT::TAG_STRING)); $nbt->writeTAG_String("Text2"); - $nbt->writeTAG_String($this->data["Text2"]); + $nbt->writeTAG_String(mb_substr($this->data["Text2"], 0, 15)); $nbt->write(chr(NBT::TAG_STRING)); $nbt->writeTAG_String("Text3"); - $nbt->writeTAG_String($this->data["Text3"]); + $nbt->writeTAG_String(mb_substr($this->data["Text3"], 0, 15)); $nbt->write(chr(NBT::TAG_STRING)); $nbt->writeTAG_String("Text4"); - $nbt->writeTAG_String($this->data["Text4"]); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("id"); - $nbt->writeTAG_String($this->class); + $nbt->writeTAG_String(mb_substr($this->data["Text4"], 0, 15)); $nbt->write(chr(NBT::TAG_INT)); $nbt->writeTAG_String("x"); From 627590cb86a3febcda41e31b8bd1ef58be1e9732 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 23 Oct 2025 18:26:01 +0300 Subject: [PATCH 125/146] fixed parseProperties parsing stuff inside comments --- src/utils/Config.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils/Config.php b/src/utils/Config.php index cd38811c..0664ad1a 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -185,7 +185,12 @@ class Config{ * @param $content */ private function parseProperties($content){ - if(preg_match_all('/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches + $content2 = ""; + foreach(explode("\n", $content) as $cnt){ //regex is very evil and malicious + if((trim($cnt)[0] ?? "") == "#") continue; + $content2 .= $cnt . "\n"; + } + if(preg_match_all("/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u", $content2, $matches) > 0){ //false or 0 matches foreach($matches[1] as $i => $k){ $v = trim($matches[2][$i]); switch(strtolower($v)){ From 3256a957d39c824cc042bd226dde5e60c1e294a1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 2 Nov 2025 11:29:56 +0300 Subject: [PATCH 126/146] slightly faster random block ticking --- src/world/Level.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/world/Level.php b/src/world/Level.php index 30f15d8a..8714b4aa 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -721,12 +721,15 @@ class Level{ for($cZ = 0; $cZ < 16; ++$cZ){ $index = $this->level->getIndex($cX, $cZ); if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) continue; + $ch = &$this->level->chunks[$index]; for($c = 0; $c <= 20; ++$c){ $xyz = mt_rand(0, 0xffffffff) >> 2; $x = $xyz & 0xf; $z = ($xyz >> 8) & 0xf; $y = ($xyz >> 16) & 0x7f; - $id = $this->level->fastGetBlockID($cX, $y >> 4, $cZ, $x, $y & 0xf, $z, $index); + $cy = $ch[$y >> 4]; + if($cy === false) continue; + $id = ord($cy[($y & 0xf) + ($x << 5) + ($z << 9)]); if(isset(self::$randomUpdateBlocks[$id])){ $cl = StaticBlock::$prealloc[$id]; $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); @@ -734,6 +737,7 @@ class Level{ } } } + $this->totalMobsAmount = 0; $post = []; From a6d96c4ac7943fd957cd0f53de26bb0690032d1e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 30 Nov 2025 01:55:31 +0300 Subject: [PATCH 127/146] added ice and snow layer melting based on light level --- src/material/block/nonfull/SnowLayerBlock.php | 6 ++++ src/material/block/solid/IceBlock.php | 30 ++++--------------- src/world/Level.php | 5 ++-- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/material/block/nonfull/SnowLayerBlock.php b/src/material/block/nonfull/SnowLayerBlock.php index 9d03ff58..ccabc6ed 100644 --- a/src/material/block/nonfull/SnowLayerBlock.php +++ b/src/material/block/nonfull/SnowLayerBlock.php @@ -28,6 +28,12 @@ class SnowLayerBlock extends FlowableBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } + + public static function onRandomTick(Level $level, $x, $y, $z){ + if($level->level->getBlockLight($x, $y, $z) > 11){ + $level->fastSetBlockUpdate($x, $y, $z, 0, true); + } + } public function getDrops(Item $item, Player $player){ if($item->isShovel() !== false){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 3b74cab2..89ebdd7a 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -29,33 +29,13 @@ class IceBlock extends TransparentBlock{ } return true; } - - private function scanForNearbyLightSources($offsetX, $offsetY, $offsetZ){ - for($x = -$offsetX; $x <= $offsetX; ++$x){ //i hope it is possible to optimize it - for($z = -$offsetZ; $z <= $offsetZ; ++$z){ - for($y = -$offsetY; $y <= $offsetY; ++$y){ - $pX = $this->x+$x; - $pY = $this->y+$y; - $pZ = $this->z+$z; - $block = $this->level->getBlock(new Vector3($pX, $pY, $pZ)); //D= slow what was u thinking of gameherobrine from 2022 - if($block instanceof LightingBlock){ //idk is it possible to make it better - return $block; - } - } - } + + public static function onRandomTick(Level $level, $x, $y, $z){ + if($level->level->getBlockLight($x, $y, $z) > 11 - StaticBlock::$lightBlock[ICE]){ + $level->fastSetBlockUpdate($x, $y, $z, STILL_WATER, true); } } - //public static function onUpdate(Level $level, $x, $y, $z, $type){ /*Taken from https://github.com/PocketMine/PocketMine-MP/issues/3249*/ - /*if($type === BLOCK_UPDATE_RANDOM){ - $light = $this->scanForNearbyLightSources(3,3,3); - if(LightUtils::getLightValueFromNearbySource($light,$this) > 12){ - $this->level->setBlock($this, new WaterBlock(), true); - ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), 10, BLOCK_UPDATE_NORMAL); //additional request for update - } - return BLOCK_UPDATE_RANDOM; - } - return false;*/ //TODO ice melting(after light?) - //} + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; diff --git a/src/world/Level.php b/src/world/Level.php index 8714b4aa..b14f7dc6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -43,8 +43,9 @@ class Level{ WHEAT_BLOCK => true, DIRT => true, GRASS => true, - ICE => true, - LEAVES => true + LEAVES => true, + SNOW_LAYER => true, + ICE => true ]; public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ From f81c8b17bf7b3658480b0451318cd4d7fcd56621 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 21 Dec 2025 14:57:27 +0300 Subject: [PATCH 128/146] added mushroom spreading --- src/API/ServerAPI.php | 5 ++ src/material/Block.php | 2 + .../block/plant/BaseMushroomBlock.php | 56 +++++++++++++++++++ .../block/plant/BrownMushroomBlock.php | 23 +------- src/material/block/plant/RedMushroomBlock.php | 22 +------- src/world/Level.php | 4 +- 6 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 src/material/block/plant/BaseMushroomBlock.php diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 9f570ead..4cadc27f 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -138,6 +138,7 @@ class ServerAPI{ "enable-mob-ai" => true, "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT, "ticking-mode" => "legacy", + "mushroom-spread" => Block::$mushroomSpread ], comments: [ "level-type" => [ "Alowed types:", @@ -178,8 +179,12 @@ class ServerAPI{ " decoration(treecount= grasscount=) - sets amount of decorations", "Preset example:", "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)" + ], + "mushroom-spread" => [ + "Enable or disable mushroom spreading" ] ]); + Block::$mushroomSpread = $this->getProperty("mushroom-spread", Block::$mushroomSpread); Entity::$allowFly = $this->getProperty("allow-flight", true); if(!Entity::$allowFly){ ConsoleAPI::warn("Fly checking is enabled! Players may experience issues with kicking while not flying!"); diff --git a/src/material/Block.php b/src/material/Block.php index 34209442..b26c2b6b 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -1,6 +1,8 @@ "AirBlock", STONE => "StoneBlock", diff --git a/src/material/block/plant/BaseMushroomBlock.php b/src/material/block/plant/BaseMushroomBlock.php new file mode 100644 index 00000000..a7e18958 --- /dev/null +++ b/src/material/block/plant/BaseMushroomBlock.php @@ -0,0 +1,56 @@ +hardness = 0; + $this->breakTime = 0; + $this->material = Material::$plant; + } + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(static::$blockID, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + } + } + + public static function onRandomTick(Level $level, $x, $y, $z){ + if(Block::$mushroomSpread/* && mt_rand(0, 24) == 0*/){ + $maxcnt = 5; + for($xx = $x-4; $xx <= $x+4; ++$xx){ + for($zz = $z-4; $zz <= $z+4; ++$zz){ + for($yy = $y-1; $yy <= $y+1; ++$yy){ + if($level->level->getBlockID($xx, $yy, $zz) == static::$blockID){ + if(--$maxcnt <= 0) return; + } + } + } + } + + for($att = 0; $att < 4; ++$att){ + $nx = $x + mt_rand(-1, 1); + $ny = $y + mt_rand(-1, 1); + $nz = $z + mt_rand(-1, 1); + if($ny < 127 && $ny > 0 && $level->level->getBlockID($nx, $ny, $nz) == 0){ + $idb = $level->level->getBlockID($nx, $ny-1, $nz); + if(!StaticBlock::getIsTransparent($idb)){ //TODO better canSurvive check + if($level->getRawBrightness($nx, $ny, $nz) <= 12){ + $level->fastSetBlockUpdate($nx, $ny, $nz, static::$blockID, 0); + return; + } + } + } + } + } + } + + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + $down = $this->getSide(0); + if($down->isTransparent === false){ + $this->level->setBlock($block, $this, true, false, true); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index e252640c..6cf2ebe1 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -1,28 +1,9 @@ hardness = 0; - $this->breakTime = 0; - $this->material = Material::$plant; $this->lightEmission = 1; } - - public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ - if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(BROWN_MUSHROOM)); - $level->fastSetBlockUpdate($x, $y, $z, 0, 0); - } - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->isTransparent === false){ - $this->level->setBlock($block, $this, true, false, true); - return true; - } - return false; - } } \ No newline at end of file diff --git a/src/material/block/plant/RedMushroomBlock.php b/src/material/block/plant/RedMushroomBlock.php index a3507380..d2564bb6 100644 --- a/src/material/block/plant/RedMushroomBlock.php +++ b/src/material/block/plant/RedMushroomBlock.php @@ -1,27 +1,9 @@ hardness = 0; - $this->breakTime = 0; - $this->material = Material::$plant; } - - public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ - if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(RED_MUSHROOM)); - $level->fastSetBlockUpdate($x, $y, $z, 0, 0); - } - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->isTransparent === false){ - $this->level->setBlock($block, $this, true, false, true); - return true; - } - return false; - } } \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index b14f7dc6..779afa59 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -45,7 +45,9 @@ class Level{ GRASS => true, LEAVES => true, SNOW_LAYER => true, - ICE => true + ICE => true, + RED_MUSHROOM => true, + BROWN_MUSHROOM => true ]; public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ From f93aa2601a7a6e93dd5499607c5cc4ebe8033290 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 21 Dec 2025 14:58:08 +0300 Subject: [PATCH 129/146] uncommented randomness --- src/material/block/plant/BaseMushroomBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/block/plant/BaseMushroomBlock.php b/src/material/block/plant/BaseMushroomBlock.php index a7e18958..02e5df30 100644 --- a/src/material/block/plant/BaseMushroomBlock.php +++ b/src/material/block/plant/BaseMushroomBlock.php @@ -16,7 +16,7 @@ class BaseMushroomBlock extends FlowableBlock{ } public static function onRandomTick(Level $level, $x, $y, $z){ - if(Block::$mushroomSpread/* && mt_rand(0, 24) == 0*/){ + if(Block::$mushroomSpread && mt_rand(0, 24) == 0){ $maxcnt = 5; for($xx = $x-4; $xx <= $x+4; ++$xx){ for($zz = $z-4; $zz <= $z+4; ++$zz){ From 4c072e91cd34bd83cf25d20971e909e325776781 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 3 Jan 2026 15:55:22 +0300 Subject: [PATCH 130/146] fixed player posiiton on bed, hopefully fixed players not receiving packets sometimes --- src/Player.php | 16 +++++++++++----- src/entity/Entity.php | 6 ++++-- src/material/block/misc/BedBlock.php | 2 ++ src/world/Explosion.php | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Player.php b/src/Player.php index 7758c880..4e53a623 100755 --- a/src/Player.php +++ b/src/Player.php @@ -274,9 +274,10 @@ class Player{ $this->data->set("bed-position", $this->bedPosition); } + /** - * @param Vector3 $pos - * + * @param Vector3 $pos position of the top block of the bed + * * @return boolean */ public function sleepOn(Position $pos){ @@ -291,9 +292,14 @@ class Player{ $this->setBedPosition($pos); $this->isSleeping = $pos; $this->sleepingTime = 0; - $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level), false, false, false, false); + $meta = $this->level->level->getBlockDamage($pos->x, $pos->y, $pos->z); + $xoff = BedBlock::PLAYER_BED_OFFS_X[$meta & 3]; + $zoff = BedBlock::PLAYER_BED_OFFS_Z[$meta & 3]; + $this->teleport(new Position($pos->x + $xoff, $pos->y + 0.9375, $pos->z + $zoff, $this->level), false, false, false, false); //TODO change player hitbox size after he starts sleeping if($this->entity instanceof Entity){ + $this->entity->forceUpdateMovement = true; + $this->entity->updateMovement(); $this->entity->updateMetadata(); } @@ -3468,8 +3474,8 @@ class Player{ $this->resendQueue[$count] =& $this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); - }else{ - ++$this->packetStats[1]; //lost and wont be recovered + }else{ //lost and wont be recovered + ++$this->packetStats[1]; } } break; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0d3970c0..1323cae5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -7,6 +7,7 @@ class Entity extends Position const CLASS_TYPE = - 1; const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player + public $forceUpdateMovement = false; public $searchForClosestPlayers = false; public $modifySpeedY = false; public $modifedSpeedY = 0.0; @@ -1029,7 +1030,8 @@ class Entity extends Position return false; } $now = microtime(true); - if($this->isStatic === false and ($this->lastX != $this->x or $this->lastY != $this->y or $this->lastZ != $this->z or $this->lastYaw != $this->yaw or $this->lastPitch != $this->pitch or $this->lastHeadYaw != $this->headYaw)){ + if($this->isStatic === false && ($this->lastX != $this->x || $this->lastY != $this->y || $this->lastZ != $this->z || $this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw || $this->forceUpdateMovement)){ + $this->forceUpdateMovement = false; if($this->class === ENTITY_PLAYER){ if($this->server->api->handle("entity.move", $this) === false){ if($this->class === ENTITY_PLAYER){ @@ -1043,7 +1045,7 @@ class Entity extends Position $pk = new MovePlayerPacket(); $pk->eid = $this->eid; $pk->x = $this->x; - $pk->y = $this->y; + $pk->y = $this->y - 1 - ($this->player->isSleeping ? 0.7375 : 0); $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index 58fb6c2b..8c63f82b 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -14,6 +14,8 @@ class BedBlock extends TransparentBlock{ [0, -1], [1, 0] ]; + const PLAYER_BED_OFFS_X = [0.5, 0.1, 0.5, 0.9]; + const PLAYER_BED_OFFS_Z = [0.9, 0.5, 0.1, 0.5]; public function __construct($type = 0){ parent::__construct(BED_BLOCK, $type, "Bed Block"); $this->isActivable = true; diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 25a05aef..abbbd233 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -97,7 +97,7 @@ class Explosion{ $pk->radius = $this->size; $pk->records = []; //ExplodePacket doesnt seem to use the records provided by this packet, so no need to send more data foreach ($this->level->players as $player){ - $player->blockQueueDataPacket($pk); + $player->blockQueueDataPacket(clone $pk); } return; } @@ -214,7 +214,7 @@ class Explosion{ $pk->radius = $this->size; $pk->records = []; //ExplodePacket doesnt seem to use the records provided by this packet, so no need to send more data foreach ($this->level->players as $player){ - $player->blockQueueDataPacket($pk); + $player->blockQueueDataPacket(clone $pk); } } From 8555a06ddad6aa352c6ebd530a494b6635215926 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 3 Jan 2026 15:57:35 +0300 Subject: [PATCH 131/146] small fix --- src/entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 1323cae5..e5097253 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1045,7 +1045,7 @@ class Entity extends Position $pk = new MovePlayerPacket(); $pk->eid = $this->eid; $pk->x = $this->x; - $pk->y = $this->y - 1 - ($this->player->isSleeping ? 0.7375 : 0); + $pk->y = $this->y - ($this->player->isSleeping ? 1.7375 : 0); $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; From 1272932257e5c002f3d7240a4a80a0c0f0e25224 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 4 Jan 2026 10:54:03 +0300 Subject: [PATCH 132/146] ported leaf decay logic from b1.7.3 --- src/entity/Projectile.php | 2 +- src/material/block/solid/LeavesBlock.php | 74 +++++++++++++++++++----- src/material/block/solid/WoodBlock.php | 6 +- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index b6a916b1..444edd73 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -77,7 +77,7 @@ abstract class Projectile extends Entity{ $this->lastSpeedY = $this->speedY; $this->lastSpeedZ = $this->speedZ; $this->needsUpdate = true; - $this->handleWaterMovement(); //TODO: maybe just call parent::update(); ? + $this->handleWaterMovement(); if($this->fire > 0){ if(!$this->isImmuneToFire){ if(($this->fire % 20) == 0){ diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index ee2e6a59..5129752b 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -27,6 +27,9 @@ class LeavesBlock extends TransparentBlock{ public static function createIndex($x, $y, $z){ return "$x.$y.$z"; } + /** + * @deprecated leaf decay was rewritten + */ public static function findLog(Level $level, $x, $y, $z, array &$visited, $distance){ //port from newest pocketmine $index = self::createIndex($x, $y, $z); if(isset($visited[$index])){ @@ -49,37 +52,76 @@ class LeavesBlock extends TransparentBlock{ return ($visited[$index] = false); } + private static $treeBlocksNearby = null; public static function onRandomTick(Level $level, $x, $y, $z){ $b = $level->level->getBlock($x, $y, $z); $id = $b[0]; $meta = $b[1]; - if(($meta & 0b00001100) === 0x08){ - $meta &= 0x03; - $visited = array(); - if(!self::findLog($level, $x, $y, $z, $visited, 0)){ - //$this->level->setBlock($this, new AirBlock(), false, false, true); - $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + //TODO MCPE uses 0x04 for detecting updates + if(($meta & 0b1100) == 0b1000){ + if(self::$treeBlocksNearby == null) self::$treeBlocksNearby = array_fill(0, 32*32*32, 0); + for ($xx = - 4; $xx <= 4; $xx ++) { + for ($yy = - 4; $yy <= 4; $yy ++) { + for ($zz = - 4; $zz <= 4; $zz ++) { + $k3 = $level->level->getBlockID($x + $xx, $y + $yy, $z + $zz); + if ($k3 == TRUNK) { + self::$treeBlocksNearby[($xx + 16) * 1024 + ($yy + 16) * 32 + ($zz + 16)] = 0; + } else if ($k3 == LEAVES) { + self::$treeBlocksNearby[($xx + 16) * 1024 + ($yy + 16) * 32 + ($zz + 16)] = - 2; + } else { + self::$treeBlocksNearby[($xx + 16) * 1024 + ($yy + 16) * 32 + ($zz + 16)] = - 1; + } + } + } + } + + for ($i2 = 1; $i2 <= 4; $i2 ++) { + for ($l2 = - 4; $l2 <= 4; $l2 ++) { + for ($j3 = - 4; $j3 <= 4; $j3 ++) { + for ($l3 = - 4; $l3 <= 4; $l3 ++) { + if (self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16) * 32 + ($l3 + 16)] != $i2 - 1) continue; + if (self::$treeBlocksNearby[(($l2 + 16) - 1) * 1024 + ($j3 + 16) * 32 + ($l3 + 16)] == - 2) { + self::$treeBlocksNearby[(($l2 + 16) - 1) * 1024 + ($j3 + 16) * 32 + ($l3 + 16)] = $i2; + } + if (self::$treeBlocksNearby[($l2 + 16 + 1) * 1024 + ($j3 + 16) * 32 + ($l3 + 16)] == - 2) { + self::$treeBlocksNearby[($l2 + 16 + 1) * 1024 + ($j3 + 16) * 32 + ($l3 + 16)] = $i2; + } + if (self::$treeBlocksNearby[($l2 + 16) * 1024 + (($j3 + 16) - 1) * 32 + ($l3 + 16)] == - 2) { + self::$treeBlocksNearby[($l2 + 16) * 1024 + (($j3 + 16) - 1) * 32 + ($l3 + 16)] = $i2; + } + if (self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16 + 1) * 32 + ($l3 + 16)] == - 2) { + self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16 + 1) * 32 + ($l3 + 16)] = $i2; + } + if (self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16) * 32 + (($l3 + 16) - 1)] == - 2) { + self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16) * 32 + (($l3 + 16) - 1)] = $i2; + } + if (self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16) * 32 + ($l3 + 16 + 1)] == - 2) { + self::$treeBlocksNearby[($l2 + 16) * 1024 + ($j3 + 16) * 32 + ($l3 + 16 + 1)] = $i2; + } + } + } + } + } + $j2 = self::$treeBlocksNearby[(32/2) * (32*32) + (32/2) * 32 + (32/2)]; + if($j2 >= 0) + { + $level->level->setBlockDamage($x, $y, $z, $meta & 0xf7); + } else + { if(mt_rand(1,20) === 1){ //Saplings ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(SAPLING, $meta & 0x03, 1)); } if(($meta & 0x03) === LeavesBlock::OAK and mt_rand(1,200) === 1){ //Apples ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(APPLE, 0, 1)); } - }else{ - $level->fastSetBlockUpdate($x, $y, $z, $id, $meta); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, false); } } } - public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ - [$id, $meta] = $level->level->getBlock($x, $y, $z); - if(($meta & 0b00001100) === 0){ - $meta |= 0x08; - $level->fastSetBlockUpdate($x, $y, $z, $id, $meta); //TODO maybe use $level->level->setBlock directly, the client doesnt need to know thism info. - } - } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $this->meta |= 0x04; + //TODO MCPE uses 0x08 for detecting was it placed by player or not + if($player instanceof Player && $player != PlayerNull::$INSTANCE) $this->meta |= 0x04; $this->level->setBlock($this, $this, true, false, true); } diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index c9d2fafa..4b688228 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -41,12 +41,12 @@ class WoodBlock extends SolidBlock{ for($xOff = -4; $xOff <= 4; ++$xOff){ for($yOff = -4; $yOff <= 4; ++$yOff){ for($zOff = -4; $zOff <= 4; ++$zOff){ - $b = $player->level->level->getBlock($this->x + $xOff, $this->y + $yOff, $this->z + $zOff); + $b = $this->level->level->getBlock($this->x + $xOff, $this->y + $yOff, $this->z + $zOff); $id = $b[0]; $meta = $b[1]; if($id === LEAVES){ - if(($meta & 0x8) === 0){ - $player->level->fastSetBlockUpdate($this->x + $xOff, $this->y + $yOff, $this->z + $zOff, $id, $meta | 8); + if(($meta & 8) == 0){//TODO MCPE uses 0x04 for detecting updates + $b = $this->level->level->setBlockDamage($this->x + $xOff, $this->y + $yOff, $this->z + $zOff, $meta | 8); } } } From 2f6e2ffcab819c1f2bb75a59516677ac4b10c2a8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 5 Jan 2026 10:39:06 +0300 Subject: [PATCH 133/146] some fixes to prevent deprecated warning spam on php 8.2 --- src/Player.php | 3 +-- src/entity/ai/tasks/TaskFollowParent.php | 1 + src/entity/ai/tasks/TaskMate.php | 1 + src/entity/ai/tasks/TaskRangedAttack.php | 2 +- src/entity/ai/tasks/TaskTempt.php | 2 ++ src/entity/monster/Zombie.php | 2 +- src/entity/object/Arrow.php | 1 + src/entity/object/ItemEntity.php | 2 +- src/utils/XorShift128Random.php | 1 + src/world/MobSpawner.php | 4 ++-- 10 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index 4e53a623..ad5db96b 100755 --- a/src/Player.php +++ b/src/Player.php @@ -961,7 +961,7 @@ class Player{ } $this->lastChunk = [$x, $z]; - if($this->lastOrderX != ($this->entity->x >> 4) || $this->lastOrderZ != ($this->entity->z >> 4)){ + if($this->lastOrderX != ((int)$this->entity->x >> 4) || $this->lastOrderZ != ((int)$this->entity->z >> 4)){ $this->orderChunks(); } $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); @@ -1940,7 +1940,6 @@ class Player{ } $this->entityMovementQueueLength += $len; - $this->entityMovementPacketsPerSecond += $packets; } /** diff --git a/src/entity/ai/tasks/TaskFollowParent.php b/src/entity/ai/tasks/TaskFollowParent.php index 99dcdbe6..0501e99b 100644 --- a/src/entity/ai/tasks/TaskFollowParent.php +++ b/src/entity/ai/tasks/TaskFollowParent.php @@ -2,6 +2,7 @@ class TaskFollowParent extends TaskBase { + public $speedMultiplier; public function __construct($speed){ $this->speedMultiplier = $speed; } diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php index 5bfdd26b..9aa7c584 100644 --- a/src/entity/ai/tasks/TaskMate.php +++ b/src/entity/ai/tasks/TaskMate.php @@ -2,6 +2,7 @@ class TaskMate extends TaskBase { + public $speedMultiplier; public function __construct($speed){ $this->speedMultiplier = $speed; } diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php index afba75fe..7267e8f1 100644 --- a/src/entity/ai/tasks/TaskRangedAttack.php +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -5,6 +5,7 @@ class TaskRangedAttack extends \TaskBase public $server; public $attackCounter = 0; public $seenTicks = 0; + public $rangeSquared = 0; public function __construct($speed, $range){ $this->speedMultiplier = $speed; $this->rangeSquared = $range*$range; @@ -14,7 +15,6 @@ class TaskRangedAttack extends \TaskBase public function onStart(EntityAI $ai) { $this->selfCounter = 1; - $this->ticksNoSeen = 0; } public function onEnd(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index eee2f39a..14081cd2 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -2,6 +2,8 @@ class TaskTempt extends TaskBase { + public $speedMultiplier; + public $targetDistance; public function __construct($speed){ $this->speedMultiplier = $speed; $this->targetDistance = 10*10; diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 95315008..f53d7248 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -32,7 +32,7 @@ class Zombie extends Monster{ } for($y = (int)$this->y; $y < 129; $y++){ - $block = $this->level->level->getBlockID($this->x, $y, $this->z); + $block = $this->level->level->getBlockID(floor($this->x), $y, floor($this->z)); if(StaticBlock::getIsSolid($block)){ return false; } diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index c98ec521..3175b23b 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -13,6 +13,7 @@ class Arrow extends Entity{ public $criticial = false; public $groundTicks = 0; public $shotByPlayer = false; + public $shotByEntity = false; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 0d77fbff..56b07195 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -200,7 +200,7 @@ class ItemEntity extends Entity{ $friction = 0.98; if($this->onGround){ $friction = 0.588; - $v3 = $this->level->level->getBlockID($this->x, floor($this->boundingBox->minY) - 1, $this->z); + $v3 = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); if($v3 > 0) $friction = StaticBlock::getSlipperiness($v3); } diff --git a/src/utils/XorShift128Random.php b/src/utils/XorShift128Random.php index 8c874065..0e69b02a 100644 --- a/src/utils/XorShift128Random.php +++ b/src/utils/XorShift128Random.php @@ -28,6 +28,7 @@ class XorShift128Random{ } public function setSeed($seed){ + $seed = (int)$seed; $this->x = 123456789 ^ $seed; $this->y = 362436069 ^ ($seed << 17) | (($seed >> 15) & 0x7fffffff) & 0xffffffff; $this->z = 521288629 ^ ($seed << 31) | (($seed >> 1 ) & 0x7fffffff) & 0xffffffff; diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index ff8de7ae..21091ccd 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -71,8 +71,8 @@ class MobSpawner{ continue; } - $x = mt_rand($player->entity->x - 32, $player->entity->x + 32); - $z = mt_rand($player->entity->z - 32, $player->entity->z + 32); + $x = mt_rand(floor($player->entity->x - 32), ceil($player->entity->x + 32)); + $z = mt_rand(floor($player->entity->z - 32), ceil($player->entity->z + 32)); $diffX = $x-$player->entity->x; $diffZ = $z-$player->entity->z; $dist = $diffX*$diffX + $diffZ*$diffZ; From 6af453dcc3e31fdc03adf8000dc40607ffad9fd2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 6 Jan 2026 08:58:35 +0300 Subject: [PATCH 134/146] better bed head block detection, fixed bonemeal not decrementing from slot when used, fixed players seeing teleport to -256,128,-256 when dying --- src/entity/Entity.php | 2 +- src/material/block/misc/BedBlock.php | 17 ++++------------- src/material/block/plant/BeetrootBlock.php | 2 +- src/material/block/plant/CarrotBlock.php | 2 +- src/material/block/plant/CyanFlowerBlock.php | 2 +- src/material/block/plant/DandelionBlock.php | 2 +- src/material/block/plant/MelonStemBlock.php | 2 +- src/material/block/plant/PotatoBlock.php | 2 +- src/material/block/plant/PumpkinStemBlock.php | 2 +- src/material/block/plant/SaplingBlock.php | 2 +- src/material/block/plant/SugarcaneBlock.php | 2 +- src/material/block/plant/WheatBlock.php | 2 +- 12 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e5097253..bfb6a2b1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1724,7 +1724,7 @@ class Entity extends Position $pk->yaw = 0; $pk->pitch = 0; foreach($this->level->players as $player){ - if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk); + if($player->hasEntity($this) && $player != $this->player) $player->entityQueueDataPacket(clone $pk); } }else{ $pk = new EntityEventPacket; diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index 8c63f82b..be845c70 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -84,22 +84,13 @@ class BedBlock extends TransparentBlock{ return true; } - $blockNorth = $this->getSide(2); //Gets the blocks around them - $blockSouth = $this->getSide(3); - $blockEast = $this->getSide(5); - $blockWest = $this->getSide(4); if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed $b = $this; }else{ //Bottom Part of Bed - if($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x08) === 0x08){ - $b = $blockNorth; - }elseif($blockSouth->getID() === $this->id and ($blockSouth->meta & 0x08) === 0x08){ - $b = $blockSouth; - }elseif($blockEast->getID() === $this->id and ($blockEast->meta & 0x08) === 0x08){ - $b = $blockEast; - }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ - $b = $blockWest; - }else{ + $dir = ($this->meta & 3); + $o = BedBlock::HEAD_DIRECTION_OFFSETS[$dir]; + $b = $this->level->getBlockWithoutVector($this->x + $o[0], $this->y, $this->z + $o[1]); + if($b->getID() != BED_BLOCK){ $player->sendChat("This bed is incomplete"); return true; } diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index 312b63bd..0c68a3b4 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -33,7 +33,7 @@ class BeetrootBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index 4bcd86b9..5abe9ad8 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -26,7 +26,7 @@ class CarrotBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/CyanFlowerBlock.php b/src/material/block/plant/CyanFlowerBlock.php index c48704e0..2674620b 100644 --- a/src/material/block/plant/CyanFlowerBlock.php +++ b/src/material/block/plant/CyanFlowerBlock.php @@ -38,7 +38,7 @@ class CyanFlowerBlock extends FlowableBlock{ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ $random = new Random(); self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index bc4d56f2..7388a132 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -33,7 +33,7 @@ class DandelionBlock extends FlowableBlock{ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ $random = new Random(); self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index 8042d889..fc4fb4be 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -104,7 +104,7 @@ class MelonStemBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index 15866171..9af1dbb3 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -35,7 +35,7 @@ class PotatoBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index 874e1a8e..42c966a1 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -103,7 +103,7 @@ class PumpkinStemBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index 49ae119e..d622130a 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -37,7 +37,7 @@ class SaplingBlock extends FlowableBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal TreeObject::growTree($this->level, $this, new Random(), $this->meta & 0x03); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/SugarcaneBlock.php b/src/material/block/plant/SugarcaneBlock.php index 279952e2..6e5bcb94 100644 --- a/src/material/block/plant/SugarcaneBlock.php +++ b/src/material/block/plant/SugarcaneBlock.php @@ -31,7 +31,7 @@ class SugarcaneBlock extends FlowableBlock{ $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $this->meta, true); } - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 3e25d2ea..caaab02a 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -26,7 +26,7 @@ class WheatBlock extends FlowableBlock{ $this->meta = 7; } $this->level->setBlock($this, $this, true, false, true); - $player->consumeSingleItem(); + $player->consumeSingleItem(send:true); return true; } return false; From d990c7c6b32416ea5fde38a6662b0e9111f58921 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 6 Jan 2026 09:41:18 +0300 Subject: [PATCH 135/146] idk maybe it will fix desyncs --- src/Player.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Player.php b/src/Player.php index ad5db96b..6e15baf9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1757,7 +1757,7 @@ class Player{ $len = 1 + strlen($pk->buffer); $MTU = $this->MTU - 24; if($len > $MTU) return $this->blockQueueDataPacket_big($pk); - if(($this->blockUpdateQueueLength + $len) >= $MTU){ + if(($this->blockUpdateQueueLength + $len+10) >= $MTU){ $this->sendBlockUpdateQueue(); } @@ -1801,7 +1801,7 @@ class Player{ $MTU = $this->MTU - 24; if($len > $MTU) return $this->entityQueueDataPacket_big($pk); - if(($this->entityDataQueueLength + $len) >= $MTU){ + if(($this->entityDataQueueLength + $len+10) >= $MTU){ $this->sendEntityDataQueue(); } @@ -2051,7 +2051,7 @@ class Player{ $MTU = $this->MTU - 24; if($len > $MTU) return $this->sendChatMessagePacket_big($packet); - if(($this->chatMessagesQueueLength + $len) >= $MTU) $this->sendChatBuffer(); + if(($this->chatMessagesQueueLength + $len+10) >= $MTU) $this->sendChatBuffer(); $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE_ORDERED; $packet->messageIndex = $this->counter[3]++; From f36ab5aa6c7d1f54371145cdca02a9ea84f3f500 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 Jan 2026 23:32:19 +0300 Subject: [PATCH 136/146] baby sheep color is based on its parents colours --- src/entity/Animal.php | 8 ++++---- src/entity/Breedable.php | 2 +- src/entity/ai/tasks/TaskMate.php | 2 +- src/entity/animal/Sheep.php | 13 +++++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 1f2bc748..f4098922 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -61,9 +61,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{ return $this->getAge() < 0; } - public function breed(){ - if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){ - $c = $this->spawnChild(); + public function breed($mate){ + if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "parent2" => $mate]) !== false){ + $c = $this->spawnChild($mate); $c->parent = $this; //TODO save eid to avoid memory leaks? $this->server->api->entity->spawnToAll($c); return $c; @@ -108,7 +108,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ $this->age = $i; } - public function spawnChild() + public function spawnChild($mate) { return $this->server->api->entity->add($this->level, $this->class, $this->type, [ "x" => $this->x + lcg_value() * mt_rand(-1, 1), diff --git a/src/entity/Breedable.php b/src/entity/Breedable.php index 59cc21ec..39c1ae6b 100644 --- a/src/entity/Breedable.php +++ b/src/entity/Breedable.php @@ -3,7 +3,7 @@ interface Breedable{ public function isFood($id); //original name - public function spawnChild(); + public function spawnChild($mate); public function isInLove(); } \ No newline at end of file diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php index 9aa7c584..71480b0c 100644 --- a/src/entity/ai/tasks/TaskMate.php +++ b/src/entity/ai/tasks/TaskMate.php @@ -43,7 +43,7 @@ class TaskMate extends TaskBase /** * @var Animal $baby */ - $baby = $ai->entity->breed(); + $baby = $ai->entity->breed($entpar2); //this.theAnimal.setGrowingAge(6000); TODO growing age? //this.targetMate.setGrowingAge(6000); diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 4ce3e6ab..f651bcd7 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -24,6 +24,19 @@ class Sheep extends Animal{ $this->ai->addTask(new TaskFollowParent(1.0)); } + public function spawnChild($mate) + { + $child = parent::spawnChild($mate); + if($child instanceof Sheep){ + if($mate instanceof Sheep){ + $child->setColor(mt_rand(0, 1) == 0 ? $this->getColor() : $mate->getColor()); + }else{ + $child->setColor($this->getColor()); + } + } + return $child; + } + public function createSaveData(){ $data = parent::createSaveData(); $data["Color"] = @$this->data["Color"]; From 3fb4198210f617b2893f6ae18ac1b04a95f32595 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 16 Jan 2026 23:48:02 +0300 Subject: [PATCH 137/146] fixed small memory leak --- src/Player.php | 5 ++--- src/network/protocol/ProtocolInfo.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Player.php b/src/Player.php index 6e15baf9..e3861235 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2316,7 +2316,7 @@ class Player{ $this->evid[] = $this->server->event("player.pickup", [$this, "eventHandler"]); $this->evid[] = $this->server->event("tile.container.slot", [$this, "eventHandler"]); $this->evid[] = $this->server->event("tile.update", [$this, "eventHandler"]); - $this->server->addHandler("player.invisible", [$this, "invisibilityHandler"]); + $this->evid[] = $this->server->event("player.invisible", [$this, "invisibilityHandler"]); $this->lastMeasure = microtime(true); $this->server->schedule(50, [$this, "measureLag"], [], true); @@ -3521,7 +3521,7 @@ class Player{ } } } - + /** * @return string */ @@ -3562,7 +3562,6 @@ class Player{ * @deprecated 16x16x16 chunk sending was removed */ public static $smallChunks = false; - } class PlayerNull extends Player{ diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index efa7e47e..86dde7ed 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -79,4 +79,4 @@ abstract class ProtocolInfo{ */ /***REM_START***/ require_once(FILE_PATH . "src/network/raknet/RakNetDataPacket.php"); -/***REM_END***/ \ No newline at end of file +/***REM_END***/ From 5eb458e14e2e5cf433e38de64e323f55aae2bc87 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 3 Feb 2026 09:08:55 +0300 Subject: [PATCH 138/146] unknown -> randomly (fixed #68) --- src/Player.php | 3 ++- src/network/protocol/packet/DropItemPacket.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Player.php b/src/Player.php index e3861235..4bcb8769 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2935,7 +2935,8 @@ class Player{ $this->toCraft = []; $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $data["eid"] = $packet->eid; - $data["unknown"] = $packet->unknown; + $data["unknown"] = $packet->randomly; + $data["randomly"] = $packet->randomly; $data["item"] = $packet->item; $data["player"] = $this; if($this->blocked === false and $this->server->handle("player.drop", $data) !== false){ diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 30c487bf..dd76ee21 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -2,7 +2,7 @@ class DropItemPacket extends RakNetDataPacket{ public $eid; - public $unknown; + public $randomly; public $item; public function pid(){ @@ -11,7 +11,7 @@ class DropItemPacket extends RakNetDataPacket{ public function decode(){ $this->eid = $this->getInt(); - $this->unknown = $this->getByte(); + $this->randomly = $this->getByte(); $this->item = $this->getSlot(); } @@ -27,4 +27,4 @@ class DropItemPacket extends RakNetDataPacket{ } return true; } -} \ No newline at end of file +} From ab797975875d15391303e3ddf601ba5317e03f1b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Feb 2026 12:32:07 +0300 Subject: [PATCH 139/146] added illegal windows filenames(fixed #69) --- src/Player.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 4bcb8769..33eae0fc 100755 --- a/src/Player.php +++ b/src/Player.php @@ -4,7 +4,36 @@ class Player{ const CHATMESSAGE_ORDER_CHANNEL = 1; const BLOCKUPDATE_ORDER_CHANNEL = 2; const ENTITY_ORDER_CHANNEL = 3; - + public static $blacklistedUsernames = [ + "rcon" => true, + "console" => true, + "server" => true, + //windows my beloved + "con" => true, + "prn" => true, + "aux" => true, + "nul" => true, + "com1" => true, + "com2" => true, + "com3" => true, + "com4" => true, + "com5" => true, + "com6" => true, + "com7" => true, + "com8" => true, + "com9" => true, + "com0" => true, + "lpt1" => true, + "lpt2" => true, + "lpt3" => true, + "lpt4" => true, + "lpt5" => true, + "lpt6" => true, + "lpt7" => true, + "lpt8" => true, + "lpt9" => true, + "lpt0" => true + ]; public static $experimentalHotbar = false; public static $allowDroppingSingleItems = true; public static $disableEmojisInChat = true; @@ -2159,7 +2188,7 @@ class Player{ $this->close("Incorrect protocol #" . $packet->protocol1, false); return; } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16){ + if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || isset(Player::$blacklistedUsernames[$this->iusername]) || strlen($this->iusername) > 16){ $this->close("Bad username", false); return; } From b4873bfd3b4251f74e723a2f348b0d7e74e0468f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 9 Feb 2026 11:03:00 +0300 Subject: [PATCH 140/146] fixed ladder collisions with entities --- src/material/block/attachable/LadderBlock.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/material/block/attachable/LadderBlock.php b/src/material/block/attachable/LadderBlock.php index d4ae8a6f..c117cf26 100644 --- a/src/material/block/attachable/LadderBlock.php +++ b/src/material/block/attachable/LadderBlock.php @@ -23,6 +23,11 @@ class LadderBlock extends TransparentBlock{ return parent::getAABB($level, $x, $y, $z); } + + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ + return [static::getAABB($level, $x, $y, $z)]; + } + public function __construct($meta = 0){ parent::__construct(LADDER, $meta, "Ladder"); $this->isSolid = false; From e16f3f5668d29e34e60eb6026f3197f9b5bd872b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Feb 2026 22:44:43 +0300 Subject: [PATCH 141/146] readInt/Float/LDouble/LLong will return 0 if str has not enough bytes --- src/utils/Utils.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utils/Utils.php b/src/utils/Utils.php index c4f4a1ee..66c36938 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -576,7 +576,7 @@ class Utils{ } public static function readInt($str){ - if(strlen($str) <= 0) return; + if(strlen($str) < 4) return 0; return @unpack("N", $str)[1] << 32 >> 32; //php has no signed long unpack } @@ -589,7 +589,8 @@ class Utils{ } public static function readFloat($str){ - list(, $value) = unpack("G", $str); + if(strlen($str) < 4) return 0; + list(, $value) = @unpack("G", $str); return $value; } @@ -611,6 +612,7 @@ class Utils{ } public static function readLDouble($str){ + if(strlen($str) < 8) return 0; list(, $value) = @unpack("e", $str); return $value; } @@ -620,6 +622,7 @@ class Utils{ } public static function readLLong($str){ + if(strlen($str) < 8) return 0; return unpack("P", $str)[1]; } From a97da0f719b9da3ab8a79fc1e6299e35401de8e1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 5 Mar 2026 19:49:37 +0300 Subject: [PATCH 142/146] send time after startgame so the time would be synchronized when the client is ready] --- src/Player.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index 33eae0fc..117db768 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2306,7 +2306,7 @@ class Player{ $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); $this->addEntity($this->entity); - + $pk = new StartGamePacket; $pk->seed = $this->level->getSeed(); $pk->x = $this->data->get("position")["x"]; @@ -2321,6 +2321,12 @@ class Player{ $this->entity->z = $pk->z; $this->dataPacket($pk); + //should cache it for client(unless the queue was full and startgamepacket was sent without settime + //and client receives time before startgame - TODO maybe make it ordered?) + $pk = new SetTimePacket; + $pk->time = $this->level->getTime(); + $pk->started = !$this->level->isTimeStopped(); + $this->dataPacket($pk); if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); From 878fc5f74bfd667c2750f3bcf0d9fa0ca1e85cc0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 8 Mar 2026 08:04:09 +0300 Subject: [PATCH 143/146] fixed varied size of ContainerSetContentPacket when writing --- src/network/protocol/packet/ContainerSetContentPacket.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index a4afe1db..6451b717 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -35,7 +35,9 @@ class ContainerSetContentPacket extends RakNetDataPacket{ foreach($this->hotbar as $slot){ $this->putInt($slot); } + }else{ + $this->putShort(0); } } -} \ No newline at end of file +} From d6c57e4aa6b99b63ffa35a4d092bec74de829604 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 10 Mar 2026 21:50:47 +0300 Subject: [PATCH 144/146] second array in ContainerSetContentPacket will be read no matter is windowid==0 --- .../protocol/packet/ContainerSetContentPacket.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 6451b717..8d1fc165 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -15,11 +15,10 @@ class ContainerSetContentPacket extends RakNetDataPacket{ for($s = 0; $s < $count and !$this->feof(); ++$s){ $this->slots[$s] = $this->getSlot(); } - if($this->windowid === 0){ - $count = $this->getShort(); - for($s = 0; $s < $count and !$this->feof(); ++$s){ - $this->hotbar[$s] = $this->getInt(); - } + + $count = $this->getShort(); + for($s = 0; $s < $count and !$this->feof(); ++$s){ + $this->hotbar[$s] = $this->getInt(); } } From 7d167f70554a78f0171df1ba97744807ff15dbc9 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <121912649+yefengeeeeeeeeeee@users.noreply.github.com> Date: Fri, 27 Mar 2026 23:15:26 +0800 Subject: [PATCH 145/146] Bucket Water/Lava can be placed at torch block Vanilla feature --- src/material/item/generic/BucketItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/material/item/generic/BucketItem.php b/src/material/item/generic/BucketItem.php index 79e9d765..a96ede89 100644 --- a/src/material/item/generic/BucketItem.php +++ b/src/material/item/generic/BucketItem.php @@ -39,7 +39,7 @@ class BucketItem extends Item{ return true; } }elseif($this->meta === WATER){ - if($block->getID() === AIR || $block instanceof LiquidBlock){ + if($block->getID() === AIR || $block->getID() === TORCH || $block instanceof LiquidBlock){ $water = new WaterBlock(); $level->setBlock($block, $water, true, false, true); //$water->place($this, $player, $block, $target, $face, $fx, $fy, $fz); @@ -54,7 +54,7 @@ class BucketItem extends Item{ return true; } }elseif($this->meta === LAVA){ - if($block->getID() === AIR || $block instanceof LiquidBlock){ + if($block->getID() === AIR || $block->getID() === TORCH || $block instanceof LiquidBlock){ $lava = new LavaBlock(); $level->setBlock($block, $lava, true, false, true); //$lava->place(clone $this, $player, $block, $target, $face, $fx, $fy, $fz); From c984b31c25becd70f95bb1f559751bd980dd01ac Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 28 Mar 2026 15:26:27 +0800 Subject: [PATCH 146/146] Sign should be placable on glass pane, ironbars and stonewalls --- src/material/block/attachable/SignPostBlock.php | 2 +- src/material/block/attachable/WallSignBlock.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/material/block/attachable/SignPostBlock.php b/src/material/block/attachable/SignPostBlock.php index 8a1db4e2..eab861ba 100644 --- a/src/material/block/attachable/SignPostBlock.php +++ b/src/material/block/attachable/SignPostBlock.php @@ -21,7 +21,7 @@ class SignPostBlock extends TransparentBlock{ } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - if(($target->isSolid || $target->getID() === SIGN_POST || $target->getID() === WALL_SIGN) && $face !== 0){ + if(($target->isSolid || $target->getID() === SIGN_POST || $target->getID() === WALL_SIGN || $target->getID() === GLASS_PANE || $target->getID() === IRON_BARS || $target->getID() === STONE_WALL) && $face !== 0){ if(!isset(self::$faces[$face])){ $this->meta = floor((($player->entity->yaw + 180) * 16 / 360) + 0.5) & 0x0F; $this->level->setBlock($block, BlockAPI::get(SIGN_POST, $this->meta), true, false, true); diff --git a/src/material/block/attachable/WallSignBlock.php b/src/material/block/attachable/WallSignBlock.php index 5fe7179d..d92d67ba 100644 --- a/src/material/block/attachable/WallSignBlock.php +++ b/src/material/block/attachable/WallSignBlock.php @@ -23,7 +23,7 @@ class WallSignBlock extends SignPostBlock{ default => WALL_SIGN }; - if(!StaticBlock::getIsSolid($attached) && $attached != SIGN_POST && $attached != WALL_SIGN){ + if(!StaticBlock::getIsSolid($attached) && $attached != SIGN_POST && $attached != WALL_SIGN && $attached != GLASS_PANE && $attached != IRON_BARS && $attached != STONE_WALL){ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, true); (ServerAPI::request())->api->entity->drop(new Position($x + 0.5, $y + 0.5, $z + 0.5, $level), BlockAPI::getItem(SIGN, 0, 1)); }