1: <?php
2: namespace aliuly\common;
3: use pocketmine\item\Item;
4:
5: 6: 7:
8: abstract class ItemName {
9:
10: static protected $xnames = null;
11:
12: static protected $items = [];
13:
14: static protected $usrnames = [];
15: 16: 17: 18:
19: static public function initUsrNames(array $names) {
20: self::$usrnames = $names;
21: }
22: 23: 24: 25: 26: 27:
28: public static function loadUsrNames($f) {
29: $tx = file($f, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
30: $i = 0;
31: if ($tx === false) return -1;
32: foreach ($tx as $x) {
33: $x = trim($x);
34: if (substr($x,0,1) == "#" || substr($x,0,1) == ";") continue;
35: $x = preg_split('/\s*=\s*/',$x,2);
36: if (count($x) != 2) continue;
37: ++$i;
38: self::$usrnames[$x[0]] = $x[1];
39: }
40: return $i;
41: }
42:
43: 44: 45:
46: static protected function initXnames() {
47: self::$xnames = [
48: Item::DYE => [
49: 0 => "Ink Sac",
50: 1 => "Rose Red",
51: 2 => "Cactus Green",
52: 3 => "Cocoa Beans",
53: 4 => "Lapis Lazuli",
54: 5 => "Purple Dye",
55: 6 => "Cyan Dye",
56: 7 => "Light Gray Dye",
57: 8 => "Gray Dye",
58: 9 => "Pink Dye",
59: 10 => "Lime Dye",
60: 11 => "Dandelion Yellow",
61: 12 => "Light Blue Dye",
62: 13 => "Magenta Dye",
63: 14 => "Orange Dye",
64: 15 => "Bone Meal",
65: "*" => "Dye",
66: ],
67: Item::SPAWN_EGG => [
68: "*" => "Spawn Egg",
69: 32 => "Spawn Zombie",
70: 33 => "Spawn Creeper",
71: 34 => "Spawn Skeleton",
72: 35 => "Spawn Spider",
73: 36 => "Spawn Zombie Pigman",
74: 37 => "Spawn Slime",
75: 38 => "Spawn Enderman",
76: 39 => "Spawn Silverfish",
77: 40 => "Spawn Cave Spider",
78: 41 => "Spawn Ghast",
79: 42 => "Spawn Magma Cube",
80: 10 => "Spawn Chicken",
81: 11 => "Spawn Cow",
82: 12 => "Spawn Pig",
83: 13 => "Spawn Sheep",
84: 14 => "Spawn Wolf",
85: 16 => "Spawn Mooshroom",
86: 17 => "Spawn Squid",
87: 19 => "Spawn Bat",
88: 15 => "Spawn Villager",
89: ]
90: ];
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: static public function str(Item $item) {
101: $id = $item->getId();
102: $meta = $item->getDamage();
103: if (isset(self::$usrnames[$id.":".$meta])) return self::$usrnames[$id.":".$meta];
104: if (isset(self::$usrnames[$id])) return self::$usrnames[$id];
105: if (self::$xnames == null) self::initXnames();
106:
107: if (isset(self::$xnames[$id])) {
108: if (isset(self::$xnames[$id][$meta])) {
109: return self::$xnames[$id][$meta];
110: } elseif (isset(self::$xnames[$id]["*"])) {
111: return self::$xnames[$id]["*"];
112: } else {
113: return self::$xnames[$id][0];
114: }
115: }
116: $n = $item->getName();
117: if ($n != "Unknown") return $n;
118: if (count(self::$items) == 0) {
119: $constants = array_keys((new \ReflectionClass("pocketmine\\item\\Item"))->getConstants());
120: foreach ($constants as $constant) {
121: $cid = constant("pocketmine\\item\\Item::$constant");
122: $constant = str_replace("_", " ", $constant);
123: self::$items[$cid] = $constant;
124: }
125: }
126: if (isset(self::$items[$id])) return self::$items[$id];
127: return $n;
128: }
129: }
130: