Skip to content

Commit

Permalink
[6.1.0][dev] 更新 UI 模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jan 28, 2024
1 parent 316f659 commit 17d3276
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import taboolib.module.ui.virtual.virtualize
* @author arasple
* @since 2023/11/18 16:50
*/
open class Anvil(title: String = "...") : Basic(title) {
open class Anvil(title: String = "...") : Basic(title), AnvilCallback {

internal var renameCallback: ((Player, String, Inventory) -> Unit)? = null

/** 当物品被重命名时 */
fun onRename(callback: (Player, String, Inventory) -> Unit) {
open fun onRename(callback: (Player, String, Inventory) -> Unit) {
renameCallback = callback
}

override fun invoke(player: Player, text: String, inventory: Inventory) {
renameCallback?.invoke(player, text, inventory)
}

override fun build(): Inventory {
var inventory = Bukkit.createInventory(holderCallback(this), InventoryType.ANVIL, title)
if (virtual) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package taboolib.module.ui.type

import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory

/**
* TabooLib
* taboolib.module.ui.type.AnvilCallback
*
* @author 坏黑
* @since 2024/1/28 18:28
*/
interface AnvilCallback {

fun invoke(player: Player, text: String, inventory: Inventory)
}
32 changes: 16 additions & 16 deletions module/module-ui/src/main/kotlin/taboolib/module/ui/type/Basic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,52 @@ import java.util.concurrent.CopyOnWriteArrayList
open class Basic(title: String = "chest") : Menu(title) {

/** 虚拟化 */
internal var virtual = false
protected var virtual = false

/** 最后一次构建的页面 */
internal lateinit var lastInventory: Inventory
protected lateinit var lastInventory: Inventory

/** 虚拟化时玩家背包内容 */
internal var storageContents: List<ItemStack>? = null
protected var storageContents: List<ItemStack>? = null

/** 行数 **/
internal var rows = 1
protected var rows = 1

/** 锁定主手 **/
internal var handLocked = true
protected var handLocked = true

/** MenuHolder 回调 **/
internal var holderCallback: ((menu: Basic) -> MenuHolder) = { MenuHolder(it) }
protected var holderCallback: ((menu: Basic) -> MenuHolder) = { MenuHolder(it) }

/** 点击回调 **/
internal val clickCallback = CopyOnWriteArrayList<(event: ClickEvent) -> Unit>()
protected val clickCallback = CopyOnWriteArrayList<(event: ClickEvent) -> Unit>()

/** 点击回调 **/
internal var selfClickCallback: (event: ClickEvent) -> Unit = {}
protected var selfClickCallback: (event: ClickEvent) -> Unit = {}

/** 关闭回调 **/
internal var closeCallback: ((event: InventoryCloseEvent) -> Unit) = { isOpened = false }
protected var closeCallback: ((event: InventoryCloseEvent) -> Unit) = { isOpened = false }

/** 只触发一次关闭回调 **/
internal var onceCloseCallback = false
protected var onceCloseCallback = false

/** 忽略 updateTitle 的关闭回调 **/
internal var skipCloseCallbackOnUpdateTitle = true
protected var skipCloseCallbackOnUpdateTitle = true

/** 是否刷新标题 */
internal var isUpdateTitle = false
protected var isUpdateTitle = false

/** 构建回调 **/
internal var buildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> isOpened = true }
protected var buildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> isOpened = true }

/** 异步构建回调 **/
internal var asyncBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }
protected var asyncBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }

/** 构建回调 **/
internal var selfBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }
protected var selfBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }

/** 异步构建回调 **/
internal var selfAsyncBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }
protected var selfAsyncBuildCallback: ((player: Player, inventory: Inventory) -> Unit) = { _, _ -> }

