PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
NameableTrait.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
29
35 private $customName = null;
36
37 abstract public function getDefaultName() : string;
38
39 public function getName() : string{
40 return $this->customName ?? $this->getDefaultName();
41 }
42
43 public function setName(string $name) : void{
44 if($name === ""){
45 $this->customName = null;
46 }else{
47 $this->customName = $name;
48 }
49 }
50
51 public function hasName() : bool{
52 return $this->customName !== null;
53 }
54
55 public function addAdditionalSpawnData(CompoundTag $nbt) : void{
56 if($this->customName !== null){
57 $nbt->setString(Nameable::TAG_CUSTOM_NAME, $this->customName);
58 }
59 }
60
61 protected function loadName(CompoundTag $tag) : void{
62 if(($customNameTag = $tag->getTag(Nameable::TAG_CUSTOM_NAME)) instanceof StringTag){
63 $this->customName = $customNameTag->getValue();
64 }
65 }
66
67 protected function saveName(CompoundTag $tag) : void{
68 if($this->customName !== null){
69 $tag->setString(Nameable::TAG_CUSTOM_NAME, $this->customName);
70 }
71 }
72
76 public function copyDataFromItem(Item $item) : void{
77 parent::copyDataFromItem($item);
78 if($item->hasCustomName()){ //this should take precedence over saved NBT
79 $this->setName($item->getCustomName());
80 }
81 }
82}
setString(string $name, string $value)
copyDataFromItem(Item $item)