130 private const MAX_FORM_RESPONSE_DEPTH = 2;
132 protected float $lastRightClickTime = 0.0;
135 protected ?
Vector3 $lastPlayerAuthInputPosition =
null;
136 protected ?
float $lastPlayerAuthInputYaw =
null;
137 protected ?
float $lastPlayerAuthInputPitch =
null;
138 protected ?
int $lastPlayerAuthInputFlags =
null;
140 public bool $forceMoveSync =
false;
142 protected ?
string $lastRequestedFullSkinId =
null;
144 public function __construct(
150 public function handleText(
TextPacket $packet) :
bool{
151 if($packet->type === TextPacket::TYPE_CHAT){
152 return $this->player->chat($packet->message);
164 private function resolveOnOffInputFlags(
int $inputFlags,
int $startFlag,
int $stopFlag) : ?
bool{
165 $enabled = ($inputFlags & (1 << $startFlag)) !== 0;
166 $disabled = ($inputFlags & (1 << $stopFlag)) !== 0;
167 if($enabled !== $disabled){
175 $rawPos = $packet->getPosition();
176 $rawYaw = $packet->getYaw();
177 $rawPitch = $packet->getPitch();
178 foreach([$rawPos->x, $rawPos->y, $rawPos->z, $rawYaw, $packet->getHeadYaw(), $rawPitch] as $float){
179 if(is_infinite($float) || is_nan($float)){
180 $this->session->getLogger()->debug(
"Invalid movement received, contains NAN/INF components");
185 if($rawYaw !== $this->lastPlayerAuthInputYaw || $rawPitch !== $this->lastPlayerAuthInputPitch){
186 $this->lastPlayerAuthInputYaw = $rawYaw;
187 $this->lastPlayerAuthInputPitch = $rawPitch;
189 $yaw = fmod($rawYaw, 360);
190 $pitch = fmod($rawPitch, 360);
195 $this->player->setRotation($yaw, $pitch);
198 $hasMoved = $this->lastPlayerAuthInputPosition ===
null || !$this->lastPlayerAuthInputPosition->equals($rawPos);
199 $newPos = $rawPos->subtract(0, 1.62, 0)->round(4);
201 if($this->forceMoveSync && $hasMoved){
202 $curPos = $this->player->getLocation();
204 if($newPos->distanceSquared($curPos) > 1){
205 $this->session->getLogger()->debug(
"Got outdated pre-teleport movement, received " . $newPos .
", expected " . $curPos);
211 $this->forceMoveSync =
false;
214 $inputFlags = $packet->getInputFlags();
215 if($inputFlags !== $this->lastPlayerAuthInputFlags){
216 $this->lastPlayerAuthInputFlags = $inputFlags;
218 $sneaking = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SNEAKING, PlayerAuthInputFlags::STOP_SNEAKING);
219 $sprinting = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SPRINTING, PlayerAuthInputFlags::STOP_SPRINTING);
220 $swimming = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SWIMMING, PlayerAuthInputFlags::STOP_SWIMMING);
221 $gliding = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_GLIDING, PlayerAuthInputFlags::STOP_GLIDING);
222 $flying = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_FLYING, PlayerAuthInputFlags::STOP_FLYING);
224 ($sneaking !==
null && !$this->player->toggleSneak($sneaking)) |
225 ($sprinting !==
null && !$this->player->toggleSprint($sprinting)) |
226 ($swimming !==
null && !$this->player->toggleSwim($swimming)) |
227 ($gliding !==
null && !$this->player->toggleGlide($gliding)) |
228 ($flying !==
null && !$this->player->toggleFlight($flying));
229 if((
bool) $mismatch){
230 $this->player->sendData([$this->player]);
234 $this->player->jump();
237 $this->player->missSwing();
241 if(!$this->forceMoveSync && $hasMoved){
242 $this->lastPlayerAuthInputPosition = $rawPos;
244 $this->player->handleMovement($newPos);
247 $packetHandled =
true;
249 $blockActions = $packet->getBlockActions();
250 if($blockActions !==
null){
251 if(count($blockActions) > 100){
254 foreach($blockActions as $k => $blockAction){
255 $actionHandled =
false;
257 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(),
new BlockPosition(0, 0, 0), Facing::DOWN);
259 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(), $blockAction->getBlockPosition(), $blockAction->getFace());
263 $packetHandled =
false;
264 $this->session->getLogger()->debug(
"Unhandled player block action at offset $k in PlayerAuthInputPacket");
269 $useItemTransaction = $packet->getItemInteractionData();
270 if($useItemTransaction !==
null){
271 if(count($useItemTransaction->getTransactionData()->getActions()) > 100){
275 $this->inventoryManager->setCurrentItemStackRequestId($useItemTransaction->getRequestId());
276 $this->inventoryManager->addRawPredictedSlotChanges($useItemTransaction->getTransactionData()->getActions());
277 if(!$this->handleUseItemTransaction($useItemTransaction->getTransactionData())){
278 $packetHandled =
false;
279 $this->session->getLogger()->debug(
"Unhandled transaction in PlayerAuthInputPacket (type " . $useItemTransaction->getTransactionData()->getActionType() .
")");
281 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
283 $this->inventoryManager->setCurrentItemStackRequestId(
null);
286 $itemStackRequest = $packet->getItemStackRequest();
287 if($itemStackRequest !==
null){
288 $result = $this->handleSingleItemStackRequest($itemStackRequest);
292 return $packetHandled;
300 if($packet->actorRuntimeId !== $this->player->getId()){
302 return $packet->actorRuntimeId === ActorEvent::EATING_ITEM;
305 switch($packet->eventId){
306 case ActorEvent::EATING_ITEM:
307 $item = $this->player->getInventory()->getItemInHand();
311 $this->player->broadcastAnimation(
new ConsumingItemAnimation($this->player, $this->player->getInventory()->getItemInHand()));
323 if(count($packet->trData->getActions()) > 50){
326 if(count($packet->requestChangedSlots) > 10){
330 $this->inventoryManager->setCurrentItemStackRequestId($packet->requestId);
331 $this->inventoryManager->addRawPredictedSlotChanges($packet->trData->getActions());
334 $result = $this->handleNormalTransaction($packet->trData, $packet->requestId);
336 $this->session->getLogger()->debug(
"Mismatch transaction received");
337 $this->inventoryManager->requestSyncAll();
340 $result = $this->handleUseItemTransaction($packet->trData);
342 $result = $this->handleUseItemOnEntityTransaction($packet->trData);
344 $result = $this->handleReleaseItemTransaction($packet->trData);
347 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
353 foreach($packet->requestChangedSlots as $containerInfo){
354 foreach($containerInfo->getChangedSlotIndexes() as $netSlot){
356 $inventoryAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slot);
357 if($inventoryAndSlot !==
null){
358 $this->inventoryManager->onSlotChange($inventoryAndSlot[0], $inventoryAndSlot[1]);
363 $this->inventoryManager->setCurrentItemStackRequestId(
null);
367 private function executeInventoryTransaction(
InventoryTransaction $transaction,
int $requestId) :
bool{
368 $this->player->setUsingItem(
false);
370 $this->inventoryManager->setCurrentItemStackRequestId($requestId);
371 $this->inventoryManager->addTransactionPredictedSlotChanges($transaction);
375 $this->inventoryManager->requestSyncAll();
376 $logger = $this->session->getLogger();
377 $logger->debug(
"Invalid inventory transaction $requestId: " . $e->getMessage());
381 $this->session->getLogger()->debug(
"Inventory transaction $requestId cancelled by a plugin");
385 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
386 $this->inventoryManager->setCurrentItemStackRequestId(
null);
392 private function handleNormalTransaction(
NormalTransactionData $data,
int $itemStackRequestId) :
bool{
399 if($actionCount > 2){
400 if($actionCount > 5){
406 $this->session->getLogger()->debug(
"Ignoring normal inventory transaction with $actionCount actions (drop-item should have exactly 2 actions)");
411 $clientItemStack =
null;
412 $droppedCount =
null;
414 foreach($data->
getActions() as $networkInventoryAction){
415 if($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_WORLD && $networkInventoryAction->inventorySlot == NetworkInventoryAction::ACTION_MAGIC_SLOT_DROP_ITEM){
416 $droppedCount = $networkInventoryAction->newItem->getItemStack()->getCount();
417 if($droppedCount <= 0){
420 }elseif($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && $networkInventoryAction->windowId === ContainerIds::INVENTORY){
422 $sourceSlot = $networkInventoryAction->inventorySlot;
423 $clientItemStack = $networkInventoryAction->oldItem->getItemStack();
425 $this->session->getLogger()->debug(
"Unexpected inventory action type $networkInventoryAction->sourceType in drop item transaction");
429 if($sourceSlot ===
null || $clientItemStack ===
null || $droppedCount ===
null){
430 $this->session->getLogger()->debug(
"Missing information in drop item transaction, need source slot, client item stack and dropped count");
434 $inventory = $this->player->getInventory();
436 if(!$inventory->slotExists($sourceSlot)){
440 $sourceSlotItem = $inventory->getItem($sourceSlot);
441 if($sourceSlotItem->getCount() < $droppedCount){
444 $serverItemStack = $this->session->getTypeConverter()->coreItemStackToNet($sourceSlotItem);
448 $serverItemStack->getId() !== $clientItemStack->getId() ||
449 $serverItemStack->getMeta() !== $clientItemStack->getMeta() ||
450 $serverItemStack->getCount() !== $clientItemStack->getCount() ||
451 $serverItemStack->getBlockRuntimeId() !== $clientItemStack->getBlockRuntimeId()
461 $droppedItem = $sourceSlotItem->pop($droppedCount);
464 $builder->getInventory($inventory)->setItem($sourceSlot, $sourceSlotItem);
468 return $this->executeInventoryTransaction($transaction, $itemStackRequestId);
472 $this->player->selectHotbarSlot($data->getHotbarSlot());
474 switch($data->getActionType()){
475 case UseItemTransactionData::ACTION_CLICK_BLOCK:
477 $clickPos = $data->getClickPosition();
478 $spamBug = ($this->lastRightClickData !==
null &&
479 microtime(
true) - $this->lastRightClickTime < 0.1 &&
480 $this->lastRightClickData->getPlayerPosition()->distanceSquared($data->getPlayerPosition()) < 0.00001 &&
481 $this->lastRightClickData->getBlockPosition()->equals($data->getBlockPosition()) &&
482 $this->lastRightClickData->getClickPosition()->distanceSquared($clickPos) < 0.00001
485 $this->lastRightClickData = $data;
486 $this->lastRightClickTime = microtime(
true);
492 self::validateFacing($data->getFace());
494 $blockPos = $data->getBlockPosition();
495 $vBlockPos =
new Vector3($blockPos->getX(), $blockPos->getY(), $blockPos->getZ());
496 $this->player->interactBlock($vBlockPos, $data->getFace(), $clickPos);
501 $this->syncBlocksNearby($vBlockPos, $data->getFace());
503 case UseItemTransactionData::ACTION_BREAK_BLOCK:
504 $blockPos = $data->getBlockPosition();
505 $vBlockPos =
new Vector3($blockPos->getX(), $blockPos->getY(), $blockPos->getZ());
506 if(!$this->player->breakBlock($vBlockPos)){
507 $this->syncBlocksNearby($vBlockPos,
null);
510 case UseItemTransactionData::ACTION_CLICK_AIR:
511 if($this->player->isUsingItem()){
512 if(!$this->player->consumeHeldItem()){
513 $hungerAttr = $this->player->getAttributeMap()->get(Attribute::HUNGER) ??
throw new AssumptionFailedError();
514 $hungerAttr->markSynchronized(
false);
518 $this->player->useHeldItem();
528 private static function validateFacing(
int $facing) :
void{
529 if(!in_array($facing, Facing::ALL,
true)){
537 private function syncBlocksNearby(
Vector3 $blockPos, ?
int $face) :
void{
538 if($blockPos->distanceSquared($this->player->getLocation()) < 10000){
541 $sidePos = $blockPos->
getSide($face);
544 array_push($blocks, ...$sidePos->sidesArray());
546 $blocks[] = $blockPos;
548 foreach($this->player->getWorld()->createBlockUpdatePackets($blocks) as $packet){
549 $this->session->sendDataPacket($packet);
555 $target = $this->player->getWorld()->getEntity($data->getActorRuntimeId());
556 if($target ===
null){
560 $this->player->selectHotbarSlot($data->getHotbarSlot());
562 switch($data->getActionType()){
563 case UseItemOnEntityTransactionData::ACTION_INTERACT:
564 $this->player->interactEntity($target, $data->getClickPosition());
566 case UseItemOnEntityTransactionData::ACTION_ATTACK:
567 $this->player->attackEntity($target);
575 $this->player->selectHotbarSlot($data->getHotbarSlot());
577 if($data->getActionType() == ReleaseItemTransactionData::ACTION_RELEASE){
578 $this->player->releaseHeldItem();
601 $transaction = $executor->generateInventoryTransaction();
602 $result = $this->executeInventoryTransaction($transaction, $request->getRequestId());
605 $this->session->getLogger()->debug(
"ItemStackRequest #" . $request->getRequestId() .
" failed: " . $e->getMessage());
606 $this->session->getLogger()->debug(implode(
"\n", Utils::printableExceptionInfo($e)));
607 $this->inventoryManager->requestSyncAll();
611 return new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $request->getRequestId());
613 return $executor->buildItemStackResponse();
618 if(count($packet->getRequests()) > 80){
622 foreach($packet->getRequests() as $request){
623 $responses[] = $this->handleSingleItemStackRequest($request);
632 if($packet->windowId === ContainerIds::OFFHAND){
635 if($packet->windowId === ContainerIds::INVENTORY){
636 $this->inventoryManager->onClientSelectHotbarSlot($packet->hotbarSlot);
637 if(!$this->player->selectHotbarSlot($packet->hotbarSlot)){
638 $this->inventoryManager->syncSelectedHotbarSlot();
650 if($packet->action === InteractPacket::ACTION_MOUSEOVER){
658 $target = $this->player->getWorld()->getEntity($packet->targetActorRuntimeId);
659 if($target ===
null){
662 if($packet->action === InteractPacket::ACTION_OPEN_INVENTORY && $target === $this->player){
663 $this->inventoryManager->onClientOpenMainInventory();
670 return $this->player->pickBlock(
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ()), $packet->addUserData);
674 return $this->player->pickEntity($packet->actorUniqueId);
678 return $this->handlePlayerActionFromData($packet->action, $packet->blockPosition, $packet->face);
681 private function handlePlayerActionFromData(
int $action,
BlockPosition $blockPosition,
int $face) :
bool{
682 $pos =
new Vector3($blockPosition->getX(), $blockPosition->getY(), $blockPosition->getZ());
685 case PlayerAction::START_BREAK:
686 self::validateFacing($face);
687 if(!$this->player->attackBlock($pos, $face)){
688 $this->syncBlocksNearby($pos, $face);
693 case PlayerAction::ABORT_BREAK:
694 case PlayerAction::STOP_BREAK:
695 $this->player->stopBreakBlock($pos);
697 case PlayerAction::START_SLEEPING:
700 case PlayerAction::STOP_SLEEPING:
701 $this->player->stopSleep();
703 case PlayerAction::CRACK_BREAK:
704 self::validateFacing($face);
705 $this->player->continueBreakBlock($pos, $face);
707 case PlayerAction::INTERACT_BLOCK:
709 case PlayerAction::CREATIVE_PLAYER_DESTROY_BLOCK:
712 case PlayerAction::START_ITEM_USE_ON:
713 case PlayerAction::STOP_ITEM_USE_ON:
717 $this->session->getLogger()->debug(
"Unhandled/unknown player action type " . $action);
721 $this->player->setUsingItem(
false);
735 $this->inventoryManager->onClientRemoveWindow($packet->windowId);
744 $pos =
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ());
745 if($pos->distanceSquared($this->player->getLocation()) > 10000){
749 $block = $this->player->getLocation()->getWorld()->getBlock($pos);
750 $nbt = $packet->nbt->getRoot();
754 $frontTextTag = $nbt->getTag(Sign::TAG_FRONT_TEXT);
756 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($frontTextTag) .
" for tag \"" . Sign::TAG_FRONT_TEXT .
"\" in sign update data");
758 $textBlobTag = $frontTextTag->getTag(Sign::TAG_TEXT_BLOB);
760 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($textBlobTag) .
" for tag \"" . Sign::TAG_TEXT_BLOB .
"\" in sign update data");
764 $text = SignText::fromBlob($textBlobTag->getValue());
765 }
catch(\InvalidArgumentException $e){
766 throw PacketHandlingException::wrap($e,
"Invalid sign text update");
770 if(!$block->updateText($this->player, $text)){
771 foreach($this->player->getWorld()->createBlockUpdatePackets([$pos]) as $updatePacket){
772 $this->session->sendDataPacket($updatePacket);
775 }
catch(\UnexpectedValueException $e){
776 throw PacketHandlingException::wrap($e);
790 $gameMode = $this->session->getTypeConverter()->protocolGameModeToCore($packet->gamemode);
791 if($gameMode !== $this->player->getGamemode()){
793 $this->session->syncGameMode($this->player->getGamemode(),
true);
807 $this->player->setViewDistance($packet->radius);
821 if(str_starts_with($packet->command,
'/')){
822 $this->player->chat($packet->command);
833 if($packet->skin->getFullSkinId() === $this->lastRequestedFullSkinId){
836 $this->session->getLogger()->debug(
"Refused duplicate skin change request");
839 $this->lastRequestedFullSkinId = $packet->skin->getFullSkinId();
841 $this->session->getLogger()->debug(
"Processing skin change request");
843 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData($packet->skin);
845 throw PacketHandlingException::wrap($e,
"Invalid skin in PlayerSkinPacket");
847 return $this->player->changeSkin($skin, $packet->newSkinName, $packet->oldSkinName);
857 private function checkBookText(
string $string,
string $fieldName,
int $softLimit,
int $hardLimit,
bool &$cancel) :
string{
858 if(strlen($string) > $hardLimit){
859 throw new PacketHandlingException(sprintf(
"Book %s must be at most %d bytes, but have %d bytes", $fieldName, $hardLimit, strlen($string)));
862 $result = TextFormat::clean($string,
false);
864 if(strlen($result) > $softLimit * 4 || mb_strlen($result,
'UTF-8') > $softLimit){
866 $this->session->getLogger()->debug(
"Cancelled book edit due to $fieldName exceeded soft limit of $softLimit chars");
873 $inventory = $this->player->getInventory();
874 if(!$inventory->slotExists($packet->inventorySlot)){
878 $oldBook = $inventory->getItem($packet->inventorySlot);
883 $newBook = clone $oldBook;
886 switch($packet->type){
887 case BookEditPacket::TYPE_REPLACE_PAGE:
888 $text = self::checkBookText($packet->text,
"page text", 256, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
889 $newBook->setPageText($packet->pageNumber, $text);
890 $modifiedPages[] = $packet->pageNumber;
892 case BookEditPacket::TYPE_ADD_PAGE:
893 if(!$newBook->pageExists($packet->pageNumber)){
898 $text = self::checkBookText($packet->text,
"page text", 256, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
899 $newBook->insertPage($packet->pageNumber, $text);
900 $modifiedPages[] = $packet->pageNumber;
902 case BookEditPacket::TYPE_DELETE_PAGE:
903 if(!$newBook->pageExists($packet->pageNumber)){
906 $newBook->deletePage($packet->pageNumber);
907 $modifiedPages[] = $packet->pageNumber;
909 case BookEditPacket::TYPE_SWAP_PAGES:
910 if(!$newBook->pageExists($packet->pageNumber) || !$newBook->pageExists($packet->secondaryPageNumber)){
912 $newBook->addPage(max($packet->pageNumber, $packet->secondaryPageNumber));
914 $newBook->swapPages($packet->pageNumber, $packet->secondaryPageNumber);
915 $modifiedPages = [$packet->pageNumber, $packet->secondaryPageNumber];
917 case BookEditPacket::TYPE_SIGN_BOOK:
918 $title = self::checkBookText($packet->title,
"title", 16, Limits::INT16_MAX, $cancel);
920 $author = self::checkBookText($packet->author,
"author", 256, Limits::INT16_MAX, $cancel);
922 $newBook = VanillaItems::WRITTEN_BOOK()
923 ->setPages($oldBook->getPages())
926 ->setGeneration(WrittenBook::GENERATION_ORIGINAL);
933 $action = match($packet->type){
934 BookEditPacket::TYPE_REPLACE_PAGE => PlayerEditBookEvent::ACTION_REPLACE_PAGE,
935 BookEditPacket::TYPE_ADD_PAGE => PlayerEditBookEvent::ACTION_ADD_PAGE,
936 BookEditPacket::TYPE_DELETE_PAGE => PlayerEditBookEvent::ACTION_DELETE_PAGE,
937 BookEditPacket::TYPE_SWAP_PAGES => PlayerEditBookEvent::ACTION_SWAP_PAGES,
938 BookEditPacket::TYPE_SIGN_BOOK => PlayerEditBookEvent::ACTION_SIGN_BOOK,
946 $oldPageCount = count($oldBook->getPages());
947 $newPageCount = count($newBook->getPages());
948 if(($newPageCount > $oldPageCount && $newPageCount > 50)){
949 $this->session->getLogger()->debug(
"Cancelled book edit due to adding too many pages (new page count would be $newPageCount)");
953 $event =
new PlayerEditBookEvent($this->player, $oldBook, $newBook, $action, $modifiedPages);
959 if($event->isCancelled()){
963 $this->player->getInventory()->setItem($packet->inventorySlot, $event->getNewBook());
969 if($packet->cancelReason !==
null){
971 return $this->player->onFormSubmit($packet->formId,
null);
972 }elseif($packet->formData !==
null){
974 $responseData = json_decode($packet->formData,
true, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR);
975 }
catch(\JsonException $e){
976 throw PacketHandlingException::wrap($e,
"Failed to decode form response data");
978 return $this->player->onFormSubmit($packet->formId, $responseData);
980 throw new PacketHandlingException(
"Expected either formData or cancelReason to be set in ModalFormResponsePacket");
993 $pos = $packet->blockPosition;
994 $chunkX = $pos->getX() >> Chunk::COORD_BIT_SIZE;
995 $chunkZ = $pos->getZ() >> Chunk::COORD_BIT_SIZE;
996 $world = $this->player->getWorld();
997 if(!$world->isChunkLoaded($chunkX, $chunkZ) || $world->isChunkLocked($chunkX, $chunkZ)){
1001 $lectern = $world->getBlockAt($pos->getX(), $pos->getY(), $pos->getZ());
1002 if($lectern instanceof
Lectern && $this->player->canInteract($lectern->getPosition(), 15)){
1003 if(!$lectern->onPageTurn($packet->page)){
1004 $this->syncBlocksNearby($lectern->getPosition(),
null);
1026 public function handleEmote(
EmotePacket $packet) :
bool{
1027 $this->player->emote($packet->getEmoteId());