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