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