1+ package createcommands.arguments.suggestions
2+
3+ import com.mojang.brigadier.Message
4+ import dev.jorel.commandapi.BukkitTooltip
5+ import dev.jorel.commandapi.CommandAPICommand
6+ import dev.jorel.commandapi.IStringTooltip
7+ import dev.jorel.commandapi.StringTooltip
8+ import dev.jorel.commandapi.arguments.Argument
9+ import dev.jorel.commandapi.arguments.ArgumentSuggestions
10+ import dev.jorel.commandapi.arguments.LocationArgument
11+ import dev.jorel.commandapi.arguments.PlayerArgument
12+ import dev.jorel.commandapi.arguments.SafeSuggestions
13+ import dev.jorel.commandapi.arguments.StringArgument
14+ import dev.jorel.commandapi.executors.PlayerCommandExecutor
15+ import org.bukkit.Location
16+ import org.bukkit.Material
17+ import org.bukkit.entity.Player
18+ import org.bukkit.inventory.ItemStack
19+
20+ fun stringSuggestionTooltipsExample () {
21+ // #region stringSuggestionTooltipsExampleDeclare
22+ val arguments = mutableListOf<Argument <* >>()
23+ arguments.add(
24+ StringArgument (" emote" )
25+ .replaceSuggestions(ArgumentSuggestions .stringsWithTooltips { info ->
26+ arrayOf<IStringTooltip >(
27+ StringTooltip .ofString(" wave" , " Waves at a player" ),
28+ StringTooltip .ofString(" hug" , " Gives a player a hug" ),
29+ StringTooltip .ofString(" glare" , " Gives a player the death glare" )
30+ )
31+ })
32+ )
33+ arguments.add(PlayerArgument (" target" ))
34+ // #endregion stringSuggestionTooltipsExampleDeclare
35+
36+ // #region stringSuggestionTooltipsExampleRegister
37+ CommandAPICommand (" emote" )
38+ .withArguments(* arguments.toTypedArray())
39+ .executesPlayer(PlayerCommandExecutor { player, args ->
40+ val emote = args[" emote" ] as String
41+ val target = args[" target" ] as Player
42+
43+ when (emote) {
44+ " wave" -> target.sendMessage(" ${player.name} waves at you!" )
45+ " hug" -> target.sendMessage(" ${player.name} hugs you!" )
46+ " glare" -> target.sendMessage(" ${player.name} gives you the death glare..." )
47+ }
48+ })
49+ .register()
50+ // #endregion stringSuggestionTooltipsExampleRegister
51+ }
52+
53+ fun customTooltipExample () {
54+ // #region customTooltipExampleDeclare
55+ class CustomItem (val item : ItemStack , val name : String , lore : String ) : IStringTooltip {
56+ init {
57+ val meta = item.itemMeta
58+ meta.setDisplayName(name)
59+ meta.lore = listOf (lore)
60+ item.itemMeta = meta
61+ }
62+
63+ override fun getSuggestion (): String = this .item.itemMeta.displayName
64+
65+ override fun getTooltip (): Message = BukkitTooltip .messageFromString(this .item.itemMeta.lore?.get(0 ) ? : " " )
66+ }
67+ // #endregion customTooltipExampleDeclare
68+
69+ // #region customTooltipExampleRegister
70+ val customItems = arrayOf<CustomItem >(
71+ CustomItem (ItemStack (Material .DIAMOND_SWORD ), " God sword" , " A sword from the heavens" ),
72+ CustomItem (ItemStack (Material .PUMPKIN_PIE ), " Sweet pie" , " Just like grandma used to make" )
73+ )
74+
75+ CommandAPICommand (" giveitem" )
76+ .withArguments(StringArgument (" item" ).replaceSuggestions(ArgumentSuggestions .stringsWithTooltips(* customItems))) // We use customItems[] as the input for our suggestions with tooltips
77+ .executesPlayer(PlayerCommandExecutor { player, args ->
78+ val itemName = args[" item" ] as String
79+
80+ // Give them the item
81+ for (item in customItems) {
82+ if (item.name == itemName) {
83+ player.inventory.addItem(item.item)
84+ break
85+ }
86+ }
87+ })
88+ .register()
89+ // #endregion customTooltipExampleRegister
90+ }
91+
92+ fun tooltipsWithSafeSuggestionsExample () {
93+ // #region tooltipsWithSafeSuggestionsExampleDeclare
94+ val arguments = listOf<Argument <* >>(
95+ LocationArgument (" location" )
96+ .replaceSafeSuggestions(SafeSuggestions .tooltips { info ->
97+ // We know the sender is a player if we use .executesPlayer()
98+ val player = info.sender() as Player
99+ BukkitTooltip .arrayOf(
100+ BukkitTooltip .ofString(player.world.spawnLocation, " World spawn" ),
101+ BukkitTooltip .ofString(player.bedSpawnLocation, " Your bed" ),
102+ BukkitTooltip .ofString(player.getTargetBlockExact(256 )?.location, " Target block" )
103+ )
104+ })
105+ )
106+ // #endregion tooltipsWithSafeSuggestionsExampleDeclare
107+
108+ // #region tooltipsWithSafeSuggestionsExampleRegister
109+ CommandAPICommand (" warp" )
110+ .withArguments(arguments)
111+ .executesPlayer(PlayerCommandExecutor { player, args ->
112+ player.teleport(args[" location" ] as Location )
113+ })
114+ .register()
115+ // #endregion tooltipsWithSafeSuggestionsExampleRegister
116+ }
0 commit comments