PocketMine-MP 5.39.4 git-fc9be17cf51063d4d1d23df927963d00a2f03f09
Loading...
Searching...
No Matches
InGamePacketHandler.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\handler;
25
92use pocketmine\network\mcpe\protocol\types\inventory\PredictedResult;
109use function array_push;
110use function count;
111use function fmod;
112use function get_debug_type;
113use function implode;
114use function in_array;
115use function is_infinite;
116use function is_nan;
117use function json_decode;
118use function max;
119use function mb_strlen;
120use function microtime;
121use function sprintf;
122use function str_starts_with;
123use function strlen;
124use const JSON_THROW_ON_ERROR;
125
130 private const MAX_FORM_RESPONSE_SIZE = 10 * 1024; //10 KiB should be more than enough
131 private const MAX_FORM_RESPONSE_DEPTH = 2; //modal/simple will be 1, custom forms 2 - they will never contain anything other than string|int|float|bool|null
132
133 //TODO: The client-side per-page character limit is inconsistent for non-ASCII text,
134 //allowing input beyond 256 chars. Use a slightly higher bounded soft limit to
135 //prevent rejected edits while still mitigating book-bomb attacks
136 private const PAGE_LENGTH_SOFT_LIMIT_CHARS = 512;
137
138 protected float $lastRightClickTime = 0.0;
139 protected ?UseItemTransactionData $lastRightClickData = null;
140
141 protected ?Vector3 $lastPlayerAuthInputPosition = null;
142 protected ?float $lastPlayerAuthInputYaw = null;
143 protected ?float $lastPlayerAuthInputPitch = null;
144 protected ?BitSet $lastPlayerAuthInputFlags = null;
145
146 protected ?BlockPosition $lastBlockAttacked = null;
147
148 public bool $forceMoveSync = false;
149
150 protected ?string $lastRequestedFullSkinId = null;
151
152 public function __construct(
153 private Player $player,
154 private NetworkSession $session,
155 private InventoryManager $inventoryManager
156 ){}
157
158 public function handleText(TextPacket $packet) : bool{
159 if($packet->type === TextPacket::TYPE_CHAT){
160 return $this->player->chat($packet->message);
161 }
162
163 return false;
164 }
165
166 public function handleMovePlayer(MovePlayerPacket $packet) : bool{
167 //The client sends this every time it lands on the ground, even when using PlayerAuthInputPacket.
168 //Silence the debug spam that this causes.
169 return true;
170 }
171
172 private function resolveOnOffInputFlags(BitSet $inputFlags, int $startFlag, int $stopFlag) : ?bool{
173 $enabled = $inputFlags->get($startFlag);
174 $disabled = $inputFlags->get($stopFlag);
175 if($enabled !== $disabled){
176 return $enabled;
177 }
178 //neither flag was set, or both were set
179 return null;
180 }
181
182 public function handlePlayerAuthInput(PlayerAuthInputPacket $packet) : bool{
183 $rawPos = $packet->getPosition();
184 $rawYaw = $packet->getYaw();
185 $rawPitch = $packet->getPitch();
186 foreach([$rawPos->x, $rawPos->y, $rawPos->z, $rawYaw, $packet->getHeadYaw(), $rawPitch] as $float){
187 if(is_infinite($float) || is_nan($float)){
188 $this->session->getLogger()->debug("Invalid movement received, contains NAN/INF components");
189 return false;
190 }
191 }
192
193 if($rawYaw !== $this->lastPlayerAuthInputYaw || $rawPitch !== $this->lastPlayerAuthInputPitch){
194 $this->lastPlayerAuthInputYaw = $rawYaw;
195 $this->lastPlayerAuthInputPitch = $rawPitch;
196
197 $yaw = fmod($rawYaw, 360);
198 $pitch = fmod($rawPitch, 360);
199 if($yaw < 0){
200 $yaw += 360;
201 }
202
203 $this->player->setRotation($yaw, $pitch);
204 }
205
206 $hasMoved = $this->lastPlayerAuthInputPosition === null || !$this->lastPlayerAuthInputPosition->equals($rawPos);
207 $newPos = $rawPos->subtract(0, 1.62, 0)->round(4);
208
209 if($this->forceMoveSync && $hasMoved){
210 $curPos = $this->player->getLocation();
211
212 if($newPos->distanceSquared($curPos) > 1){ //Tolerate up to 1 block to avoid problems with client-sided physics when spawning in blocks
213 $this->session->getLogger()->debug("Got outdated pre-teleport movement, received " . $newPos . ", expected " . $curPos);
214 //Still getting movements from before teleport, ignore them
215 return true;
216 }
217
218 // Once we get a movement within a reasonable distance, treat it as a teleport ACK and remove position lock
219 $this->forceMoveSync = false;
220 }
221
222 $inputFlags = $packet->getInputFlags();
223 if($this->lastPlayerAuthInputFlags === null || !$inputFlags->equals($this->lastPlayerAuthInputFlags)){
224 $this->lastPlayerAuthInputFlags = $inputFlags;
225
226 $sneakPressed = $inputFlags->get(PlayerAuthInputFlags::SNEAKING);
227
228 $sneaking = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SNEAKING, PlayerAuthInputFlags::STOP_SNEAKING);
229 $sprinting = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SPRINTING, PlayerAuthInputFlags::STOP_SPRINTING);
230 $swimming = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_SWIMMING, PlayerAuthInputFlags::STOP_SWIMMING);
231 $gliding = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_GLIDING, PlayerAuthInputFlags::STOP_GLIDING);
232 $flying = $this->resolveOnOffInputFlags($inputFlags, PlayerAuthInputFlags::START_FLYING, PlayerAuthInputFlags::STOP_FLYING);
233 $mismatch =
234 (!$this->player->toggleSneak($sneaking ?? $this->player->isSneaking(), $sneakPressed)) |
235 ($sprinting !== null && !$this->player->toggleSprint($sprinting)) |
236 ($swimming !== null && !$this->player->toggleSwim($swimming)) |
237 ($gliding !== null && !$this->player->toggleGlide($gliding)) |
238 ($flying !== null && !$this->player->toggleFlight($flying));
239 if((bool) $mismatch){
240 $this->player->sendData([$this->player]);
241 }
242
243 if($inputFlags->get(PlayerAuthInputFlags::START_JUMPING)){
244 $this->player->jump();
245 }
246 if($inputFlags->get(PlayerAuthInputFlags::MISSED_SWING)){
247 $this->player->missSwing();
248 }
249 }
250
251 if(!$this->forceMoveSync && $hasMoved){
252 $this->lastPlayerAuthInputPosition = $rawPos;
253 //TODO: this packet has WAYYYYY more useful information that we're not using
254 $this->player->handleMovement($newPos);
255 }
256
257 $packetHandled = true;
258
259 $useItemTransaction = $packet->getItemInteractionData();
260 if($useItemTransaction !== null){
261 if(count($useItemTransaction->getTransactionData()->getActions()) > 100){
262 throw new PacketHandlingException("Too many actions in item use transaction");
263 }
264
265 $this->inventoryManager->setCurrentItemStackRequestId($useItemTransaction->getRequestId());
266 $this->inventoryManager->addRawPredictedSlotChanges($useItemTransaction->getTransactionData()->getActions());
267 if(!$this->handleUseItemTransaction($useItemTransaction->getTransactionData())){
268 $packetHandled = false;
269 $this->session->getLogger()->debug("Unhandled transaction in PlayerAuthInputPacket (type " . $useItemTransaction->getTransactionData()->getActionType() . ")");
270 }else{
271 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
272 }
273 $this->inventoryManager->setCurrentItemStackRequestId(null);
274 }
275
276 $itemStackRequest = $packet->getItemStackRequest();
277 $itemStackResponseBuilder = $itemStackRequest !== null ? $this->handleSingleItemStackRequest($itemStackRequest) : null;
278
279 //itemstack request or transaction may set predictions for the outcome of these actions, so these need to be
280 //processed last
281 $blockActions = $packet->getBlockActions();
282 if($blockActions !== null){
283 if(count($blockActions) > 100){
284 throw new PacketHandlingException("Too many block actions in PlayerAuthInputPacket");
285 }
286 foreach(Utils::promoteKeys($blockActions) as $k => $blockAction){
287 $actionHandled = false;
288 if($blockAction instanceof PlayerBlockActionStopBreak){
289 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(), new BlockPosition(0, 0, 0), Facing::DOWN);
290 }elseif($blockAction instanceof PlayerBlockActionWithBlockInfo){
291 $actionHandled = $this->handlePlayerActionFromData($blockAction->getActionType(), $blockAction->getBlockPosition(), $blockAction->getFace());
292 }
293
294 if(!$actionHandled){
295 $packetHandled = false;
296 $this->session->getLogger()->debug("Unhandled player block action at offset $k in PlayerAuthInputPacket");
297 }
298 }
299 }
300
301 if($itemStackRequest !== null){
302 $itemStackResponse = $itemStackResponseBuilder?->build() ?? new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $itemStackRequest->getRequestId());
303 $this->session->sendDataPacket(ItemStackResponsePacket::create([$itemStackResponse]));
304 }
305
306 return $packetHandled;
307 }
308
309 public function handleActorEvent(ActorEventPacket $packet) : bool{
310 return true; //not used
311 }
312
313 public function handleInventoryTransaction(InventoryTransactionPacket $packet) : bool{
314 $result = true;
315
316 if(count($packet->trData->getActions()) > 50){
317 throw new PacketHandlingException("Too many actions in inventory transaction");
318 }
319 if(count($packet->requestChangedSlots) > 10){
320 throw new PacketHandlingException("Too many slot sync requests in inventory transaction");
321 }
322
323 $this->inventoryManager->setCurrentItemStackRequestId($packet->requestId);
324 $this->inventoryManager->addRawPredictedSlotChanges($packet->trData->getActions());
325
326 if($packet->trData instanceof NormalTransactionData){
327 $result = $this->handleNormalTransaction($packet->trData, $packet->requestId);
328 }elseif($packet->trData instanceof MismatchTransactionData){
329 $this->session->getLogger()->debug("Mismatch transaction received");
330 $this->inventoryManager->requestSyncAll();
331 $result = true;
332 }elseif($packet->trData instanceof UseItemTransactionData){
333 $result = $this->handleUseItemTransaction($packet->trData);
334 }elseif($packet->trData instanceof UseItemOnEntityTransactionData){
335 $result = $this->handleUseItemOnEntityTransaction($packet->trData);
336 }elseif($packet->trData instanceof ReleaseItemTransactionData){
337 $result = $this->handleReleaseItemTransaction($packet->trData);
338 }
339
340 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
341
342 //requestChangedSlots asks the server to always send out the contents of the specified slots, even if they
343 //haven't changed. Handling these is necessary to ensure the client inventory stays in sync if the server
344 //rejects the transaction. The most common example of this is equipping armor by right-click, which doesn't send
345 //a legacy prediction action for the destination armor slot.
346 foreach($packet->requestChangedSlots as $containerInfo){
347 foreach($containerInfo->getChangedSlotIndexes() as $netSlot){
348 [$windowId, $slot] = ItemStackContainerIdTranslator::translate($containerInfo->getContainerId(), $this->inventoryManager->getCurrentWindowId(), $netSlot);
349 $inventoryAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slot);
350 if($inventoryAndSlot !== null){ //trigger the normal slot sync logic
351 $this->inventoryManager->onSlotChange($inventoryAndSlot[0], $inventoryAndSlot[1]);
352 }
353 }
354 }
355
356 $this->inventoryManager->setCurrentItemStackRequestId(null);
357 return $result;
358 }
359
360 private function executeInventoryTransaction(InventoryTransaction $transaction, int $requestId) : bool{
361 $this->player->setUsingItem(false);
362
363 $this->inventoryManager->setCurrentItemStackRequestId($requestId);
364 $this->inventoryManager->addTransactionPredictedSlotChanges($transaction);
365 try{
366 $transaction->execute();
368 $this->inventoryManager->requestSyncAll();
369 $logger = $this->session->getLogger();
370 $logger->debug("Invalid inventory transaction $requestId: " . $e->getMessage());
371
372 return false;
374 $this->session->getLogger()->debug("Inventory transaction $requestId cancelled by a plugin");
375
376 return false;
377 }finally{
378 $this->inventoryManager->syncMismatchedPredictedSlotChanges();
379 $this->inventoryManager->setCurrentItemStackRequestId(null);
380 }
381
382 return true;
383 }
384
385 private function handleNormalTransaction(NormalTransactionData $data, int $itemStackRequestId) : bool{
386 //When the ItemStackRequest system is used, this transaction type is used for dropping items by pressing Q.
387 //I don't know why they don't just use ItemStackRequest for that too, which already supports dropping items by
388 //clicking them outside an open inventory menu, but for now it is what it is.
389 //Fortunately, this means we can be much stricter about the validation criteria.
390
391 $actionCount = count($data->getActions());
392 if($actionCount > 2){
393 if($actionCount > 5){
394 throw new PacketHandlingException("Too many actions ($actionCount) in normal inventory transaction");
395 }
396
397 //Due to a bug in the game, this transaction type is still sent when a player edits a book. We don't need
398 //these transactions for editing books, since we have BookEditPacket, so we can just ignore them.
399 $this->session->getLogger()->debug("Ignoring normal inventory transaction with $actionCount actions (drop-item should have exactly 2 actions)");
400 return false;
401 }
402
403 $sourceSlot = null;
404 $clientItemStack = null;
405 $droppedCount = null;
406
407 foreach($data->getActions() as $networkInventoryAction){
408 if($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_WORLD && $networkInventoryAction->inventorySlot === NetworkInventoryAction::ACTION_MAGIC_SLOT_DROP_ITEM){
409 $droppedCount = $networkInventoryAction->newItem->getItemStack()->getCount();
410 if($droppedCount <= 0){
411 throw new PacketHandlingException("Expected positive count for dropped item");
412 }
413 }elseif($networkInventoryAction->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && $networkInventoryAction->windowId === ContainerIds::INVENTORY){
414 //mobile players can drop an item from a non-selected hotbar slot
415 $sourceSlot = $networkInventoryAction->inventorySlot;
416 $clientItemStack = $networkInventoryAction->oldItem->getItemStack();
417 }else{
418 $this->session->getLogger()->debug("Unexpected inventory action type $networkInventoryAction->sourceType in drop item transaction");
419 return false;
420 }
421 }
422 if($sourceSlot === null || $clientItemStack === null || $droppedCount === null){
423 $this->session->getLogger()->debug("Missing information in drop item transaction, need source slot, client item stack and dropped count");
424 return false;
425 }
426
427 $inventory = $this->player->getInventory();
428
429 if(!$inventory->slotExists($sourceSlot)){
430 return false; //TODO: size desync??
431 }
432
433 $sourceSlotItem = $inventory->getItem($sourceSlot);
434 if($sourceSlotItem->getCount() < $droppedCount){
435 return false;
436 }
437 $serverItemStack = $this->session->getTypeConverter()->coreItemStackToNet($sourceSlotItem);
438 //Sadly we don't have itemstack IDs here, so we have to compare the basic item properties to ensure that we're
439 //dropping the item the client expects (inventory might be out of sync with the client).
440 if(
441 $serverItemStack->getId() !== $clientItemStack->getId() ||
442 $serverItemStack->getMeta() !== $clientItemStack->getMeta() ||
443 $serverItemStack->getCount() !== $clientItemStack->getCount() ||
444 $serverItemStack->getBlockRuntimeId() !== $clientItemStack->getBlockRuntimeId()
445 //Raw extraData may not match because of TAG_Compound key ordering differences, and decoding it to compare
446 //is costly. Assume that we're in sync if id+meta+count+runtimeId match.
447 //NB: Make sure $clientItemStack isn't used to create the dropped item, as that would allow the client
448 //to change the item NBT since we're not validating it.
449 ){
450 return false;
451 }
452
453 //this modifies $sourceSlotItem
454 $droppedItem = $sourceSlotItem->pop($droppedCount);
455
456 $builder = new TransactionBuilder();
457 $builder->getInventory($inventory)->setItem($sourceSlot, $sourceSlotItem);
458 $builder->addAction(new DropItemAction($droppedItem));
459
460 $transaction = new InventoryTransaction($this->player, $builder->generateActions());
461 return $this->executeInventoryTransaction($transaction, $itemStackRequestId);
462 }
463
464 private function handleUseItemTransaction(UseItemTransactionData $data) : bool{
465 $this->player->selectHotbarSlot($data->getHotbarSlot());
466
467 switch($data->getActionType()){
468 case UseItemTransactionData::ACTION_CLICK_BLOCK:
469 //TODO: start hack for client spam bug
470 $clickPos = $data->getClickPosition();
471 $spamBug = ($this->lastRightClickData !== null &&
472 microtime(true) - $this->lastRightClickTime < 0.1 && //100ms
473 $this->lastRightClickData->getPlayerPosition()->distanceSquared($data->getPlayerPosition()) < 0.00001 &&
474 $this->lastRightClickData->getBlockPosition()->equals($data->getBlockPosition()) &&
475 $this->lastRightClickData->getClickPosition()->distanceSquared($clickPos) < 0.00001 //signature spam bug has 0 distance, but allow some error
476 );
477 //get rid of continued spam if the player clicks and holds right-click
478 $this->lastRightClickData = $data;
479 $this->lastRightClickTime = microtime(true);
480 if($spamBug){
481 throw new FilterNoisyPacketException();
482 }
483 //TODO: end hack for client spam bug
484
485 self::validateFacing($data->getFace());
486
487 $blockPos = $data->getBlockPosition();
488 $vBlockPos = new Vector3($blockPos->getX(), $blockPos->getY(), $blockPos->getZ());
489 $this->player->interactBlock($vBlockPos, $data->getFace(), $clickPos);
490 if($data->getClientInteractPrediction() === PredictedResult::SUCCESS){
491 //If the item has an associated blockstate ID, this means it will only place one block.
492 //We can avoid syncing the adjacent blocks of the place position in this case, since that's only
493 //necessary if there might be multiple blocks around the placement location affected.
494 //Adjacents of the clicked block are still always synced, since it's too complicated to figure out
495 //if the client might've predicted something in this case. However, since the clicked block is always
496 //"behind" the placed block, this shouldn't affect bridging or fast placement.
497 //This would be much easier if the client would just tell us which blocks it thinks changed...
498 $syncAdjacentFace = null;
499 if($data->getItemInHand()->getItemStack()->getBlockRuntimeId() === ItemTranslator::NO_BLOCK_RUNTIME_ID){
500 $this->session->getLogger()->debug("Placing held item might place multiple blocks client-side; doing full adjacent sync");
501 $syncAdjacentFace = $data->getFace();
502 }
503 $this->syncBlocksNearby($vBlockPos, $syncAdjacentFace);
504 }
505 return true;
506 case UseItemTransactionData::ACTION_CLICK_AIR:
507 if($this->player->isUsingItem()){
508 if(!$this->player->consumeHeldItem()){
509 $hungerAttr = $this->player->getAttributeMap()->get(Attribute::HUNGER) ?? throw new AssumptionFailedError();
510 $hungerAttr->markSynchronized(false);
511 }
512 //TODO: workaround goat horns getting stuck in the "using item" state
513 //this timed-trigger behaviour is also used for other items apart from food
514 //in the future we'll generalise this logic and add proper hooks for it
515 $this->player->setUsingItem(false);
516 return true;
517 }
518 $this->player->useHeldItem();
519 return true;
520 }
521
522 return false;
523 }
524
528 private static function validateFacing(int $facing) : void{
529 if(!in_array($facing, Facing::ALL, true)){
530 throw new PacketHandlingException("Invalid facing value $facing");
531 }
532 }
533
537 private function syncBlocksNearby(Vector3 $blockPos, ?int $face) : void{
538 if($blockPos->distanceSquared($this->player->getLocation()) < 10000){
539 $blocks = $blockPos->sidesArray();
540 if($face !== null){
541 $sidePos = $blockPos->getSide($face);
542
544 array_push($blocks, ...$sidePos->sidesArray()); //getAllSides() on each of these will include $blockPos and $sidePos because they are next to each other
545 }else{
546 $blocks[] = $blockPos;
547 }
548 foreach($this->player->getWorld()->createBlockUpdatePackets($blocks) as $packet){
549 $this->session->sendDataPacket($packet);
550 }
551 }
552 }
553
554 private function handleUseItemOnEntityTransaction(UseItemOnEntityTransactionData $data) : bool{
555 $target = $this->player->getWorld()->getEntity($data->getActorRuntimeId());
556 //TODO: HACK! We really shouldn't be keeping disconnected players (and generally flagged-for-despawn entities)
557 //in the world's entity table, but changing that is too risky for a hotfix. This workaround will do for now.
558 if($target === null || $target->isFlaggedForDespawn()){
559 return false;
560 }
561
562 $this->player->selectHotbarSlot($data->getHotbarSlot());
563
564 switch($data->getActionType()){
565 case UseItemOnEntityTransactionData::ACTION_INTERACT:
566 $this->player->interactEntity($target, $data->getClickPosition());
567 return true;
568 case UseItemOnEntityTransactionData::ACTION_ATTACK:
569 $this->player->attackEntity($target);
570 return true;
571 }
572
573 return false;
574 }
575
576 private function handleReleaseItemTransaction(ReleaseItemTransactionData $data) : bool{
577 $this->player->selectHotbarSlot($data->getHotbarSlot());
578
579 if($data->getActionType() === ReleaseItemTransactionData::ACTION_RELEASE){
580 $this->player->releaseHeldItem();
581 return true;
582 }
583
584 return false;
585 }
586
587 private function handleSingleItemStackRequest(ItemStackRequest $request) : ?ItemStackResponseBuilder{
588 if(count($request->getActions()) > 60){
589 //recipe book auto crafting can affect all slots of the inventory when consuming inputs or producing outputs
590 //this means there could be as many as 50 CraftingConsumeInput actions or Place (taking the result) actions
591 //in a single request (there are certain ways items can be arranged which will result in the same stack
592 //being taken from multiple times, but this is behaviour with a calculable limit)
593 //this means there SHOULD be AT MOST 53 actions in a single request, but 60 is a nice round number.
594 //n64Stacks = ?
595 //n1Stacks = 45 - n64Stacks
596 //nItemsRequiredFor1Craft = 9
597 //nResults = floor((n1Stacks + (n64Stacks * 64)) / nItemsRequiredFor1Craft)
598 //nTakeActionsTotal = floor(64 / nResults) + max(1, 64 % nResults) + ((nResults * nItemsRequiredFor1Craft) - (n64Stacks * 64))
599 throw new PacketHandlingException("Too many actions in ItemStackRequest");
600 }
601 $executor = new ItemStackRequestExecutor($this->player, $this->inventoryManager, $request);
602 try{
603 $transaction = $executor->generateInventoryTransaction();
604 if($transaction !== null){
605 $result = $this->executeInventoryTransaction($transaction, $request->getRequestId());
606 }else{
607 $result = true; //predictions only, just send responses
608 }
610 $result = false;
611 $this->session->getLogger()->debug("ItemStackRequest #" . $request->getRequestId() . " failed: " . $e->getMessage());
612 $this->session->getLogger()->debug(implode("\n", Utils::printableExceptionInfo($e)));
613 $this->inventoryManager->requestSyncAll();
614 }
615
616 return $result ? $executor->getItemStackResponseBuilder() : null;
617 }
618
619 public function handleItemStackRequest(ItemStackRequestPacket $packet) : bool{
620 $responses = [];
621 if(count($packet->getRequests()) > 80){
622 //TODO: we can probably lower this limit, but this will do for now
623 throw new PacketHandlingException("Too many requests in ItemStackRequestPacket");
624 }
625 foreach($packet->getRequests() as $request){
626 $responses[] = $this->handleSingleItemStackRequest($request)?->build() ?? new ItemStackResponse(ItemStackResponse::RESULT_ERROR, $request->getRequestId());
627 }
628
629 $this->session->sendDataPacket(ItemStackResponsePacket::create($responses));
630
631 return true;
632 }
633
634 public function handleMobEquipment(MobEquipmentPacket $packet) : bool{
635 if($packet->windowId === ContainerIds::OFFHAND){
636 return true; //this happens when we put an item into the offhand
637 }
638 if($packet->windowId === ContainerIds::INVENTORY){
639 $this->inventoryManager->onClientSelectHotbarSlot($packet->hotbarSlot);
640 if(!$this->player->selectHotbarSlot($packet->hotbarSlot)){
641 $this->inventoryManager->syncSelectedHotbarSlot();
642 }
643 return true;
644 }
645 return false;
646 }
647
648 public function handleMobArmorEquipment(MobArmorEquipmentPacket $packet) : bool{
649 return true; //Not used
650 }
651
652 public function handleInteract(InteractPacket $packet) : bool{
653 if($packet->action === InteractPacket::ACTION_MOUSEOVER){
654 //TODO HACK: silence useless spam (MCPE 1.8)
655 //due to some messy Mojang hacks, it sends this when changing the held item now, which causes us to think
656 //the inventory was closed when it wasn't.
657 //this is also sent whenever entity metadata updates, which can get really spammy.
658 //TODO: implement handling for this where it matters
659 return true;
660 }
661 $target = $this->player->getWorld()->getEntity($packet->targetActorRuntimeId);
662 if($target === null){
663 return false;
664 }
665 if($packet->action === InteractPacket::ACTION_OPEN_INVENTORY && $target === $this->player){
666 $this->inventoryManager->onClientOpenMainInventory();
667 return true;
668 }
669 return false; //TODO
670 }
671
672 public function handleBlockPickRequest(BlockPickRequestPacket $packet) : bool{
673 return $this->player->pickBlock(new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ()), $packet->addUserData);
674 }
675
676 public function handleActorPickRequest(ActorPickRequestPacket $packet) : bool{
677 return $this->player->pickEntity($packet->actorUniqueId);
678 }
679
680 public function handlePlayerAction(PlayerActionPacket $packet) : bool{
681 return $this->handlePlayerActionFromData($packet->action, $packet->blockPosition, $packet->face);
682 }
683
684 private function handlePlayerActionFromData(int $action, BlockPosition $blockPosition, int $face) : bool{
685 $pos = new Vector3($blockPosition->getX(), $blockPosition->getY(), $blockPosition->getZ());
686
687 switch($action){
688 case PlayerAction::START_BREAK:
689 case PlayerAction::CONTINUE_DESTROY_BLOCK: //destroy the next block while holding down left click
690 self::validateFacing($face);
691 if($this->lastBlockAttacked !== null && $blockPosition->equals($this->lastBlockAttacked)){
692 //the client will send CONTINUE_DESTROY_BLOCK for the currently targeted block directly before it
693 //sends PREDICT_DESTROY_BLOCK, but also when it starts to break the block
694 //this seems like a bug in the client and would cause spurious left-click events if we allowed it to
695 //be delivered to the player
696 $this->session->getLogger()->debug("Ignoring PlayerAction $action on $pos because we were already destroying this block");
697 break;
698 }
699 if(!$this->player->attackBlock($pos, $face)){
700 $this->syncBlocksNearby($pos, $face);
701 }
702 $this->lastBlockAttacked = $blockPosition;
703
704 break;
705
706 case PlayerAction::ABORT_BREAK:
707 case PlayerAction::STOP_BREAK:
708 $this->player->stopBreakBlock($pos);
709 $this->lastBlockAttacked = null;
710 break;
711 case PlayerAction::START_SLEEPING:
712 //unused
713 break;
714 case PlayerAction::STOP_SLEEPING:
715 $this->player->stopSleep();
716 break;
717 case PlayerAction::CRACK_BREAK:
718 self::validateFacing($face);
719 $this->player->continueBreakBlock($pos, $face);
720 $this->lastBlockAttacked = $blockPosition;
721 break;
722 case PlayerAction::INTERACT_BLOCK: //TODO: ignored (for now)
723 break;
724 case PlayerAction::CREATIVE_PLAYER_DESTROY_BLOCK:
725 //in server auth block breaking, we get PREDICT_DESTROY_BLOCK anyway, so this action is redundant
726 break;
727 case PlayerAction::PREDICT_DESTROY_BLOCK:
728 self::validateFacing($face);
729 if(!$this->player->breakBlock($pos)){
730 $this->syncBlocksNearby($pos, $face);
731 }
732 $this->lastBlockAttacked = null;
733 break;
734 case PlayerAction::START_ITEM_USE_ON:
735 case PlayerAction::STOP_ITEM_USE_ON:
736 //TODO: this has no obvious use and seems only used for analytics in vanilla - ignore it
737 break;
738 default:
739 $this->session->getLogger()->debug("Unhandled/unknown player action type " . $action);
740 return false;
741 }
742
743 $this->player->setUsingItem(false);
744
745 return true;
746 }
747
748 public function handleSetActorMotion(SetActorMotionPacket $packet) : bool{
749 return true; //Not used: This packet is (erroneously) sent to the server when the client is riding a vehicle.
750 }
751
752 public function handleAnimate(AnimatePacket $packet) : bool{
753 //this spams harder than a firehose on left click if "Improved Input Response" is enabled, and we don't even
754 //use it anyway :<
755 throw new FilterNoisyPacketException();
756 }
757
758 public function handleContainerClose(ContainerClosePacket $packet) : bool{
759 $this->inventoryManager->onClientRemoveWindow($packet->windowId);
760 return true;
761 }
762
763 public function handlePlayerHotbar(PlayerHotbarPacket $packet) : bool{
764 return true; //this packet is useless
765 }
766
770 private function updateSignText(CompoundTag $nbt, string $tagName, bool $frontFace, BaseSign $block, Vector3 $pos) : bool{
771 $textTag = $nbt->getTag($tagName);
772 if(!$textTag instanceof CompoundTag){
773 throw new PacketHandlingException("Invalid tag type " . get_debug_type($textTag) . " for tag \"$tagName\" in sign update data");
774 }
775 $textBlobTag = $textTag->getTag(Sign::TAG_TEXT_BLOB);
776 if(!$textBlobTag instanceof StringTag){
777 throw new PacketHandlingException("Invalid tag type " . get_debug_type($textBlobTag) . " for tag \"" . Sign::TAG_TEXT_BLOB . "\" in sign update data");
778 }
779
780 try{
781 $text = SignText::fromBlob($textBlobTag->getValue());
782 }catch(\InvalidArgumentException $e){
783 throw PacketHandlingException::wrap($e, "Invalid sign text update");
784 }
785
786 $oldText = $block->getFaceText($frontFace);
787 if($text->getLines() === $oldText->getLines()){
788 return false;
789 }
790
791 try{
792 if(!$block->updateFaceText($this->player, $frontFace, $text)){
793 foreach($this->player->getWorld()->createBlockUpdatePackets([$pos]) as $updatePacket){
794 $this->session->sendDataPacket($updatePacket);
795 }
796 return false;
797 }
798 return true;
799 }catch(\UnexpectedValueException $e){
800 throw PacketHandlingException::wrap($e);
801 }
802 }
803
804 public function handleBlockActorData(BlockActorDataPacket $packet) : bool{
805 $pos = new Vector3($packet->blockPosition->getX(), $packet->blockPosition->getY(), $packet->blockPosition->getZ());
806 if($pos->distanceSquared($this->player->getLocation()) > 10000){
807 return false;
808 }
809
810 $block = $this->player->getLocation()->getWorld()->getBlock($pos);
811 $nbt = $packet->nbt->getRoot();
812 if(!($nbt instanceof CompoundTag)) throw new AssumptionFailedError("PHPStan should ensure this is a CompoundTag"); //for phpstorm's benefit
813
814 if($block instanceof BaseSign){
815 if(!$this->updateSignText($nbt, Sign::TAG_FRONT_TEXT, true, $block, $pos)){
816 //only one side can be updated at a time
817 $this->updateSignText($nbt, Sign::TAG_BACK_TEXT, false, $block, $pos);
818 }
819
820 return true;
821 }
822
823 return false;
824 }
825
826 public function handleSetPlayerGameType(SetPlayerGameTypePacket $packet) : bool{
827 $gameMode = $this->session->getTypeConverter()->protocolGameModeToCore($packet->gamemode);
828 if($gameMode !== $this->player->getGamemode()){
829 //Set this back to default. TODO: handle this properly
830 $this->session->syncGameMode($this->player->getGamemode(), true);
831 }
832 return true;
833 }
834
835 public function handleSpawnExperienceOrb(SpawnExperienceOrbPacket $packet) : bool{
836 return false; //TODO
837 }
838
839 public function handleMapInfoRequest(MapInfoRequestPacket $packet) : bool{
840 return false; //TODO
841 }
842
843 public function handleRequestChunkRadius(RequestChunkRadiusPacket $packet) : bool{
844 $this->player->setViewDistance($packet->radius);
845
846 return true;
847 }
848
849 public function handleBossEvent(BossEventPacket $packet) : bool{
850 return false; //TODO
851 }
852
853 public function handleShowCredits(ShowCreditsPacket $packet) : bool{
854 return false; //TODO: handle resume
855 }
856
857 public function handleCommandRequest(CommandRequestPacket $packet) : bool{
858 if(str_starts_with($packet->command, '/')){
859 $this->player->chat($packet->command);
860 return true;
861 }
862 return false;
863 }
864
865 public function handleCommandBlockUpdate(CommandBlockUpdatePacket $packet) : bool{
866 return false; //TODO
867 }
868
869 public function handlePlayerSkin(PlayerSkinPacket $packet) : bool{
870 if($packet->skin->getFullSkinId() === $this->lastRequestedFullSkinId){
871 //TODO: HACK! In 1.19.60, the client sends its skin back to us if we sent it a skin different from the one
872 //it's using. We need to prevent this from causing a feedback loop.
873 $this->session->getLogger()->debug("Refused duplicate skin change request");
874 return true;
875 }
876 $this->lastRequestedFullSkinId = $packet->skin->getFullSkinId();
877
878 $this->session->getLogger()->debug("Processing skin change request");
879 try{
880 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData($packet->skin);
881 }catch(InvalidSkinException $e){
882 throw PacketHandlingException::wrap($e, "Invalid skin in PlayerSkinPacket");
883 }
884 return $this->player->changeSkin($skin, $packet->newSkinName, $packet->oldSkinName);
885 }
886
887 public function handleSubClientLogin(SubClientLoginPacket $packet) : bool{
888 return false; //TODO
889 }
890
894 private function checkBookText(string $string, string $fieldName, int $softLimit, int $hardLimit, bool &$cancel) : string{
895 if(strlen($string) > $hardLimit){
896 throw new PacketHandlingException(sprintf("Book %s must be at most %d bytes, but have %d bytes", $fieldName, $hardLimit, strlen($string)));
897 }
898
899 $result = TextFormat::clean($string, false);
900 //strlen() is O(1), mb_strlen() is O(n)
901 if(strlen($result) > $softLimit * 4 || mb_strlen($result, 'UTF-8') > $softLimit){
902 $cancel = true;
903 $this->session->getLogger()->debug("Cancelled book edit due to $fieldName exceeded soft limit of $softLimit chars");
904 }
905
906 return $result;
907 }
908
909 public function handleBookEdit(BookEditPacket $packet) : bool{
910 $inventory = $this->player->getInventory();
911 if(!$inventory->slotExists($packet->inventorySlot)){
912 return false;
913 }
914 //TODO: break this up into book API things
915 $oldBook = $inventory->getItem($packet->inventorySlot);
916 if(!($oldBook instanceof WritableBook)){
917 return false;
918 }
919
920 $newBook = clone $oldBook;
921 $modifiedPages = [];
922 $cancel = false;
923 switch($packet->type){
924 case BookEditPacket::TYPE_REPLACE_PAGE:
925 $text = self::checkBookText($packet->text, "page text", self::PAGE_LENGTH_SOFT_LIMIT_CHARS, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
926 $newBook->setPageText($packet->pageNumber, $text);
927 $modifiedPages[] = $packet->pageNumber;
928 break;
929 case BookEditPacket::TYPE_ADD_PAGE:
930 if(!$newBook->pageExists($packet->pageNumber)){
931 //this may only come before a page which already exists
932 //TODO: the client can send insert-before actions on trailing client-side pages which cause odd behaviour on the server
933 return false;
934 }
935 $text = self::checkBookText($packet->text, "page text", self::PAGE_LENGTH_SOFT_LIMIT_CHARS, WritableBookPage::PAGE_LENGTH_HARD_LIMIT_BYTES, $cancel);
936 $newBook->insertPage($packet->pageNumber, $text);
937 $modifiedPages[] = $packet->pageNumber;
938 break;
939 case BookEditPacket::TYPE_DELETE_PAGE:
940 if(!$newBook->pageExists($packet->pageNumber)){
941 return false;
942 }
943 $newBook->deletePage($packet->pageNumber);
944 $modifiedPages[] = $packet->pageNumber;
945 break;
946 case BookEditPacket::TYPE_SWAP_PAGES:
947 if(!$newBook->pageExists($packet->pageNumber) || !$newBook->pageExists($packet->secondaryPageNumber)){
948 //the client will create pages on its own without telling us until it tries to switch them
949 $newBook->addPage(max($packet->pageNumber, $packet->secondaryPageNumber));
950 }
951 $newBook->swapPages($packet->pageNumber, $packet->secondaryPageNumber);
952 $modifiedPages = [$packet->pageNumber, $packet->secondaryPageNumber];
953 break;
954 case BookEditPacket::TYPE_SIGN_BOOK:
955 $title = self::checkBookText($packet->title, "title", 16, Limits::INT16_MAX, $cancel);
956 //this one doesn't have a limit in vanilla, so we have to improvise
957 $author = self::checkBookText($packet->author, "author", 256, Limits::INT16_MAX, $cancel);
958
959 $newBook = VanillaItems::WRITTEN_BOOK()
960 ->setPages($oldBook->getPages())
961 ->setAuthor($author)
962 ->setTitle($title)
963 ->setGeneration(WrittenBook::GENERATION_ORIGINAL);
964 break;
965 default:
966 return false;
967 }
968
969 //for redundancy, in case of protocol changes, we don't want to pass these directly
970 $action = match($packet->type){
971 BookEditPacket::TYPE_REPLACE_PAGE => PlayerEditBookEvent::ACTION_REPLACE_PAGE,
972 BookEditPacket::TYPE_ADD_PAGE => PlayerEditBookEvent::ACTION_ADD_PAGE,
973 BookEditPacket::TYPE_DELETE_PAGE => PlayerEditBookEvent::ACTION_DELETE_PAGE,
974 BookEditPacket::TYPE_SWAP_PAGES => PlayerEditBookEvent::ACTION_SWAP_PAGES,
975 BookEditPacket::TYPE_SIGN_BOOK => PlayerEditBookEvent::ACTION_SIGN_BOOK,
976 default => throw new AssumptionFailedError("We already filtered unknown types in the switch above")
977 };
978
979 /*
980 * Plugins may have created books with more than 50 pages; we allow plugins to do this, but not players.
981 * Don't allow the page count to grow past 50, but allow deleting, swapping or altering text of existing pages.
982 */
983 $oldPageCount = count($oldBook->getPages());
984 $newPageCount = count($newBook->getPages());
985 if(($newPageCount > $oldPageCount && $newPageCount > 50)){
986 $this->session->getLogger()->debug("Cancelled book edit due to adding too many pages (new page count would be $newPageCount)");
987 $cancel = true;
988 }
989
990 $event = new PlayerEditBookEvent($this->player, $oldBook, $newBook, $action, $modifiedPages);
991 if($cancel){
992 $event->cancel();
993 }
994
995 $event->call();
996 if($event->isCancelled()){
997 return true;
998 }
999
1000 $this->player->getInventory()->setItem($packet->inventorySlot, $event->getNewBook());
1001
1002 return true;
1003 }
1004
1005 public function handleModalFormResponse(ModalFormResponsePacket $packet) : bool{
1006 if($packet->cancelReason !== null){
1007 //TODO: make APIs for this to allow plugins to use this information
1008 return $this->player->onFormSubmit($packet->formId, null);
1009 }elseif($packet->formData !== null){
1010 if(strlen($packet->formData) > self::MAX_FORM_RESPONSE_SIZE){
1011 throw new PacketHandlingException("Form response data too large, refusing to decode (received" . strlen($packet->formData) . " bytes, max " . self::MAX_FORM_RESPONSE_SIZE . " bytes)");
1012 }
1013 if(!$this->player->hasPendingForm($packet->formId)){
1014 $this->session->getLogger()->debug("Got unexpected response for form $packet->formId");
1015 return false;
1016 }
1017 try{
1018 $responseData = json_decode($packet->formData, true, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR);
1019 }catch(\JsonException $e){
1020 throw PacketHandlingException::wrap($e, "Failed to decode form response data");
1021 }
1022 return $this->player->onFormSubmit($packet->formId, $responseData);
1023 }else{
1024 throw new PacketHandlingException("Expected either formData or cancelReason to be set in ModalFormResponsePacket");
1025 }
1026 }
1027
1028 public function handleServerSettingsRequest(ServerSettingsRequestPacket $packet) : bool{
1029 return false; //TODO: GUI stuff
1030 }
1031
1032 public function handleLabTable(LabTablePacket $packet) : bool{
1033 return false; //TODO
1034 }
1035
1036 public function handleLecternUpdate(LecternUpdatePacket $packet) : bool{
1037 $pos = $packet->blockPosition;
1038 $chunkX = $pos->getX() >> Chunk::COORD_BIT_SIZE;
1039 $chunkZ = $pos->getZ() >> Chunk::COORD_BIT_SIZE;
1040 $world = $this->player->getWorld();
1041 if(!$world->isChunkLoaded($chunkX, $chunkZ) || $world->isChunkLocked($chunkX, $chunkZ)){
1042 return false;
1043 }
1044
1045 $lectern = $world->getBlockAt($pos->getX(), $pos->getY(), $pos->getZ());
1046 if($lectern instanceof Lectern && $this->player->canInteract($lectern->getPosition(), 15)){
1047 if(!$lectern->onPageTurn($packet->page)){
1048 $this->syncBlocksNearby($lectern->getPosition(), null);
1049 }
1050 return true;
1051 }
1052
1053 return false;
1054 }
1055
1056 public function handleNetworkStackLatency(NetworkStackLatencyPacket $packet) : bool{
1057 return true; //TODO: implement this properly - this is here to silence debug spam from MCPE dev builds
1058 }
1059
1060 public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{
1061 /*
1062 * We don't handle this - all sounds are handled by the server now.
1063 * However, some plugins find this useful to detect events like left-click-air, which doesn't have any other
1064 * action bound to it.
1065 * In addition, we use this handler to silence debug noise, since this packet is frequently sent by the client.
1066 */
1067 return true;
1068 }
1069
1070 public function handleEmote(EmotePacket $packet) : bool{
1071 $this->player->emote($packet->getEmoteId());
1072 return true;
1073 }
1074}
updateFaceText(Player $author, bool $frontFace, SignText $text)
Definition BaseSign.php:306
sidesArray(bool $keys=false, int $step=1)
Definition Vector3.php:187
getSide(int $side, int $step=1)
Definition Vector3.php:120
static translate(int $containerInterfaceId, int $currentWindowId, int $slotId)