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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package com.earth2me.essentials.storage;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
public class BukkitConstructor extends Constructor
{
private final transient Pattern NUMPATTERN = Pattern.compile("\\d+");
public BukkitConstructor(Class clazz)
{
super(clazz);
yamlClassConstructors.put(NodeId.scalar, new ConstructBukkitScalar());
yamlClassConstructors.put(NodeId.mapping, new ConstructBukkitMapping());
}
private class ConstructBukkitScalar extends ConstructScalar
{
@Override
public Object construct(final Node node)
{
if (node.getType().equals(Material.class))
{
final String val = (String)constructScalar((ScalarNode)node);
Material mat;
if (NUMPATTERN.matcher(val).matches())
{
final int typeId = Integer.parseInt(val);
mat = Material.getMaterial(typeId);
}
else
{
mat = Material.matchMaterial(val);
}
return mat;
}
if (node.getType().equals(MaterialData.class))
{
final String val = (String)constructScalar((ScalarNode)node);
if (val.isEmpty())
{
return null;
}
final String[] split = val.split("[:+',;.]", 2);
if (split.length == 0)
{
return null;
}
Material mat;
if (NUMPATTERN.matcher(split[0]).matches())
{
final int typeId = Integer.parseInt(split[0]);
mat = Material.getMaterial(typeId);
}
else
{
mat = Material.matchMaterial(split[0]);
}
byte data = 0;
if (split.length == 2 && NUMPATTERN.matcher(split[1]).matches())
{
data = Byte.parseByte(split[1]);
}
return new MaterialData(mat, data);
}
if (node.getType().equals(ItemStack.class))
{
final String val = (String)constructScalar((ScalarNode)node);
if (val.isEmpty())
{
return null;
}
final String[] split1 = val.split("\\W", 2);
if (split1.length == 0)
{
return null;
}
final String[] split2 = split1[0].split("[:+',;.]", 2);
if (split2.length == 0)
{
return null;
}
Material mat;
if (NUMPATTERN.matcher(split2[0]).matches())
{
final int typeId = Integer.parseInt(split2[0]);
mat = Material.getMaterial(typeId);
}
else
{
mat = Material.matchMaterial(split2[0]);
}
short data = 0;
if (split2.length == 2 && NUMPATTERN.matcher(split2[1]).matches())
{
data = Short.parseShort(split2[1]);
}
int size = mat.getMaxStackSize();
if (split1.length == 2 && NUMPATTERN.matcher(split1[1]).matches())
{
size = Integer.parseInt(split1[1]);
}
return new ItemStack(mat, size, data);
}
return super.construct(node);
}
}
private class ConstructBukkitMapping extends ConstructMapping
{
@Override
public Object construct(final Node node)
{
if (node.getType().equals(Location.class))
{
//TODO: NPE checks
final MappingNode mnode = (MappingNode)node;
String worldName = "";
double x = 0, y = 0, z = 0;
float yaw = 0, pitch = 0;
if (mnode.getValue().size() < 4)
{
return null;
}
for (NodeTuple nodeTuple : mnode.getValue())
{
final String key = (String)constructScalar((ScalarNode)nodeTuple.getKeyNode());
final ScalarNode snode = (ScalarNode)nodeTuple.getValueNode();
if (key.equalsIgnoreCase("world"))
{
worldName = (String)constructScalar(snode);
}
if (key.equalsIgnoreCase("x"))
{
x = Double.parseDouble((String)constructScalar(snode));
}
if (key.equalsIgnoreCase("y"))
{
y = Double.parseDouble((String)constructScalar(snode));
}
if (key.equalsIgnoreCase("z"))
{
z = Double.parseDouble((String)constructScalar(snode));
}
if (key.equalsIgnoreCase("yaw"))
{
yaw = Float.parseFloat((String)constructScalar(snode));
}
if (key.equalsIgnoreCase("pitch"))
{
pitch = Float.parseFloat((String)constructScalar(snode));
}
}
if (worldName == null || worldName.isEmpty())
{
return null;
}
final World world = Bukkit.getWorld(worldName);
if (world == null)
{
return null;
}
return new Location(world, x, y, z, yaw, pitch);
}
return super.construct(node);
}
}
}
|