Skip to content

Commit 41cffcb

Browse files
Make height arguments optional for polygonal region creation
1 parent 7b826a6 commit 41cffcb

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/main/java/org/skriptlang/skriptworldguard/elements/effects/EffCreateRegion.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
" At least three points must be provided to create a polygonal region.",
3939
"Note that if you do not specify the world for a region, you must be sure that the locations provided all have the same world.",
4040
"Note that Region IDs are only valid if they contain letters, numbers, underscores, commas, single quotation marks, dashes, pluses, and forward slashes.",
41-
"Note that if you attempt to create a region in a world where a region with the same ID already exists, that region will be replaced."
41+
"Note that if you attempt to create a region in a world where a region with the same ID already exists, that region will be replaced.",
42+
"Note that if you do not specify the minimum and maximum heights for a polygonal region, those values will be calculated from the points."
4243
})
4344
@Example("create a temporary global region named \"temporary_global_region\" in the player's world")
4445
@Example("create region \"cuboid_region\" in player's world between the location (0, 60, 0) and the location (10, 70, 10)")
@@ -51,7 +52,7 @@ public static void register(SyntaxRegistry registry) {
5152
.supplier(EffCreateRegion::new)
5253
.addPatterns("create [a] [:temporary] global [worldguard] region [named] %string% [in %world%]",
5354
"create [a] [:temporary] [cuboid|rectangular] [worldguard] region [named] %string% [in %-world%] (between|from) %location% (to|and) %location%",
54-
"create [a] [:temporary] polygonal [worldguard] region [named] %string% [in %-world%] with [a] min[imum] height of %number% and [a] max[imum] height of %number% with [the] points %locations%")
55+
"create [a] [:temporary] polygonal [worldguard] region [named] %string% [in %-world%] [with [a] min[imum] height of %-integer% and [a] max[imum] height of %-integer%] with [the] points %locations%")
5556
.build());
5657
}
5758

@@ -63,8 +64,8 @@ public static void register(SyntaxRegistry registry) {
6364
private @Nullable Expression<Location> firstCorner;
6465
private @Nullable Expression<Location> secondCorner;
6566
// Polygonal Region Values
66-
private @Nullable Expression<Number> minY;
67-
private @Nullable Expression<Number> maxY;
67+
private @Nullable Expression<Integer> minY;
68+
private @Nullable Expression<Integer> maxY;
6869
private @Nullable Expression<Location> points;
6970

7071
@Override
@@ -77,8 +78,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
7778
firstCorner = (Expression<Location>) exprs[2];
7879
secondCorner = (Expression<Location>) exprs[3];
7980
} else if (matchedPattern == 2) { // Polygonal Region
80-
minY = (Expression<Number>) exprs[2];
81-
maxY = (Expression<Number>) exprs[3];
81+
minY = (Expression<Integer>) exprs[2];
82+
maxY = (Expression<Integer>) exprs[3];
8283
points = (Expression<Location>) exprs[4];
8384
}
8485
return true;
@@ -121,20 +122,41 @@ protected void execute(Event event) {
121122
}
122123

123124
region = new ProtectedCuboidRegion(id, temporary,
124-
BukkitAdapter.asBlockVector(firstCorner),BukkitAdapter.asBlockVector(secondCorner));
125-
} else if (minY != null) { // Polygonal Region
126-
assert this.maxY != null && this.points != null;
127-
Number minY = this.minY.getSingle(event);
128-
Number maxY = this.maxY.getSingle(event);
125+
BukkitAdapter.asBlockVector(firstCorner), BukkitAdapter.asBlockVector(secondCorner));
126+
} else if (points != null) { // Polygonal Region
129127
Location[] points = this.points.getArray(event);
130-
if (minY == null || maxY == null || points.length == 0) {
128+
if (points.length == 0) {
131129
return;
132130
}
133131
if (points.length < 3) {
134132
error("A polygonal region needs at least 3 points, but only " + points.length + " points were provided");
135133
return;
136134
}
137135

136+
int minY;
137+
int maxY;
138+
if (this.minY != null) {
139+
assert this.maxY != null;
140+
Integer minYInteger = this.minY.getSingle(event);
141+
Integer maxYInteger = this.maxY.getSingle(event);
142+
if (minYInteger == null || maxYInteger == null) {
143+
return;
144+
}
145+
minY = minYInteger;
146+
maxY = maxYInteger;
147+
if (minY > maxY) {
148+
error("The provided minimum height cannot be greater than the maximum height");
149+
return;
150+
}
151+
} else {
152+
minY = points[0].getBlockY();
153+
maxY = minY;
154+
for (Location point : points) {
155+
minY = Math.min(minY, point.getBlockY());
156+
maxY = Math.max(maxY, point.getBlockY());
157+
}
158+
}
159+
138160
if (world == null) { // Okay... we can try to get one from the locations
139161
world = points[0].getWorld();
140162
for (Location point : points) { // We want the locations to have matching worlds
@@ -154,7 +176,7 @@ protected void execute(Event event) {
154176
pointVectors.add(BlockVector2.at(point.getBlockX(), point.getBlockZ()));
155177
}
156178

157-
region = new ProtectedPolygonalRegion(id, temporary, pointVectors, minY.intValue(), maxY.intValue());
179+
region = new ProtectedPolygonalRegion(id, temporary, pointVectors, minY, maxY);
158180
} else { // Global Region
159181
if (world == null) { // This is a global region, so there are no locations to use as a backup
160182
return;
@@ -182,13 +204,16 @@ public String toString(@Nullable Event event, boolean debug) {
182204
builder.append("in", world);
183205
}
184206
builder.append("between", firstCorner, "and", secondCorner);
185-
} else if (minY != null) { // Polygonal region
186-
assert maxY != null && points != null;
207+
} else if (points != null) { // Polygonal region
187208
builder.append("polygonal region named", id);
188209
if (world != null) {
189210
builder.append("in", world);
190211
}
191-
builder.append("with a minimum height of", minY, "and a maximum height of", maxY, "with the points", points);
212+
if (minY != null) {
213+
assert maxY != null;
214+
builder.append("with a minimum height of", minY, "and a maximum height of", maxY);
215+
}
216+
builder.append("with the points", points);
192217
} else { // Global region
193218
assert world != null;
194219
builder.append("global region named", id, "in", world);

0 commit comments

Comments
 (0)