Unfuck banners ...

This commit is contained in:
Dylan K. Taylor 2021-04-28 18:42:29 +01:00
parent d5e5a81cff
commit b33bf1f433
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 210 additions and 57 deletions

View File

@ -24,8 +24,8 @@ declare(strict_types=1);
namespace pocketmine\block\tile;
use pocketmine\block\utils\BannerPatternLayer;
use pocketmine\block\utils\BannerPatternType;
use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\BannerPatternTypeIdMap;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
@ -69,23 +69,30 @@ class Banner extends Spawnable{
$this->baseColor = DyeColor::BLACK(); //TODO: this should be an error
}
$patternTypeIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $nbt->getListTag(self::TAG_PATTERNS);
if($patterns !== null){
/** @var CompoundTag $pattern */
foreach($patterns as $pattern){
$patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK(); //TODO: missing pattern colour should be an error
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($pattern->getString(self::TAG_PATTERN_NAME)), $patternColor);
$patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME));
if($patternType === null){
continue; //TODO: this should be an error, but right now we don't have the setup to deal with it
}
$this->patterns[] = new BannerPatternLayer($patternType, $patternColor);
}
}
}
protected function writeSaveData(CompoundTag $nbt) : void{
$colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance();
$nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
$patterns = new ListTag();
foreach($this->patterns as $pattern){
$patterns->push(CompoundTag::create()
->setString(self::TAG_PATTERN_NAME, $pattern->getType()->getPatternId())
->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
);
}
@ -94,11 +101,12 @@ class Banner extends Spawnable{
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
$colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance();
$nbt->setInt(self::TAG_BASE, $colorIdMap->toInvertedId($this->baseColor));
$patterns = new ListTag();
foreach($this->patterns as $pattern){
$patterns->push(CompoundTag::create()
->setString(self::TAG_PATTERN_NAME, $pattern->getType()->getPatternId())
->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
);
}

View File

@ -70,59 +70,48 @@ use pocketmine\utils\EnumTrait;
* @method static BannerPatternType TRIANGLE_TOP()
*/
final class BannerPatternType{
use EnumTrait {
__construct as Enum___construct;
}
use EnumTrait;
protected static function setup() : void{
self::registerAll(
new self("border", "bo"),
new self("bricks", "bri"),
new self("circle", "mc"),
new self("creeper", "cre"),
new self("cross", "cr"),
new self("curly_border", "cbo"),
new self("diagonal_left", "lud"),
new self("diagonal_right", "rd"),
new self("diagonal_up_left", "ld"),
new self("diagonal_up_right", "rud"),
new self("flower", "flo"),
new self("gradient", "gra"),
new self("gradient_up", "gru"),
new self("half_horizontal", "hh"),
new self("half_horizontal_bottom", "hhb"),
new self("half_vertical", "vh"),
new self("half_vertical_right", "vhr"),
new self("mojang", "moj"),
new self("rhombus", "mr"),
new self("skull", "sku"),
new self("small_stripes", "ss"),
new self("square_bottom_left", "bl"),
new self("square_bottom_right", "br"),
new self("square_top_left", "tl"),
new self("square_top_right", "tr"),
new self("straight_cross", "sc"),
new self("stripe_bottom", "bs"),
new self("stripe_center", "cs"),
new self("stripe_downleft", "dls"),
new self("stripe_downright", "drs"),
new self("stripe_left", "ls"),
new self("stripe_middle", "ms"),
new self("stripe_right", "rs"),
new self("stripe_top", "ts"),
new self("triangle_bottom", "bt"),
new self("triangle_top", "tt"),
new self("triangles_bottom", "bts"),
new self("triangles_top", "tts")
new self("border"),
new self("bricks"),
new self("circle"),
new self("creeper"),
new self("cross"),
new self("curly_border"),
new self("diagonal_left"),
new self("diagonal_right"),
new self("diagonal_up_left"),
new self("diagonal_up_right"),
new self("flower"),
new self("gradient"),
new self("gradient_up"),
new self("half_horizontal"),
new self("half_horizontal_bottom"),
new self("half_vertical"),
new self("half_vertical_right"),
new self("mojang"),
new self("rhombus"),
new self("skull"),
new self("small_stripes"),
new self("square_bottom_left"),
new self("square_bottom_right"),
new self("square_top_left"),
new self("square_top_right"),
new self("straight_cross"),
new self("stripe_bottom"),
new self("stripe_center"),
new self("stripe_downleft"),
new self("stripe_downright"),
new self("stripe_left"),
new self("stripe_middle"),
new self("stripe_right"),
new self("stripe_top"),
new self("triangle_bottom"),
new self("triangle_top"),
new self("triangles_bottom"),
new self("triangles_top")
);
}
private string $patternId;
private function __construct(string $name, string $patternId){
$this->Enum___construct($name);
$this->patternId = $patternId;
}
public function getPatternId() : string{ return $this->patternId; }
}

View File

@ -0,0 +1,100 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\data\bedrock;
use pocketmine\block\utils\BannerPatternType;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;
final class BannerPatternTypeIdMap{
use SingletonTrait;
/**
* @var BannerPatternType[]
* @phpstan-var array<string, BannerPatternType>
*/
private array $idToEnum = [];
/**
* @var string[]
* @phpstan-var array<int, string>
*/
private array $enumToId = [];
public function __construct(){
$this->register("bo", BannerPatternType::BORDER());
$this->register("bri", BannerPatternType::BRICKS());
$this->register("mc", BannerPatternType::CIRCLE());
$this->register("cre", BannerPatternType::CREEPER());
$this->register("cr", BannerPatternType::CROSS());
$this->register("cbo", BannerPatternType::CURLY_BORDER());
$this->register("lud", BannerPatternType::DIAGONAL_LEFT());
$this->register("rd", BannerPatternType::DIAGONAL_RIGHT());
$this->register("ld", BannerPatternType::DIAGONAL_UP_LEFT());
$this->register("rud", BannerPatternType::DIAGONAL_UP_RIGHT());
$this->register("flo", BannerPatternType::FLOWER());
$this->register("gra", BannerPatternType::GRADIENT());
$this->register("gru", BannerPatternType::GRADIENT_UP());
$this->register("hh", BannerPatternType::HALF_HORIZONTAL());
$this->register("hhb", BannerPatternType::HALF_HORIZONTAL_BOTTOM());
$this->register("vh", BannerPatternType::HALF_VERTICAL());
$this->register("vhr", BannerPatternType::HALF_VERTICAL_RIGHT());
$this->register("moj", BannerPatternType::MOJANG());
$this->register("mr", BannerPatternType::RHOMBUS());
$this->register("sku", BannerPatternType::SKULL());
$this->register("ss", BannerPatternType::SMALL_STRIPES());
$this->register("bl", BannerPatternType::SQUARE_BOTTOM_LEFT());
$this->register("br", BannerPatternType::SQUARE_BOTTOM_RIGHT());
$this->register("tl", BannerPatternType::SQUARE_TOP_LEFT());
$this->register("tr", BannerPatternType::SQUARE_TOP_RIGHT());
$this->register("sc", BannerPatternType::STRAIGHT_CROSS());
$this->register("bs", BannerPatternType::STRIPE_BOTTOM());
$this->register("cs", BannerPatternType::STRIPE_CENTER());
$this->register("dls", BannerPatternType::STRIPE_DOWNLEFT());
$this->register("drs", BannerPatternType::STRIPE_DOWNRIGHT());
$this->register("ls", BannerPatternType::STRIPE_LEFT());
$this->register("ms", BannerPatternType::STRIPE_MIDDLE());
$this->register("rs", BannerPatternType::STRIPE_RIGHT());
$this->register("ts", BannerPatternType::STRIPE_TOP());
$this->register("bt", BannerPatternType::TRIANGLE_BOTTOM());
$this->register("tt", BannerPatternType::TRIANGLE_TOP());
$this->register("bts", BannerPatternType::TRIANGLES_BOTTOM());
$this->register("tts", BannerPatternType::TRIANGLES_TOP());
}
public function register(string $stringId, BannerPatternType $type) : void{
$this->idToEnum[$stringId] = $type;
$this->enumToId[$type->id()] = $stringId;
}
public function fromId(string $id) : ?BannerPatternType{
return $this->idToEnum[$id] ?? null;
}
public function toId(BannerPatternType $type) : string{
if(!array_key_exists($type->id(), $this->enumToId)){
throw new \InvalidArgumentException("Missing mapping for banner pattern type " . $type->name());
}
return $this->enumToId[$type->id()];
}
}

View File

@ -26,8 +26,8 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\block\tile\Banner as TileBanner;
use pocketmine\block\utils\BannerPatternLayer;
use pocketmine\block\utils\BannerPatternType;
use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\BannerPatternTypeIdMap;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
@ -100,12 +100,17 @@ class Banner extends ItemBlockWallOrFloor{
$this->patterns = [];
$colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $tag->getListTag(self::TAG_PATTERNS);
if($patterns !== null){
/** @var CompoundTag $t */
foreach($patterns as $t){
$patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK(); //TODO: missing pattern colour should be an error
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($t->getString(self::TAG_PATTERN_NAME)), $patternColor);
$patternType = $patternIdMap->fromId($t->getString(self::TAG_PATTERN_NAME));
if($patternType === null){
continue; //TODO: this should be an error
}
$this->patterns[] = new BannerPatternLayer($patternType, $patternColor);
}
}
}
@ -116,9 +121,10 @@ class Banner extends ItemBlockWallOrFloor{
if(count($this->patterns) > 0){
$patterns = new ListTag();
$colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance();
foreach($this->patterns as $pattern){
$patterns->push(CompoundTag::create()
->setString(self::TAG_PATTERN_NAME, $pattern->getType()->getPatternId())
->setString(self::TAG_PATTERN_NAME, $patternIdMap->toId($pattern->getType()))
->setInt(self::TAG_PATTERN_COLOR, $colorIdMap->toInvertedId($pattern->getColor()))
);
}

View File

@ -0,0 +1,50 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\item;
use PHPStan\Testing\TestCase;
use pocketmine\block\utils\BannerPatternLayer;
use pocketmine\block\utils\BannerPatternType;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\VanillaBlocks;
use function assert;
final class BannerTest extends TestCase{
public function testBannerPatternSaveRestore() : void{
$item = VanillaBlocks::BANNER()->asItem();
assert($item instanceof Banner);
$item->setPatterns([
new BannerPatternLayer(BannerPatternType::FLOWER(), DyeColor::RED())
]);
$data = $item->nbtSerialize();
$item2 = Item::nbtDeserialize($data);
self::assertTrue($item->equalsExact($item2));
self::assertInstanceOf(Banner::class, $item2);
$patterns = $item2->getPatterns();
self::assertCount(1, $patterns);
self::assertTrue(BannerPatternType::FLOWER()->equals($patterns[0]->getType()));
}
}