Skip to content

Commit 0b2bc4b

Browse files
committed
feat: add customPlayerClick and playerCallback methods to DialogActionBuilder for player-specific interactions
1 parent 90f1e71 commit 0b2bc4b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

surf-api-bukkit/surf-api-bukkit-api/api/surf-api-bukkit-api.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,11 @@ public final class dev/slne/surf/surfapi/bukkit/api/dialog/builder/DialogActionB
10451045
public final fun customClick (Lnet/kyori/adventure/text/event/ClickCallback$Options;Lio/papermc/paper/registry/data/dialog/action/DialogActionCallback;)V
10461046
public static synthetic fun customClick$default (Ldev/slne/surf/surfapi/bukkit/api/dialog/builder/DialogActionBuilder;Lnet/kyori/adventure/key/Key;Lnet/kyori/adventure/nbt/api/BinaryTagHolder;ILjava/lang/Object;)V
10471047
public static synthetic fun customClick$default (Ldev/slne/surf/surfapi/bukkit/api/dialog/builder/DialogActionBuilder;Lnet/kyori/adventure/text/event/ClickCallback$Options;Lio/papermc/paper/registry/data/dialog/action/DialogActionCallback;ILjava/lang/Object;)V
1048+
public final fun customPlayerClick (Lnet/kyori/adventure/text/event/ClickCallback$Options;Lkotlin/jvm/functions/Function2;)V
1049+
public static synthetic fun customPlayerClick$default (Ldev/slne/surf/surfapi/bukkit/api/dialog/builder/DialogActionBuilder;Lnet/kyori/adventure/text/event/ClickCallback$Options;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
10481050
public final fun openUrl (Ljava/lang/String;)V
10491051
public final fun openUrl (Ljava/net/URL;)V
1052+
public final fun playerCallback (Lnet/kyori/adventure/text/event/ClickCallback;)V
10501053
public final fun showDialog (Lkotlin/jvm/functions/Function1;)V
10511054
public final fun showDialog (Lnet/kyori/adventure/dialog/DialogLike;)V
10521055
public final fun staticAction (Lnet/kyori/adventure/text/event/ClickEvent;)V

surf-api-bukkit/surf-api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/dialog/builder/DialogActionBuilder.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package dev.slne.surf.surfapi.bukkit.api.dialog.builder
44

55
import dev.slne.surf.surfapi.bukkit.api.dialog.dialog
6+
import dev.slne.surf.surfapi.core.api.messages.adventure.sendText
7+
import io.papermc.paper.dialog.DialogResponseView
68
import io.papermc.paper.registry.data.dialog.DialogRegistryEntry
79
import io.papermc.paper.registry.data.dialog.action.DialogAction
810
import io.papermc.paper.registry.data.dialog.action.DialogActionCallback
@@ -14,6 +16,7 @@ import net.kyori.adventure.nbt.TagStringIO
1416
import net.kyori.adventure.nbt.api.BinaryTagHolder
1517
import net.kyori.adventure.text.event.ClickCallback
1618
import net.kyori.adventure.text.event.ClickEvent
19+
import org.bukkit.entity.Player
1720
import java.net.URL
1821

1922
class DialogActionBuilder {
@@ -52,6 +55,15 @@ class DialogActionBuilder {
5255
staticAction(ClickEvent.callback(callback))
5356
}
5457

58+
fun playerCallback(callback: ClickCallback<Player>) {
59+
callback(ClickCallback.widen(callback, Player::class.java) { other ->
60+
other.sendText {
61+
appendPrefix()
62+
error("Only players can use this action!")
63+
}
64+
})
65+
}
66+
5567
fun commandTemplate(template: String) {
5668
action = DialogAction.commandTemplate(template)
5769
}
@@ -76,6 +88,24 @@ class DialogActionBuilder {
7688
action = DialogAction.customClick(callback, options)
7789
}
7890

91+
fun customPlayerClick(
92+
options: ClickCallback.Options? = null,
93+
callback: (response: DialogResponseView, player: Player) -> Unit,
94+
) {
95+
customClick(
96+
options
97+
) { response, audience ->
98+
if (audience !is Player) {
99+
audience.sendText {
100+
appendPrefix()
101+
error("Only players can use this action!")
102+
}
103+
} else {
104+
callback(response, audience)
105+
}
106+
}
107+
}
108+
79109
internal fun build(): DialogAction {
80110
val action = action
81111
require(action != null) { "DialogAction must be built" }

0 commit comments

Comments
 (0)