1: <?php
2: namespace aliuly\common;
3:
4: use pocketmine\entity\Human;
5: use pocketmine\level\Location;
6:
7: use pocketmine\nbt\tag\ByteTag;
8: use pocketmine\nbt\tag\Tag;
9: use pocketmine\nbt\tag\CompoundTag;
10: use pocketmine\nbt\tag\DoubleTag;
11: use pocketmine\nbt\tag\EnumTag;
12: use pocketmine\nbt\tag\FloatTag;
13: use pocketmine\nbt\tag\StringTag;
14: use pocketmine\nbt\tag\IntTag;
15:
16: 17: 18:
19: class Npc extends Human {
20: 21: 22: 23: 24: 25:
26: static public function spawnNpc($name,Location $pos,$classname,$opts=null){
27: if ($opts == null) $opts = [];
28: if (!isset($opts["skin"])) {
29: $opts["skin"] =
30: str_repeat("\xFF", 32*16*2) . str_repeat("\xFF", 32*16*2) .
31: str_repeat("\xFF", 32*16*2) . str_repeat("\xFF", 32*16*2) .
32: str_repeat("\x80", 32*16*2) . str_repeat("\x80", 32*16*2) .
33: str_repeat("\x80", 32*16*2) . str_repeat("\x80", 32*16*2);
34: }
35: if (!isset($opts["slim"])) $opts["slim"] = false;
36: $ndat = [];
37: $ndat["Pos"] = new EnumTag("Pos", [
38: new DoubleTag("", $pos->x),
39: new DoubleTag("", $pos->y),
40: new DoubleTag("", $pos->z)]);
41: if (isset($opts["motion"])) {
42: $ndat["Motion"] = new EnumTag("Motion", [
43: new DoubleTag("",$opts["motion"][0]),
44: new DoubleTag("",$opts["motion"][1]),
45: new DoubleTag("",$opts["motion"][2])]);
46: unset($opts["motion"]);
47: } else {
48: $ndat["Motion"] = new EnumTag("Motion", [
49: new DoubleTag("",0),
50: new DoubleTag("",0),
51: new DoubleTag("",0)]);
52: }
53: if (isset($opts["rotation"])) {
54: $ndat["Rotation"] = new EnumTag("Rotation", [
55: new FloatTag("",$opts["rotation"][0]),
56: new FloatTag("",$opts["rotation"][1])]);
57: unset($opts["rotation"]);
58: } else {
59: $ndat["Rotation"] = new EnumTag("Rotation", [
60: new FloatTag("",$pos->yaw),
61: new FloatTag("",$pos->pitch)]);
62: }
63: $ndat["Skin"] = new CompoundTag("Skin", [
64: "Data" => new StringTag("Data", $opts["skin"]),
65: "Slim" => new ByteTag("Slim", $opts["slim"] ? 1 : 0)]);
66: unset($opts["skin"]);
67: unset($opts["slim"]);
68:
69: foreach ($opts as $k=>$v) {
70: if ($v instanceof Tag) {
71: $ndat[$k] = $v;
72: continue;
73: }
74: if (is_array($v) && count($v) == 2) {
75: list($type,$value) = $v;
76: $type = "pocketmine\\nbt\\tag\\".$type;
77: $ndat[$k] = new $type($k,$value);
78: continue;
79: }
80: switch (gettype($v)) {
81: case "boolean":
82: $ndat[$k] = new ByteTag($k,$v ? 1 : 0);
83: break;
84: case "integer":
85: $ndat[$k] = new IntTag($k, $v);
86: break;
87: case "double":
88: $ndat[$k] = new DoubleTag($k,$v);
89: break;
90: case "string":
91: $ndat[$k] = new StringTag($k,$v);
92: }
93: }
94: $npc = new $classname($pos->getLevel()->getChunk($pos->getX()>>4,
95: $pos->getZ()>>4),
96: new CompoundTag("",$ndat));
97: $npc->setNameTag($name);
98: return $npc;
99: }
100: }
101: