bukkit序列化存储坐标

Bukkit用于序列化存储对象的例子

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
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
public class Position implements ConfigurationSerializable{
private String world;
private double x, y, z;
public Position(Location location){
this(location.getWorld().getName(), location.getX(), location.getY(), location.getZ());
}
public Position(String world, double x, double y, double z){
setWorld(world);
setX(x);
setY(y);
setZ(z);
}
public String getWorld() {
return world;
}
public void setWorld(String world) {
this.world = world;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public Location getLocation(){
return new Location(Bukkit.getWorld(getWorld()), getZ(), getY(), getZ());
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> result = new HashMap<String, Object>();
result.put("world", getWorld());
result.put("x", getX());
result.put("y", getY());
result.put("z", getZ());
return result;
}
public static Position deserialize(Map<String, Object> args){
return new Position((String)args.get("world"), (Double)args.get("x"), (Double)args.get("y"), (Double)args.get("z"));
}
}

然后不要忘了在主类中注册

1
2
3
static{
ConfigurationSerialization.registerClass(Position.class);
}