Skip to content

Commit ad9762c

Browse files
committed
Add expr to get the show item hover tag for a given item.
1 parent 8507ae1 commit ad9762c

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/main/java/org/skriptlang/skript/bukkit/text/TextModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void loadSelf(SkriptAddon addon) {
6464
EffSendTitle::register,
6565
ExprColored::register,
6666
ExprRawString::register,
67+
ExprItemHoverText::register,
6768
ExprStringColor::register
6869
);
6970
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.skriptlang.skript.bukkit.text.elements.expressions;
2+
3+
import ch.njol.skript.doc.Description;
4+
import ch.njol.skript.doc.Example;
5+
import ch.njol.skript.doc.Name;
6+
import ch.njol.skript.doc.Since;
7+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
8+
import net.kyori.adventure.text.Component;
9+
import org.bukkit.inventory.ItemStack;
10+
import org.skriptlang.skript.bukkit.text.TextComponentParser;
11+
import org.skriptlang.skript.registration.SyntaxRegistry;
12+
13+
@Name("Show Item Hover Text")
14+
@Description("""
15+
Creates a '<hover:show_item:...>' string from an item stack, which can be used to show the item's hover text in chat.
16+
""")
17+
@Example("""
18+
on right click on a chest:
19+
send "Contents:"
20+
loop items in inventory of event-block:
21+
send formatted " - %item hover text of loop-item%%loop-item%"
22+
""")
23+
@Since("INSERT VERSION")
24+
public class ExprItemHoverText extends SimplePropertyExpression<ItemStack, String> {
25+
26+
public static void register(SyntaxRegistry registry) {
27+
registry.register(SyntaxRegistry.EXPRESSION,
28+
infoBuilder(ExprItemHoverText.class, String.class, "[show] item hover text", "itemstacks", false)
29+
.supplier(ExprItemHoverText::new)
30+
.build());
31+
}
32+
33+
@Override
34+
public String convert(ItemStack from) {
35+
Component dummy = Component.empty().hoverEvent(from.asHoverEvent());
36+
return TextComponentParser.instance().toString(dummy);
37+
}
38+
39+
@Override
40+
public Class<? extends String> getReturnType() {
41+
return String.class;
42+
}
43+
44+
@Override
45+
protected String getPropertyName() {
46+
return "item hover text";
47+
}
48+
49+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test "item hover text":
2+
assert item hover text of a diamond sword is "<hover:show_item:diamond_sword>"
3+
assert item hover text of 33 carrots is "<hover:show_item:carrot:33>"
4+
assert item hover text of 99 carrots of mending 2 is "<hover:show_item:carrot:99:enchantments:'{""minecraft:mending"":2}'>"

0 commit comments

Comments
 (0)