baseColor = $nbt->getInt(self::TAG_BASE, self::COLOR_BLACK, true); $this->patterns = $nbt->getListTag(self::TAG_PATTERNS) ?? new ListTag(self::TAG_PATTERNS); $this->loadName($nbt); } protected function writeSaveData(CompoundTag $nbt) : void{ $nbt->setInt(self::TAG_BASE, $this->baseColor); $nbt->setTag($this->patterns); $this->saveName($nbt); } protected function addAdditionalSpawnData(CompoundTag $nbt) : void{ $nbt->setInt(self::TAG_BASE, $this->baseColor); $nbt->setTag($this->patterns); $this->addNameSpawnData($nbt); } /** * Returns the color of the banner base. * * @return int */ public function getBaseColor() : int{ return $this->baseColor; } /** * Sets the color of the banner base. * * @param int $color */ public function setBaseColor(int $color) : void{ $this->baseColor = $color; $this->onChanged(); } /** * Applies a new pattern on the banner with the given color. * * @param string $pattern * @param int $color * * @return int ID of pattern. */ public function addPattern(string $pattern, int $color) : int{ $this->patterns->push(new CompoundTag("", [ new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new StringTag(self::TAG_PATTERN_NAME, $pattern) ])); $this->onChanged(); return $this->patterns->count() - 1; //Last offset in the list } /** * Returns whether a pattern with the given ID exists on the banner or not. * * @param int $patternId * * @return bool */ public function patternExists(int $patternId) : bool{ return $this->patterns->isset($patternId); } /** * Returns the data of a pattern with the given ID. * * @param int $patternId * * @return array */ public function getPatternData(int $patternId) : array{ if(!$this->patternExists($patternId)){ return []; } $patternTag = $this->patterns->get($patternId); assert($patternTag instanceof CompoundTag); return [ self::TAG_PATTERN_COLOR => $patternTag->getInt(self::TAG_PATTERN_COLOR), self::TAG_PATTERN_NAME => $patternTag->getString(self::TAG_PATTERN_NAME) ]; } /** * Changes the pattern of a previously existing pattern. * * @param int $patternId * @param string $pattern * @param int $color * * @return bool indicating success. */ public function changePattern(int $patternId, string $pattern, int $color) : bool{ if(!$this->patternExists($patternId)){ return false; } $this->patterns->set($patternId, new CompoundTag("", [ new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new StringTag(self::TAG_PATTERN_NAME, $pattern) ])); $this->onChanged(); return true; } /** * Deletes a pattern from the banner with the given ID. * * @param int $patternId * * @return bool indicating whether the pattern existed or not. */ public function deletePattern(int $patternId) : bool{ if(!$this->patternExists($patternId)){ return false; } $this->patterns->remove($patternId); $this->onChanged(); return true; } /** * Deletes the top most pattern of the banner. * * @return bool indicating whether the banner was empty or not. */ public function deleteTopPattern() : bool{ return $this->deletePattern($this->getPatternCount() - 1); } /** * Deletes the bottom pattern of the banner. * * @return bool indicating whether the banner was empty or not. */ public function deleteBottomPattern() : bool{ return $this->deletePattern(0); } /** * Returns the total count of patterns on this banner. * * @return int */ public function getPatternCount() : int{ return $this->patterns->count(); } /** * @return ListTag */ public function getPatterns() : ListTag{ return $this->patterns; } protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ $nbt->setInt(self::TAG_BASE, $item !== null ? $item->getDamage() & 0x0f : 0); if($item !== null){ if($item->getNamedTag()->hasTag(self::TAG_PATTERNS, ListTag::class)){ $nbt->setTag($item->getNamedTag()->getListTag(self::TAG_PATTERNS)); } self::createNameNBT($nbt, $pos, $face, $item, $player); } } public function getDefaultName() : string{ return "Banner"; } }