/
Anchor.java
96 lines (79 loc) · 2.76 KB
/
Anchor.java
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
package net.citizensnpcs.util;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
/*
* Anchor object which holds a Location with a name to identify.
*/
public class Anchor {
private Location location;
private final String name;
// Needed for Anchors defined that can't currently have a valid 'Location'
private final String unloaded_value;
// Allow construction of anchor for unloaded worlds
public Anchor(String name, String unloaded_value) {
this.location = null;
this.unloaded_value = unloaded_value;
this.name = name;
}
public Anchor(String name, Location location) {
this.location = location;
this.name = name;
this.unloaded_value = location.getWorld().getName() + ';' + location.getX() + ';' + location.getY() + ';'
+ location.getZ();
}
public boolean isLoaded() {
return location != null;
}
public boolean load() {
try {
String[] parts = getUnloadedValue();
this.location = new Location(Bukkit.getWorld(parts[0]), Double.valueOf(parts[1]), Double.valueOf(parts[2]),
Double.valueOf(parts[3]));
} catch (Exception e) {
// Still not able to be loaded
}
return location != null;
}
@Override
public boolean equals(Object object) {
if (object == null)
return false;
if (object == this)
return true;
if (object.getClass() != getClass())
return false;
Anchor op = (Anchor) object;
return new EqualsBuilder().append(name, op.name).isEquals();
}
public Location getLocation() {
return location;
}
public String getName() {
return name;
}
/**
* Returns a String[] of the 'world_name, x, y, z' information needed to create the Location that is associated with
* the Anchor, in that order.
*
* @return a String array of the anchor's location data
*/
public String[] getUnloadedValue() {
return unloaded_value.split(";");
}
@Override
public int hashCode() {
return new HashCodeBuilder(13, 21).append(name).toHashCode();
}
// A friendly representation for use in saves.yml
public String stringValue() {
return name + ';' + unloaded_value;
}
@Override
public String toString() {
String[] parts = getUnloadedValue();
return "Anchor{Name='" + name + "';World='" + parts[0] + "';Location='" + parts[1] + ',' + parts[2] + ','
+ parts[3] + "';}";
}
}