/** 物品与对应抽象字符关系 **/
var items = ConcurrentHashMap<Char, ItemStack>()
Expand Down
34 changes: 17 additions & 17 deletions module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ open class Linked<T>(title: String) : Basic(title) {
private set

/** 锁定所有位置 **/
internal var menuLocked = true
protected var menuLocked = true

/** 页面可用位置 **/
internal val menuSlots = CopyOnWriteArrayList<Int>()
protected val menuSlots = CopyOnWriteArrayList<Int>()

/** 页面可用元素回调 **/
internal var elementsCallback: (() -> List<T>) = { CopyOnWriteArrayList() }
protected var elementsCallback: (() -> List<T>) = { CopyOnWriteArrayList() }

/** 页面可用元素缓存 **/
internal var elementsCache = emptyList<T>()
protected var elementsCache = emptyList<T>()

/** 点击事件回调 **/
internal var elementClickCallback: ((event: ClickEvent, element: T) -> Unit) = { _, _ -> }
protected var elementClickCallback: ((event: ClickEvent, element: T) -> Unit) = { _, _ -> }

/** 元素生成回调 **/
internal var generateCallback: ((player: Player, element: T, index: Int, slot: Int) -> ItemStack) = { _, _, _, _ -> ItemStack(Material.AIR) }
protected var generateCallback: ((player: Player, element: T, index: Int, slot: Int) -> ItemStack) = { _, _, _, _ -> ItemStack(Material.AIR) }

/** 异步元素生成回调 **/
internal var asyncGenerateCallback: ((player: Player, element: T, index: Int, slot: Int) -> ItemStack) = { _, _, _, _ -> ItemStack(Material.AIR) }
protected var asyncGenerateCallback: ((player: Player, element: T, index: Int, slot: Int) -> ItemStack) = { _, _, _, _ -> ItemStack(Material.AIR) }

/** 页面切换回调 */
internal var pageChangeCallback: ((player: Player) -> Unit) = { _ -> }
protected var pageChangeCallback: ((player: Player) -> Unit) = { _ -> }

/** 页面玩家 **/
private lateinit var player: Player
private lateinit var viewer: Player

/**
* 是否锁定所有位置
Expand Down Expand Up @@ -119,11 +119,11 @@ open class Linked<T>(title: String) : Basic(title) {
page++
// 刷新页面
if (virtual) {
player.openVirtualInventory(build() as VirtualInventory).inject(this)
viewer.openVirtualInventory(build() as VirtualInventory).inject(this)
} else {
player.openInventory(build())
viewer.openInventory(build())
}
pageChangeCallback(player)
pageChangeCallback(viewer)
}
}
}
Expand All @@ -140,11 +140,11 @@ open class Linked<T>(title: String) : Basic(title) {
page--
// 刷新页面
if (virtual) {
player.openVirtualInventory(build() as VirtualInventory).inject(this)
viewer.openVirtualInventory(build() as VirtualInventory).inject(this)
} else {
player.openInventory(build())
viewer.openInventory(build())
}
pageChangeCallback(player)
pageChangeCallback(viewer)
}
}
}
Expand Down Expand Up @@ -193,13 +193,13 @@ open class Linked<T>(title: String) : Basic(title) {
* 构建事件处理函数
*/
fun processBuild(p: Player, inventory: Inventory, async: Boolean) {
player = p
viewer = p
elementItems.forEachIndexed { index, item ->
val slot = menuSlots.getOrNull(index) ?: 0
elementMap[slot] = item
// 生成元素对应物品
val callback = if (async) asyncGenerateCallback else generateCallback
val itemStack = callback(player, item, index, slot)
val itemStack = callback(viewer, item, index, slot)
if (itemStack.isNotAir()) {
inventory.setItem(slot, itemStack)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import taboolib.platform.util.isNotAir
open class Stored(title: String) : Basic(title) {

/** 页面规则 **/
internal val rule: Rule = EmptyRule
protected val rule: Rule = EmptyRule

/** 被改写 Rule 的 Stored 不支持虚拟页面 */
override fun virtualize(storageContents: List<ItemStack>?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import taboolib.module.nms.PacketReceiveEvent
import taboolib.module.nms.nmsProxy
import taboolib.module.ui.MenuHolder
import taboolib.module.ui.type.Anvil
import taboolib.module.ui.type.AnvilCallback
import java.util.concurrent.ConcurrentHashMap

/**
Expand Down Expand Up @@ -112,16 +113,16 @@ abstract class InventoryHandler {
val virtualInventory = playerRemoteInventoryMap[player.name]?.inventory
if (virtualInventory != null) {
val builder = MenuHolder.fromInventory(virtualInventory)
if (builder is Anvil) {
builder.renameCallback?.invoke(player, text, virtualInventory)
if (builder is AnvilCallback) {
builder.invoke(player, text, virtualInventory)
}
}
// 普通容器处理
else {
val openInventory = player.openInventory.topInventory
val builder = MenuHolder.fromInventory(openInventory)
if (builder is Anvil) {
builder.renameCallback?.invoke(player, text, openInventory)
if (builder is AnvilCallback) {
builder.invoke(player, text, openInventory)
}
}
}
Expand Down

0 comments on commit 17d3276

Please sign in to comment.