Skip to content

Commit cf035d0

Browse files
committed
feat: add kotlin source, finish code ref in tooltips.md
1 parent 95afabf commit cf035d0

File tree

3 files changed

+128
-18
lines changed

3 files changed

+128
-18
lines changed

docs/en/create-commands/arguments/suggestions/tooltips.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ First, we'll declare our arguments. Here, we'll use the `stringsWithTooltips` me
5252
===Java
5353
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#stringSuggestionTooltipsExampleDeclare
5454
===Kotlin
55-
```kotlin
56-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips1}}
57-
```
55+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#stringSuggestionTooltipsExampleDeclare
5856
:::
5957

6058
Finally, we declare our command as normal:
@@ -63,9 +61,7 @@ Finally, we declare our command as normal:
6361
===Java
6462
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#stringSuggestionTooltipsExampleRegister
6563
===Kotlin
66-
```kotlin
67-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips2}}
68-
```
64+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#stringSuggestionTooltipsExampleRegister
6965
:::
7066

7167
::::
@@ -93,9 +89,7 @@ Let's say we've created a simple plugin which has custom items. For a custom ite
9389
===Java
9490
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#customTooltipExampleDeclare
9591
===Kotlin
96-
```kotlin
97-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips3}}
98-
```
92+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#customTooltipExampleDeclare
9993
:::
10094

10195
We make use of the `Tooltip.messageFromString()` method to generate a Brigadier `Message` object from our string tooltip.
@@ -106,9 +100,7 @@ Let's also say that our plugin has registered lots of `CustomItem`s and has this
106100
===Java
107101
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#customTooltipExampleRegister
108102
===Kotlin
109-
```kotlin
110-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips4}}
111-
```
103+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#customTooltipExampleRegister
112104
:::
113105

114106
::::
@@ -154,9 +146,7 @@ First, we'll declare our arguments. Here, we use a `LocationArgument` and use th
154146
===Java
155147
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#tooltipsWithSafeSuggestionsExampleDeclare
156148
===Kotlin
157-
```kotlin
158-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips5}}
159-
```
149+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#tooltipsWithSafeSuggestionsExampleDeclare
160150
:::
161151

162152
In the arguments declaration, we've cast the command sender to a player. To ensure that the command sender is definitely a player, we'll use the `executesPlayer` command execution method in our command declaration:
@@ -165,9 +155,7 @@ In the arguments declaration, we've cast the command sender to a player. To ensu
165155
===Java
166156
<<< @/../reference-code/src/main/java/createcommands/arguments/suggestions/Tooltips.java#tooltipsWithSafeSuggestionsExampleRegister
167157
===Kotlin
168-
```kotlin
169-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:tooltips6}}
170-
```
158+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/suggestions/Tooltips.kt#tooltipsWithSafeSuggestionsExampleRegister
171159
:::
172160

173161
::::

reference-code/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<execution>
4848
<id>compile</id>
4949
<phase>process-sources</phase>
50+
<configuration>
51+
<sourceDirs>
52+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
53+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
54+
</sourceDirs>
55+
</configuration>
5056
<goals>
5157
<goal>compile</goal>
5258
</goals>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)