blob: da81d78290989fdfd4e14d8f9e1369992530e7a5 (
plain)
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
|
package org.bukkit.craftbukkit.util;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import net.minecraft.server.MojangsonParser;
import net.minecraft.server.NBTBase;
import net.minecraft.server.NBTList;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagList;
import net.minecraft.server.NBTTagString;
public class CraftNBTTagConfigSerializer {
private static final Pattern ARRAY = Pattern.compile("^\\[.*]");
private static final MojangsonParser MOJANGSON_PARSER = new MojangsonParser(new StringReader(""));
public static Object serialize(NBTBase base) {
if (base instanceof NBTTagCompound) {
Map<String, Object> innerMap = new HashMap<>();
for (String key : ((NBTTagCompound) base).getKeys()) {
innerMap.put(key, serialize(((NBTTagCompound) base).get(key)));
}
return innerMap;
} else if (base instanceof NBTTagList) {
List<Object> baseList = new ArrayList<>();
for (int i = 0; i < ((NBTList) base).size(); i++) {
baseList.add(serialize(((NBTList) base).get(i)));
}
return baseList;
} else if (base instanceof NBTTagString) {
return base.b_(); //PAIL Rename getString
}
return base.toString();
}
public static NBTBase deserialize(Object object) {
if (object instanceof Map) {
NBTTagCompound compound = new NBTTagCompound();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) object).entrySet()) {
compound.set(entry.getKey(), deserialize(entry.getValue()));
}
return compound;
} else if (object instanceof List) {
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new NBTTagList(); // Default
}
NBTTagList tagList = new NBTTagList();
for (Object tag : list) {
tagList.add(deserialize(tag));
}
return tagList;
} else if (object instanceof String) {
String string = (String) object;
if (ARRAY.matcher(string).matches()) {
try {
return new MojangsonParser(new StringReader(string)).h(); // PAIL Rename parseTagList
} catch (CommandSyntaxException e) {
throw new RuntimeException("Could not deserialize found list ", e);
}
} else {
return MOJANGSON_PARSER.b(string); // PAIL Rename parse tagBase
}
}
throw new RuntimeException("Could not deserialize NBTBase");
}
}
|