PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
item/Banner.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\item;
25
26use pocketmine\block\tile\Banner as TileBanner;
28use pocketmine\block\utils\DyeColor;
35use function count;
36
38 public const TAG_PATTERNS = TileBanner::TAG_PATTERNS;
39 public const TAG_PATTERN_COLOR = TileBanner::TAG_PATTERN_COLOR;
40 public const TAG_PATTERN_NAME = TileBanner::TAG_PATTERN_NAME;
41
42 private DyeColor $color = DyeColor::BLACK;
43
48 private array $patterns = [];
49
50 public function getColor() : DyeColor{
51 return $this->color;
52 }
53
55 public function setColor(DyeColor $color) : self{
56 $this->color = $color;
57 return $this;
58 }
59
60 protected function describeState(RuntimeDataDescriber $w) : void{
61 $w->enum($this->color);
62 }
63
68 public function getPatterns() : array{
69 return $this->patterns;
70 }
71
79 public function setPatterns(array $patterns) : self{
80 $this->patterns = $patterns;
81 return $this;
82 }
83
84 public function getFuelTime() : int{
85 return 300;
86 }
87
88 protected function deserializeCompoundTag(CompoundTag $tag) : void{
89 parent::deserializeCompoundTag($tag);
90
91 $this->patterns = [];
92
93 $colorIdMap = DyeColorIdMap::getInstance();
94 $patternIdMap = BannerPatternTypeIdMap::getInstance();
95 $patterns = $tag->getListTag(self::TAG_PATTERNS);
96 if($patterns !== null && $patterns->getTagType() === NBT::TAG_Compound){
98 foreach($patterns as $t){
99 $patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error
100 $patternType = $patternIdMap->fromId($t->getString(self::TAG_PATTERN_NAME));
101 if($patternType === null){
102 continue; //TODO: this should be an error
103 }
104 $this->patterns[] = new BannerPatternLayer($patternType, $patternColor);
105 }
106 }
107 }
108
109 protected function serializeCompoundTag(CompoundTag $tag) : void{
110 parent::serializeCompoundTag($tag);
111
112 if(count($this->patterns) > 0){
113 $patterns = new ListTag();
114 $colorIdMap = DyeColorIdMap::getInstance();
115 $patternIdMap = BannerPatternTypeIdMap::getInstance();
116 foreach($this->patterns as $pattern){
117 $patterns->push(CompoundTag::create()
118 ->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
119 ->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
120 );
121 }
122
123 $tag->setTag(self::TAG_PATTERNS, $patterns);
124 }else{
125 $tag->removeTag(self::TAG_PATTERNS);
126 }
127 }
128}
describeState(RuntimeDataDescriber $w)
setPatterns(array $patterns)
setColor(DyeColor $color)
deserializeCompoundTag(CompoundTag $tag)