PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
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
45 private DyeColor $baseColor = DyeColor::BLACK;
46
51 private array $patterns = [];
52
53 public function readSaveData(CompoundTag $nbt) : void{
54 $colorIdMap = DyeColorIdMap::getInstance();
55 if(
56 ($baseColorTag = $nbt->getTag(self::TAG_BASE)) instanceof IntTag &&
57 ($baseColor = $colorIdMap->fromInvertedId($baseColorTag->getValue())) !== null
58 ){
59 $this->baseColor = $baseColor;
60 }else{
61 $this->baseColor = DyeColor::BLACK; //TODO: this should be an error
62 }
63
64 $patternTypeIdMap = BannerPatternTypeIdMap::getInstance();
65
66 $patterns = $nbt->getListTag(self::TAG_PATTERNS);
67 if($patterns !== null){
69 foreach($patterns as $pattern){
70 $patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error
71 $patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME));
72 if($patternType === null){
73 continue; //TODO: this should be an error, but right now we don't have the setup to deal with it
74 }
75 $this->patterns[] = new BannerPatternLayer($patternType, $patternColor);
76 }
77 }
78 }
79
80 protected function writeSaveData(CompoundTag $nbt) : void{
81 $colorIdMap = DyeColorIdMap::getInstance();
82 $patternIdMap = BannerPatternTypeIdMap::getInstance();
83 $nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
84 $patterns = new ListTag();
85 foreach($this->patterns as $pattern){
86 $patterns->push(CompoundTag::create()
87 ->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
88 ->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
89 );
90 }
91 $nbt->setTag(self::TAG_PATTERNS, $patterns);
92 }
93
94 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
95 $colorIdMap = DyeColorIdMap::getInstance();
96 $patternIdMap = BannerPatternTypeIdMap::getInstance();
97 $nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
98 $patterns = new ListTag();
99 foreach($this->patterns as $pattern){
100 $patterns->push(CompoundTag::create()
101 ->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
102 ->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
103 );
104 }
105 $nbt->setTag(self::TAG_PATTERNS, $patterns);
106 }
107
111 public function getBaseColor() : DyeColor{
112 return $this->baseColor;
113 }
114
118 public function setBaseColor(DyeColor $color) : void{
119 $this->baseColor = $color;
120 }
121
126 public function getPatterns() : array{
127 return $this->patterns;
128 }
129
135 public function setPatterns(array $patterns) : void{
136 $this->patterns = $patterns;
137 }
138
139 public function getDefaultName() : string{
140 return "Banner";
141 }
142}
addAdditionalSpawnData(CompoundTag $nbt)
setInt(string $name, int $value)
setTag(string $name, Tag $tag)