forked from Spirit55555/PHP-Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftJsonColors.php
More file actions
99 lines (86 loc) · 2.6 KB
/
MinecraftJsonColors.php
File metadata and controls
99 lines (86 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Based on http://wiki.vg/Chat
*/
class MinecraftJsonColors {
const COLOR_CHAR = "ยง";
static private $colors = array(
'black' => '0',
'dark_blue' => '1',
'dark_green' => '2',
'dark_aqua' => '3',
'dark_red' => '4',
'dark_purple' => '5',
'gold' => '6',
'gray' => '7',
'dark_gray' => '8',
'blue' => '9',
'green' => 'a',
'aqua' => 'b',
'red' => 'c',
'light_purple' => 'd',
'yellow' => 'e',
'white' => 'f',
);
static private $formatting = array(
'obfuscated' => 'k',
'bold' => 'l',
'strikethrough' => 'm',
'underline' => 'n',
'italic' => 'o',
'reset' => 'r'
);
public static function convertToLegacy($json) {
$legacy = '';
if (isset($json['extra'])) {
foreach ($json['extra'] as $component) {
if (is_string($component)) {
$legacy .= $component;
} else {
//reset the formatting to make the components independent
$legacy .= self::parseElement($component) . self::COLOR_CHAR . self::$formatting['reset'];
}
}
}
$legacy .= self::parseElement($json);
return $legacy;
}
private static function parseElement($json) {
$legacy = '';
if (isset($json['obfuscated'])) {
if ($json['obfuscated']) {
$legacy .= self::COLOR_CHAR . self::$formatting['obfuscated'];
}
}
if (isset($json['strikethrough'])) {
if ($json['strikethrough']) {
$legacy .= self::COLOR_CHAR . self::$formatting['strikethrough'];
}
}
if (isset($json['underlined'])) {
if ($json['underlined']) {
$legacy .= self::COLOR_CHAR . self::$formatting['underline'];
}
}
if (isset($json['italic'])) {
if ($json['italic']) {
$legacy .= self::COLOR_CHAR . self::$formatting['italic'];
}
}
if (isset($json['bold'])) {
if ($json['bold']) {
$legacy .= self::COLOR_CHAR . self::$formatting['bold'];
}
}
if (isset($json['color'])) {
$color = $json['color'];
if (isset(self::$colors[$color])) {
$legacy .= self::COLOR_CHAR . self::$colors[$color];
}
}
if (isset($json['text'])) {
$legacy .= $json['text'];
}
return $legacy;
}
}