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