Skip to content

Commit 38bf676

Browse files
authored
Brush-related expressions (#6902)
* initial commit * syntax change * remove comment * slight change in test * forgot these * changes * test * test * changes * nevermind * Apply suggestions from code review woah!! Co-authored-by: Moderocky <admin@moderocky.com> Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> Co-authored-by: Fusezion <fusezionstream@gmail.com> * initial commit * syntax change * remove comment * slight change in test * forgot these * changes * test * test * changes * nevermind * Apply suggestions from code review woah!! Co-authored-by: Moderocky <admin@moderocky.com> Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> Co-authored-by: Fusezion <fusezionstream@gmail.com> * UPDATEEEEEEE * Suggested changes. * Apply suggestions from code review Co-authored-by: burbulinis <131194155+Burbulinis@users.noreply.github.com> * Suggested Changes * Apply suggestions from code review Co-authored-by: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> * Suggested Changes * Update src/test/skript/tests/syntaxes/expressions/ExprDusted.sk Co-authored-by: Moderocky <admin@moderocky.com> * Suggested * fix: logic error * feat: add blockdata support and multiple changers
1 parent b39b5f0 commit 38bf676

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package ch.njol.skript.expressions;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.classes.Changer;
5+
import ch.njol.skript.classes.Changer.ChangeMode;
6+
import ch.njol.skript.doc.*;
7+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
8+
import ch.njol.util.coll.CollectionUtils;
9+
import org.bukkit.block.Block;
10+
import org.bukkit.block.BrushableBlock;
11+
import org.bukkit.block.BlockState;
12+
import org.bukkit.event.Event;
13+
import org.bukkit.inventory.ItemStack;
14+
import org.jetbrains.annotations.Nullable;
15+
16+
@Name("Buried Item")
17+
@Description({
18+
"Represents the item that is uncovered when dusting.",
19+
"The only blocks that can currently be \"dusted\" are Suspicious Gravel and Suspicious Sand."
20+
})
21+
@Examples({
22+
"send target block's brushable item",
23+
"set {_gravel}'s brushable item to emerald"
24+
})
25+
@Since("INSERT VERSION")
26+
@RequiredPlugins("Minecraft 1.20+")
27+
public class ExprBrushableItem extends SimplePropertyExpression<Block, ItemStack> {
28+
29+
private static final boolean SUPPORTS_DUSTING = Skript.classExists("org.bukkit.block.BrushableBlock");
30+
31+
static {
32+
if (SUPPORTS_DUSTING)
33+
register(ExprBrushableItem.class, ItemStack.class,
34+
"(brushable|buried) item",
35+
"blocks");
36+
}
37+
38+
@Override
39+
public @Nullable ItemStack convert(Block block) {
40+
if (block.getState() instanceof BrushableBlock brushableBlock) {
41+
return brushableBlock.getItem();
42+
}
43+
return null;
44+
}
45+
46+
@Override
47+
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
48+
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE) {
49+
return CollectionUtils.array(ItemStack.class);
50+
}
51+
return null;
52+
}
53+
54+
@Override
55+
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
56+
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE) {
57+
ItemStack newItem = (mode == ChangeMode.SET) ? (ItemStack) delta[0] : null;
58+
for (Block block : getExpr().getArray(event)) {
59+
BlockState state = block.getState();
60+
if (state instanceof BrushableBlock brushableBlock) {
61+
brushableBlock.setItem(newItem);
62+
state.update();
63+
}
64+
}
65+
}
66+
}
67+
68+
@Override
69+
public Class<? extends ItemStack> getReturnType() {
70+
return ItemStack.class;
71+
}
72+
73+
@Override
74+
protected String getPropertyName() {
75+
return "brushable item";
76+
}
77+
78+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package ch.njol.skript.expressions;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.classes.Changer.ChangeMode;
5+
import ch.njol.skript.doc.*;
6+
import ch.njol.skript.expressions.base.PropertyExpression;
7+
import ch.njol.skript.lang.Expression;
8+
import ch.njol.skript.lang.SkriptParser.ParseResult;
9+
import ch.njol.util.Kleenean;
10+
import ch.njol.util.coll.CollectionUtils;
11+
import org.bukkit.block.Block;
12+
import org.bukkit.block.data.BlockData;
13+
import org.bukkit.block.data.Brushable;
14+
import org.bukkit.event.Event;
15+
import org.jetbrains.annotations.Nullable;
16+
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+
@Examples({
23+
"send target block's maximum dusted stage",
24+
"set {_sand}'s dusted stage to 2"
25+
})
26+
@Since("INSERT VERSION")
27+
@RequiredPlugins("Minecraft 1.20+")
28+
public class ExprDustedStage extends PropertyExpression<Object, Integer> {
29+
30+
private static final boolean SUPPORTS_DUSTING = Skript.classExists("org.bukkit.block.data.Brushable");
31+
32+
static {
33+
if (SUPPORTS_DUSTING)
34+
register(ExprDustedStage.class, Integer.class,
35+
"[:max[imum]] dust[ed|ing] (value|stage|progress[ion])",
36+
"blocks/blockdatas");
37+
}
38+
39+
private boolean isMax;
40+
41+
@Override
42+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
43+
setExpr((Expression<Block>) exprs[0]);
44+
isMax = parseResult.hasTag("max");
45+
return true;
46+
}
47+
48+
@Override
49+
protected Integer @Nullable [] get(Event event, Object[] source) {
50+
return get(source, obj -> {
51+
Brushable brushable = getBrushable(obj);
52+
if (brushable != null) {
53+
return isMax ? brushable.getMaximumDusted() : brushable.getDusted();
54+
}
55+
return null;
56+
});
57+
}
58+
59+
@Override
60+
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
61+
if (isMax) {
62+
Skript.error("Attempting to modify the max dusted stage is not supported.");
63+
return null;
64+
}
65+
66+
return switch (mode) {
67+
case SET, ADD, REMOVE, RESET -> CollectionUtils.array(Integer.class);
68+
default -> null;
69+
};
70+
}
71+
72+
@Override
73+
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
74+
if (isMax) return;
75+
Integer value = (delta != null && delta.length > 0) ? (Integer) delta[0] : null;
76+
77+
for (Object obj : getExpr().getArray(event)) {
78+
Brushable brushable = getBrushable(obj);
79+
if (brushable == null)
80+
continue;
81+
82+
int currentValue = brushable.getDusted();
83+
int maxValue = brushable.getMaximumDusted();
84+
int newValue = currentValue;
85+
86+
switch (mode) {
87+
case SET -> {
88+
if (value != null) {
89+
newValue = value;
90+
}
91+
}
92+
case ADD -> {
93+
if (value != null) {
94+
newValue = currentValue + value;
95+
}
96+
}
97+
case REMOVE -> {
98+
if (value != null) {
99+
newValue = currentValue - value;
100+
}
101+
}
102+
case RESET -> newValue = 0;
103+
default -> {
104+
return;
105+
}
106+
}
107+
108+
newValue = Math.max(0, Math.min(newValue, maxValue));
109+
110+
brushable.setDusted(newValue);
111+
if (obj instanceof Block) {
112+
((Block) obj).setBlockData(brushable);
113+
}
114+
}
115+
}
116+
117+
@Nullable
118+
private Brushable getBrushable(Object obj) {
119+
if (obj instanceof Block block) {
120+
BlockData blockData = block.getBlockData();
121+
if (blockData instanceof Brushable)
122+
return (Brushable) blockData;
123+
} else if (obj instanceof Brushable brushable) {
124+
return brushable;
125+
}
126+
return null;
127+
}
128+
129+
130+
@Override
131+
public Class<? extends Integer> getReturnType() {
132+
return Integer.class;
133+
}
134+
135+
@Override
136+
public String toString(@Nullable Event event, boolean debug) {
137+
return getExpr().toString(event, debug) + "'s " + (isMax ? "maximum " : "") + " dusted stage";
138+
}
139+
140+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
test "Dusted Stage" when running minecraft "1.20":
2+
set block 1 block under event-location to stone
3+
set block at event-location to suspicious gravel
4+
set {_block} to block at event-location
5+
set {_blockdata} to block data of {_block}
6+
7+
set dusted stage of {_block} to 2
8+
assert dusted stage of {_block} is 2 with "Failed to set dusted stage to 2"
9+
10+
assert maximum dusted stage of {_block} is 3 with "Maximum dusted stage should be 3"
11+
12+
add 1 to dusted stage of {_block}
13+
assert dusted stage of {_block} is 3 with "Failed to add 1 to dusted stage"
14+
15+
add 5 to dusted stage of {_block}
16+
assert dusted stage of {_block} is 3 with "Dusted stage should be capped at maximum value"
17+
18+
remove 2 from dusted stage of {_block}
19+
assert dusted stage of {_block} is 1 with "Failed to remove 2 from dusted stage"
20+
21+
remove 5 from dusted stage of {_block}
22+
assert dusted stage of {_block} is 0 with "Dusted stage should be capped at minimum value"
23+
24+
set dusted stage of {_block} to 2
25+
reset dusted stage of {_block}
26+
assert dusted stage of {_block} is 0 with "Failed to reset dusted stage"
27+
28+
set dusted stage of {_blockdata} to 2
29+
assert dusted stage of {_blockdata} is 2 with "Failed to set dusted stage on blockdata"
30+
31+
set block at event-location to suspicious sand
32+
set {_sand} to block at event-location
33+
34+
set dusted stage of {_sand} to 1
35+
assert dusted stage of {_sand} is 1 with "Failed to set dusted stage on suspicious sand"
36+
add 1 to dusted stage of {_sand}
37+
assert dusted stage of {_sand} is 2 with "Failed to add to dusted stage on suspicious sand"
38+
39+
set block at event-location to air
40+
set block 1 block under event-location to air
41+
42+
test "Brushable Item" when running minecraft "1.20":
43+
set block 1 block under event-location to stone
44+
set block at event-location to suspicious gravel
45+
set {_block} to block at event-location
46+
47+
set {_block}'s brushable item to gold nugget
48+
assert {_block}'s brushable item is gold nugget with "Failed to set brushable item to gold nugget"
49+
50+
set {_block}'s brushable item to diamond
51+
assert {_block}'s brushable item is diamond with "Failed to set brushable item to diamond"
52+
53+
set {_block}'s brushable item to air
54+
assert {_block}'s brushable item is air with "Failed to clear brushable item"
55+
set block at {_block} to air
56+
set block 1 block under {_block} to air
57+

0 commit comments

Comments
 (0)