11package org .skriptlang .skriptworldguard .elements .conditions ;
22
3+ import ch .njol .skript .conditions .base .PropertyCondition ;
4+ import ch .njol .skript .conditions .base .PropertyCondition .PropertyType ;
35import ch .njol .skript .doc .Description ;
4- import ch .njol .skript .doc .Examples ;
6+ import ch .njol .skript .doc .Example ;
57import ch .njol .skript .doc .Name ;
68import ch .njol .skript .doc .RequiredPlugins ;
79import ch .njol .skript .doc .Since ;
810import ch .njol .skript .lang .Condition ;
911import ch .njol .skript .lang .Expression ;
1012import ch .njol .skript .lang .SkriptParser .ParseResult ;
13+ import ch .njol .skript .lang .SyntaxStringBuilder ;
1114import ch .njol .skript .lang .util .SimpleExpression ;
1215import ch .njol .skript .util .Direction ;
1316import ch .njol .util .Kleenean ;
1417import org .bukkit .Location ;
1518import org .bukkit .entity .Player ;
1619import org .bukkit .event .Event ;
17- import org .jetbrains .annotations .NotNull ;
1820import org .jetbrains .annotations .Nullable ;
1921import org .skriptlang .skript .registration .SyntaxInfo ;
2022import org .skriptlang .skript .registration .SyntaxRegistry ;
2123import org .skriptlang .skriptworldguard .worldguard .RegionUtils ;
2224import 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- " \t description: set the block at your crosshair to a different type" ,
29- " \t trigger:" ,
30- " \t \t if the player cannot build at the targeted block:" ,
31- " \t \t \t message \ " <red>You do not have permission to change blocks there! \" " ,
32- " \t \t else:" ,
33- " \t \t \t set 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" )
3739public 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}
0 commit comments