Skip to content

Commit 9d2f34b

Browse files
Significant class cleanup
1 parent 29966c2 commit 9d2f34b

18 files changed

+424
-389
lines changed

src/main/java/org/skriptlang/skriptworldguard/SkriptWorldGuard.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public boolean canParse(@NotNull ParseContext context) {
133133
@Override
134134
public @NotNull Fields serialize(WorldGuardRegion region) {
135135
Fields fields = new Fields();
136-
fields.putObject("world", region.getWorld());
137-
fields.putObject("id", region.getRegion().getId());
136+
fields.putObject("world", region.world());
137+
fields.putObject("id", region.region().getId());
138138
return fields;
139139
}
140140

@@ -169,7 +169,7 @@ protected boolean canBeInstantiated() {
169169
}));
170170

171171
Classes.registerClass(new EnumClassInfo<>(MoveType.class, "worldguardmovetype", "worldguard move types")
172-
.user("worldguard ?move ?types?")
172+
.user("worldguard ?move(ment)? ?types?")
173173
.name("WorldGuard Move Type")
174174
.description("The move type in a WorldGuard enter/leave event.")
175175
.requiredPlugins("WorldGuard 7")
Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
package org.skriptlang.skriptworldguard.elements.conditions;
22

3+
import ch.njol.skript.conditions.base.PropertyCondition;
4+
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
35
import ch.njol.skript.doc.Description;
4-
import ch.njol.skript.doc.Examples;
6+
import ch.njol.skript.doc.Example;
57
import ch.njol.skript.doc.Name;
68
import ch.njol.skript.doc.RequiredPlugins;
79
import ch.njol.skript.doc.Since;
810
import ch.njol.skript.lang.Condition;
911
import ch.njol.skript.lang.Expression;
1012
import ch.njol.skript.lang.SkriptParser.ParseResult;
13+
import ch.njol.skript.lang.SyntaxStringBuilder;
1114
import ch.njol.skript.lang.util.SimpleExpression;
1215
import ch.njol.skript.util.Direction;
1316
import ch.njol.util.Kleenean;
1417
import org.bukkit.Location;
1518
import org.bukkit.entity.Player;
1619
import org.bukkit.event.Event;
17-
import org.jetbrains.annotations.NotNull;
1820
import org.jetbrains.annotations.Nullable;
1921
import org.skriptlang.skript.registration.SyntaxInfo;
2022
import org.skriptlang.skript.registration.SyntaxRegistry;
2123
import org.skriptlang.skriptworldguard.worldguard.RegionUtils;
2224
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;
2325

2426
@Name("Can Build In Regions")
25-
@Description("A condition that tests whether the given players can build in the given regions or the regions of the given locations.")
26-
@Examples({
27-
"command /setblock <material>:",
28-
"\tdescription: set the block at your crosshair to a different type",
29-
"\ttrigger:",
30-
"\t\tif the player cannot build at the targeted block:",
31-
"\t\t\tmessage \"<red>You do not have permission to change blocks there!\"",
32-
"\t\telse:",
33-
"\t\t\tset the targeted block to argument"
34-
})
27+
@Description("A condition to test whether a player can build in a region or at a specific location.")
28+
@Example("""
29+
command /setblock <material>:
30+
description: Sets your targeted block to a different type.
31+
trigger:
32+
if the player cannot build at the targeted block:
33+
message "<red>You do not have permission to modify your targeted block!"
34+
else:
35+
set the targeted block to the first argument
36+
""")
3537
@RequiredPlugins("WorldGuard 7")
3638
@Since("1.0")
3739
public class CondCanBuildInRegions extends Condition {
3840

3941
public static void register(SyntaxRegistry registry) {
4042
registry.register(SyntaxRegistry.CONDITION, SyntaxInfo.builder(CondCanBuildInRegions.class)
4143
.supplier(CondCanBuildInRegions::new)
42-
.addPatterns("%players% (can|(is|are) allowed to) build (%-directions% %-locations%|[in] %-worldguardregions%)",
43-
"%players% (can('t|not)|(is|are)(n't| not) allowed to) build (%-directions% %-locations%|[in] %-worldguardregions%)")
44+
.addPatterns(PropertyCondition.getPatterns(PropertyType.BE, "allowed to build (%-directions% %-locations%|[in] %-worldguardregions%)", "players"))
45+
.addPatterns(PropertyCondition.getPatterns(PropertyType.CAN, "build (%-directions% %-locations%|[in] %-worldguardregions%)", "players"))
4446
.build());
4547
}
4648

@@ -49,47 +51,44 @@ public static void register(SyntaxRegistry registry) {
4951
private Expression<WorldGuardRegion> regions;
5052

5153
@Override
52-
@SuppressWarnings("unchecked")
53-
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
54+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
55+
//noinspection unchecked
5456
players = (Expression<Player>) exprs[0];
55-
if (exprs[1] != null) { // We are using directions and locations
56-
locations = Direction.combine(
57-
(Expression<? extends Direction>) exprs[1],
58-
(Expression<? extends Location>) exprs[2]
59-
);
57+
if (exprs[1] != null) {
58+
//noinspection unchecked
59+
locations = Direction.combine((Expression<? extends Direction>) exprs[1], (Expression<? extends Location>) exprs[2]);
6060
} else {
61+
//noinspection unchecked
6162
regions = (Expression<WorldGuardRegion>) exprs[3];
6263
}
63-
setNegated(matchedPattern == 1);
64+
setNegated(matchedPattern % 2 == 0);
6465
return true;
6566
}
6667

6768
@Override
68-
public boolean check(@NotNull Event event) {
69+
public boolean check(Event event) {
6970
if (locations != null) {
7071
Location[] locations = this.locations.getAll(event); // get all to avoid double-eval + permit or lists.
71-
return players.check(event, player -> SimpleExpression.check(
72-
locations,
73-
location -> RegionUtils.canBuild(player, location),
74-
false,
75-
this.locations.getAnd()
76-
), isNegated());
72+
return players.check(event, player -> SimpleExpression.check(locations,
73+
location -> RegionUtils.canBuild(player, location), false, this.locations.getAnd()), isNegated());
7774
} else {
7875
assert regions != null;
7976
WorldGuardRegion[] regions = this.regions.getAll(event); // get all to avoid double-eval + permit or lists.
80-
return players.check(event, player -> SimpleExpression.check(
81-
regions,
82-
region -> RegionUtils.canBuild(player, region),
83-
false,
84-
this.regions.getAnd()
85-
), isNegated());
77+
return players.check(event, player -> SimpleExpression.check(regions,
78+
region -> RegionUtils.canBuild(player, region), false, this.regions.getAnd()), isNegated());
8679
}
8780
}
8881

8982
@Override
90-
public @NotNull String toString(@Nullable Event event, boolean debug) {
91-
assert (locations != null && regions == null) || (locations == null && regions != null);
92-
return players.toString(event, debug) + " can build " + ((locations != null ? locations : regions).toString(event, debug));
83+
public String toString(@Nullable Event event, boolean debug) {
84+
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
85+
builder.append(players, "can build");
86+
if (locations != null) {
87+
builder.append(locations);
88+
} else {
89+
builder.append(regions);
90+
}
91+
return builder.toString();
9392
}
9493

9594
}

src/main/java/org/skriptlang/skriptworldguard/elements/conditions/CondIsMemberOwner.java

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
import ch.njol.skript.conditions.base.PropertyCondition;
44
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
55
import ch.njol.skript.doc.Description;
6-
import ch.njol.skript.doc.Examples;
6+
import ch.njol.skript.doc.Example;
77
import ch.njol.skript.doc.Name;
88
import ch.njol.skript.doc.RequiredPlugins;
99
import ch.njol.skript.doc.Since;
1010
import ch.njol.skript.lang.Condition;
1111
import ch.njol.skript.lang.Expression;
1212
import ch.njol.skript.lang.SkriptParser.ParseResult;
13+
import ch.njol.skript.lang.SyntaxStringBuilder;
1314
import ch.njol.skript.lang.util.SimpleExpression;
1415
import ch.njol.util.Kleenean;
15-
import com.sk89q.worldguard.LocalPlayer;
16-
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
17-
import org.jetbrains.annotations.NotNull;
16+
import com.sk89q.worldguard.domains.DefaultDomain;
1817
import org.jetbrains.annotations.Nullable;
1918
import org.skriptlang.skript.registration.SyntaxInfo;
2019
import org.skriptlang.skript.registration.SyntaxRegistry;
@@ -23,17 +22,18 @@
2322
import org.bukkit.event.Event;
2423

2524
@Name("Is Member/Owner of Region")
26-
@Description("A condition that tests whether the given players or groups are a member or owner of the given regions.")
27-
@Examples({
28-
"on region enter:",
29-
"\tplayer is the owner of the region",
30-
"\tmessage \"Welcome back to %region%!\"",
31-
"\tsend \"%player% just entered %region%!\" to the members of the region"
32-
})
25+
@Description("A condition to test whether a player/group is a member/owner of a region.")
26+
@Example("""
27+
on region enter:
28+
player is the owner of the region
29+
message "Welcome back to %region%"
30+
message "%player's name% just entered %region%" to the members of the region
31+
""")
3332
@RequiredPlugins("WorldGuard 7")
3433
@Since("1.0")
3534
public class CondIsMemberOwner extends Condition {
3635

36+
// TODO 'direct' flag for whether or not to consider parent regions?
3737
public static void register(SyntaxRegistry registry) {
3838
registry.register(SyntaxRegistry.CONDITION, SyntaxInfo.builder(CondIsMemberOwner.class)
3939
.supplier(CondIsMemberOwner::new)
@@ -45,49 +45,71 @@ public static void register(SyntaxRegistry registry) {
4545

4646
private Expression<Object> users;
4747
private Expression<WorldGuardRegion> regions;
48-
private boolean owner;
48+
private boolean isOwner;
4949

5050
@Override
51-
@SuppressWarnings("unchecked")
52-
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
51+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
52+
//noinspection unchecked
5353
users = (Expression<Object>) exprs[0];
54+
//noinspection unchecked
5455
regions = (Expression<WorldGuardRegion>) exprs[1];
55-
owner = parseResult.hasTag("owner");
56+
isOwner = parseResult.hasTag("owner");
5657
setNegated(matchedPattern == 1);
5758
return true;
5859
}
5960

6061
@Override
61-
public boolean check(@NotNull Event event) {
62+
public boolean check(Event event) {
6263
WorldGuardRegion[] regions = this.regions.getAll(event);
63-
return users.check(event, user -> {
64-
if (user instanceof OfflinePlayer) {
65-
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapOfflinePlayer((OfflinePlayer) user);
66-
return SimpleExpression.check(
67-
regions,
68-
region -> owner ? region.getRegion().isOwner(localPlayer) : region.getRegion().isMember(localPlayer),
69-
false,
70-
this.regions.getAnd()
71-
);
72-
} else { // It's a String (group)
73-
String group = (String) user;
74-
return SimpleExpression.check(
75-
regions,
76-
region -> (owner ? region.getRegion().getOwners() : region.getRegion().getMembers()).getGroups().contains(group),
77-
false,
78-
this.regions.getAnd()
79-
);
80-
}
81-
}, isNegated());
64+
return users.check(event, user -> SimpleExpression.check(regions,
65+
region -> check(region, isOwner, user), false, this.regions.getAnd()), isNegated());
66+
}
67+
68+
private static boolean check(WorldGuardRegion region, boolean owner, Object object) {
69+
DefaultDomain domain;
70+
if (owner) {
71+
domain = region.region().getOwners();
72+
} else {
73+
domain = region.region().getMembers();
74+
}
75+
if (object instanceof OfflinePlayer player) {
76+
return domain.contains(player.getUniqueId());
77+
} else if (object instanceof String group) {
78+
return domain.getGroups().contains(group);
79+
} else {
80+
throw new IllegalArgumentException("object must be OfflinePlayer or String");
81+
}
8282
}
8383

8484
@Override
85-
public @NotNull String toString(@Nullable Event event, boolean debug) {
85+
public String toString(@Nullable Event event, boolean debug) {
86+
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
8687
boolean isSingle = users.isSingle();
87-
return users.toString(event, debug) + " " + (isSingle ? "is" : "are")
88-
+ (isNegated() ? " not" : "")
89-
+ " the " + (owner ? "owner" : "member") + (isSingle ? "" : "s")
90-
+ " of " + regions.toString(event, debug);
88+
builder.append(users);
89+
if (isSingle) {
90+
builder.append("is");
91+
} else {
92+
builder.append("are");
93+
}
94+
if (isNegated()) {
95+
builder.append("not");
96+
}
97+
builder.append("the");
98+
if (isOwner) {
99+
if (isSingle) {
100+
builder.append("owner");
101+
} else {
102+
builder.append("owners");
103+
}
104+
} else {
105+
if (isSingle) {
106+
builder.append("member");
107+
} else {
108+
builder.append("members");
109+
}
110+
}
111+
builder.append("of", regions);
112+
return builder.toString();
91113
}
92114

93115
}

src/main/java/org/skriptlang/skriptworldguard/elements/conditions/CondIsValidId.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
import ch.njol.skript.conditions.base.PropertyCondition;
44
import ch.njol.skript.doc.Description;
5-
import ch.njol.skript.doc.Examples;
5+
import ch.njol.skript.doc.Example;
66
import ch.njol.skript.doc.Name;
77
import ch.njol.skript.doc.RequiredPlugins;
88
import ch.njol.skript.doc.Since;
99
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
10-
import org.jetbrains.annotations.NotNull;
1110
import org.skriptlang.skript.registration.SyntaxRegistry;
1211

1312
@Name("Is Valid Region ID")
1413
@Description({
15-
"A condition that tests whether the given string(s) is/are a valid region id.",
16-
"Region IDs are only valid if they contain letters, numbers, underscores, commas, single quotation marks, dashes, pluses, or forward slashes."
14+
"A condition to test whether a string is a valid region ID.",
15+
"Valid region IDs only contain letters, numbers, underscores, commas, single quotation marks, dashes, pluses, or forward slashes."
1716
})
18-
@Examples("send \"I am a valid region ID!\" if \"global_region\" is a valid region id")
17+
@Example("""
18+
command createregion <text>:
19+
if the text-argument is not a valid region id:
20+
message "<red>'%text-argument%' is not a valid region ID")
21+
# here is where the rest of the command would go :)
22+
""")
1923
@RequiredPlugins("WorldGuard 7")
2024
@Since("1.0")
2125
public class CondIsValidId extends PropertyCondition<String> {
@@ -30,7 +34,7 @@ public boolean check(String id) {
3034
}
3135

3236
@Override
33-
protected @NotNull String getPropertyName() {
37+
protected String getPropertyName() {
3438
return "valid region id";
3539
}
3640

0 commit comments

Comments
 (0)