PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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
65 abstract protected function getOminousVersion() : Block;
66
67 public function writeStateToWorld() : void{
68 parent::writeStateToWorld();
69 $tile = $this->position->getWorld()->getTile($this->position);
70 assert($tile instanceof TileBanner);
71 $tile->setBaseColor($this->color);
72 $tile->setPatterns($this->patterns);
73 }
74
75 public function isSolid() : bool{
76 return false;
77 }
78
79 public function getMaxStackSize() : int{
80 return 16;
81 }
82
87 public function getPatterns() : array{
88 return $this->patterns;
89 }
90
97 public function setPatterns(array $patterns) : self{
98 foreach($patterns as $pattern){
99 if(!$pattern instanceof BannerPatternLayer){
100 throw new \TypeError("Array must only contain " . BannerPatternLayer::class . " objects");
101 }
102 }
103 $this->patterns = $patterns;
104 return $this;
105 }
106
107 protected function recalculateCollisionBoxes() : array{
108 return [];
109 }
110
111 public function getSupportType(Facing $facing) : SupportType{
112 return SupportType::NONE;
113 }
114
115 private function canBeSupportedBy(Block $block) : bool{
116 return $block->isSolid();
117 }
118
119 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
120 if(!$this->canBeSupportedBy($blockReplace->getSide($this->getSupportingFace()))){
121 return false;
122 }
123 if($item instanceof ItemBanner){
124 $this->color = $item->getColor();
125 $this->setPatterns($item->getPatterns());
126 }
127
128 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
129 }
130
131 abstract protected function getSupportingFace() : Facing;
132
133 public function onNearbyBlockChange() : void{
134 if(!$this->canBeSupportedBy($this->getSide($this->getSupportingFace()))){
135 $this->position->getWorld()->useBreakOn($this->position);
136 }
137 }
138
139 public function getDropsForCompatibleTool(Item $item) : array{
140 $drop = $this->asItem();
141 if($drop instanceof ItemBanner && count($this->patterns) > 0){
142 $drop->setPatterns($this->patterns);
143 }
144
145 return [$drop];
146 }
147
148 public function getPickedItem(bool $addUserData = false) : Item{
149 $result = $this->asItem();
150 if($addUserData && $result instanceof ItemBanner && count($this->patterns) > 0){
151 $result->setPatterns($this->patterns);
152 }
153 return $result;
154 }
155
156 public function asItem() : Item{
157 return VanillaItems::BANNER()->setColor($this->color);
158 }
159}
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)