77import ch .njol .skript .lang .Expression ;
88import ch .njol .skript .lang .SkriptParser .ParseResult ;
99import ch .njol .util .Kleenean ;
10+ import ch .njol .util .Math2 ;
1011import ch .njol .util .coll .CollectionUtils ;
1112import org .bukkit .block .Block ;
1213import org .bukkit .block .data .BlockData ;
1314import org .bukkit .block .data .Brushable ;
1415import org .bukkit .event .Event ;
1516import org .jetbrains .annotations .Nullable ;
1617
17- @ Name ("Dusted Stage" )
18- @ Description ({
19- "Represents how far the block has been uncovered." ,
20- "The only blocks that can currently be \" dusted\" are Suspicious Gravel and Suspicious Sand."
21- })
22- @ Example ("send target block's maximum dusted stage" )
23- @ Example ("set {_sand}'s dusted stage to 2" )
18+ @ Name ("Brushing Stage" )
19+ @ Description ("""
20+ Represents how far the block has been uncovered.
21+ The only blocks that can currently be "brushed" are Suspicious Gravel and Suspicious Sand.
22+ 0 means the block is untouched, the max (usually 3) means nearly fulled brushed.
23+ Resetting this value will set it to 0.
24+ """ )
25+ @ Example ("""
26+ # prevent dusting past level 1
27+ on player change block:
28+ if dusting progress of future event-blockdata > 1:
29+ cancel event
30+ """ )
31+ @ Example ("""
32+ # draw particles when dusting is complete!
33+ on player change block:
34+ if brushing progress of event-block is max brushing stage of event-block:
35+ draw 20 totem of undying particles at event-block
36+ """ )
2437@ Since ("2.12" )
25- @ RequiredPlugins ( "Minecraft 1.20+" )
38+ @ Keywords ({ "brush" , "brushing" , "dusting" } )
2639public class ExprDustedStage extends PropertyExpression <Object , Integer > {
2740
28- private static final boolean SUPPORTS_DUSTING = Skript .classExists ("org.bukkit.block.data.Brushable" );
29-
3041 static {
31- if (SUPPORTS_DUSTING )
32- register (ExprDustedStage .class , Integer .class ,
33- "[:max[imum]] dust[ed|ing] (value|stage|progress[ion])" ,
34- "blocks/blockdatas" );
42+ register (ExprDustedStage .class , Integer .class ,
43+ "[:max[imum]] (dust|brush)[ed|ing] (value|stage|progress[ion])" ,
44+ "blocks/blockdatas" );
3545 }
3646
3747 private boolean isMax ;
3848
3949 @ Override
4050 public boolean init (Expression <?>[] exprs , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
41- setExpr (( Expression < Block >) exprs [0 ]);
51+ setExpr (exprs [0 ]);
4252 isMax = parseResult .hasTag ("max" );
4353 return true ;
4454 }
@@ -70,7 +80,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
7080 @ Override
7181 public void change (Event event , Object @ Nullable [] delta , ChangeMode mode ) {
7282 if (isMax ) return ;
73- Integer value = ( delta != null && delta . length > 0 ) ? (Integer ) delta [0 ] : null ;
83+ int value = delta == null ? 0 : (Integer ) delta [0 ];
7484
7585 for (Object obj : getExpr ().getArray (event )) {
7686 Brushable brushable = getBrushable (obj );
@@ -79,35 +89,15 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
7989
8090 int currentValue = brushable .getDusted ();
8191 int maxValue = brushable .getMaximumDusted ();
82- int newValue = currentValue ;
83-
84- switch (mode ) {
85- case SET -> {
86- if (value != null ) {
87- newValue = value ;
88- }
89- }
90- case ADD -> {
91- if (value != null ) {
92- newValue = currentValue + value ;
93- }
94- }
95- case REMOVE -> {
96- if (value != null ) {
97- newValue = currentValue - value ;
98- }
99- }
100- case RESET -> newValue = 0 ;
101- default -> {
102- return ;
103- }
104- }
105-
106- newValue = Math .max (0 , Math .min (newValue , maxValue ));
107-
108- brushable .setDusted (newValue );
109- if (obj instanceof Block ) {
110- ((Block ) obj ).setBlockData (brushable );
92+ long newValue = switch (mode ) {
93+ case SET , RESET -> value ;
94+ case ADD -> Math2 .addClamped (currentValue , value );
95+ case REMOVE -> Math2 .addClamped (currentValue , -value );
96+ default -> throw new IllegalArgumentException ("Change mode " + mode + " is not valid for ExprDustedStage!" );
97+ };
98+ brushable .setDusted (Math .clamp (newValue , 0 , maxValue ));
99+ if (obj instanceof Block block ) {
100+ block .setBlockData (brushable );
111101 }
112102 }
113103 }
@@ -116,15 +106,14 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
116106 private Brushable getBrushable (Object obj ) {
117107 if (obj instanceof Block block ) {
118108 BlockData blockData = block .getBlockData ();
119- if (blockData instanceof Brushable )
120- return ( Brushable ) blockData ;
109+ if (blockData instanceof Brushable brushable )
110+ return brushable ;
121111 } else if (obj instanceof Brushable brushable ) {
122112 return brushable ;
123113 }
124114 return null ;
125115 }
126116
127-
128117 @ Override
129118 public Class <? extends Integer > getReturnType () {
130119 return Integer .class ;
0 commit comments