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