Skip to content

Commit f45d490

Browse files
committed
ExprNameBlock - add expression for custom name of block
- Ref SkriptLang/Skript#6826
1 parent 14163ec commit f45d490

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.shanebeestudios.skbee.elements.text.expressions;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.classes.Changer.ChangeMode;
5+
import ch.njol.skript.doc.Description;
6+
import ch.njol.skript.doc.Examples;
7+
import ch.njol.skript.doc.Name;
8+
import ch.njol.skript.doc.Since;
9+
import ch.njol.util.coll.CollectionUtils;
10+
import com.shanebeestudios.skbee.api.skript.base.SimplePropertyExpression;
11+
import com.shanebeestudios.skbee.api.wrapper.ComponentWrapper;
12+
import net.kyori.adventure.text.Component;
13+
import org.bukkit.block.Block;
14+
import org.bukkit.block.BlockState;
15+
import org.bukkit.block.Container;
16+
import org.bukkit.block.Skull;
17+
import org.bukkit.event.Event;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
@Name("TextComponent - Block Name")
21+
@Description({"Get/set/delete the custom name of a block. This will work on container blocks and skulls.",
22+
"Skulls require Paper 1.21.4(Build 214)+",
23+
"Even though the custom name of any skull can be set, only a player head will retain its name when broken (Skeleton/creeper/etc skulls will not)."})
24+
@Examples({"set {_name} to component block name of target block",
25+
"set component block name of target block to mini message from \"<rainbow>Mr Potato Head!\"",
26+
"delete component block name of target block"})
27+
@Since("INSERT VERSION")
28+
public class ExprNameBlock extends SimplePropertyExpression<Block, ComponentWrapper> {
29+
30+
private static final boolean HAS_SKULL_NAME = Skript.methodExists(Skull.class, "customName");
31+
32+
static {
33+
register(ExprNameBlock.class, ComponentWrapper.class,
34+
"component [custom] block name", "block");
35+
}
36+
37+
@Override
38+
public @Nullable ComponentWrapper convert(Block block) {
39+
BlockState state = block.getState();
40+
Component component = null;
41+
if (state instanceof Container container) {
42+
component = container.customName();
43+
} else if (state instanceof Skull skull && HAS_SKULL_NAME) {
44+
component = skull.customName();
45+
}
46+
if (component != null) return ComponentWrapper.fromComponent(component);
47+
return null;
48+
}
49+
50+
@Override
51+
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
52+
if (mode == ChangeMode.SET) return CollectionUtils.array(ComponentWrapper.class);
53+
else if (mode == ChangeMode.DELETE) return CollectionUtils.array();
54+
return null;
55+
}
56+
57+
@Override
58+
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
59+
Component name = null;
60+
if (delta != null && delta[0] instanceof ComponentWrapper cw) {
61+
name = cw.getComponent();
62+
}
63+
for (Block block : getExpr().getArray(event)) {
64+
setName(block, name);
65+
}
66+
}
67+
68+
private void setName(Block block, Component name) {
69+
BlockState state = block.getState(false);
70+
if (state instanceof Container container) {
71+
container.customName(name);
72+
} else if (state instanceof Skull skull && HAS_SKULL_NAME) {
73+
skull.customName(name);
74+
}
75+
}
76+
77+
@Override
78+
protected String getPropertyName() {
79+
return "component block custom name";
80+
}
81+
82+
@Override
83+
public Class<? extends ComponentWrapper> getReturnType() {
84+
return ComponentWrapper.class;
85+
}
86+
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
test "SkBee - Text/ExprNameBlock":
2+
set {_loc} to event-location
3+
set {_data} to blockdata of block at {_loc}
4+
5+
set block at {_loc} to a player head
6+
assert component block name of block at {_loc} is not set with "Should not have a name yet"
7+
set component block name of block at {_loc} to mini message from "test"
8+
assert component block name of block at {_loc} = mini message from "test" with "Name should now be 'test'"
9+
delete component block name of block at {_loc}
10+
assert component block name of block at {_loc} is not set with "Should no longer have a name after reset"
11+
12+
set block at {_loc} to {_data}

0 commit comments

Comments
 (0)