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