PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
BaseBanner.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
24namespace pocketmine\block;
25
26use pocketmine\block\tile\Banner as TileBanner;
29use pocketmine\block\utils\ColoredTrait;
30use pocketmine\block\utils\SupportType;
31use pocketmine\item\Banner as ItemBanner;
38use function assert;
39use function count;
40
41abstract class BaseBanner extends Transparent implements Colored{
42 use ColoredTrait;
43
48 protected array $patterns = [];
49
50 public function readStateFromWorld() : Block{
51 parent::readStateFromWorld();
52 $tile = $this->position->getWorld()->getTile($this->position);
53 if($tile instanceof TileBanner){
54 if($tile->getType() === TileBanner::TYPE_OMINOUS){
55 //illager banner is implemented as a separate block, as it doesn't support base color or custom patterns
56 return $this->getOminousVersion();
57 }
58 $this->color = $tile->getBaseColor();
59 $this->setPatterns($tile->getPatterns());
60 }
61
62 return $this;
63 }
64
68 protected function getOminousVersion() : Block{
69 return VanillaBlocks::AIR();
70 }
71
72 public function writeStateToWorld() : void{
73 parent::writeStateToWorld();
74 $tile = $this->position->getWorld()->getTile($this->position);
75 assert($tile instanceof TileBanner);
76 $tile->setBaseColor($this->color);
77 $tile->setPatterns($this->patterns);
78 }
79
80 public function isSolid() : bool{
81 return false;
82 }
83
84 public function getMaxStackSize() : int{
85 return 16;
86 }
87
92 public function getPatterns() : array{
93 return $this->patterns;
94 }
95
102 public function setPatterns(array $patterns) : self{
103 foreach($patterns as $pattern){
104 if(!$pattern instanceof BannerPatternLayer){
105 throw new \TypeError("Array must only contain " . BannerPatternLayer::class . " objects");
106 }
107 }
108 $this->patterns = $patterns;
109 return $this;
110 }
111
112 protected function recalculateCollisionBoxes() : array{
113 return [];
114 }
115
116 public function getSupportType(Facing $facing) : SupportType{
117 return SupportType::NONE;
118 }
119
120 private function canBeSupportedBy(Block $block) : bool{
121 return $block->isSolid();
122 }
123
124 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
125 if(!$this->canBeSupportedBy($blockReplace->getSide($this->getSupportingFace()))){
126 return false;
127 }
128 if($item instanceof ItemBanner){
129 $this->color = $item->getColor();
130 $this->setPatterns($item->getPatterns());
131 }
132
133 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
134 }
135
136 abstract protected function getSupportingFace() : Facing;
137
138 public function onNearbyBlockChange() : void{
139 if(!$this->canBeSupportedBy($this->getSide($this->getSupportingFace()))){
140 $this->position->getWorld()->useBreakOn($this->position);
141 }
142 }
143
144 public function getDropsForCompatibleTool(Item $item) : array{
145 $drop = $this->asItem();
146 if($drop instanceof ItemBanner && count($this->patterns) > 0){
147 $drop->setPatterns($this->patterns);
148 }
149
150 return [$drop];
151 }
152
153 public function getPickedItem(bool $addUserData = false) : Item{
154 $result = $this->asItem();
155 if($addUserData && $result instanceof ItemBanner && count($this->patterns) > 0){
156 $result->setPatterns($this->patterns);
157 }
158 return $result;
159 }
160
161 public function asItem() : Item{
162 return VanillaItems::BANNER()->setColor($this->color);
163 }
164}
getDropsForCompatibleTool(Item $item)
getSupportType(Facing $facing)
setPatterns(array $patterns)
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
getPickedItem(bool $addUserData=false)