PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
tile/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\tile;
25
33
38class ItemFrame extends Spawnable{
39 public const TAG_ITEM_ROTATION = "ItemRotation";
40 public const TAG_ITEM_DROP_CHANCE = "ItemDropChance";
41 public const TAG_ITEM = "Item";
42
43 private Item $item;
44 private int $itemRotation = 0;
45 private float $itemDropChance = 1.0;
46
47 public function __construct(World $world, Vector3 $pos){
48 $this->item = VanillaItems::AIR();
49 parent::__construct($world, $pos);
50 }
51
52 public function readSaveData(CompoundTag $nbt) : void{
53 if(($itemTag = $nbt->getCompoundTag(self::TAG_ITEM)) !== null){
54 $this->item = Item::nbtDeserialize($itemTag);
55 }
56 if($nbt->getTag(self::TAG_ITEM_ROTATION) instanceof FloatTag){
57 $this->itemRotation = (int) ($nbt->getFloat(self::TAG_ITEM_ROTATION, $this->itemRotation * 45) / 45);
58 } else {
59 $this->itemRotation = $nbt->getByte(self::TAG_ITEM_ROTATION, $this->itemRotation);
60 }
61 $this->itemDropChance = $nbt->getFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance);
62 }
63
64 protected function writeSaveData(CompoundTag $nbt) : void{
65 $nbt->setFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance);
66 $nbt->setFloat(self::TAG_ITEM_ROTATION, $this->itemRotation * 45);
67 if(!$this->item->isNull()){
68 $nbt->setTag(self::TAG_ITEM, $this->item->nbtSerialize());
69 }
70 }
71
72 public function hasItem() : bool{
73 return !$this->item->isNull();
74 }
75
76 public function getItem() : Item{
77 return clone $this->item;
78 }
79
80 public function setItem(?Item $item) : void{
81 if($item !== null && !$item->isNull()){
82 $this->item = clone $item;
83 }else{
84 $this->item = VanillaItems::AIR();
85 }
86 }
87
88 public function getItemRotation() : int{
89 return $this->itemRotation;
90 }
91
92 public function setItemRotation(int $rotation) : void{
93 $this->itemRotation = $rotation;
94 }
95
96 public function getItemDropChance() : float{
97 return $this->itemDropChance;
98 }
99
100 public function setItemDropChance(float $chance) : void{
101 $this->itemDropChance = $chance;
102 }
103
104 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
105 $nbt->setFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance);
106 $nbt->setFloat(self::TAG_ITEM_ROTATION, $this->itemRotation * 45);
107 if(!$this->item->isNull()){
108 $nbt->setTag(self::TAG_ITEM, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($this->item));
109 }
110 }
111}
addAdditionalSpawnData(CompoundTag $nbt)
setFloat(string $name, float $value)