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 if(!$this->player->interactBlock($vBlockPos, $data->getFace(), $clickPos)){
497 $this->onFailedBlockAction($vBlockPos, $data->getFace());
500 case UseItemTransactionData::ACTION_BREAK_BLOCK:
501 $blockPos = $data->getBlockPosition();
502 $vBlockPos =
new Vector3($blockPos->getX(), $blockPos->getY(), $blockPos->getZ());
503 if(!$this->player->breakBlock($vBlockPos)){
504 $this->onFailedBlockAction($vBlockPos,
null);
507 case UseItemTransactionData::ACTION_CLICK_AIR:
508 if($this->player->isUsingItem()){
509 if(!$this->player->consumeHeldItem()){
510 $hungerAttr = $this->player->getAttributeMap()->get(Attribute::HUNGER) ??
throw new AssumptionFailedError();
511 $hungerAttr->markSynchronized(
false);
515 $this->player->useHeldItem();
525 private static function validateFacing(
int $facing) :
void{
526 if(!in_array($facing, Facing::ALL,
true)){
534 private function onFailedBlockAction(
Vector3 $blockPos, ?
int $face) :
void{
535 if($blockPos->distanceSquared($this->player->getLocation()) < 10000){
538 $sidePos = $blockPos->
getSide($face);
541 array_push($blocks, ...$sidePos->sidesArray());
543 $blocks[] = $blockPos;
545 foreach($this->player->getWorld()->createBlockUpdatePackets($blocks) as $packet){
546 $this->session->sendDataPacket($packet);
552 $target = $this->player->getWorld()->getEntity($data->getActorRuntimeId());
553 if($target ===
null){
557 $this->player->selectHotbarSlot($data->getHotbarSlot());
559 switch($data->getActionType()){
560 case UseItemOnEntityTransactionData::ACTION_INTERACT:
561 $this->player->interactEntity($target, $data->getClickPosition());
563 case UseItemOnEntityTransactionData::ACTION_ATTACK:
564 $this->player->attackEntity($target);
572 $this->player->selectHotbarSlot($data->getHotbarSlot());
574 if($data->getActionType() == ReleaseItemTransactionData::ACTION_RELEASE){
575 $this->player->releaseHeldItem();
598 $transaction = $executor->generateInventoryTransaction();
599 $result = $this->executeInventoryTransaction($transaction, $request->getRequestId());
602 $this->session->getLogger()->debug(
"ItemStackRequest #" . $request->getRequestId() .
" failed: " . $e->getMessage());
603 $this->session->getLogger()->debug(implode(
"\n", Utils::printableExceptionInfo($e)));
604 $this->inventoryManager->requestSyncAll();
608 return new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $request->getRequestId());
610 return $executor->buildItemStackResponse();
615 if(count($packet->getRequests()) > 80){
619 foreach($packet->getRequests() as $request){
620 $responses[] = $this->handleSingleItemStackRequest($request);
629 if($packet->windowId === ContainerIds::OFFHAND){
632 if($packet->windowId === ContainerIds::INVENTORY){
633 $this->inventoryManager->onClientSelectHotbarSlot($packet->hotbarSlot);
634 if(!$this->player->selectHotbarSlot($packet->hotbarSlot)){
635 $this->inventoryManager->syncSelectedHotbarSlot();
647 if($packet->action === InteractPacket::ACTION_MOUSEOVER){
655 $target = $this->player->getWorld()->getEntity($packet->targetActorRuntimeId);
656 if($target ===
null){
659 if($packet->action === InteractPacket::ACTION_OPEN_INVENTORY && $target === $this->player){
660 $this->inventoryManager->onClientOpenMainInventory();
667 return $this->player->pickBlock(
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ()), $packet->addUserData);
675 return $this->handlePlayerActionFromData($packet->action, $packet->blockPosition, $packet->face);
678 private function handlePlayerActionFromData(
int $action,
BlockPosition $blockPosition,
int $face) :
bool{
679 $pos =
new Vector3($blockPosition->getX(), $blockPosition->getY(), $blockPosition->getZ());
682 case PlayerAction::START_BREAK:
683 self::validateFacing($face);
684 if(!$this->player->attackBlock($pos, $face)){
685 $this->onFailedBlockAction($pos, $face);
690 case PlayerAction::ABORT_BREAK:
691 case PlayerAction::STOP_BREAK:
692 $this->player->stopBreakBlock($pos);
694 case PlayerAction::START_SLEEPING:
697 case PlayerAction::STOP_SLEEPING:
698 $this->player->stopSleep();
700 case PlayerAction::CRACK_BREAK:
701 self::validateFacing($face);
702 $this->player->continueBreakBlock($pos, $face);
704 case PlayerAction::INTERACT_BLOCK:
706 case PlayerAction::CREATIVE_PLAYER_DESTROY_BLOCK:
709 case PlayerAction::START_ITEM_USE_ON:
710 case PlayerAction::STOP_ITEM_USE_ON:
714 $this->session->getLogger()->debug(
"Unhandled/unknown player action type " . $action);
718 $this->player->setUsingItem(
false);
732 $this->inventoryManager->onClientRemoveWindow($packet->windowId);
741 $pos =
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ());
742 if($pos->distanceSquared($this->player->getLocation()) > 10000){
746 $block = $this->player->getLocation()->getWorld()->getBlock($pos);
747 $nbt = $packet->nbt->getRoot();
751 $frontTextTag = $nbt->getTag(Sign::TAG_FRONT_TEXT);
753 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($frontTextTag) .
" for tag \"" . Sign::TAG_FRONT_TEXT .
"\" in sign update data");
755 $textBlobTag = $frontTextTag->getTag(Sign::TAG_TEXT_BLOB);
757 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($textBlobTag) .
" for tag \"" . Sign::TAG_TEXT_BLOB .
"\" in sign update data");
761 $text = SignText::fromBlob($textBlobTag->getValue());
762 }
catch(\InvalidArgumentException $e){
763 throw PacketHandlingException::wrap($e,
"Invalid sign text update");
767 if(!$block->updateText($this->player, $text)){
768 foreach($this->player->getWorld()->createBlockUpdatePackets([$pos]) as $updatePacket){
769 $this->session->sendDataPacket($updatePacket);
772 }
catch(\UnexpectedValueException $e){
773 throw PacketHandlingException::wrap($e);
787 $gameMode = $this->session->getTypeConverter()->protocolGameModeToCore($packet->gamemode);
788 if($gameMode !== $this->player->getGamemode()){
790 $this->session->syncGameMode($this->player->getGamemode(),
true);
804 $this->player->setViewDistance($packet->radius);
818 if(str_starts_with($packet->command,
'/')){
819 $this->player->chat($packet->command);
830 if($packet->skin->getFullSkinId() === $this->lastRequestedFullSkinId){
833 $this->session->getLogger()->debug(
"Refused duplicate skin change request");
836 $this->lastRequestedFullSkinId = $packet->skin->getFullSkinId();
838 $this->session->getLogger()->debug(
"Processing skin change request");
840 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData($packet->skin);
842 throw PacketHandlingException::wrap($e,
"Invalid skin in PlayerSkinPacket");
844 return $this->player->changeSkin($skin, $packet->newSkinName, $packet->oldSkinName);
854 private function checkBookText(
string $string,
string $fieldName,
int $softLimit,
int $hardLimit,
bool &$cancel) :
string{
855 if(strlen($string) > $hardLimit){
856 throw new PacketHandlingException(sprintf(
"Book %s must be at most %d bytes, but have %d bytes", $fieldName, $hardLimit, strlen($string)));
859 $result = TextFormat::clean($string,
false);
861 if(strlen($result) > $softLimit * 4 || mb_strlen($result,
'UTF-8') > $softLimit){
863 $this->session->getLogger()->debug(
"Cancelled book edit due to $fieldName exceeded soft limit of $softLimit chars");
870 $inventory = $this->player->getInventory();
871 if(!$inventory->slotExists($packet->inventorySlot)){
875 $oldBook = $inventory->getItem($packet->inventorySlot);
880 $newBook = clone $oldBook;
883 switch($packet->type){
884 case BookEditPacket::TYPE_REPLACE_PAGE:
885 $text = self::checkBookText($packet->text,
"page text", 256, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
886 $newBook->setPageText($packet->pageNumber, $text);
887 $modifiedPages[] = $packet->pageNumber;
889 case BookEditPacket::TYPE_ADD_PAGE:
890 if(!$newBook->pageExists($packet->pageNumber)){
895 $text = self::checkBookText($packet->text,
"page text", 256, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
896 $newBook->insertPage($packet->pageNumber, $text);
897 $modifiedPages[] = $packet->pageNumber;
899 case BookEditPacket::TYPE_DELETE_PAGE:
900 if(!$newBook->pageExists($packet->pageNumber)){
903 $newBook->deletePage($packet->pageNumber);
904 $modifiedPages[] = $packet->pageNumber;
906 case BookEditPacket::TYPE_SWAP_PAGES:
907 if(!$newBook->pageExists($packet->pageNumber) || !$newBook->pageExists($packet->secondaryPageNumber)){
909 $newBook->addPage(max($packet->pageNumber, $packet->secondaryPageNumber));
911 $newBook->swapPages($packet->pageNumber, $packet->secondaryPageNumber);
912 $modifiedPages = [$packet->pageNumber, $packet->secondaryPageNumber];
914 case BookEditPacket::TYPE_SIGN_BOOK:
915 $title = self::checkBookText($packet->title,
"title", 16, Limits::INT16_MAX, $cancel);
917 $author = self::checkBookText($packet->author,
"author", 256, Limits::INT16_MAX, $cancel);
919 $newBook = VanillaItems::WRITTEN_BOOK()
920 ->setPages($oldBook->getPages())
923 ->setGeneration(WrittenBook::GENERATION_ORIGINAL);
930 $action = match($packet->type){
931 BookEditPacket::TYPE_REPLACE_PAGE => PlayerEditBookEvent::ACTION_REPLACE_PAGE,
932 BookEditPacket::TYPE_ADD_PAGE => PlayerEditBookEvent::ACTION_ADD_PAGE,
933 BookEditPacket::TYPE_DELETE_PAGE => PlayerEditBookEvent::ACTION_DELETE_PAGE,
934 BookEditPacket::TYPE_SWAP_PAGES => PlayerEditBookEvent::ACTION_SWAP_PAGES,
935 BookEditPacket::TYPE_SIGN_BOOK => PlayerEditBookEvent::ACTION_SIGN_BOOK,
943 $oldPageCount = count($oldBook->getPages());
944 $newPageCount = count($newBook->getPages());
945 if(($newPageCount > $oldPageCount && $newPageCount > 50)){
946 $this->session->getLogger()->debug(
"Cancelled book edit due to adding too many pages (new page count would be $newPageCount)");
950 $event =
new PlayerEditBookEvent($this->player, $oldBook, $newBook, $action, $modifiedPages);
956 if($event->isCancelled()){
960 $this->player->getInventory()->setItem($packet->inventorySlot, $event->getNewBook());
966 if($packet->cancelReason !==
null){
968 return $this->player->onFormSubmit($packet->formId,
null);
969 }elseif($packet->formData !==
null){
971 $responseData = json_decode($packet->formData,
true, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR);
972 }
catch(\JsonException $e){
973 throw PacketHandlingException::wrap($e,
"Failed to decode form response data");
975 return $this->player->onFormSubmit($packet->formId, $responseData);
977 throw new PacketHandlingException(
"Expected either formData or cancelReason to be set in ModalFormResponsePacket");
990 $pos = $packet->blockPosition;
991 $chunkX = $pos->getX() >> Chunk::COORD_BIT_SIZE;
992 $chunkZ = $pos->getZ() >> Chunk::COORD_BIT_SIZE;
993 $world = $this->player->getWorld();
994 if(!$world->isChunkLoaded($chunkX, $chunkZ) || $world->isChunkLocked($chunkX, $chunkZ)){
998 $lectern = $world->getBlockAt($pos->getX(), $pos->getY(), $pos->getZ());
999 if($lectern instanceof
Lectern && $this->player->canInteract($lectern->getPosition(), 15)){
1000 if(!$lectern->onPageTurn($packet->page)){
1001 $this->onFailedBlockAction($lectern->getPosition(),
null);
1023 public function handleEmote(
EmotePacket $packet) :
bool{
1024 $this->player->emote($packet->getEmoteId());