PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
EntityDataHelper.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\entity;
25
34use function count;
35use function is_infinite;
36use function is_nan;
37
38final class EntityDataHelper{
39
40 private function __construct(){
41 //NOOP
42 }
43
47 private static function validateFloat(string $tagName, string $component, float $value) : void{
48 if(is_infinite($value)){
49 throw new SavedDataLoadingException("$component component of '$tagName' contains invalid infinite value");
50 }
51 if(is_nan($value)){
52 throw new SavedDataLoadingException("$component component of '$tagName' contains invalid NaN value");
53 }
54 }
55
59 public static function parseLocation(CompoundTag $nbt, World $world) : Location{
60 $pos = self::parseVec3($nbt, Entity::TAG_POS, false);
61
62 $yawPitch = $nbt->getTag(Entity::TAG_ROTATION);
63 if(!($yawPitch instanceof ListTag) || $yawPitch->getTagType() !== NBT::TAG_Float){
64 throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List<Float>");
65 }
67 $values = $yawPitch->getValue();
68 if(count($values) !== 2){
69 throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'");
70 }
71 self::validateFloat(Entity::TAG_ROTATION, "yaw", $values[0]->getValue());
72 self::validateFloat(Entity::TAG_ROTATION, "pitch", $values[1]->getValue());
73
74 return Location::fromObject($pos, $world, $values[0]->getValue(), $values[1]->getValue());
75 }
76
80 public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{
81 $pos = $nbt->getTag($tagName);
82 if($pos === null && $optional){
83 return Vector3::zero();
84 }
85 if(!($pos instanceof ListTag) || ($pos->getTagType() !== NBT::TAG_Double && $pos->getTagType() !== NBT::TAG_Float)){
86 throw new SavedDataLoadingException("'$tagName' should be a List<Double> or List<Float>");
87 }
89 $values = $pos->getValue();
90 if(count($values) !== 3){
91 throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag");
92 }
93
94 $x = $values[0]->getValue();
95 $y = $values[1]->getValue();
96 $z = $values[2]->getValue();
97
98 self::validateFloat($tagName, "x", $x);
99 self::validateFloat($tagName, "y", $y);
100 self::validateFloat($tagName, "z", $z);
101
102 return new Vector3($x, $y, $z);
103 }
104}
static parseVec3(CompoundTag $nbt, string $tagName, bool $optional)
static parseLocation(CompoundTag $nbt, World $world)
static fromObject(Vector3 $pos, ?World $world, float $yaw=0.0, float $pitch=0.0)
Definition Location.php:44