PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
SetSpawnPositionPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
23
25 public const NETWORK_ID = ProtocolInfo::SET_SPAWN_POSITION_PACKET;
26
27 public const TYPE_PLAYER_SPAWN = 0;
28 public const TYPE_WORLD_SPAWN = 1;
29
30 public int $spawnType;
31 public BlockPosition $spawnPosition;
32 public int $dimension;
39
43 private static function create(int $spawnType, BlockPosition $spawnPosition, int $dimension, BlockPosition $causingBlockPosition) : self{
44 $result = new self;
45 $result->spawnType = $spawnType;
46 $result->spawnPosition = $spawnPosition;
47 $result->dimension = $dimension;
48 $result->causingBlockPosition = $causingBlockPosition;
49 return $result;
50 }
51
52 public static function playerSpawn(BlockPosition $spawnPosition, int $dimension, BlockPosition $causingBlockPosition) : self{
53 return self::create(self::TYPE_PLAYER_SPAWN, $spawnPosition, $dimension, $causingBlockPosition);
54 }
55
56 public static function worldSpawn(BlockPosition $spawnPosition, int $dimension) : self{
57 return self::create(self::TYPE_WORLD_SPAWN, $spawnPosition, $dimension, new BlockPosition(Limits::INT32_MIN, Limits::INT32_MIN, Limits::INT32_MIN));
58 }
59
60 protected function decodePayload(ByteBufferReader $in) : void{
61 $this->spawnType = VarInt::readSignedInt($in);
62 $this->spawnPosition = CommonTypes::getBlockPosition($in);
63 $this->dimension = VarInt::readSignedInt($in);
64 $this->causingBlockPosition = CommonTypes::getBlockPosition($in);
65 }
66
67 protected function encodePayload(ByteBufferWriter $out) : void{
68 VarInt::writeSignedInt($out, $this->spawnType);
69 CommonTypes::putBlockPosition($out, $this->spawnPosition);
70 VarInt::writeSignedInt($out, $this->dimension);
71 CommonTypes::putBlockPosition($out, $this->causingBlockPosition);
72 }
73
74 public function handle(PacketHandlerInterface $handler) : bool{
75 return $handler->handleSetSpawnPosition($this);
76 }
77}