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