mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-30 00:49:44 +00:00
these parameters did not make any sense to be optional, but were forced to be this way because of the way nullable types worked before 7.1.
104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\tile;
|
|
|
|
use pocketmine\item\Item;
|
|
use pocketmine\item\ItemFactory;
|
|
use pocketmine\level\Level;
|
|
use pocketmine\math\Vector3;
|
|
use pocketmine\nbt\tag\CompoundTag;
|
|
|
|
class ItemFrame extends Spawnable{
|
|
public const TAG_ITEM_ROTATION = "ItemRotation";
|
|
public const TAG_ITEM_DROP_CHANCE = "ItemDropChance";
|
|
public const TAG_ITEM = "Item";
|
|
|
|
/** @var Item */
|
|
private $item;
|
|
/** @var int */
|
|
private $itemRotation = 0;
|
|
/** @var float */
|
|
private $itemDropChance = 1.0;
|
|
|
|
public function __construct(Level $level, Vector3 $pos){
|
|
$this->item = ItemFactory::air();
|
|
parent::__construct($level, $pos);
|
|
}
|
|
|
|
public function readSaveData(CompoundTag $nbt) : void{
|
|
if(($itemTag = $nbt->getCompoundTag(self::TAG_ITEM)) !== null){
|
|
$this->item = Item::nbtDeserialize($itemTag);
|
|
}
|
|
$this->itemRotation = $nbt->getByte(self::TAG_ITEM_ROTATION, $this->itemRotation, true);
|
|
$this->itemDropChance = $nbt->getFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance, true);
|
|
}
|
|
|
|
protected function writeSaveData(CompoundTag $nbt) : void{
|
|
$nbt->setFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance);
|
|
$nbt->setByte(self::TAG_ITEM_ROTATION, $this->itemRotation);
|
|
$nbt->setTag($this->item->nbtSerialize(-1, self::TAG_ITEM));
|
|
}
|
|
|
|
public function hasItem() : bool{
|
|
return !$this->item->isNull();
|
|
}
|
|
|
|
public function getItem() : Item{
|
|
return clone $this->item;
|
|
}
|
|
|
|
public function setItem(?Item $item){
|
|
if($item !== null and !$item->isNull()){
|
|
$this->item = clone $item;
|
|
}else{
|
|
$this->item = ItemFactory::air();
|
|
}
|
|
$this->onChanged();
|
|
}
|
|
|
|
public function getItemRotation() : int{
|
|
return $this->itemRotation;
|
|
}
|
|
|
|
public function setItemRotation(int $rotation){
|
|
$this->itemRotation = $rotation;
|
|
$this->onChanged();
|
|
}
|
|
|
|
public function getItemDropChance() : float{
|
|
return $this->itemDropChance;
|
|
}
|
|
|
|
public function setItemDropChance(float $chance){
|
|
$this->itemDropChance = $chance;
|
|
$this->onChanged();
|
|
}
|
|
|
|
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
|
|
$nbt->setFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance);
|
|
$nbt->setByte(self::TAG_ITEM_ROTATION, $this->itemRotation);
|
|
$nbt->setTag($this->item->nbtSerialize(-1, self::TAG_ITEM));
|
|
}
|
|
}
|