Skip to content

Commit

Permalink
impl experimental designer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed May 6, 2024
1 parent a418094 commit 1eb23ad
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ open class GuiButton<T : GuiButton<T>>(
return this as T
}

fun removeSlots(vararg slot: Int) : T = apply {
if (hasParent()) {
slots?.removeAll(slot.toSet())
parent.clearSlot(*slot)
} else {
if (slots != null) {
slots?.removeAll(slot.toSet())
}
}
} as T

override infix fun slot(slot: Int): T {
if (hasParent()) {
if (slots == null) slots = arrayListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ open class GuiScreen(
return this
}

override fun clearSlot(vararg slot: Int) {
for (s in slot) {
items.remove(s)
}
}

fun slotsUsed(): List<Int> = items.map { it.key }

fun findButton(id: String) = findButtons(id).firstOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface IGuiScreen {

fun setSlot(button: IGuiButton<*>, slot: Int) : IGuiScreen

fun clearSlot(vararg slot: Int)

fun open(player: Player)

fun copy() : IGuiScreen
Expand Down
39 changes: 39 additions & 0 deletions plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mattmx.ktgui

import com.mattmx.ktgui.commands.rawCommand
import com.mattmx.ktgui.commands.simpleCommand
import com.mattmx.ktgui.creator.GuiDesigner
import com.mattmx.ktgui.examples.*
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.pretty
Expand Down Expand Up @@ -98,6 +99,44 @@ class KotlinGui : JavaPlugin() {
player.sendMessage(!"&cPlease wait before doing that again.")
}
}

val cachedDesigners = hashMapOf<String, GuiDesigner>()
subCommands += rawCommand("designer") {
permission = "ktgui.command.designer"
playerOnly = true
suggestSubCommands = true

subCommands += rawCommand("open") {
permission = "ktgui.command.designer"
playerOnly = true

runs {
val id = args.getOrNull(2)
?: return@runs source.sendMessage(!"&cProvide an id of the designer")

val designer = cachedDesigners.getOrPut(id) { GuiDesigner(id) }
designer.open(player)
}

suggestion { cachedDesigners.keys.filter { it.startsWith(lastArg, true) } }
}

subCommands += rawCommand("export") {
permission = "ktgui.command.designer"
playerOnly = true

runs {
val id = args.getOrNull(2)
?: return@runs source.sendMessage(!"&cProvide an id of the designer")

val designer = cachedDesigners.getOrPut(id) { GuiDesigner(id) }
val file = designer.save(this@KotlinGui)
source.sendMessage(!"&aSaved to /plugins/KtGUI/designer/${file.name}")
}

suggestion { cachedDesigners.keys.filter { it.startsWith(lastArg, true) } }
}
}
}.register(false)
}

Expand Down
33 changes: 31 additions & 2 deletions plugin/src/main/kotlin/com/mattmx/ktgui/creator/GuiDesigner.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package com.mattmx.ktgui.creator

import com.mattmx.ktgui.KotlinGui
import com.mattmx.ktgui.components.button.GuiButton
import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.utils.not
import java.io.File

class GuiDesigner(
val name: String
) : GuiScreen(!"Designer ($name&r)") {

init {
click.any {
click.left {
event.isCancelled = false

// todo save instance
if (slot !in 0..last()) {
return@left
}

val existing = items[slot]
items.remove(slot)
existing?.removeSlots(slot)

GuiButton(item = itemClicked) childOf this@GuiDesigner slot slot
}

click.right {
if (isButton()) {
GuiDesignerButtonCustomizer(this@GuiDesigner, button as GuiButton<*>).open(player)
}
}
}

fun save(plugin: KotlinGui): File {
val file = File("${plugin.dataFolder}/designer/$name.kt")

if (file.exists()) {
file.parentFile.mkdirs()
file.createNewFile()
}
file.writeText(export())
return file
}

fun export(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.mattmx.ktgui.creator

import com.mattmx.ktgui.components.button.GuiButton
import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.conversation.refactor.conversation
import com.mattmx.ktgui.conversation.refactor.getString
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.extensions.getOpenGui
import com.mattmx.ktgui.scheduling.sync
import com.mattmx.ktgui.utils.legacy
import com.mattmx.ktgui.utils.not
import net.kyori.adventure.text.format.TextColor
import org.bukkit.Material
import org.bukkit.entity.Player

class GuiDesignerButtonCustomizer(
val parent: GuiDesigner,
val button: GuiButton<*>
) : GuiScreen(!"Customize Button", 3) {

init {
button(Material.SPECTRAL_ARROW) {
named(!"&dBack")
leftClick {
openParent(player)
}
} slot 22

button(Material.NAME_TAG) {
named(!"&dName")
lore {
add(!"&fCurrently: ${button.item?.displayName()?.legacy()}")
}
leftClick {
conversation<Player> {
getString {
message = !"&dInput a new item name"

runs {
result.ifPresent { result ->
button.named(!result)
}
open(player)
}
}
exitOn = "cancel"
exit {
open(player)
}
} begin player
}
} slot 10

button(Material.MOJANG_BANNER_PATTERN) {
named(!"&dLore")
lore {
val isEmpty = button.item?.lore()?.isEmpty() ?: true
add(!"&fCurrently: ${if (isEmpty) "&7*None*" else ""}")

button.item?.lore()?.forEach { line -> add(line) }
}
leftClick {
conversation<Player> {
getString {
matches { result.isPresent && result.get().equals("cancel", true) }
invalid {
result.ifPresent {
button.lore(!it)
}
player.sendMessage(!"&fType 'cancel' to stop.")
player.sendMessage(!"&fCurrently:")
button.item?.lore()?.forEach { line -> player.sendMessage(line) }
}
runs {
open(player)
}
}
} begin player
}
} slot 13

button(Material.ENCHANTED_BOOK) {
named(!"&dEnchantments")
lore {
val isEmpty = button.item?.enchantments?.isEmpty() ?: true
add(!"&fCurrently: ${if (isEmpty) "&7*None*" else ""}")

button.item?.enchantments?.forEach { line ->
line.key.displayName(line.value).color(TextColor.color(0xFFFFFF))
}
}
} slot 16

close {
val player = it.player as Player
openParent(player)
}
}

fun openParent(player: Player) {
sync {
if (player.getOpenGui() == this@GuiDesignerButtonCustomizer) {
parent.open(player)
}
}
}

}

0 comments on commit 1eb23ad

Please sign in to comment.