Skip to content

Commit bd0a2e4

Browse files
Merge branch 'dev/patch' into dev/feature
2 parents b8676aa + 6ad6073 commit bd0a2e4

File tree

2 files changed

+83
-48
lines changed

2 files changed

+83
-48
lines changed

src/main/java/ch/njol/skript/expressions/ExprDustedStage.java

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,48 @@
77
import ch.njol.skript.lang.Expression;
88
import ch.njol.skript.lang.SkriptParser.ParseResult;
99
import ch.njol.util.Kleenean;
10+
import ch.njol.util.Math2;
1011
import ch.njol.util.coll.CollectionUtils;
1112
import org.bukkit.block.Block;
1213
import org.bukkit.block.data.BlockData;
1314
import org.bukkit.block.data.Brushable;
1415
import org.bukkit.event.Event;
1516
import 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"})
2639
public 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;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
test "dusting progress":
2+
set {_old} to block data of test-block
3+
set block at test-block to suspicious gravel
4+
set {_block} to block at test-block
5+
set {_blockdata} to block data of {_block}
6+
7+
# test getting initial value
8+
assert dusting progress of {_block} is 0 with "Initial dusting progress should be 0"
9+
10+
# test setting
11+
set dusting progress of {_block} to 2
12+
assert dusting progress of {_block} is 2 with "Failed to set dusting progress to 2"
13+
14+
# test max dusting progress
15+
assert max dusting progress of {_block} is 3 with "Max dusting progress should be 3"
16+
assert maximum dusting progress of {_block} is 3 with "Maximum dusting progress should be 3"
17+
18+
# test adding
19+
set dusting progress of {_block} to 1
20+
add 1 to dusting progress of {_block}
21+
assert dusting progress of {_block} is 2 with "Failed to add 1 to dusting progress"
22+
23+
# test clamping at max
24+
add 99 to dusting progress of {_block}
25+
assert dusting progress of {_block} is 3 with "Dusting progress should be clamped at max"
26+
27+
# test removing
28+
set dusting progress of {_block} to 2
29+
remove 1 from dusting progress of {_block}
30+
assert dusting progress of {_block} is 1 with "Failed to remove 1 from dusting progress"
31+
32+
# test clamping at min
33+
remove 99 from dusting progress of {_block}
34+
assert dusting progress of {_block} is 0 with "Dusting progress should be clamped at 0"
35+
36+
# test reset
37+
set dusting progress of {_block} to 2
38+
assert dusting progress of {_block} is 2 with "Failed to set dusting progress"
39+
reset dusting progress of {_block}
40+
assert dusting progress of {_block} is 0 with "Failed to reset dusting progress"
41+
42+
# test with blockdata
43+
set dusting progress of {_blockdata} to 2
44+
assert dusting progress of {_blockdata} is 2 with "Failed to set dusting progress on blockdata"
45+
46+
set block data of test-block to {_old}

0 commit comments

Comments
 (0)