PocketMine-MP 5.32.2 git-1ebd7d3960d713d56f77f610fe0c15cdee201069
Loading...
Searching...
No Matches
block/tile/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
25
27use pocketmine\block\utils\DyeColor;
33
38class Banner extends Spawnable{
39
40 public const TAG_BASE = "Base";
41 public const TAG_PATTERNS = "Patterns";
42 public const TAG_PATTERN_COLOR = "Color";
43 public const TAG_PATTERN_NAME = "Pattern";
44 public const TAG_TYPE = "Type";
45
46 public const TYPE_NORMAL = 0;
47 public const TYPE_OMINOUS = 1;
48
49 private DyeColor $baseColor = DyeColor::BLACK;
50
55 private array $patterns = [];
56
57 private int $type = self::TYPE_NORMAL;
58
59 public function readSaveData(CompoundTag $nbt) : void{
60 $colorIdMap = DyeColorIdMap::getInstance();
61 if(
62 ($baseColorTag = $nbt->getTag(self::TAG_BASE)) instanceof IntTag &&
63 ($baseColor = $colorIdMap->fromInvertedId($baseColorTag->getValue())) !== null
64 ){
65 $this->baseColor = $baseColor;
66 }else{
67 $this->baseColor = DyeColor::BLACK; //TODO: this should be an error
68 }
69
70 $patternTypeIdMap = BannerPatternTypeIdMap::getInstance();
71
72 $patterns = $nbt->getListTag(self::TAG_PATTERNS);
73 if($patterns !== null){
75 foreach($patterns as $pattern){
76 $patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error
77 $patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME));
78 if($patternType === null){
79 continue; //TODO: this should be an error, but right now we don't have the setup to deal with it
80 }
81 $this->patterns[] = new BannerPatternLayer($patternType, $patternColor);
82 }
83 }
84
85 $this->type = $nbt->getInt(self::TAG_TYPE);
86 }
87
88 protected function writeSaveData(CompoundTag $nbt) : void{
89 $colorIdMap = DyeColorIdMap::getInstance();
90 $patternIdMap = BannerPatternTypeIdMap::getInstance();
91 $nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
92 $patterns = new ListTag();
93 foreach($this->patterns as $pattern){
94 $patterns->push(CompoundTag::create()
95 ->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
96 ->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
97 );
98 }
99 $nbt->setTag(self::TAG_PATTERNS, $patterns);
100 $nbt->setInt(self::TAG_TYPE, $this->type);
101 }
102
103 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
104 $colorIdMap = DyeColorIdMap::getInstance();
105 $patternIdMap = BannerPatternTypeIdMap::getInstance();
106 $nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
107 $patterns = new ListTag();
108 foreach($this->patterns as $pattern){
109 $patterns->push(CompoundTag::create()
110 ->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
111 ->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
112 );
113 }
114 $nbt->setTag(self::TAG_PATTERNS, $patterns);
115 $nbt->setInt(self::TAG_TYPE, $this->type);
116 }
117
121 public function getBaseColor() : DyeColor{
122 return $this->baseColor;
123 }
124
128 public function setBaseColor(DyeColor $color) : void{
129 $this->baseColor = $color;
130 }
131
136 public function getPatterns() : array{
137 return $this->patterns;
138 }
139
145 public function setPatterns(array $patterns) : void{
146 $this->patterns = $patterns;
147 }
148
149 public function getType() : int{ return $this->type; }
150
151 public function setType(int $type) : void{ $this->type = $type; }
152
153 public function getDefaultName() : string{
154 return "Banner";
155 }
156}
addAdditionalSpawnData(CompoundTag $nbt)
setInt(string $name, int $value)
setTag(string $name, Tag $tag)