Skip to content

Commit 4f58735

Browse files
Add region points support
1 parent 811915f commit 4f58735

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.skriptlang.skriptworldguard.elements.expressions;
2+
3+
import ch.njol.skript.doc.Description;
4+
import ch.njol.skript.doc.Example;
5+
import ch.njol.skript.doc.Name;
6+
import ch.njol.skript.doc.Since;
7+
import ch.njol.skript.expressions.base.PropertyExpression;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.SkriptParser.ParseResult;
10+
import ch.njol.skript.lang.SyntaxStringBuilder;
11+
import ch.njol.util.Kleenean;
12+
import com.sk89q.worldedit.bukkit.BukkitAdapter;
13+
import org.bukkit.Location;
14+
import org.jetbrains.annotations.Nullable;
15+
import org.skriptlang.skript.registration.SyntaxInfo;
16+
import org.skriptlang.skript.registration.SyntaxRegistry;
17+
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;
18+
import org.bukkit.event.Event;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
@Name("Region Points")
24+
@Description({
25+
"An expression to obtain the points of a region.",
26+
"Note that when obtaining the points of a region, the region itself is projected onto an X-Z plane." +
27+
" Thus, the points do not have a y-component." +
28+
" However, for simplicity, the y-component of the minimum point is used."
29+
})
30+
@Example("""
31+
command /highlight-points <text>:
32+
trigger:
33+
set the blocks at the points of the region text-argument to red wool
34+
""")
35+
@Since("1.0")
36+
public class ExprRegionPoints extends PropertyExpression<WorldGuardRegion, Location> {
37+
38+
public static void register(SyntaxRegistry registry) {
39+
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(ExprRegionPoints.class, Location.class)
40+
.priority(DEFAULT_PRIORITY)
41+
.addPatterns(getPatterns("(min|:max)[imum] point[s]", "worldguardregions"))
42+
.addPatterns(getPatterns("points", "worldguardregions"))
43+
.build());
44+
}
45+
46+
private boolean isMax;
47+
private boolean isAll;
48+
49+
@Override
50+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
51+
isMax = parseResult.hasTag("max");
52+
isAll = matchedPattern > 1;
53+
//noinspection unchecked
54+
setExpr((Expression<? extends WorldGuardRegion>) exprs[0]);
55+
return true;
56+
}
57+
58+
@Override
59+
protected Location[] get(Event event, WorldGuardRegion[] regions) {
60+
List<Location> locations = new ArrayList<>();
61+
if (isAll) {
62+
for (WorldGuardRegion region : regions) {
63+
int minY = region.region().getMinimumPoint().getY();
64+
region.region().getPoints().stream()
65+
.map(point -> BukkitAdapter.adapt(region.world(), point.toBlockVector3(minY)))
66+
.forEach(locations::add);
67+
}
68+
} else if (isMax) {
69+
for (WorldGuardRegion region : regions) {
70+
locations.add(BukkitAdapter.adapt(region.world(), region.region().getMaximumPoint()));
71+
}
72+
} else {
73+
for (WorldGuardRegion region : regions) {
74+
locations.add(BukkitAdapter.adapt(region.world(), region.region().getMinimumPoint()));
75+
}
76+
}
77+
return locations.toArray(new Location[0]);
78+
}
79+
80+
@Override
81+
public boolean isSingle() {
82+
return !isAll && getExpr().isSingle();
83+
}
84+
85+
@Override
86+
public Class<? extends Location> getReturnType() {
87+
return Location.class;
88+
}
89+
90+
@Override
91+
public String toString(@Nullable Event event, boolean debug) {
92+
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
93+
builder.append("the");
94+
if (isAll) {
95+
builder.append("points");
96+
} else {
97+
if (isMax) {
98+
builder.append("maximum");
99+
} else {
100+
builder.append("minimum");
101+
}
102+
if (getExpr().isSingle()) {
103+
builder.append("point");
104+
} else {
105+
builder.append("points");
106+
}
107+
}
108+
builder.append("of", getExpr());
109+
return builder.toString();
110+
}
111+
112+
}

0 commit comments

Comments
 (0)