Unfuck banners ...

This commit is contained in:
Dylan K. Taylor
2021-04-28 18:42:29 +01:00
parent d5e5a81cff
commit b33bf1f433
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; }
}