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