Improve code style
This commit is contained in:
@@ -13,11 +13,8 @@ inline fun chatComponent(builder: KSpigotComponentBuilder.() -> Unit): Array<out
|
||||
}
|
||||
|
||||
class KSpigotComponentBuilder {
|
||||
|
||||
private val components = ArrayList<BaseComponent>()
|
||||
|
||||
// COMPONENTS
|
||||
|
||||
inline fun text(text: String, builder: TextComponent.() -> Unit = { }) {
|
||||
this += TextComponent(text).apply(builder)
|
||||
}
|
||||
@@ -40,13 +37,11 @@ class KSpigotComponentBuilder {
|
||||
inline fun translatable(
|
||||
translatable: String,
|
||||
with: Array<BaseComponent>,
|
||||
builder: TranslatableComponent.() -> Unit = { }
|
||||
builder: TranslatableComponent.() -> Unit = { },
|
||||
) {
|
||||
this += TranslatableComponent(translatable, with).apply(builder)
|
||||
}
|
||||
|
||||
// SPECIAL
|
||||
|
||||
fun legacyText(text: String, color: ChatColor = ChatColor.WHITE, builder: BaseComponent.() -> Unit = { }) {
|
||||
this += TextComponent.fromLegacyText(text, color).onEach { it.apply(builder) }
|
||||
}
|
||||
@@ -60,15 +55,11 @@ class KSpigotComponentBuilder {
|
||||
}
|
||||
|
||||
fun create() = components.toTypedArray()
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* BASE COMPONENT
|
||||
*/
|
||||
|
||||
// extensions
|
||||
|
||||
inline fun BaseComponent.hoverEventText(builder: KSpigotComponentBuilder.() -> Unit) {
|
||||
hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text(KSpigotComponentBuilder().apply(builder).create()))
|
||||
}
|
||||
@@ -84,7 +75,6 @@ fun BaseComponent.hoverEventEntity(type: String, id: String, baseComponent: Base
|
||||
fun BaseComponent.clickEvent(action: ClickEvent.Action, value: String) {
|
||||
clickEvent = ClickEvent(action, value)
|
||||
}
|
||||
|
||||
/*
|
||||
* GLOBAL SHORTCUTS
|
||||
*/
|
||||
|
@@ -19,7 +19,7 @@ import org.bukkit.event.Listener
|
||||
fun Player.awaitChatInput(
|
||||
question: String = "Type your input in the chat!",
|
||||
timeoutSeconds: Int = 1 * 60,
|
||||
callback: (PlayerInputResult<String>) -> Unit
|
||||
callback: (PlayerInputResult<String>) -> Unit,
|
||||
) {
|
||||
PlayerInputChat(this, callback, timeoutSeconds, question)
|
||||
}
|
||||
@@ -36,7 +36,7 @@ fun Player.awaitAnvilInput(
|
||||
"submit your input!"
|
||||
),
|
||||
timeoutSeconds: Int = 1 * 60,
|
||||
callback: (PlayerInputResult<String>) -> Unit
|
||||
callback: (PlayerInputResult<String>) -> Unit,
|
||||
) {
|
||||
PlayerInputAnvilInv(this, callback, timeoutSeconds, invTitle, startText, renameItemDescription)
|
||||
}
|
||||
@@ -48,7 +48,7 @@ fun Player.awaitAnvilInput(
|
||||
*/
|
||||
fun Player.awaitBookInputAsString(
|
||||
timeoutSeconds: Int = 1 * 60,
|
||||
callback: (PlayerInputResult<String>) -> Unit
|
||||
callback: (PlayerInputResult<String>) -> Unit,
|
||||
) {
|
||||
PlayerInputBookComprehensive(this, callback, timeoutSeconds)
|
||||
}
|
||||
@@ -61,7 +61,7 @@ fun Player.awaitBookInputAsString(
|
||||
*/
|
||||
fun Player.awaitBookInputAsList(
|
||||
timeoutSeconds: Int = 1 * 60,
|
||||
callback: (PlayerInputResult<List<String>>) -> Unit
|
||||
callback: (PlayerInputResult<List<String>>) -> Unit,
|
||||
) {
|
||||
PlayerInputBookPaged(this, callback, timeoutSeconds)
|
||||
}
|
||||
@@ -70,17 +70,13 @@ fun Player.awaitBookInputAsList(
|
||||
* @param input The input the player gave. Null on timeout or invalid input.
|
||||
*/
|
||||
class PlayerInputResult<T> internal constructor(val input: T?)
|
||||
|
||||
internal abstract class PlayerInput<T>(
|
||||
protected val player: Player,
|
||||
private val callback: (PlayerInputResult<T>) -> Unit,
|
||||
timeoutSeconds: Int
|
||||
timeoutSeconds: Int,
|
||||
) {
|
||||
|
||||
private var received = false
|
||||
|
||||
protected abstract val inputListeners: List<Listener>
|
||||
|
||||
protected fun onReceive(input: T?) {
|
||||
if (!received) {
|
||||
inputListeners.forEach { it.unregister() }
|
||||
@@ -99,5 +95,4 @@ internal abstract class PlayerInput<T>(
|
||||
onReceive(null)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -19,9 +19,8 @@ internal class PlayerInputAnvilInv(
|
||||
timeoutSeconds: Int,
|
||||
invTitle: String,
|
||||
startText: String,
|
||||
renameItemDescription: List<String>
|
||||
renameItemDescription: List<String>,
|
||||
) : PlayerInput<String>(player, callback, timeoutSeconds) {
|
||||
|
||||
private val anvilInv =
|
||||
AnvilGUI.Builder().plugin(KSpigotMainInstance)
|
||||
.onClose { onReceive(null) }
|
||||
@@ -42,7 +41,6 @@ internal class PlayerInputAnvilInv(
|
||||
)
|
||||
.text("${KColors.ORANGERED}$startText")
|
||||
.open(player)
|
||||
|
||||
override val inputListeners = listOf(
|
||||
listen<InventoryClickEvent> {
|
||||
if (it.clickedInventory == anvilInv.inventory)
|
||||
@@ -53,5 +51,4 @@ internal class PlayerInputAnvilInv(
|
||||
override fun onTimeout() {
|
||||
anvilInv.inventory.closeForViewers()
|
||||
}
|
||||
|
||||
}
|
@@ -17,7 +17,7 @@ import org.bukkit.persistence.PersistentDataType
|
||||
internal class PlayerInputBookComprehensive(
|
||||
player: Player,
|
||||
callback: (PlayerInputResult<String>) -> Unit,
|
||||
timeoutSeconds: Int
|
||||
timeoutSeconds: Int,
|
||||
) : PlayerInputBook<String>(player, callback, timeoutSeconds) {
|
||||
override fun loadBookContent(bookMeta: BookMeta) = bookMeta.content
|
||||
}
|
||||
@@ -25,7 +25,7 @@ internal class PlayerInputBookComprehensive(
|
||||
internal class PlayerInputBookPaged(
|
||||
player: Player,
|
||||
callback: (PlayerInputResult<List<String>>) -> Unit,
|
||||
timeoutSeconds: Int
|
||||
timeoutSeconds: Int,
|
||||
) : PlayerInputBook<List<String>>(player, callback, timeoutSeconds) {
|
||||
override fun loadBookContent(bookMeta: BookMeta): List<String> = bookMeta.pages
|
||||
}
|
||||
@@ -33,9 +33,8 @@ internal class PlayerInputBookPaged(
|
||||
internal abstract class PlayerInputBook<T>(
|
||||
player: Player,
|
||||
callback: (PlayerInputResult<T>) -> Unit,
|
||||
timeoutSeconds: Int
|
||||
timeoutSeconds: Int,
|
||||
) : PlayerInput<T>(player, callback, timeoutSeconds) {
|
||||
|
||||
private val id = getID()
|
||||
|
||||
init {
|
||||
@@ -47,7 +46,6 @@ internal abstract class PlayerInputBook<T>(
|
||||
}
|
||||
|
||||
abstract fun loadBookContent(bookMeta: BookMeta): T
|
||||
|
||||
override val inputListeners = listOf(
|
||||
listen<PlayerEditBookEvent> {
|
||||
val meta = it.newBookMeta
|
||||
@@ -64,16 +62,12 @@ internal abstract class PlayerInputBook<T>(
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
val idKey = NamespacedKey(KSpigotMainInstance, "kspigot_bookinput_id")
|
||||
|
||||
internal val usedIDs = ArrayList<Int>()
|
||||
fun getID(): Int {
|
||||
var returnID = (0..Int.MAX_VALUE).random()
|
||||
while (usedIDs.contains(returnID)) returnID = (0..Int.MAX_VALUE).random()
|
||||
return returnID
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -12,7 +12,7 @@ internal class PlayerInputChat(
|
||||
player: Player,
|
||||
callback: (PlayerInputResult<String>) -> Unit,
|
||||
timeoutSeconds: Int,
|
||||
question: String
|
||||
question: String,
|
||||
) : PlayerInput<String>(player, callback, timeoutSeconds) {
|
||||
|
||||
init {
|
||||
@@ -27,5 +27,4 @@ internal class PlayerInputChat(
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
}
|
Reference in New Issue
Block a user