/
WorldGuardLocationTags.java
122 lines (99 loc) · 3.63 KB
/
WorldGuardLocationTags.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
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
package net.gnomeffinway.depenizen.extensions.worldguard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.tags.Attribute;
import net.gnomeffinway.depenizen.support.Supported;
import java.util.ArrayList;
import java.util.List;
public class WorldGuardLocationTags implements Property {
public static boolean describes(dObject loc) {
return loc instanceof dLocation;
}
public static WorldGuardLocationTags getFrom(dObject loc) {
if (!describes(loc)) return null;
else return new WorldGuardLocationTags((dLocation) loc);
}
///////////////////
// Instance Fields and Methods
/////////////
private WorldGuardLocationTags(dLocation loc) {
location = loc;
}
dLocation location = null;
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
return null;
}
@Override
public String getPropertyId() {
return "WorldGuardLocationTags";
}
private ApplicableRegionSet getApplicableRegions() {
WorldGuardPlugin worldGuard = Supported.get("WORLDGUARD").getPlugin();
return worldGuard.getRegionManager(location.getWorld()).getApplicableRegions(location);
}
private boolean inRegion() {
return getApplicableRegions().size() > 0;
}
private boolean inRegion(String region) {
for (ProtectedRegion protectedRegion : getApplicableRegions()) {
if (protectedRegion.getId().equalsIgnoreCase(region)) {
return true;
}
}
return false;
}
private dList getRegions() {
List<String> regionList = new ArrayList<String>();
for (ProtectedRegion protectedRegion : getApplicableRegions()) {
regionList.add(protectedRegion.getId());
}
return new dList(regionList);
}
@Override
public String getAttribute(Attribute attribute) {
// <--[tag]
// @attribute <l@location.in_region[<name>|...]>
// @returns Element(Boolean)
// @description
// If a region name or list of names is specified, returns whether the
// location is in one of the listed regions, otherwise returns whether
// the location is in any region.
// @Plugin WorldGuard
// -->
if (attribute.startsWith("in_region")) {
// Check if the location is in the specified region
if (attribute.hasContext(1)) {
dList region_list = dList.valueOf(attribute.getContext(1));
for (String region : region_list) {
if (inRegion(region))
return Element.TRUE.getAttribute(attribute.fulfill(1));
}
return Element.FALSE.getAttribute(attribute.fulfill(1));
}
// Check if the location is in any region
else {
return new Element(inRegion()).getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.regions>
// @returns dList
// @description
// Returns a list of regions that the location is in.
// @Plugin WorldGuard
// -->
if (attribute.startsWith("regions")) {
return new dList(getRegions()).getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {}
}