Skip to content

Commit d49a572

Browse files
Add region parent support
1 parent 4f58735 commit d49a572

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.skriptlang.skriptworldguard.elements.expressions;
2+
3+
import ch.njol.skript.classes.Changer.ChangeMode;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Example;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
8+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
9+
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
10+
import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException;
11+
import org.bukkit.event.Event;
12+
import org.jetbrains.annotations.Nullable;
13+
import org.skriptlang.skript.registration.SyntaxRegistry;
14+
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;
15+
16+
@Name("Region Parent")
17+
@Description({
18+
"An expression to obtain and change the parent of a region.",
19+
"When a region has a parent, it inherits the parents members, owners, and flags (that aren't defined on the child)."
20+
})
21+
@Example("""
22+
command /setparent <text>:
23+
trigger:
24+
set the parents of the regions at the player to the region text-argument
25+
""")
26+
@Since("1.0")
27+
public class ExprRegionParent extends SimplePropertyExpression<WorldGuardRegion, WorldGuardRegion> {
28+
29+
public static void register(SyntaxRegistry registry) {
30+
register(registry, ExprRegionParent.class, WorldGuardRegion.class,
31+
"[region] parent[s]", "worldguardregions");
32+
}
33+
34+
@Override
35+
public WorldGuardRegion convert(WorldGuardRegion region) {
36+
return new WorldGuardRegion(region.world(), region.region().getParent());
37+
}
38+
39+
@Override
40+
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
41+
return switch (mode) {
42+
case SET, DELETE, RESET -> new Class[]{WorldGuardRegion.class};
43+
default -> null;
44+
};
45+
}
46+
47+
@Override
48+
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
49+
ProtectedRegion newRegion = delta != null ? ((WorldGuardRegion) delta[0]).region() : null;
50+
for (WorldGuardRegion region : getExpr().getArray(event)) {
51+
try {
52+
region.region().setParent(newRegion);
53+
} catch (CircularInheritanceException e) {
54+
assert delta != null;
55+
error("Circular region inheritance detected. '" + delta[0] + "' has '" +
56+
region + "' as an existing parent.");
57+
}
58+
}
59+
}
60+
61+
@Override
62+
protected String getPropertyName() {
63+
return "parent";
64+
}
65+
66+
@Override
67+
public Class<? extends WorldGuardRegion> getReturnType() {
68+
return WorldGuardRegion.class;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)