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