0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PaperのDialogAPIを使ってみる

Posted at

DialogAPIとは

Minecraft1.21.6から追加されたダイアログをサーバー側で実装できるようにするPaperAPI

サンプルコード

表示例(Kotlin)

val friendType = listOf(
    SingleOptionDialogInput.OptionEntry.create("allow", Component.text("許可", NamedTextColor.GREEN), false),
    SingleOptionDialogInput.OptionEntry.create("deny", Component.text("禁止", NamedTextColor.RED), false)
)

val dialog: Dialog = Dialog.create { builder -> builder.empty()
    .base(DialogBase.builder(Component.text("[例]SkyBlockワールド", NamedTextColor.LIGHT_PURPLE))
        .canCloseWithEscape(false)
        .body(
            listOf(
                DialogBody.plainMessage(Component.text("ワールド設定")),
                DialogBody.plainMessage(
                    Component.text("規約: ").append(
                        Component.text("https://example.com", NamedTextColor.AQUA)
                            .clickEvent(ClickEvent.copyToClipboard("https://example.com"))
                    )
                )
            )
        )
        .inputs(
            listOf(
                DialogInput.text("skyblock_world_name", Component.text("ワール名を入力(20文字まで)"))
                    .maxLength(20)
                    .initial("${sender.name}のSkyBlock")
                    .build(),

                DialogInput.singleOption(
                    "skyblock_friend",
                    Component.text("フレンドの参加", NamedTextColor.AQUA),
                    friendType
                ).build(),

                DialogInput.bool(
                    "skyblock_tutorial",
                    Component.text("チュートリアル")
                ).initial(true).build()
            )
        )
        .build()
    )
    .type(DialogType.confirmation(
        ActionButton.create(
            Component.text("送信", NamedTextColor.GREEN),
            null,
            150,
            DialogAction.customClick(Key.key("skyblock:user_input/send"), null)
        ),
        ActionButton.create(
            Component.text("キャンセル", NamedTextColor.RED),
            null,
            150,
            DialogAction.customClick(Key.key("skyblock:user_input/cancel"), null)
        )
    ))
}

player.showDialog(dialog)

※変数playerorg.bukkit.entity.Player
bodyはComponentなのでクリックイベントなどが使えます。

受け取り例(Kotlin)

@EventHandler
fun handleCreateWorld(event: PlayerCustomClickEvent){
    if (event.identifier != Key.key("skyblock:user_input/send")){
        return
    }

    val view = event.getDialogResponseView() ?: return

    val worldName = view.getText("skyblock_world_name").toString()
    val friend = view.getText("skyblock_friend").toString()
    val tutorial = view.getBoolean("skyblock_tutorial")
    val conn = event.commonConnection as? PlayerGameConnection ?: return
    val player = conn.player

    player.sendMessage(
        "§a[Debug] §fworldName=§b$worldName§f, friend=§b$friend§f, tutorial=§b$tutorial§f"
    )
}

送信ボタンは DialogAction.customClick() を使っているため、
サーバー側では PlayerCustomClickEvent を受け取って処理します。
※ singleOption の返り値は 選択肢の “value” ではなく “key” です(今回なら "allow" / "deny")。

レスポンスビューには

  • getText(id)
  • getBoolean(id)
  • getFloat(id)
    があります。
    意味はそのままです。

注意:playerはevent.playerなどで簡単に取得できないので回りくどい書き方になっています。

最後に

DialogAPI は GUI より直感的で、
チャット入力よりミスがなく、
設定画面やフォーム入力に最適な機能です。

  • SkyBlock の初期設定
  • RPG のキャラ作成
  • ギルド名入力
  • 購入確認、設定メニュー

など、幅広いシステムで活用できます。

この記事が実装の参考になれば幸いです。
良いプラグイン開発ライフを!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?