Skip to content

Commit 09325cf

Browse files
Add support for obtaining all regions of a world
1 parent 57720b3 commit 09325cf

File tree

2 files changed

+68
-31
lines changed

2 files changed

+68
-31
lines changed

src/main/java/org/skriptlang/skriptworldguard/elements/expressions/ExprRegionNamed.java renamed to src/main/java/org/skriptlang/skriptworldguard/elements/expressions/ExprRegions.java

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import ch.njol.skript.doc.Description;
44
import ch.njol.skript.doc.Example;
55
import ch.njol.skript.doc.Name;
6-
import ch.njol.skript.doc.RequiredPlugins;
76
import ch.njol.skript.doc.Since;
87
import ch.njol.skript.lang.Expression;
98
import ch.njol.skript.lang.SkriptParser.ParseResult;
109
import ch.njol.skript.lang.SyntaxStringBuilder;
1110
import ch.njol.skript.lang.util.SimpleExpression;
1211
import ch.njol.util.Kleenean;
12+
import org.bukkit.Bukkit;
1313
import org.jetbrains.annotations.Nullable;
1414
import org.skriptlang.skript.registration.SyntaxInfo;
1515
import org.skriptlang.skript.registration.SyntaxRegistry;
@@ -23,55 +23,69 @@
2323

2424
@Name("Regions")
2525
@Description({
26-
"An expression that obtains a region from an ID and world.",
27-
"Please note that region IDs are case insensitive."
26+
"An expression to obtain the regions of all worlds, a specific world, or the region with a specific name in a world.",
27+
"Please note that region names (IDs) are case insensitive."
2828
})
2929
@Example("the region \"region\" in world(\"world\"")
30-
@RequiredPlugins("WorldGuard 7")
30+
@Example("all of the regions in the player's world")
3131
@Since("1.0")
32-
public class ExprRegionNamed extends SimpleExpression<WorldGuardRegion> {
32+
public class ExprRegions extends SimpleExpression<WorldGuardRegion> {
3333

3434
public static void register(SyntaxRegistry registry) {
35-
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(ExprRegionNamed.class, WorldGuardRegion.class)
36-
.supplier(ExprRegionNamed::new)
35+
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(ExprRegions.class, WorldGuardRegion.class)
36+
.supplier(ExprRegions::new)
3737
.addPatterns("[the] [worldguard] region[s] [named] %strings% [(in|of) %world%]",
38-
"[the] [worldguard] region[s] with [the] (name[s]|id[s]) %strings% [(in|of) %world%]")
38+
"[the] [worldguard] region[s] with [the] (name[s]|id[s]) %strings% [(in|of) %world%]",
39+
"[all [[of] the]|the] [worldguard] regions [(in|of) %-worlds%]")
3940
.build());
4041
}
4142

42-
private Expression<String> ids;
43-
private Expression<World> world;
43+
private @Nullable Expression<String> ids;
44+
private @Nullable Expression<World> worlds;
4445

4546
@Override
4647
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
48+
if (exprs.length == 2) {
49+
//noinspection unchecked
50+
ids = (Expression<String>) exprs[0];
51+
}
4752
//noinspection unchecked
48-
ids = (Expression<String>) exprs[0];
49-
//noinspection unchecked
50-
world = (Expression<World>) exprs[1];
53+
worlds = (Expression<World>) exprs[exprs.length - 1];
5154
return true;
5255
}
5356

5457
@Override
5558
protected WorldGuardRegion [] get(Event event) {
56-
World world = this.world.getSingle(event);
57-
if (world == null) {
58-
return new WorldGuardRegion[0];
59-
}
60-
6159
List<WorldGuardRegion> regions = new ArrayList<>();
62-
for (String id : ids.getArray(event)) {
63-
WorldGuardRegion region = RegionUtils.getRegion(world, id);
64-
if (region != null) {
65-
regions.add(region);
60+
if (ids == null) {
61+
World[] worlds;
62+
if (this.worlds == null) {
63+
worlds = Bukkit.getWorlds().toArray(new World[0]);
64+
} else {
65+
worlds = this.worlds.getArray(event);
66+
}
67+
for (World world : worlds) {
68+
regions.addAll(RegionUtils.getRegions(world));
69+
}
70+
} else {
71+
assert worlds != null;
72+
World world = this.worlds.getSingle(event); // single for this pattern
73+
if (world == null) {
74+
return new WorldGuardRegion[0];
75+
}
76+
for (String id : ids.getArray(event)) {
77+
WorldGuardRegion region = RegionUtils.getRegion(world, id);
78+
if (region != null) {
79+
regions.add(region);
80+
}
6681
}
6782
}
68-
6983
return regions.toArray(new WorldGuardRegion[0]);
7084
}
7185

7286
@Override
7387
public boolean isSingle() {
74-
return ids.isSingle();
88+
return ids != null && ids.isSingle();
7589
}
7690

7791
@Override
@@ -82,20 +96,25 @@ public Class<? extends WorldGuardRegion> getReturnType() {
8296
@Override
8397
public String toString(@Nullable Event event, boolean debug) {
8498
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
85-
boolean isSingle = ids.isSingle();
99+
boolean isSingle = isSingle();
86100
builder.append("the");
87101
if (isSingle) {
88102
builder.append("region");
89103
} else {
90104
builder.append("regions");
91105
}
92-
builder.append("with the");
93-
if (isSingle) {
94-
builder.append("id");
95-
} else {
96-
builder.append("ids");
106+
if (ids != null) {
107+
builder.append("with the");
108+
if (isSingle) {
109+
builder.append("id");
110+
} else {
111+
builder.append("ids");
112+
}
113+
builder.append(ids);
114+
}
115+
if (worlds != null) {
116+
builder.append("in", worlds);
97117
}
98-
builder.append(ids, "in", world);
99118
return builder.toString();
100119
}
101120

src/main/java/org/skriptlang/skriptworldguard/worldguard/RegionUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bukkit.block.Block;
2323
import org.bukkit.entity.Player;
2424
import org.jetbrains.annotations.Nullable;
25+
import org.jetbrains.annotations.Unmodifiable;
2526

2627
import java.util.ArrayList;
2728
import java.util.Arrays;
@@ -59,6 +60,23 @@ public static RegionContainer getRegionContainer() {
5960
return getRegionContainer().get(BukkitAdapter.adapt(world));
6061
}
6162

63+
/**
64+
* A helper method to simplify getting the regions of a world.
65+
* @param world The world to obtain regions of.
66+
* @return The regions of {@code world}.
67+
* If {@link #getRegionManager(World)} is unavailable, this method returns an empty list.
68+
*/
69+
public static @Unmodifiable List<WorldGuardRegion> getRegions(World world) {
70+
RegionManager regionManager = getRegionManager(world);
71+
if (regionManager == null) {
72+
return List.of();
73+
}
74+
75+
return regionManager.getRegions().values().stream()
76+
.map(region -> new WorldGuardRegion(world, region))
77+
.toList();
78+
}
79+
6280
/**
6381
* A helper method to simplify getting a region in a world.
6482
* @param world The world of the region.

0 commit comments

Comments
 (0)