33import ch .njol .skript .doc .Description ;
44import ch .njol .skript .doc .Example ;
55import ch .njol .skript .doc .Name ;
6- import ch .njol .skript .doc .RequiredPlugins ;
76import ch .njol .skript .doc .Since ;
87import ch .njol .skript .lang .Expression ;
98import ch .njol .skript .lang .SkriptParser .ParseResult ;
109import ch .njol .skript .lang .SyntaxStringBuilder ;
1110import ch .njol .skript .lang .util .SimpleExpression ;
1211import ch .njol .util .Kleenean ;
12+ import org .bukkit .Bukkit ;
1313import org .jetbrains .annotations .Nullable ;
1414import org .skriptlang .skript .registration .SyntaxInfo ;
1515import org .skriptlang .skript .registration .SyntaxRegistry ;
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
0 commit comments