Skip to content

Commit

Permalink
refactor!: more canonical language
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdm2883 committed Jan 23, 2025
1 parent 69b564e commit 74f5e45
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
4 changes: 0 additions & 4 deletions core/src/main/kotlin/vip/cdms/allaymc/kotlinx/Typealias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ package vip.cdms.allaymc.kotlinx

import org.allaymc.api.entity.interfaces.EntityPlayer

// kotlinx
typealias Receiver<T> = T.() -> Unit

// allaymc
typealias Player = EntityPlayer
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun ModalFormBuilder(block: ModalFormBuilder.() -> Unit) = ModalFormBuilder().ap
fun SimpleFormBuilder(block: SimpleFormBuilder.() -> Unit) = SimpleFormBuilder().apply(block)

operator fun SimpleFormBuilder.plus(block: SimpleFormBuilder.() -> Unit) = plus(SimpleFormBuilder(block))
operator fun SimpleFormBuilder.plus(builder: SimpleFormBuilder) = SimpleFormBuilder outputBuilder@ {
operator fun SimpleFormBuilder.plus(builder: SimpleFormBuilder) = SimpleFormBuilder outputBuilder@{
this@outputBuilder.title = builder.title.ifBlank { this@SimpleFormBuilder.title }
this@outputBuilder.content = builder.content.ifBlank { this@SimpleFormBuilder.content }
this@outputBuilder.buttons += this@SimpleFormBuilder.buttons + builder.buttons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package vip.cdms.allaymc.kotlinx.form
import org.allaymc.api.form.Forms
import org.allaymc.api.form.type.ModalForm
import vip.cdms.allaymc.kotlinx.Player
import vip.cdms.allaymc.kotlinx.Receiver

class ModalFormBuilder : FormBuilder<ModalForm, ModalFormBuilder.Response>(){
var content = ""

data class Action(val text: String, val callbacks: MutableList<Receiver<Player>?>)
fun actionOf(text: String, callback: Receiver<Player>? = null) = Action(text, mutableListOf()).apply { callbacks += callback }
data class Action(val text: String, val callbacks: MutableList<(Player.() -> Unit)?>)
fun actionOf(text: String, callback: (Player.() -> Unit)? = null) =
Action(text, mutableListOf()).apply { callbacks += callback }
var confirm: Action? = null
var cancel: Action? = null

fun confirm(text: String? = null, callback: Receiver<Player>? = null) {
fun confirm(text: String? = null, callback: (Player.() -> Unit)? = null) {
confirm = confirm?.copy(text = text ?: confirm!!.text) ?: actionOf(text ?: "")
confirm!!.callbacks += callback
}
fun cancel(text: String? = null, callback: Receiver<Player>? = null) {
fun cancel(text: String? = null, callback: (Player.() -> Unit)? = null) {
cancel = cancel?.copy(text = text ?: cancel!!.text) ?: actionOf(text ?: "")
cancel!!.callbacks += callback
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package vip.cdms.allaymc.kotlinx.form

import org.allaymc.api.entity.interfaces.EntityPlayer
import org.allaymc.api.form.Forms
import org.allaymc.api.form.element.Button
import org.allaymc.api.form.element.ImageData
import org.allaymc.api.form.type.SimpleForm
import vip.cdms.allaymc.kotlinx.Player
import vip.cdms.allaymc.kotlinx.Receiver

class SimpleFormBuilder : FormBuilder<SimpleForm, SimpleFormBuilder.Response>() {
var content = ""
Expand All @@ -23,12 +21,12 @@ class SimpleFormBuilder : FormBuilder<SimpleForm, SimpleFormBuilder.Response>()
fun Button.convert() = Button(text, image?.convert())

val buttons = mutableListOf<Button>()
val callbacks = linkedMapOf<Button, MutableList<Receiver<EntityPlayer>?>>()
val callbacks = linkedMapOf<Button, MutableList<(Player.() -> Unit)?>>()

fun buttonOf(text: String, image: Image? = null, callback: Receiver<EntityPlayer>? = null) =
fun buttonOf(text: String, image: Image? = null, callback: (Player.() -> Unit)? = null) =
Button(text, image).apply { callbacks.getOrPut(this) { mutableListOf() } += callback }
fun button(text: String, image: Image? = null, callback: Receiver<EntityPlayer>? = null) =
buttonOf(text, image, callback).apply { buttons += this }
fun button(text: String, image: Image? = null, callback: (Player.() -> Unit)? = null) =
buttonOf(text, image, callback).also { buttons += it }

@JvmInline
value class Response(val index: Int) : FormBuilder.Response
Expand Down

0 comments on commit 74f5e45

Please sign in to comment.