22declare(strict_types=1);
27use pocketmine\block\utils\AnyFacingTrait;
28use pocketmine\block\utils\SupportType;
39use
function is_infinite;
45 public const ROTATIONS = 8;
47 protected bool $hasMap =
false;
49 protected ?
Item $framedItem =
null;
50 protected int $itemRotation = 0;
51 protected float $itemDropChance = 1.0;
54 $w->facing($this->facing);
55 $w->bool($this->hasMap);
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;
66 $this->itemRotation = $tile->getItemRotation() % self::ROTATIONS;
67 $this->itemDropChance = $tile->getItemDropChance();
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);
83 public function getFramedItem() : ?Item{
84 return $this->framedItem !== null ? clone $this->framedItem : null;
89 if($item === null || $item->isNull()){
90 $this->framedItem =
null;
91 $this->itemRotation = 0;
93 $this->framedItem = clone $item;
98 public function getItemRotation() : int{
99 return $this->itemRotation;
104 $this->itemRotation = $itemRotation;
108 public function getItemDropChance() : float{
109 return $this->itemDropChance;
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");
117 $this->itemDropChance = $itemDropChance;
121 public function hasMap() : bool{ return $this->hasMap; }
130 $this->hasMap = $hasMap;
135 if($this->framedItem !== null){
136 $this->itemRotation = ($this->itemRotation + 1) % self::ROTATIONS;
139 }elseif(!$item->isNull()){
140 $this->framedItem = $item->pop();
147 $this->position->getWorld()->setBlock($this->position, $this);
153 if($this->framedItem === null){
156 $world = $this->position->getWorld();
157 if(Utils::getRandomFloat() <= $this->itemDropChance){
158 $world->dropItem($this->position->add(0.5, 0.5, 0.5), clone $this->framedItem);
161 $this->setFramedItem(
null);
162 $world->setBlock($this->position, $this);
166 private function canBeSupportedAt(Block $block,
int $face) : bool{
167 return $block->getAdjacentSupportType($face) !== SupportType::NONE;
171 if(!$this->canBeSupportedAt($this,
Facing::opposite($this->facing))){
172 $this->position->getWorld()->useBreakOn($this->position);
177 if(!$this->canBeSupportedAt($blockReplace,
Facing::opposite($face))){
181 $this->facing = $face;
183 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
187 $drops = parent::getDropsForCompatibleTool($item);
188 if($this->framedItem !==
null && Utils::getRandomFloat() <= $this->itemDropChance){
189 $drops[] = clone $this->framedItem;
196 return $this->framedItem !== null ? clone $this->framedItem : parent::getPickedItem($addUserData);
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)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
setFramedItem(?Item $item)
getPickedItem(bool $addUserData=false)
setItemDropChance(float $itemDropChance)
setItemRotation(int $itemRotation)
getDropsForCompatibleTool(Item $item)