PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Human.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 
22 namespace pocketmine\entity;
23 
26 use pocketmine\item\Item as ItemItem;
32 use pocketmine\Network;
37 
38 class Human extends Creature implements ProjectileSource, InventoryHolder{
39 
40  protected $nameTag = "TESTIFICATE";
42  protected $inventory;
43 
44  public $width = 0.6;
45  public $length = 0.6;
46  public $height = 1.8;
47  public $eyeHeight = 1.62;
48 
49  public function getInventory(){
50  return $this->inventory;
51  }
52 
53  protected function initEntity(){
54 
55  $this->inventory = new PlayerInventory($this);
56  if($this instanceof Player){
57  $this->addWindow($this->inventory, 0);
58  }
59 
60 
61  if(isset($this->namedtag->NameTag)){
62  $this->nameTag = $this->namedtag["NameTag"];
63  }
64 
65  foreach($this->namedtag->Inventory as $item){
66  if($item["Slot"] >= 0 and $item["Slot"] < 9){ //Hotbar
67  $this->inventory->setHotbarSlotIndex($item["Slot"], isset($item["TrueSlot"]) ? $item["TrueSlot"] : -1);
68  }elseif($item["Slot"] >= 100 and $item["Slot"] < 104){ //Armor
69  $this->inventory->setItem($this->inventory->getSize() + $item["Slot"] - 100, ItemItem::get($item["id"], $item["Damage"], $item["Count"]), $this);
70  }else{
71  $this->inventory->setItem($item["Slot"] - 9, ItemItem::get($item["id"], $item["Damage"], $item["Count"]), $this);
72  }
73  }
74 
75  parent::initEntity();
76  }
77 
78  public function getName(){
79  return $this->nameTag;
80  }
81 
82  public function getDrops(){
83  $drops = [];
84  if($this->inventory instanceof PlayerInventory){
85  foreach($this->inventory->getContents() as $item){
86  $drops[] = $item;
87  }
88  }
89 
90  return $drops;
91  }
92 
93  public function saveNBT(){
94  parent::saveNBT();
95  $this->namedtag->Inventory = new Enum("Inventory", []);
96  $this->namedtag->Inventory->setTagType(NBT::TAG_Compound);
97  if($this->inventory instanceof PlayerInventory){
98  for($slot = 0; $slot < 9; ++$slot){
99  $hotbarSlot = $this->inventory->getHotbarSlotIndex($slot);
100  if($hotbarSlot !== -1){
101  $item = $this->inventory->getItem($hotbarSlot);
102  if($item->getId() !== 0 and $item->getCount() > 0){
103  $this->namedtag->Inventory[$slot] = new Compound(false, [
104  new Byte("Count", $item->getCount()),
105  new Short("Damage", $item->getDamage()),
106  new Byte("Slot", $slot),
107  new Byte("TrueSlot", $hotbarSlot),
108  new Short("id", $item->getId()),
109  ]);
110  continue;
111  }
112  }
113  $this->namedtag->Inventory[$slot] = new Compound(false, [
114  new Byte("Count", 0),
115  new Short("Damage", 0),
116  new Byte("Slot", $slot),
117  new Byte("TrueSlot", -1),
118  new Short("id", 0),
119  ]);
120  }
121 
122  //Normal inventory
123  $slotCount = Player::SURVIVAL_SLOTS + 9;
124  //$slotCount = (($this instanceof Player and ($this->gamemode & 0x01) === 1) ? Player::CREATIVE_SLOTS : Player::SURVIVAL_SLOTS) + 9;
125  for($slot = 9; $slot < $slotCount; ++$slot){
126  $item = $this->inventory->getItem($slot - 9);
127  $this->namedtag->Inventory[$slot] = new Compound(false, [
128  new Byte("Count", $item->getCount()),
129  new Short("Damage", $item->getDamage()),
130  new Byte("Slot", $slot),
131  new Short("id", $item->getId()),
132  ]);
133  }
134 
135  //Armor
136  for($slot = 100; $slot < 104; ++$slot){
137  $item = $this->inventory->getItem($this->inventory->getSize() + $slot - 100);
138  if($item instanceof ItemItem and $item->getId() !== ItemItem::AIR){
139  $this->namedtag->Inventory[$slot] = new Compound(false, [
140  new Byte("Count", $item->getCount()),
141  new Short("Damage", $item->getDamage()),
142  new Byte("Slot", $slot),
143  new Short("id", $item->getId()),
144  ]);
145  }
146  }
147  }
148  }
149 
150  public function spawnTo(Player $player){
151  if($player !== $this and !isset($this->hasSpawned[$player->getId()])){
152  $this->hasSpawned[$player->getId()] = $player;
153 
154  $pk = new AddPlayerPacket();
155  $pk->clientID = 0;
156  if($player->getRemoveFormat()){
157  $pk->username = TextFormat::clean($this->nameTag);
158  }else{
159  $pk->username = $this->nameTag;
160  }
161  $pk->eid = $this->getId();
162  $pk->x = $this->x;
163  $pk->y = $this->y;
164  $pk->z = $this->z;
165  $pk->yaw = $this->yaw;
166  $pk->pitch = $this->pitch;
167  $item = $this->getInventory()->getItemInHand();
168  $pk->item = $item->getId();
169  $pk->meta = $item->getDamage();
170  $pk->metadata = $this->getData();
171  $player->dataPacket($pk);
172 
173  $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
174 
175  $this->inventory->sendArmorContents($player);
176  }
177  }
178 
179  public function despawnFrom(Player $player){
180  if(isset($this->hasSpawned[$player->getId()])){
181  $pk = new RemovePlayerPacket();
182  $pk->eid = $this->id;
183  $pk->clientID = 0;
184  $player->dataPacket($pk);
185  unset($this->hasSpawned[$player->getId()]);
186  }
187  }
188 
189  public function getData(){ //TODO
190  $flags = 0;
191  $flags |= $this->fireTicks > 0 ? 1 : 0;
192  //$flags |= ($this->crouched === true ? 0b10:0) << 1;
193  //$flags |= ($this->inAction === true ? 0b10000:0);
194  $d = [
195  0 => ["type" => 0, "value" => $flags],
196  1 => ["type" => 1, "value" => $this->airTicks],
197  16 => ["type" => 0, "value" => 0],
198  17 => ["type" => 6, "value" => [0, 0, 0]],
199  ];
200 
201  return $d;
202  }
203 
204  public function close(){
205  if(!$this->closed){
206  if(!($this instanceof Player) or $this->loggedIn){
207  foreach($this->inventory->getViewers() as $viewer){
208  $viewer->removeWindow($this->inventory);
209  }
210  }
211  parent::close();
212  }
213  }
214 
215 }