PocketMine-MP 5.41.1 git-fcc6fb5566a921cb669160c90f56fb68f5b29123
Loading...
Searching...
No Matches
PlayerBlockActionWithBlockInfo.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\types;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
21
26 public function __construct(
27 private int $actionType,
28 private BlockPosition $blockPosition,
29 private int $face
30 ){
31 if(!self::isValidActionType($actionType)){
32 throw new \InvalidArgumentException("Invalid action type for " . self::class);
33 }
34 }
35
36 public function getActionType() : int{ return $this->actionType; }
37
38 public function getBlockPosition() : BlockPosition{ return $this->blockPosition; }
39
40 public function getFace() : int{ return $this->face; }
41
42 public static function read(ByteBufferReader $in, int $actionType) : self{
43 $blockPosition = CommonTypes::getSignedBlockPosition($in);
44 $face = VarInt::readSignedInt($in);
45 return new self($actionType, $blockPosition, $face);
46 }
47
48 public function write(ByteBufferWriter $out) : void{
49 CommonTypes::putSignedBlockPosition($out, $this->blockPosition);
50 VarInt::writeSignedInt($out, $this->face);
51 }
52
53 public static function isValidActionType(int $actionType) : bool{
54 return match($actionType){
55 PlayerAction::ABORT_BREAK,
56 PlayerAction::START_BREAK,
57 PlayerAction::CRACK_BREAK,
58 PlayerAction::PREDICT_DESTROY_BLOCK,
59 PlayerAction::CONTINUE_DESTROY_BLOCK => true,
60 default => false
61 };
62 }
63}