-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
WorldGuardLocationExtension.java
118 lines (98 loc) · 3.93 KB
/
WorldGuardLocationExtension.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
package com.denizenscript.depenizen.bukkit.extensions.worldguard;
import com.denizenscript.depenizen.bukkit.extensions.dObjectExtension;
import com.denizenscript.depenizen.bukkit.objects.worldguard.WorldGuardRegion;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.World;
import java.util.ArrayList;
import java.util.List;
public class WorldGuardLocationExtension extends dObjectExtension {
public static boolean describes(dObject loc) {
return loc instanceof dLocation;
}
public static WorldGuardLocationExtension getFrom(dObject loc) {
if (!describes(loc)) {
return null;
}
else {
return new WorldGuardLocationExtension((dLocation) loc);
}
}
public static final String[] handledTags = new String[] {
"in_region", "regions"
};
public static final String[] handledMechs = new String[] {
}; // None
private WorldGuardLocationExtension(dLocation loc) {
location = loc;
}
dLocation location = null;
private ApplicableRegionSet getApplicableRegions() {
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(location.getWorld()))
.getApplicableRegions(BukkitAdapter.asBlockVector(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(World world) {
List<String> regionList = new ArrayList<String>();
for (ProtectedRegion protectedRegion : getApplicableRegions()) {
regionList.add(new WorldGuardRegion(protectedRegion, world).identify());
}
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 DepenizenBukkit, 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 new Element(true).getAttribute(attribute.fulfill(1));
}
}
return new 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(Region)
// @description
// Returns a list of regions that the location is in.
// @Plugin DepenizenBukkit, WorldGuard
// -->
if (attribute.startsWith("regions")) {
return getRegions(location.getWorld()).getAttribute(attribute.fulfill(1));
}
return null;
}
}