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