11package org .skriptlang .skriptworldguard .elements .expressions ;
22
3+ import ch .njol .skript .Skript ;
34import ch .njol .skript .classes .Changer .ChangeMode ;
45import ch .njol .skript .doc .Description ;
56import ch .njol .skript .doc .Example ;
67import ch .njol .skript .doc .Name ;
78import ch .njol .skript .doc .Since ;
89import ch .njol .skript .expressions .base .SimplePropertyExpression ;
10+ import ch .njol .skript .lang .Expression ;
11+ import ch .njol .skript .lang .SkriptParser .ParseResult ;
12+ import ch .njol .util .Kleenean ;
913import com .sk89q .worldguard .protection .regions .ProtectedRegion ;
1014import com .sk89q .worldguard .protection .regions .ProtectedRegion .CircularInheritanceException ;
1115import org .bukkit .event .Event ;
1216import org .jetbrains .annotations .Nullable ;
1317import org .skriptlang .skript .registration .SyntaxRegistry ;
1418import org .skriptlang .skriptworldguard .worldguard .WorldGuardRegion ;
1519
20+ import java .util .ArrayList ;
21+ import java .util .List ;
22+
1623@ Name ("Region Parent" )
1724@ Description ({
18- "An expression to obtain and change the parent of a region." ,
25+ "An expression to obtain and change the direct parent of a region." ,
26+ "It is also possible to obtain all parents of a region (e.g., including a parent region's parent region)." ,
1927 "When a region has a parent, it inherits the parents members, owners, and flags (that aren't defined on the child)."
2028})
2129@ Example ("""
22- command /setparent <text>:
30+ command /setparent <text> <text> :
2331 trigger:
24- set the parents of the regions at the player to the region text-argument
32+ set the parent of the region text-argument-1 to the region text-argument-2
2533 """ )
34+ @ Example ("if any of all of the parent regions of {_region} are global" )
2635@ Since ("1.0" )
2736public class ExprRegionParent extends SimplePropertyExpression <WorldGuardRegion , WorldGuardRegion > {
2837
2938 public static void register (SyntaxRegistry registry ) {
3039 registry .register (SyntaxRegistry .EXPRESSION , infoBuilder (ExprRegionParent .class , WorldGuardRegion .class ,
31- "[region] parent[s]" , "worldguardregions" , false )
40+ "parent region [s]" , "worldguardregions" , false )
3241 .supplier (ExprRegionParent ::new )
42+ .addPattern ("all [[of] the] parent regions of %worldguardregions%" )
3343 .build ());
3444 }
3545
46+ private boolean isAll ;
47+
48+ @ Override
49+ public boolean init (Expression <?>[] expressions , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
50+ isAll = matchedPattern == 2 ;
51+ return super .init (expressions , matchedPattern , isDelayed , parseResult );
52+ }
53+
54+ @ Override
55+ protected WorldGuardRegion [] get (Event event , WorldGuardRegion [] regions ) {
56+ if (isAll ) {
57+ List <WorldGuardRegion > parents = new ArrayList <>();
58+ for (WorldGuardRegion region : regions ) {
59+ ProtectedRegion parent = region .region ().getParent ();
60+ while (parent != null ) {
61+ parents .add (new WorldGuardRegion (region .world (), parent ));
62+ parent = parent .getParent ();
63+ }
64+ }
65+ return parents .toArray (new WorldGuardRegion [0 ]);
66+ }
67+ return super .get (event , regions );
68+ }
69+
3670 @ Override
3771 public WorldGuardRegion convert (WorldGuardRegion region ) {
3872 return new WorldGuardRegion (region .world (), region .region ().getParent ());
3973 }
4074
4175 @ Override
4276 public Class <?> @ Nullable [] acceptChange (ChangeMode mode ) {
77+ if (isAll ) {
78+ Skript .error ("It is not possible to change all the parents of a region" );
79+ return null ;
80+ }
4381 return switch (mode ) {
4482 case SET , DELETE , RESET -> new Class []{WorldGuardRegion .class };
4583 default -> null ;
@@ -60,14 +98,22 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
6098 }
6199 }
62100
101+ @ Override
102+ public Class <? extends WorldGuardRegion > getReturnType () {
103+ return WorldGuardRegion .class ;
104+ }
105+
63106 @ Override
64107 protected String getPropertyName () {
65108 return "parent" ;
66109 }
67110
68111 @ Override
69- public Class <? extends WorldGuardRegion > getReturnType () {
70- return WorldGuardRegion .class ;
112+ public String toString (@ Nullable Event event , boolean debug ) {
113+ if (isAll ) {
114+ return "all the parent regions of " + getExpr ().toString (event , debug );
115+ }
116+ return super .toString (event , debug );
71117 }
72118
73119}
0 commit comments