130 private const MAX_FORM_RESPONSE_SIZE = 10 * 1024;
131 private const MAX_FORM_RESPONSE_DEPTH = 2;
136 private const PAGE_LENGTH_SOFT_LIMIT_CHARS = 512;
138 protected float $lastRightClickTime = 0.0;
141 protected ?
Vector3 $lastPlayerAuthInputPosition =
null;
142 protected ?
float $lastPlayerAuthInputYaw =
null;
143 protected ?
float $lastPlayerAuthInputPitch =
null;
144 protected ?
BitSet $lastPlayerAuthInputFlags =
null;
148 public bool $forceMoveSync =
false;
150 protected ?
string $lastRequestedFullSkinId =
null;
152 public function __construct(
158 public function handleText(
TextPacket $packet) :
bool{
159 if($packet->type === TextPacket::TYPE_CHAT){
160 return $this->player->chat($packet->message);
166 private function resolveOnOffInputFlags(
BitSet $inputFlags,
int $startFlag,
int $stopFlag) : ?
bool{
167 $enabled = $inputFlags->get($startFlag);
168 $disabled = $inputFlags->get($stopFlag);
169 if($enabled !== $disabled){
177 $rawPos = $packet->getPosition();
178 $rawYaw = $packet->getYaw();
179 $rawPitch = $packet->getPitch();
180 foreach([$rawPos->x, $rawPos->y, $rawPos->z, $rawYaw, $packet->getHeadYaw(), $rawPitch] as $float){
181 if(is_infinite($float) || is_nan($float)){
182 $this->session->getLogger()->debug(
"Invalid movement received, contains NAN/INF components");
187 if($rawYaw !== $this->lastPlayerAuthInputYaw || $rawPitch !== $this->lastPlayerAuthInputPitch){
188 $this->lastPlayerAuthInputYaw = $rawYaw;
189 $this->lastPlayerAuthInputPitch = $rawPitch;
191 $yaw = fmod($rawYaw, 360);
192 $pitch = fmod($rawPitch, 360);
197 $this->player->setRotation($yaw, $pitch);
200 $hasMoved = $this->lastPlayerAuthInputPosition ===
null || !$this->lastPlayerAuthInputPosition->equals($rawPos);
201 $newPos = $rawPos->subtract(0, 1.62, 0)->round(4);
203 if($this->forceMoveSync && $hasMoved){
204 $curPos = $this->player->getLocation();
206 if($newPos->distanceSquared($curPos) > 1){
207 $this->session->getLogger()->debug(
"Got outdated pre-teleport movement, received " . $newPos .
", expected " . $curPos);
213 $this->forceMoveSync =
false;
216 $inputFlags = $packet->getInputFlags();
217 if($this->lastPlayerAuthInputFlags ===
null || !$inputFlags->equals($this->lastPlayerAuthInputFlags)){
218 $this->lastPlayerAuthInputFlags = $inputFlags;
222 $sneaking = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SNEAKING, PlayerAuthInputFlags::STOP_SNEAKING);
223 $sprinting = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SPRINTING, PlayerAuthInputFlags::STOP_SPRINTING);
224 $swimming = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SWIMMING, PlayerAuthInputFlags::STOP_SWIMMING);
225 $gliding = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_GLIDING, PlayerAuthInputFlags::STOP_GLIDING);
226 $flying = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_FLYING, PlayerAuthInputFlags::STOP_FLYING);
228 (!$this->player->toggleSneak($sneaking ?? $this->player->isSneaking(), $sneakPressed)) |
229 ($sprinting !==
null && !$this->player->toggleSprint($sprinting)) |
230 ($swimming !==
null && !$this->player->toggleSwim($swimming)) |
231 ($gliding !==
null && !$this->player->toggleGlide($gliding)) |
232 ($flying !==
null && !$this->player->toggleFlight($flying));
233 if((
bool) $mismatch){
234 $this->player->sendData([$this->player]);
238 $this->player->jump();
241 $this->player->missSwing();
245 if(!$this->forceMoveSync && $hasMoved){
246 $this->lastPlayerAuthInputPosition = $rawPos;
248 $this->player->handleMovement($newPos);
251 $packetHandled =
true;
253 $useItemTransaction = $packet->getItemInteractionData();
254 if($useItemTransaction !==
null){
255 if(count($useItemTransaction->getTransactionData()->getActions()) > 100){
259 $this->inventoryManager->setCurrentItemStackRequestId($useItemTransaction->getRequestId());
260 $this->inventoryManager->addRawPredictedSlotChanges($useItemTransaction->getTransactionData()->getActions());
261 if(!$this->handleUseItemTransaction($useItemTransaction->getTransactionData())){
262 $packetHandled =
false;
263 $this->session->getLogger()->debug(
"Unhandled transaction in PlayerAuthInputPacket (type " . $useItemTransaction->getTransactionData()->getActionType() .
")");
265 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
267 $this->inventoryManager->setCurrentItemStackRequestId(
null);
270 $itemStackRequest = $packet->getItemStackRequest();
271 $itemStackResponseBuilder = $itemStackRequest !==
null ? $this->handleSingleItemStackRequest($itemStackRequest) :
null;
275 $blockActions = $packet->getBlockActions();
276 if($blockActions !==
null){
277 if(count($blockActions) > 100){
280 foreach(Utils::promoteKeys($blockActions) as $k => $blockAction){
281 $actionHandled =
false;
283 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(),
new BlockPosition(0, 0, 0), 0);
285 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(), $blockAction->getBlockPosition(), $blockAction->getFace());
289 $packetHandled =
false;
290 $this->session->getLogger()->debug(
"Unhandled player block action at offset $k in PlayerAuthInputPacket");
295 if($itemStackRequest !==
null){
296 $itemStackResponse = $itemStackResponseBuilder?->build() ??
new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $itemStackRequest->getRequestId());
300 return $packetHandled;
306 if(count($packet->trData->getActions()) > 50){
309 if($packet->requestChangedSlots !==
null && count($packet->requestChangedSlots) > 10){
313 $this->inventoryManager->setCurrentItemStackRequestId($packet->requestId);
314 $this->inventoryManager->addRawPredictedSlotChanges($packet->trData->getActions());
317 $result = $this->handleNormalTransaction($packet->trData, $packet->requestId);
319 $this->session->getLogger()->debug(
"Mismatch transaction received");
320 $this->inventoryManager->requestSyncAll();
323 $result = $this->handleUseItemTransaction($packet->trData);
325 $result = $this->handleUseItemOnEntityTransaction($packet->trData);
327 $result = $this->handleReleaseItemTransaction($packet->trData);
330 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
336 if($packet->requestChangedSlots !==
null){
337 foreach($packet->requestChangedSlots as $containerInfo){
338 foreach($containerInfo->getChangedSlotIndexes() as $netSlot){
340 $inventoryAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slot);
341 if($inventoryAndSlot !==
null){
342 $this->inventoryManager->requestSyncSlot($inventoryAndSlot[0], $inventoryAndSlot[1]);
348 $this->inventoryManager->setCurrentItemStackRequestId(
null);
352 private function executeInventoryTransaction(
InventoryTransaction $transaction,
int $requestId) :
bool{
353 $this->player->setUsingItem(
false);
355 $this->inventoryManager->setCurrentItemStackRequestId($requestId);
356 $this->inventoryManager->addTransactionPredictedSlotChanges($transaction);
360 $this->inventoryManager->requestSyncAll();
361 $logger = $this->session->getLogger();
362 $logger->debug(
"Invalid inventory transaction $requestId: " . $e->getMessage());
366 $this->session->getLogger()->debug(
"Inventory transaction $requestId cancelled by a plugin");
370 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
371 $this->inventoryManager->setCurrentItemStackRequestId(
null);
377 private function handleNormalTransaction(
NormalTransactionData $data,
int $itemStackRequestId) :
bool{
384 if($actionCount > 2){
385 if($actionCount > 5){
391 $this->session->getLogger()->debug(
"Ignoring normal inventory transaction with $actionCount actions (drop-item should have exactly 2 actions)");
396 $clientItemStack =
null;
397 $droppedCount =
null;
399 foreach($data->
getActions() as $networkInventoryAction){
400 if($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_WORLD && $networkInventoryAction->inventorySlot === NetworkInventoryAction::ACTION_MAGIC_SLOT_DROP_ITEM){
401 $droppedCount = $networkInventoryAction->newItem->getItemStack()->getCount();
402 if($droppedCount <= 0){
405 }elseif($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && $networkInventoryAction->windowId === ContainerIds::INVENTORY){
407 $sourceSlot = $networkInventoryAction->inventorySlot;
408 $clientItemStack = $networkInventoryAction->oldItem->getItemStack();
410 $this->session->getLogger()->debug(
"Unexpected inventory action type $networkInventoryAction->sourceType in drop item transaction");
414 if($sourceSlot ===
null || $clientItemStack ===
null || $droppedCount ===
null){
415 $this->session->getLogger()->debug(
"Missing information in drop item transaction, need source slot, client item stack and dropped count");
419 $inventory = $this->player->getInventory();
421 if(!$inventory->slotExists($sourceSlot)){
425 $sourceSlotItem = $inventory->getItem($sourceSlot);
426 if($sourceSlotItem->getCount() < $droppedCount){
429 $serverItemStack = $this->session->getTypeConverter()->coreItemStackToNet($sourceSlotItem);
433 $serverItemStack->getId() !== $clientItemStack->getId() ||
434 $serverItemStack->getMeta() !== $clientItemStack->getMeta() ||
435 $serverItemStack->getCount() !== $clientItemStack->getCount() ||
436 $serverItemStack->getBlockRuntimeId() !== $clientItemStack->getBlockRuntimeId()
446 $droppedItem = $sourceSlotItem->pop($droppedCount);
449 $window = $this->inventoryManager->getInventoryWindow($inventory) ??
throw new AssumptionFailedError(
"This should never happen");
450 $builder->getActionBuilder($window)->setItem($sourceSlot, $sourceSlotItem);
454 return $this->executeInventoryTransaction($transaction, $itemStackRequestId);
458 $this->player->selectHotbarSlot($data->getHotbarSlot());
460 switch($data->getActionType()){
461 case UseItemTransactionData::ACTION_CLICK_BLOCK:
463 $clickPos = $data->getClickPosition();
464 $spamBug = ($this->lastRightClickData !==
null &&
465 microtime(
true) - $this->lastRightClickTime < 0.1 &&
466 $this->lastRightClickData->getFace() === $data->getFace() &&
467 $this->lastRightClickData->getPlayerPosition()->distanceSquared($data->getPlayerPosition()) < 0.00001 &&
468 $this->lastRightClickData->getBlockPosition()->equals($data->getBlockPosition()) &&
469 $this->lastRightClickData->getClickPosition()->distanceSquared($clickPos) < 0.00001
472 $this->lastRightClickData = $data;
473 $this->lastRightClickTime = microtime(
true);
479 $face = self::deserializeFacing($data->getFace());
481 $blockPos = $data->getBlockPosition();
482 $vBlockPos =
new Vector3($blockPos->getX(), $blockPos->getY(), $blockPos->getZ());
483 $this->player->interactBlock($vBlockPos, $face, $clickPos);
484 if($data->getClientInteractPrediction() === PredictedResult::SUCCESS){
492 $syncAdjacentFace =
null;
493 if($data->getItemInHand()->getItemStack()->getBlockRuntimeId() === ItemTranslator::NO_BLOCK_RUNTIME_ID){
494 $this->session->getLogger()->debug(
"Placing held item might place multiple blocks client-side; doing full adjacent sync");
495 $syncAdjacentFace = $face;
497 $this->syncBlocksNearby($vBlockPos, $syncAdjacentFace);
500 case UseItemTransactionData::ACTION_CLICK_AIR:
501 if($this->player->isUsingItem()){
502 if(!$this->player->consumeHeldItem()){
503 $hungerAttr = $this->player->getAttributeMap()->get(Attribute::HUNGER) ??
throw new AssumptionFailedError();
504 $hungerAttr->markSynchronized(
false);
509 $this->player->setUsingItem(
false);
512 $this->player->useHeldItem();
522 private static function deserializeFacing(
int $facing) :
Facing{
524 $case = Facing::tryFrom($facing);
534 private function syncBlocksNearby(
Vector3 $blockPos, ?
Facing $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());
555 if($target ===
null || $target->isFlaggedForDespawn()){
559 $this->player->selectHotbarSlot($data->getHotbarSlot());
561 switch($data->getActionType()){
562 case UseItemOnEntityTransactionData::ACTION_INTERACT:
563 $this->player->interactEntity($target, $data->getClickPosition());
565 case UseItemOnEntityTransactionData::ACTION_ATTACK:
566 $this->player->attackEntity($target);
574 $this->player->selectHotbarSlot($data->getHotbarSlot());
576 if($data->getActionType() === ReleaseItemTransactionData::ACTION_RELEASE){
577 $this->player->releaseHeldItem();
600 $transaction = $executor->generateInventoryTransaction();
601 if($transaction !==
null){
602 $result = $this->executeInventoryTransaction($transaction, $request->getRequestId());
608 $this->session->getLogger()->debug(
"ItemStackRequest #" . $request->getRequestId() .
" failed: " . $e->getMessage());
609 $this->session->getLogger()->debug(implode(
"\n", Utils::printableExceptionInfo($e)));
610 $this->inventoryManager->requestSyncAll();
613 return $result ? $executor->getItemStackResponseBuilder() :
null;
618 if(count($packet->getRequests()) > 80){
622 foreach($packet->getRequests() as $request){
623 $responses[] = $this->handleSingleItemStackRequest($request)?->build() ??
new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $request->getRequestId());
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();
646 if($packet->action === InteractPacket::ACTION_MOUSEOVER){
654 $target = $this->player->getWorld()->getEntity($packet->targetActorRuntimeId);
655 if($target ===
null){
658 if($packet->action === InteractPacket::ACTION_OPEN_INVENTORY && $target === $this->player){
659 $this->inventoryManager->onClientOpenMainInventory();
666 return $this->player->pickBlock(
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ()), $packet->addUserData);
670 return $this->player->pickEntity($packet->actorUniqueId);
674 return $this->handlePlayerActionFromData($packet->action, $packet->blockPosition, $packet->face);
677 private function handlePlayerActionFromData(
int $action,
BlockPosition $blockPosition,
int $extraData) :
bool{
678 $pos =
new Vector3($blockPosition->getX(), $blockPosition->getY(), $blockPosition->getZ());
681 case PlayerAction::START_BREAK:
682 case PlayerAction::CONTINUE_DESTROY_BLOCK:
683 $face = self::deserializeFacing($extraData);
684 if($this->lastBlockAttacked !==
null && $blockPosition->equals($this->lastBlockAttacked)){
689 $this->session->getLogger()->debug(
"Ignoring PlayerAction $action on $pos because we were already destroying this block");
692 if(!$this->player->attackBlock($pos, $face)){
693 $this->syncBlocksNearby($pos, $face);
695 $this->lastBlockAttacked = $blockPosition;
699 case PlayerAction::ABORT_BREAK:
700 case PlayerAction::STOP_BREAK:
701 $this->player->stopBreakBlock($pos);
702 $this->lastBlockAttacked =
null;
704 case PlayerAction::START_SLEEPING:
707 case PlayerAction::STOP_SLEEPING:
708 $this->player->stopSleep();
710 case PlayerAction::CRACK_BREAK:
711 $face = self::deserializeFacing($extraData);
712 $this->player->continueBreakBlock($pos, $face);
713 $this->lastBlockAttacked = $blockPosition;
715 case PlayerAction::INTERACT_BLOCK:
717 case PlayerAction::CREATIVE_PLAYER_DESTROY_BLOCK:
720 case PlayerAction::PREDICT_DESTROY_BLOCK:
721 if(!$this->player->breakBlock($pos)){
722 $face = self::deserializeFacing($extraData);
723 $this->syncBlocksNearby($pos, $face);
725 $this->lastBlockAttacked =
null;
727 case PlayerAction::START_ITEM_USE_ON:
728 case PlayerAction::STOP_ITEM_USE_ON:
732 $this->session->getLogger()->debug(
"Unhandled/unknown player action type " . $action);
736 $this->player->setUsingItem(
false);
748 $this->inventoryManager->onClientRemoveWindow($packet->windowId);
756 $textTag = $nbt->
getTag($tagName);
758 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($textTag) .
" for tag \"$tagName\" in sign update data");
760 $textBlobTag = $textTag->getTag(Sign::TAG_TEXT_BLOB);
762 throw new PacketHandlingException(
"Invalid tag type " . get_debug_type($textBlobTag) .
" for tag \"" . Sign::TAG_TEXT_BLOB .
"\" in sign update data");
766 $text = SignText::fromBlob($textBlobTag->getValue());
767 }
catch(\InvalidArgumentException $e){
768 throw PacketHandlingException::wrap($e,
"Invalid sign text update");
771 $oldText = $block->getFaceText($frontFace);
772 if($text->getLines() === $oldText->getLines()){
778 foreach($this->player->getWorld()->createBlockUpdatePackets([$pos]) as $updatePacket){
779 $this->session->sendDataPacket($updatePacket);
784 }
catch(\UnexpectedValueException $e){
785 throw PacketHandlingException::wrap($e);
790 $pos =
new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ());
791 if($pos->distanceSquared($this->player->getLocation()) > 10000){
795 $block = $this->player->getLocation()->getWorld()->getBlock($pos);
796 $nbt = $packet->nbt->getRoot();
800 if(!$this->updateSignText($nbt, Sign::TAG_FRONT_TEXT,
true, $block, $pos)){
802 $this->updateSignText($nbt, Sign::TAG_BACK_TEXT,
false, $block, $pos);
812 $gameMode = $this->session->getTypeConverter()->protocolGameModeToCore($packet->gamemode);
813 if($gameMode !== $this->player->getGamemode()){
815 $this->session->syncGameMode($this->player->getGamemode(),
true);
821 $this->player->setViewDistance($packet->radius);
827 if(str_starts_with($packet->command,
'/')){
828 $this->player->chat($packet->command);
835 if($packet->skin->getFullSkinId() === $this->lastRequestedFullSkinId){
838 $this->session->getLogger()->debug(
"Refused duplicate skin change request");
841 $this->lastRequestedFullSkinId = $packet->skin->getFullSkinId();
843 $this->session->getLogger()->debug(
"Processing skin change request");
845 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData($packet->skin);
847 throw PacketHandlingException::wrap($e,
"Invalid skin in PlayerSkinPacket");
849 return $this->player->changeSkin($skin, $packet->newSkinName, $packet->oldSkinName);
855 private function checkBookText(
string $string,
string $fieldName,
int $softLimit,
int $hardLimit,
bool &$cancel) :
string{
856 if(strlen($string) > $hardLimit){
857 throw new PacketHandlingException(sprintf(
"Book %s must be at most %d bytes, but have %d bytes", $fieldName, $hardLimit, strlen($string)));
860 $result = TextFormat::clean($string,
false);
862 if(strlen($result) > $softLimit * 4 || mb_strlen($result,
'UTF-8') > $softLimit){
864 $this->session->getLogger()->debug(
"Cancelled book edit due to $fieldName exceeded soft limit of $softLimit chars");
871 $inventory = $this->player->getInventory();
872 if(!$inventory->slotExists($packet->inventorySlot)){
876 $oldBook = $inventory->getItem($packet->inventorySlot);
881 $newBook = clone $oldBook;
884 switch($packet->type){
885 case BookEditPacket::TYPE_REPLACE_PAGE:
886 $text = self::checkBookText($packet->text,
"page text", self::PAGE_LENGTH_SOFT_LIMIT_CHARS, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
887 if($packet->pageNumber < 0){
890 $newBook->setPageText($packet->pageNumber, $text);
891 $modifiedPages[] = $packet->pageNumber;
893 case BookEditPacket::TYPE_ADD_PAGE:
894 if(!$newBook->pageExists($packet->pageNumber)){
899 $text = self::checkBookText($packet->text,
"page text", self::PAGE_LENGTH_SOFT_LIMIT_CHARS, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
900 $newBook->insertPage($packet->pageNumber, $text);
901 $modifiedPages[] = $packet->pageNumber;
903 case BookEditPacket::TYPE_DELETE_PAGE:
904 if(!$newBook->pageExists($packet->pageNumber)){
907 $newBook->deletePage($packet->pageNumber);
908 $modifiedPages[] = $packet->pageNumber;
910 case BookEditPacket::TYPE_SWAP_PAGES:
911 if($packet->pageNumber < 0 || $packet->secondaryPageNumber < 0){
914 if(!$newBook->pageExists($packet->pageNumber) || !$newBook->pageExists($packet->secondaryPageNumber)){
916 $newBook->addPage(max($packet->pageNumber, $packet->secondaryPageNumber));
918 $newBook->swapPages($packet->pageNumber, $packet->secondaryPageNumber);
919 $modifiedPages = [$packet->pageNumber, $packet->secondaryPageNumber];
921 case BookEditPacket::TYPE_SIGN_BOOK:
922 $title = self::checkBookText($packet->title,
"title", 16, Limits::INT16_MAX, $cancel);
924 $author = self::checkBookText($packet->author,
"author", 256, Limits::INT16_MAX, $cancel);
926 $newBook = VanillaItems::WRITTEN_BOOK()
927 ->setPages($oldBook->getPages())
930 ->setGeneration(WrittenBook::GENERATION_ORIGINAL);
937 $action = match($packet->type){
938 BookEditPacket::TYPE_REPLACE_PAGE => PlayerEditBookEvent::ACTION_REPLACE_PAGE,
939 BookEditPacket::TYPE_ADD_PAGE => PlayerEditBookEvent::ACTION_ADD_PAGE,
940 BookEditPacket::TYPE_DELETE_PAGE => PlayerEditBookEvent::ACTION_DELETE_PAGE,
941 BookEditPacket::TYPE_SWAP_PAGES => PlayerEditBookEvent::ACTION_SWAP_PAGES,
942 BookEditPacket::TYPE_SIGN_BOOK => PlayerEditBookEvent::ACTION_SIGN_BOOK,
950 $oldPageCount = count($oldBook->getPages());
951 $newPageCount = count($newBook->getPages());
952 if(($newPageCount > $oldPageCount && $newPageCount > 50)){
953 $this->session->getLogger()->debug(
"Cancelled book edit due to adding too many pages (new page count would be $newPageCount)");
957 $event =
new PlayerEditBookEvent($this->player, $oldBook, $newBook, $action, $modifiedPages);
963 if($event->isCancelled()){
967 $this->player->getInventory()->setItem($packet->inventorySlot, $event->getNewBook());
973 if($packet->cancelReason !==
null){
975 return $this->player->onFormSubmit($packet->formId,
null);
976 }elseif($packet->formData !==
null){
977 if(strlen($packet->formData) > self::MAX_FORM_RESPONSE_SIZE){
978 throw new PacketHandlingException(
"Form response data too large, refusing to decode (received" . strlen($packet->formData) .
" bytes, max " . self::MAX_FORM_RESPONSE_SIZE .
" bytes)");
980 if(!$this->player->hasPendingForm($packet->formId)){
981 $this->session->getLogger()->debug(
"Got unexpected response for form $packet->formId");
985 $responseData = json_decode($packet->formData,
true, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR);
986 }
catch(\JsonException $e){
987 throw PacketHandlingException::wrap($e,
"Failed to decode form response data");
989 return $this->player->onFormSubmit($packet->formId, $responseData);
991 throw new PacketHandlingException(
"Expected either formData or cancelReason to be set in ModalFormResponsePacket");
996 $pos = $packet->blockPosition;
997 $chunkX = $pos->getX() >> Chunk::COORD_BIT_SIZE;
998 $chunkZ = $pos->getZ() >> Chunk::COORD_BIT_SIZE;
999 $world = $this->player->getWorld();
1000 if(!$world->isChunkLoaded($chunkX, $chunkZ) || $world->isChunkLocked($chunkX, $chunkZ)){
1004 $lectern = $world->getBlockAt($pos->getX(), $pos->getY(), $pos->getZ());
1005 if($lectern instanceof
Lectern && $this->player->canInteract($lectern->getPosition(), 15)){
1006 if(!$lectern->onPageTurn($packet->page)){
1007 $this->syncBlocksNearby($lectern->getPosition(),
null);
1015 public function handleEmote(
EmotePacket $packet) :
bool{
1016 $this->player->emote($packet->getEmoteId());