PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
ItemFrame.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\ItemFrame as TileItemFrame;
28use pocketmine\block\utils\AnyFacingTrait;
29use pocketmine\block\utils\SupportType;
40use function is_infinite;
41use function is_nan;
42
43class ItemFrame extends Flowable implements AnyFacing{
44 use AnyFacingTrait;
45
46 public const ROTATIONS = 8;
47
48 protected bool $hasMap = false; //makes frame appear large if set
49
50 protected ?Item $framedItem = null;
51 protected int $itemRotation = 0;
52 protected float $itemDropChance = 1.0;
53
54 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
55 $w->facing($this->facing);
56 $w->bool($this->hasMap);
57 }
58
59 public function readStateFromWorld() : Block{
60 parent::readStateFromWorld();
61 $tile = $this->position->getWorld()->getTile($this->position);
62 if($tile instanceof TileItemFrame){
63 $this->framedItem = $tile->getItem();
64 if($this->framedItem->isNull()){
65 $this->framedItem = null;
66 }
67 $this->itemRotation = $tile->getItemRotation() % self::ROTATIONS;
68 $this->itemDropChance = $tile->getItemDropChance();
69 }
70
71 return $this;
72 }
73
74 public function writeStateToWorld() : void{
75 parent::writeStateToWorld();
76 $tile = $this->position->getWorld()->getTile($this->position);
77 if($tile instanceof TileItemFrame){
78 $tile->setItem($this->framedItem);
79 $tile->setItemRotation($this->itemRotation);
80 $tile->setItemDropChance($this->itemDropChance);
81 }
82 }
83
84 public function getFramedItem() : ?Item{
85 return $this->framedItem !== null ? clone $this->framedItem : null;
86 }
87
89 public function setFramedItem(?Item $item) : self{
90 if($item === null || $item->isNull()){
91 $this->framedItem = null;
92 $this->itemRotation = 0;
93 }else{
94 $this->framedItem = clone $item;
95 }
96 return $this;
97 }
98
99 public function getItemRotation() : int{
100 return $this->itemRotation;
101 }
102
104 public function setItemRotation(int $itemRotation) : self{
105 $this->itemRotation = $itemRotation;
106 return $this;
107 }
108
109 public function getItemDropChance() : float{
110 return $this->itemDropChance;
111 }
112
114 public function setItemDropChance(float $itemDropChance) : self{
115 if($itemDropChance < 0.0 || $itemDropChance > 1.0 || is_nan($itemDropChance) || is_infinite($itemDropChance)){
116 throw new \InvalidArgumentException("Drop chance must be in range 0-1");
117 }
118 $this->itemDropChance = $itemDropChance;
119 return $this;
120 }
121
122 public function hasMap() : bool{ return $this->hasMap; }
123
130 public function setHasMap(bool $hasMap) : self{
131 $this->hasMap = $hasMap;
132 return $this;
133 }
134
135 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
136 if($this->framedItem !== null){
137 $this->itemRotation = ($this->itemRotation + 1) % self::ROTATIONS;
138
139 $this->position->getWorld()->addSound($this->position, new ItemFrameRotateItemSound());
140 }elseif(!$item->isNull()){
141 $this->framedItem = $item->pop();
142
143 $this->position->getWorld()->addSound($this->position, new ItemFrameAddItemSound());
144 }else{
145 return true;
146 }
147
148 $this->position->getWorld()->setBlock($this->position, $this);
149
150 return true;
151 }
152
153 public function onAttack(Item $item, int $face, ?Player $player = null) : bool{
154 if($this->framedItem === null){
155 return false;
156 }
157 $world = $this->position->getWorld();
158 if(Utils::getRandomFloat() <= $this->itemDropChance){
159 $world->dropItem($this->position->add(0.5, 0.5, 0.5), clone $this->framedItem);
160 $world->addSound($this->position, new ItemFrameRemoveItemSound());
161 }
162 $this->setFramedItem(null);
163 $world->setBlock($this->position, $this);
164 return true;
165 }
166
167 private function canBeSupportedAt(Block $block, int $face) : bool{
168 return $block->getAdjacentSupportType($face) !== SupportType::NONE;
169 }
170
171 public function onNearbyBlockChange() : void{
172 if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing))){
173 $this->position->getWorld()->useBreakOn($this->position);
174 }
175 }
176
177 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
178 if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
179 return false;
180 }
181
182 $this->facing = $face;
183
184 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
185 }
186
187 public function getDropsForCompatibleTool(Item $item) : array{
188 $drops = parent::getDropsForCompatibleTool($item);
189 if($this->framedItem !== null && Utils::getRandomFloat() <= $this->itemDropChance){
190 $drops[] = clone $this->framedItem;
191 }
192
193 return $drops;
194 }
195
196 public function getPickedItem(bool $addUserData = false) : Item{
197 return $this->framedItem !== null ? clone $this->framedItem : parent::getPickedItem($addUserData);
198 }
199}
onAttack(Item $item, int $face, ?Player $player=null)
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition ItemFrame.php:54
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
getPickedItem(bool $addUserData=false)
setItemDropChance(float $itemDropChance)
setItemRotation(int $itemRotation)
getDropsForCompatibleTool(Item $item)