From 5304705b11b99d3fdbd2a72edb1d5de2dcfd97a9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 02:08:39 +0300 Subject: [PATCH 01/17] fixed some warnings --- src/Player.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 96a6bd0d..239684bc 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2415,7 +2415,9 @@ class Player{ if($packet->windowid === 0){ $craft = false; $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + + if($slot->count >= $packet->item->count && (($slot->getID() === $packet->item->getID() && $slot->getMetadata() === $packet->item->getMetadata()) || ($packet->item->getID() === AIR && $packet->item->count === 0)) && !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + if(!isset($this->toCraft[-1])) $this->toCraft[-1] = 0; $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); $this->craftingItems[$packet->slot] = $use; $craft = true; @@ -2681,7 +2683,11 @@ class Player{ public function craftItems(array $craft, array $recipe, $type){ $craftItem = [0, true, 0]; unset($craft[-1]); - + + if($type !== 0 && $type !== 1 && $type !== 2){ //allow only small(0), big(1) or stone(2) + return false; + } + foreach($craft as $slot => $item){ if($item instanceof Item){ $craftItem[0] = $item->getID(); From 952af19a5cfaddd7c70af42060263a9021062cb9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 02:58:55 +0300 Subject: [PATCH 02/17] farmland random tick tweaks --- src/material/block/nonfull/FarmlandBlock.php | 37 +++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index 580f78d3..dcf05a39 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -29,20 +29,31 @@ class FarmlandBlock extends TransparentBlock{ } public static function onRandomTick(Level $level, $x, $y, $z){ - $meta = $level->level->getBlockDamage($x, $y, $z); - $b = $level->level->getBlockID($x, $y + 1, $z); - if(!StaticBlock::getIsFlowable($b)){ - $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); - }elseif($meta === 0 && mt_rand(0, 5) === 0){ - $water = self::checkWaterStatic($level, $x, $y, $z); - if($water){ - $level->fastSetBlockUpdate($x, $y, $z, FARMLAND, 1, true); - }elseif($b != 0 && StaticBlock::getIsTransparent($b)){ - $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); + for($xx = $x-4; $xx <= $x+4; ++$xx){ + for($yy = $y; $yy <= $y+1; ++$yy){ + for($zz = $z-4; $zz <= $z+4; ++$zz){ + $id = $level->level->getBlockID($xx, $yy, $zz); + if($id == WATER || $id == STILL_WATER){ + $dt = 7; + goto set_wet_level; + } + } + } + } + $dt = $level->level->getBlockDamage($x, $y, $z); + if($dt > 0){ + $dt -= 1; + set_wet_level: + $level->fastSetBlockUpdateMeta($x, $y, $z, $dt, true); + return; + } + $ab = $level->level->getBlockID($x, $y+1, $z); + if($ab != WHEAT_BLOCK){ + //nc: added carrot/potato/beetroot checks + if($ab != CARROT_BLOCK && $ab != POTATO_BLOCK && $ab != BEETROOT_BLOCK){ + $level->fastSetBlockUpdate($x, $y, $z, DIRT, true); } } - - } public static function checkWaterStatic(Level $level, $x, $y, $z) @@ -51,7 +62,7 @@ class FarmlandBlock extends TransparentBlock{ for ($by = $y; $by <= $y + 1; $by++) { for ($bz = $z - 4; $bz <= $z + 4; $bz++) { $id = $level->level->getBlockID($bx, $by, $bz); - if ($id === 8 || $id === 9) { + if ($id === WATER || $id === STILL_WATER) { return true; } } From 9d37312d6381acd706281138e72c6a23f44f91cd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 03:12:29 +0300 Subject: [PATCH 03/17] replaced 4 spaces with tabs in some files --- src/entity/ai/MobController.php | 22 +++++++++++----------- src/entity/object/Painting.php | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index fd6d772e..05eb3a9d 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -168,18 +168,18 @@ class MobController /* Some stuff for pathfinder - nc doesnt have it now * float var11 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset); - if (!this.entity.getNavigator().noPath()) - { - if (var11 < -75.0F) - { - this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F; - } + if (!this.entity.getNavigator().noPath()) + { + if (var11 < -75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F; + } - if (var11 > 75.0F) - { - this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F; - } - } + if (var11 > 75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F; + } + } */ //$this->entity->lastHeadYaw = $this->entity->headYaw; diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 21f30927..9b2aa8b5 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -43,9 +43,9 @@ class Painting extends Entity{ public $counter = 0; public function update($now){ - if($this->closed === true){ - return false; - } + if($this->closed === true){ + return false; + } if(++$this->counter >= 100){ $this->counter = 0; From 1370ccf6b4a7d4960ec717e2541a1620a7ddb2d2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 03:48:28 +0300 Subject: [PATCH 04/17] inc sac -> ink sac --- src/material/item/generic/DyeItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/material/item/generic/DyeItem.php b/src/material/item/generic/DyeItem.php index 10ca822e..08e7c3c6 100755 --- a/src/material/item/generic/DyeItem.php +++ b/src/material/item/generic/DyeItem.php @@ -4,7 +4,7 @@ class DyeItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(DYE, $meta, $count, "Dye"); $names = array( - 0 => "Inc Sac", + 0 => "Ink Sac", 1 => "Rose Red", 2 => "Cactus Green", 3 => "Cocoa Beans", @@ -23,4 +23,4 @@ class DyeItem extends Item{ ); $this->name = $names[$this->meta]; } -} \ No newline at end of file +} From 21fc67cf1a974d628bfddcb9b0d303aa2246d529 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 10:09:41 +0300 Subject: [PATCH 05/17] emoji checks for say, tell and me --- src/API/ChatAPI.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 9f2bfac3..69fade6d 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -35,6 +35,7 @@ class ChatAPI{ break; } $sender = ($issuer instanceof Player) ? "Server" : ucfirst($issuer); + if(Utils::hasEmoji($s)) return "Your message contains illegal characters!"; $this->server->api->chat->broadcast("[$sender] " . $s); break; case "me": @@ -52,7 +53,9 @@ class ChatAPI{ }else{ $sender = $issuer->username; } - $this->broadcast("* $sender " . implode(" ", $params)); + $msg = implode(" ", $params); + if(Utils::hasEmoji($msg)) return "Your message contains illegal characters!"; + $this->broadcast("* $sender $msg"); break; case "tell": if(!isset($params[0]) or !isset($params[1])){ @@ -80,6 +83,7 @@ class ChatAPI{ return "You can't send message to yourself."; } $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); From 16324d32f2b7669dfa38778ac4bd30d901dd7aa9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 11:17:05 +0300 Subject: [PATCH 06/17] tweaked fallingsand physics --- src/entity/object/FallingSand.php | 17 ++++++++++------- src/material/block/GenericBlock.php | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 517d4ca7..1857a9ad 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -27,6 +27,7 @@ class FallingSand extends Entity{ $this->close(); return; } + $this->lastX = $this->x; $this->lastY = $this->y; $this->lastZ = $this->z; @@ -42,6 +43,14 @@ class FallingSand extends Entity{ $y = floor($this->y); $z = floor($this->z); + if(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ + //TODO vanilla-like checking? + if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ + $this->close(); + ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata + return; + } + } if($this->onGround){ $this->speedX *= 0.7; $this->speedZ *= 0.7; @@ -49,12 +58,6 @@ class FallingSand extends Entity{ $this->close(); //TODO vanilla-like checking? $this->level->fastSetBlockUpdate($x, $y, $z, $this->data["Tile"], 0, true); //TODO add metadata - }elseif(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ - //TODO vanilla-like checking? - if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ - $this->close(); - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata - } } } -} \ No newline at end of file +} diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index e0780dd1..cf1617d0 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -94,4 +94,4 @@ class GenericBlock extends Block{ public function onActivate(Item $item, Player $player){ return $this->isActivable; } -} \ No newline at end of file +} From 38ee5daed2b04535f02b32123bbcd358f6f52b16 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 14:36:33 +0300 Subject: [PATCH 07/17] prevent useitem if heldItem on client doesnt match server --- src/Player.php | 15 +++++++++++++++ src/network/protocol/packet/UseItemPacket.php | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Player.php b/src/Player.php index 239684bc..cba2df99 100755 --- a/src/Player.php +++ b/src/Player.php @@ -686,6 +686,7 @@ class Player{ $pk->hotbar = $hotbar; $this->dataPacket($pk); } + /** * @param $type * @param $damage @@ -1948,6 +1949,20 @@ class Player{ $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; + //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."); + $this->sendInventory(); + if($packet->face >= 0 && $packet->face <= 5){ + $target = $this->level->getBlock($blockVector); + $block = $target->getSide($packet->face); + $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; + } + if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place if($this->entity->inAction === true){ $this->entity->inAction = false; diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 0136ed5b..32e2c651 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -25,18 +25,18 @@ class UseItemPacket extends RakNetDataPacket{ $this->z = $this->getInt(); $this->face = $this->getInt(); $this->item = $this->getShort(); - $this->meta = $this->getByte(); //Mojang: fix this + $this->meta = $this->getByte(); $this->eid = $this->getInt(); $this->fx = $this->getFloat(); $this->fy = $this->getFloat(); $this->fz = $this->getFloat(); $this->posX = $this->getFloat(); $this->posY = $this->getFloat(); - $this->posZ = $this->getFloat(); + $this->posZ = $this->getFloat(); } public function encode(){ } -} \ No newline at end of file +} From aef063c8787d4da1dd4438ed05c4d93d4fb9ba7f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 15:00:31 +0300 Subject: [PATCH 08/17] better autojump for mobs --- src/API/ConsoleAPI.php | 2 -- src/entity/ai/MobController.php | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 1efb0943..540bc739 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -53,9 +53,7 @@ class ConsoleAPI{ if(!defined("NO_THREADS")){ self::$loop->stop(); self::$loop->notify(); - //@fclose($this->loop->fp); usleep(50000); - //$this->loop->join(); } } diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 05eb3a9d..1e1eec00 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -107,7 +107,9 @@ class MobController $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); $this->entity->setAIMoveSpeed($this->speedMultiplier * $this->entity->getSpeed() * $this->entity->getSpeedModifer()); - if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); + if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1 && $this->entity->isCollidedHorizontally){ + $this->setJumping(true); + } } } //TODO handle jumps somewhere From a842424d255f1fe99db9ef9d6a1d613de1f1eb93 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 18:29:28 +0300 Subject: [PATCH 09/17] better chunk recovery --- src/Player.php | 75 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/src/Player.php b/src/Player.php index cba2df99..b3cb5fbc 100755 --- a/src/Player.php +++ b/src/Player.php @@ -70,6 +70,12 @@ class Player{ private $chunkCount = []; private $received = []; + /** + * Stores packets that must be always received by clients. + * @var array + */ + public $packetAlwaysRecoverQueue = []; + public $slotCount = 7; public $entityMovementQueue; @@ -372,6 +378,42 @@ class Player{ return []; } + public function sendChunkDataPacket(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; + if($size <= 0) return false; + $buffer = str_split($pk->buffer, $size); + $bigCnt = $this->bigCnt; + $this->bigCnt = ($this->bigCnt + 1) % 0x10000; + $cnts = []; + $bufCount = count($buffer); + foreach($buffer as $i => $buf){ + $cnts[] = $count = $this->counter[0]++; + + $pk = new UnknownPacket; + $pk->packetID = $pk->pid(); + $pk->reliability = 2; + $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; + } + private function directBigRawPacket(RakNetDataPacket $packet){ if($this->connected === false){ return false; @@ -557,7 +599,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->dataPacket($pk); + $cnt = $this->sendChunkDataPacket($pk); $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; @@ -579,8 +621,9 @@ class Player{ if($this->connected === false or $world != $this->level){ return false; } + foreach($this->chunkCount as $count => $t){ - if(isset($this->recoveryQueue[$count]) or isset($this->resendQueue[$count])){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); return; }else{ @@ -634,7 +677,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->dataPacket($pk); + $cnt = $this->sendChunkDataPacket($pk); if($cnt === false){ return false; } @@ -1231,11 +1274,17 @@ class Player{ $limit = $time - 5; //max lag foreach($this->recoveryQueue as $count => $data){ if($data->sendtime > $limit){ - break; + continue; } unset($this->recoveryQueue[$count]); $this->resendQueue[$count] = $data; } + + foreach($this->packetAlwaysRecoverQueue as $cnt => $data){ + if($time - $data->sendtime >= 5){ + $this->resendQueue[$cnt] = $data; + } + } if(($resendCnt = count($this->resendQueue)) > 0){ foreach($this->resendQueue as $count => $data){ @@ -1243,13 +1292,7 @@ class Player{ $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; $data->sendtime = $time; - - $cnt = $this->send($data); - if(isset($this->chunkCount[$count])){ - unset($this->chunkCount[$count]); - if(!is_null($cnt) and !is_null($cnt[0])) - $this->chunkCount[$cnt[0]] = true; - } + $this->send($data); } } } @@ -2816,7 +2859,9 @@ class Player{ switch($packet->pid()){ case RakNetInfo::NACK: foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$count])){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ + $this->packetAlwaysRecoverQueue[$count]->sendtime = 0; //forces resend next time it will be checked + }else if(isset($this->recoveryQueue[$count])){ $this->resendQueue[$count] =& $this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); @@ -2828,7 +2873,11 @@ class Player{ case RakNetInfo::ACK: foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$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])){ $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); unset($this->resendQueue[$count]); From e7c4c3b8e6c971865a31e468e87acd8fce0ce2e5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 18:47:46 +0300 Subject: [PATCH 10/17] fixed logging off when sneaking teleporting you down 1 block --- src/Player.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Player.php b/src/Player.php index b3cb5fbc..07db541a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1704,13 +1704,13 @@ class Player{ $pk = new StartGamePacket; $pk->seed = $this->level->getSeed(); $pk->x = $this->data->get("position")["x"]; - $pk->y = $this->data->get("position")["y"]; + $pk->y = ceil($this->data->get("position")["y"]); $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; $this->hotbar = []; @@ -1734,9 +1734,9 @@ 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->entity->x = $this->data->get("position")["x"]; - $this->entity->y = $this->data->get("position")["y"]; - $this->entity->z = $this->data->get("position")["z"]; + $this->entity->x = $pk->x; + $this->entity->y = $pk->y; + $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 62b846e6735b209d01d8e4deba87a46c7ae4e4a3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 9 May 2025 19:27:22 +0300 Subject: [PATCH 11/17] falling tile fixes x2 --- src/entity/object/FallingSand.php | 13 ++++++++++++- src/material/block/GenericBlock.php | 3 +-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 1857a9ad..81b6fa9b 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -27,7 +27,18 @@ class FallingSand extends Entity{ $this->close(); return; } - + //$level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + if($this->fallTime == 0){ + $x = floor($this->x); + $y = floor($this->y); + $z = floor($this->z); + if($this->level->level->getBlockID($x, $y, $z) != $this->data["Tile"]){ + $this->close(); + return; + } + $this->level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + } + $this->lastX = $this->x; $this->lastY = $this->y; $this->lastZ = $this->z; diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index cf1617d0..f7a128cb 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -62,12 +62,11 @@ class GenericBlock extends Block{ if($down == AIR || StaticBlock::getIsLiquid($down)){ $data = array( "x" => $x + 0.5, - "y" => $y - 0.5, + "y" => $y, "z" => $z + 0.5, "Tile" => $id, ); $server = ServerAPI::request(); - $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); $server->api->entity->spawnToAll($e); } From a38c1c109ca1e1f98e877fec71f4750d0a75248e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 02:25:29 +0300 Subject: [PATCH 12/17] some random stuff --- src/Player.php | 122 +++++++++++------- src/PocketMinecraftServer.php | 1 + src/material/block/solid/StonecutterBlock.php | 2 +- src/material/block/solid/WorkbenchBlock.php | 2 +- src/recipes/CraftingRecipes.php | 18 ++- 5 files changed, 88 insertions(+), 57 deletions(-) diff --git a/src/Player.php b/src/Player.php index 07db541a..c4558c1d 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2460,56 +2460,31 @@ class Player{ if($this->spawned === false or $this->blocked === true){ break; } - - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ - $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ - $this->toCraft = []; - } - $this->craftingItems = []; - } - - if($packet->windowid === 0){ - $craft = false; + + if($packet->windowid === 0){ //crafting + $slot = $this->getSlot($packet->slot); - - if($slot->count >= $packet->item->count && (($slot->getID() === $packet->item->getID() && $slot->getMetadata() === $packet->item->getMetadata()) || ($packet->item->getID() === AIR && $packet->item->count === 0)) && !isset($this->craftingItems[$packet->slot])){ //Crafting recipe - if(!isset($this->toCraft[-1])) $this->toCraft[-1] = 0; - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); - $this->craftingItems[$packet->slot] = $use; - $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); - $this->craftingItems[$packet->slot] = $use; - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - } - - if($craft === true){ - $this->lastCraft = microtime(true); - } - - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ - $this->sendInventory(); - $this->toCraft = []; + $citem = $packet->item; + if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ + if($citem->count > $slot->count){ //item added, result + $this->addCraftingResult($slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); + }else if($citem->count < $slot->count){ //item removed, ingridient + $this->addCraftingIngridient($slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); }else{ - $this->toCraft = [-1 => $this->toCraft[-1]]; + //item synchronized } + }else if($citem->getID() == 0 && $slot->getID() > 0){ //client sent air, server has not air, ingridient + $this->addCraftingIngridient($slot->getID(), $slot->getMetadata(), $slot->count); + }else if($slot->getID() == 0 && $citem->getID() > 0){ //client sent non-air, server has air, result + $this->addCraftingResult($citem->getID(), $citem->getMetadata(), $citem->count); + }else{ + $this->sendInventory(); + $this->toCraft = []; $this->craftingItems = []; + ConsoleAPI::warn("{$this->username} tried modifying inventory! Server item: $slot({$slot->count}), Client item: $citem({$citem->count})"); + $this->sendChat("[NC] Haxxor(or bug?)."); } + }else{ $this->toCraft = []; $this->craftingItems = []; @@ -2697,6 +2672,63 @@ class Player{ } } + public $craftingType = 0; + public function tryCraft(){ + if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; + + if(count($this->toCraft) > 1){ + ConsoleAPI::warn("Trying to craft more than 1 item, not supported for now."); + return false; + } + foreach($this->toCraft as $i => $e){ + $toCraft = [$i >> 8, $i & 0xff, $e]; + } + $ingridients = []; + foreach($this->craftingItems as $i => $e){ + $ingridients[$i >> 8] = [$i >> 8, $i & 0xff, $e]; + } + + + $cc = CraftingRecipes::canCraft($toCraft, $ingridients, $this->craftingType); + if(!is_array($cc) && $this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ + $cc = CraftingRecipes::canCraft($toCraft, $ingridients, CraftingRecipes::TYPE_INVENTORY); + if(!is_array($cc)){ + return; //recipe not found + } + } + + var_dump($cc); + $this->addItem($toCraft[0], $toCraft[1], $toCraft[2], false); + foreach($ingridients as [$id, $meta, $count]){ + $this->removeItem($id, $meta, $count, false); + } + $this->sendInventory(); + $this->toCraft = []; + $this->craftingItems = []; + } + + public function addCraftingResult($id, $meta, $count){ + $index = ($id << 8) | $meta; + + if(!isset($this->toCraft[$index])) $this->toCraft[$index] = 0; + $this->toCraft[$index] += $count; + console("Result: $id, $meta, $count"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + + public function addCraftingIngridient($id, $meta, $count){ + $index = ($id << 8) | $meta; + + if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = 0; + $this->craftingItems[$index] += $count; + console("Ingridient: $id, $meta, $count"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + public function hasItem($type, $damage = false){ foreach($this->inventory as $s => $item){ if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1b281cfc..dcdbb9da 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -12,6 +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 $EXPERIMENTAL_INVENTORY = true; function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index b5fc3fe7..110747d1 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -8,7 +8,7 @@ class StonecutterBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 2; + $player->craftingType = CraftingRecipes::TYPE_STONECUTTER; return true; } diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index db605a65..080ac13c 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -9,7 +9,7 @@ class WorkbenchBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 1; + $player->craftingType = CraftingRecipes::TYPE_CRAFTIGTABLE; return true; } diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 47064577..d9a838b3 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -1,7 +1,10 @@ CLAY_BLOCK:0x1", @@ -204,19 +207,19 @@ class CraftingRecipes{ $id = 1; foreach(CraftingRecipes::$small as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 0; //Type + $recipe[3] = self::TYPE_INVENTORY; //Type CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$big as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 1; + $recipe[3] = self::TYPE_CRAFTIGTABLE; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$stone as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 2; + $recipe[3] = self::TYPE_STONECUTTER; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } @@ -268,9 +271,6 @@ class CraftingRecipes{ ksort($recipeItems); $recipeString = ""; foreach($recipeItems as $item){ - if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal - $item[2] = 3; - } $recipeString .= $item[0] . "x" . $item[2] . ","; } $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; @@ -287,9 +287,7 @@ class CraftingRecipes{ break; } $oitem = $recipeItems[$item[0]]; - if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 - $oitem[2] = 3; - } + if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ $continue = false; break; From af71be9f3c958a41a9e24768f8cffb11b9f39c4f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 03:42:02 +0300 Subject: [PATCH 13/17] slot based crafting --- src/Player.php | 81 ++++++++++++++++++++--------- src/PocketMinecraftServer.php | 2 +- src/network/raknet/RakNetParser.php | 2 + 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/Player.php b/src/Player.php index c4558c1d..26181704 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2467,22 +2467,19 @@ class Player{ $citem = $packet->item; if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ if($citem->count > $slot->count){ //item added, result - $this->addCraftingResult($slot->getID(), $slot->getMetadata(), $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($slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); }else{ //item synchronized } }else if($citem->getID() == 0 && $slot->getID() > 0){ //client sent air, server has not air, ingridient - $this->addCraftingIngridient($slot->getID(), $slot->getMetadata(), $slot->count); + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count); }else if($slot->getID() == 0 && $citem->getID() > 0){ //client sent non-air, server has air, result - $this->addCraftingResult($citem->getID(), $citem->getMetadata(), $citem->count); - }else{ - $this->sendInventory(); - $this->toCraft = []; - $this->craftingItems = []; - ConsoleAPI::warn("{$this->username} tried modifying inventory! Server item: $slot({$slot->count}), Client item: $citem({$citem->count})"); - $this->sendChat("[NC] Haxxor(or bug?)."); + $this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count); + }else{ //client sent result, server has ingridients + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count); + $this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count); } }else{ @@ -2677,15 +2674,19 @@ class Player{ if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; if(count($this->toCraft) > 1){ + $this->sendInventory(); + $this->toCraft = []; + $this->craftingItems = []; + safe_var_dump($this->toCraft); ConsoleAPI::warn("Trying to craft more than 1 item, not supported for now."); return false; } - foreach($this->toCraft as $i => $e){ - $toCraft = [$i >> 8, $i & 0xff, $e]; + foreach($this->toCraft as $i => $slotz){ + $toCraft = [$i >> 8, $i & 0xff, array_sum($slotz)]; } $ingridients = []; - foreach($this->craftingItems as $i => $e){ - $ingridients[$i >> 8] = [$i >> 8, $i & 0xff, $e]; + foreach($this->craftingItems as $i => $slotz){ + $ingridients[$i >> 8] = [$i >> 8, $i & 0xff, array_sum($slotz)]; } @@ -2697,33 +2698,61 @@ class Player{ } } - var_dump($cc); - $this->addItem($toCraft[0], $toCraft[1], $toCraft[2], false); - foreach($ingridients as [$id, $meta, $count]){ - $this->removeItem($id, $meta, $count, false); + foreach($this->craftingItems as $i => $slotz){ + $id = $i >> 8; + $meta = $i & 0xff; + foreach($slotz as $slot => $count){ + $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); + }else{ + ConsoleAPI::warn("{$slt->getID()} != $id && {$slt->getMetadata()} != $meta!!!"); + } + } } + + foreach($this->toCraft as $i => $slotz){ + $id = $i >> 8; + $meta = $i & 0xff; + foreach($slotz as $slot => $count){ + $slt = $this->getSlot($slot); + if($slt->getID() == $id && $slt->getMetadata() == $meta){ + $slt->count += $count; + }else if($slt->getID() == 0){ + $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false); + }else{ + ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!"); + } + //TODO actually check id/meta + } + } + $this->sendInventory(); $this->toCraft = []; $this->craftingItems = []; } - public function addCraftingResult($id, $meta, $count){ + public function addCraftingResult($slot, $id, $meta, $count){ $index = ($id << 8) | $meta; - if(!isset($this->toCraft[$index])) $this->toCraft[$index] = 0; - $this->toCraft[$index] += $count; - console("Result: $id, $meta, $count"); + if(!isset($this->toCraft[$index])) $this->toCraft[$index] = []; + if(!isset($this->toCraft[$index][$slot])) $this->toCraft[$index][$slot] = 0; + $this->toCraft[$index][$slot] += $count; + + console("Result: $id, $meta, $count into $slot"); if($this->tryCraft() === false){ $this->sendInventory(); } } - public function addCraftingIngridient($id, $meta, $count){ + public function addCraftingIngridient($slot, $id, $meta, $count){ $index = ($id << 8) | $meta; - if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = 0; - $this->craftingItems[$index] += $count; - console("Ingridient: $id, $meta, $count"); + if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = []; + if(!isset($this->craftingItems[$index][$slot])) $this->craftingItems[$index][$slot] = 0; + $this->craftingItems[$index][$slot] += $count; + console("Ingridient: $id, $meta, $count into $slot"); if($this->tryCraft() === false){ $this->sendInventory(); } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index dcdbb9da..9086b01e 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 $EXPERIMENTAL_INVENTORY = true; + function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 1b346cf3..f5318b9c 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -56,6 +56,7 @@ class RakNetParser{ case RakNetInfo::DATA_PACKET_F: $this->packet->seqNumber = $this->getLTriad(); $this->packet->data = []; + while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ $this->packet->data[] = $pk; } @@ -122,6 +123,7 @@ class RakNetParser{ $reliability = ($packetFlags & 0b11100000) >> 5; $hasSplit = ($packetFlags & 0b00010000) > 0; $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 or $reliability === 3 or $reliability === 4 From b811734b2795a53d7c1a7cf00f19781b1be0f699 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 03:57:00 +0300 Subject: [PATCH 14/17] drop crafted item if inventory is full --- src/Player.php | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Player.php b/src/Player.php index 26181704..0964060b 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2348,8 +2348,7 @@ class Player{ if($item->getID() == 0) goto inv_desync_on_drop; } - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); + $this->addCraftingResult(-1, $prevItem->getID(), $prevItem->getMetadata(), $prevItem->count); break; }else{ inv_desync_on_drop: @@ -2698,6 +2697,8 @@ class Player{ } } + console("success"); + foreach($this->craftingItems as $i => $slotz){ $id = $i >> 8; $meta = $i & 0xff; @@ -2716,15 +2717,29 @@ class Player{ $id = $i >> 8; $meta = $i & 0xff; foreach($slotz as $slot => $count){ - $slt = $this->getSlot($slot); - if($slt->getID() == $id && $slt->getMetadata() == $meta){ - $slt->count += $count; - }else if($slt->getID() == 0){ - $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false); + if($slot < 0){ //drop item + $item = BlockAPI::getItem($id, $meta, $count); + $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, $item, $sX, $sY, $sZ); }else{ - ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!"); + $slt = $this->getSlot($slot); + if($slt->getID() == $id && $slt->getMetadata() == $meta){ + $slt->count += $count; + }else if($slt->getID() == 0){ + $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false); + }else{ + ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!"); + } } - //TODO actually check id/meta } } From 77395e8e5e2e30b01cff7d976400f50e859053a0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 04:14:50 +0300 Subject: [PATCH 15/17] api bump, player.craft event changed --- src/Player.php | 177 +++++++++++++++---------------------------------- src/config.php | 2 +- 2 files changed, 55 insertions(+), 124 deletions(-) diff --git a/src/Player.php b/src/Player.php index 0964060b..fe910f36 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2668,12 +2668,46 @@ class Player{ } } + public function checkCraftAchievements($id){ + switch($id){ + case WORKBENCH: + AchievementAPI::grantAchievement($this, "buildWorkBench"); + break; + case WOODEN_PICKAXE: + AchievementAPI::grantAchievement($this, "buildPickaxe"); + break; + case FURNACE: + AchievementAPI::grantAchievement($this, "buildFurnace"); + break; + case WOODEN_HOE: + AchievementAPI::grantAchievement($this, "buildHoe"); + break; + case BREAD: + AchievementAPI::grantAchievement($this, "makeBread"); + break; + case CAKE: + AchievementAPI::grantAchievement($this, "bakeCake"); + break; + case STONE_PICKAXE: + case GOLDEN_PICKAXE: + case IRON_PICKAXE: + case DIAMOND_PICKAXE: + AchievementAPI::grantAchievement($this, "buildBetterPickaxe"); + break; + case WOODEN_SWORD: + AchievementAPI::grantAchievement($this, "buildSword"); + break; + case DIAMOND: + AchievementAPI::grantAchievement($this, "diamond"); + break; + } + } + public $craftingType = 0; public function tryCraft(){ if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; if(count($this->toCraft) > 1){ - $this->sendInventory(); $this->toCraft = []; $this->craftingItems = []; safe_var_dump($this->toCraft); @@ -2697,7 +2731,11 @@ class Player{ } } - console("success"); + if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){ + $this->toCraft = []; + $this->craftingItems = []; + return false; + } foreach($this->craftingItems as $i => $slotz){ $id = $i >> 8; @@ -2716,6 +2754,7 @@ class Player{ foreach($this->toCraft as $i => $slotz){ $id = $i >> 8; $meta = $i & 0xff; + $this->checkCraftAchievements($id); foreach($slotz as $slot => $count){ if($slot < 0){ //drop item $item = BlockAPI::getItem($id, $meta, $count); @@ -2807,127 +2846,6 @@ class Player{ return true; } - /** - * @param array $craft - * @param array $recipe - * @param $type - * - * @return array|bool - */ - public function craftItems(array $craft, array $recipe, $type){ - $craftItem = [0, true, 0]; - unset($craft[-1]); - - if($type !== 0 && $type !== 1 && $type !== 2){ //allow only small(0), big(1) or stone(2) - return false; - } - - foreach($craft as $slot => $item){ - if($item instanceof Item){ - $craftItem[0] = $item->getID(); - if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true){ - $craftItem[1] = false; - }else{ - $craftItem[1] = $item->getMetadata(); - } - $craftItem[2] += $item->count; - } - - } - - $recipeItems = []; - foreach($recipe as $slot => $item){ - if(!isset($recipeItems[$item->getID()])){ - $recipeItems[$item->getID()] = [$item->getID(), $item->getMetadata(), $item->count]; - }else{ - if($item->getMetadata() !== $recipeItems[$item->getID()][1]){ - $recipeItems[$item->getID()][1] = false; - } - $recipeItems[$item->getID()][2] += $item->count; - } - } - - $res = CraftingRecipes::canCraft($craftItem, $recipeItems, $type); - - if(!is_array($res) and $type === 1){ - $res2 = CraftingRecipes::canCraft($craftItem, $recipeItems, 0); - if(is_array($res2)){ - $res = $res2; - } - } - - if(is_array($res)){ - if($this->server->api->dhandle("player.craft", ["player" => $this, "recipe" => $recipe, "craft" => $craft, "type" => $type]) === false){ - return false; - } - foreach($recipe as $slot => $item){ - $s = $this->getSlot($slot); - $s->count -= $item->count; - if($s->count <= 0){ - $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0), false); - } - } - foreach($craft as $slot => $item){ - $s = $this->getSlot($slot); - if($s->count <= 0 or $s->getID() === AIR){ - $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $item->count), false); - }else if($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->getMaxStackSize()){ - $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); - }else{ - $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, $item, $sX, $sY, $sZ); - } - - $this->sendInventory(); //force send on crafting - - switch($item->getID()){ - case WORKBENCH: - AchievementAPI::grantAchievement($this, "buildWorkBench"); - break; - case WOODEN_PICKAXE: - AchievementAPI::grantAchievement($this, "buildPickaxe"); - break; - case FURNACE: - AchievementAPI::grantAchievement($this, "buildFurnace"); - break; - case WOODEN_HOE: - AchievementAPI::grantAchievement($this, "buildHoe"); - break; - case BREAD: - AchievementAPI::grantAchievement($this, "makeBread"); - break; - case CAKE: - AchievementAPI::grantAchievement($this, "bakeCake"); - $this->addItem(BUCKET, 0, 3); - break; - case STONE_PICKAXE: - case GOLDEN_PICKAXE: - case IRON_PICKAXE: - case DIAMOND_PICKAXE: - AchievementAPI::grantAchievement($this, "buildBetterPickaxe"); - break; - case WOODEN_SWORD: - AchievementAPI::grantAchievement($this, "buildSword"); - break; - case DIAMOND: - AchievementAPI::grantAchievement($this, "diamond"); - break; - - } - } - } - return $res; - } - public function handlePacket(RakNetPacket $packet){ if($this->connected === true){ $time = microtime(true); @@ -3006,6 +2924,19 @@ class Player{ } return $this->clientID; } + + + /** + * @deprecated craft system changed + * @param array $craft + * @param array $recipe + * @param $type + * + * @return bool + */ + public function craftItems(array $craft, array $recipe, $type){ + return false; + } } diff --git a/src/config.php b/src/config.php index b592e4d7..fcaaaea8 100644 --- a/src/config.php +++ b/src/config.php @@ -106,7 +106,7 @@ define("START_TIME", microtime(true)); define("MAJOR_VERSION", "1.1.0dev"); define("CODENAME", "懐かしさ (Nostalgia)"); //i'm not very creative - kotyaralih define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); -define("CURRENT_API_VERSION", '12.1'); +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! From 2b38e4b20d15466f46d5733e6fe5a98ef9623c1f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 04:34:02 +0300 Subject: [PATCH 16/17] added multiple output support in recipes, fixed meta using 1 byte instead of 2 in crafting related arrays --- src/Player.php | 38 ++++++++-------- src/recipes/CraftingRecipes.php | 81 +++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/src/Player.php b/src/Player.php index fe910f36..68f2d78b 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2707,25 +2707,19 @@ class Player{ public function tryCraft(){ if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; - if(count($this->toCraft) > 1){ - $this->toCraft = []; - $this->craftingItems = []; - safe_var_dump($this->toCraft); - ConsoleAPI::warn("Trying to craft more than 1 item, not supported for now."); - return false; - } + $results = []; foreach($this->toCraft as $i => $slotz){ - $toCraft = [$i >> 8, $i & 0xff, array_sum($slotz)]; + $results[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } $ingridients = []; foreach($this->craftingItems as $i => $slotz){ - $ingridients[$i >> 8] = [$i >> 8, $i & 0xff, array_sum($slotz)]; + $ingridients[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; } - $cc = CraftingRecipes::canCraft($toCraft, $ingridients, $this->craftingType); + $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); if(!is_array($cc) && $this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ - $cc = CraftingRecipes::canCraft($toCraft, $ingridients, CraftingRecipes::TYPE_INVENTORY); + $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); if(!is_array($cc)){ return; //recipe not found } @@ -2738,8 +2732,8 @@ class Player{ } foreach($this->craftingItems as $i => $slotz){ - $id = $i >> 8; - $meta = $i & 0xff; + $id = $i >> 16; + $meta = $i & 0xffff; foreach($slotz as $slot => $count){ $slt = $this->getSlot($slot); if($slt->getID() == $id && $slt->getMetadata() == $meta){ @@ -2752,8 +2746,8 @@ class Player{ } foreach($this->toCraft as $i => $slotz){ - $id = $i >> 8; - $meta = $i & 0xff; + $id = $i >> 16; + $meta = $i & 0xffff; $this->checkCraftAchievements($id); foreach($slotz as $slot => $count){ if($slot < 0){ //drop item @@ -2788,25 +2782,31 @@ class Player{ } public function addCraftingResult($slot, $id, $meta, $count){ - $index = ($id << 8) | $meta; + $id &= 0xffff; + $meta &= 0xffff; + + $index = ($id << 16) | $meta; if(!isset($this->toCraft[$index])) $this->toCraft[$index] = []; 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(); } } public function addCraftingIngridient($slot, $id, $meta, $count){ - $index = ($id << 8) | $meta; + $id &= 0xffff; + $meta &= 0xffff; + + $index = ($id << 16) | $meta; if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = []; if(!isset($this->craftingItems[$index][$slot])) $this->craftingItems[$index][$slot] = 0; $this->craftingItems[$index][$slot] += $count; - console("Ingridient: $id, $meta, $count into $slot"); + //console("Ingridient: $id, $meta, $count into $slot"); if($this->tryCraft() === false){ $this->sendInventory(); } diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index d9a838b3..d4678234 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -135,7 +135,7 @@ class CraftingRecipes{ "BEETROOT:?x4,BOWL:?x1=>BEETROOT_SOUP:0x1", "WOODEN_PLANKS:?x3=>BOWL:0x4", "WHEAT:?x3=>BREAD:0x1", - "WHEAT:?x3,BUCKET:1x3,EGG:?x1,SUGAR:?x2=>CAKE:0x1", + "WHEAT:?x3,BUCKET:1x3,EGG:?x1,SUGAR:?x2=>CAKE:0x1,BUCKET:0x3", "DIAMOND:?x4=>DIAMOND_BOOTS:0x1", "DIAMOND:?x8=>DIAMOND_CHESTPLATE:0x1", "DIAMOND:?x5=>DIAMOND_HELMET:0x1", @@ -200,6 +200,13 @@ class CraftingRecipes{ "COBBLESTONE:?x6=>COBBLESTONE_STAIRS:0x4", ]; + /** + * Each element structure: + * * 0 - ingridients
+ * * 1 - results
+ * * 2 - recipe string
+ * @var array[] + */ private static $recipes = []; public static function init(){ @@ -230,9 +237,9 @@ class CraftingRecipes{ } private static function parseRecipe($recipe){ - $recipe = explode("=>", $recipe); + [$ingridients, $results] = explode("=>", $recipe); $recipeItems = []; - foreach(explode(",", $recipe[0]) as $item){ + foreach(explode(",", $ingridients) as $item){ $item = explode("x", $item); $id = explode(":", $item[0]); $meta = array_pop($id); @@ -249,31 +256,53 @@ class CraftingRecipes{ } } ksort($recipeItems); - $item = explode("x", $recipe[1]); - $id = explode(":", $item[0]); - $meta = array_pop($id); - $id = $id[0]; - - $it = BlockAPI::fromString($id); - - $craftItem = [$it->getID(), intval($meta) & 0xFFFF, intval($item[1])]; + + $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] . ","; + $recipeString .= "{$item[0]}x{$item[2]},"; } - $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; + foreach($craftItems as $item){ + $resultString .= "{$item[0]}x{$item[2]},"; + } + + $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); - return [$recipeItems, $craftItem, $recipeString]; + return [$recipeItems, $craftItems, $recipeString]; } - public static function canCraft(array $craftItem, array $recipeItems, $type){ + public static function canCraft(array $craftItems, array $recipeItems, $type){ ksort($recipeItems); + ksort($craftItems); + $recipeString = ""; + $resultString = ""; foreach($recipeItems as $item){ $recipeString .= $item[0] . "x" . $item[2] . ","; } - $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; + foreach($craftItems as $item){ + $resultString .= "{$item[0]}x{$item[2]},"; + } + + $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){ @@ -281,7 +310,7 @@ class CraftingRecipes{ while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ $continue = true; $recipe = CraftingRecipes::$recipes[$r[0]]; - foreach($recipe[0] as $item){ + foreach($recipe[0] as $item){ //check ingridients if(!isset($recipeItems[$item[0]])){ $continue = false; break; @@ -293,7 +322,23 @@ class CraftingRecipes{ break; } } - if($continue === false or $craftItem[0] !== $recipe[1][0]){ + if($continue === false){ + $continue = false; + continue; + } + foreach($recipe[1] as $item){ //check results + if(!isset($craftItems[$item[0]])){ + $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($continue === false){ $continue = false; continue; } From 6b0b1aa985da577763fa67b0b54ea33191d4686d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 04:41:55 +0300 Subject: [PATCH 17/17] reset crafting type whenever resetting crafting arrays --- src/Player.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index 68f2d78b..9279f6c0 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1975,6 +1975,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $packet->eid = $this->eid; $data = []; $data["eid"] = $packet->eid; @@ -2101,7 +2102,8 @@ class Player{ $packet->eid = $this->eid; $this->craftingItems = []; $this->toCraft = []; - + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; + switch($packet->action){ case 5: //Shot arrow if($this->entity->inAction){ @@ -2182,6 +2184,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->server->api->block->playerBlockBreak($this, $blockVector); break; case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: @@ -2190,7 +2193,8 @@ 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]; @@ -2234,6 +2238,7 @@ class Player{ $data["action"] = $packet->action; $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $target = $this->server->api->entity->get($packet->target); if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ $data["targetentity"] = $packet->target; @@ -2261,6 +2266,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->checkSpawnPosition(); $this->teleport($this->spawnPosition, false, false, true, false); @@ -2299,6 +2305,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $packet->eid = $this->eid; if($this->entity->inAction === true){ $this->entity->inAction = false; @@ -2359,6 +2366,7 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $data["eid"] = $packet->eid; $data["unknown"] = $packet->unknown; $data["item"] = $packet->item; @@ -2391,6 +2399,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; if(trim($packet->message) != "" and strlen($packet->message) <= 255){ $message = $packet->message; if($message[0] === "/"){ //Command @@ -2428,6 +2437,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; if(isset($this->windows[$packet->windowid])){ if(is_array($this->windows[$packet->windowid])){ foreach($this->windows[$packet->windowid] as $ob){ @@ -2484,6 +2494,7 @@ class Player{ }else{ $this->toCraft = []; $this->craftingItems = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; } if(!isset($this->windows[$packet->windowid])){ break; @@ -2597,6 +2608,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $t = $this->server->api->tile->get(new Position($packet->x, $packet->y, $packet->z, $this->level)); if(($t instanceof Tile) and $t->class === TILE_SIGN){ if($t->data["creator"] !== $this->username){