diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Anvil.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Anvil.kt index ccb309767..8d3b7bbd6 100644 --- a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Anvil.kt +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Anvil.kt @@ -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) { diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/AnvilCallback.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/AnvilCallback.kt new file mode 100644 index 000000000..1823a25e2 --- /dev/null +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/AnvilCallback.kt @@ -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) +} \ No newline at end of file diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Basic.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Basic.kt index a8d63d0b1..5f60d0844 100644 --- a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Basic.kt +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Basic.kt @@ -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? = null + protected var storageContents: List? = 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() diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt index f0d44918c..b0f3681c5 100644 --- a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt @@ -19,31 +19,31 @@ open class Linked(title: String) : Basic(title) { private set /** 锁定所有位置 **/ - internal var menuLocked = true + protected var menuLocked = true /** 页面可用位置 **/ - internal val menuSlots = CopyOnWriteArrayList() + protected val menuSlots = CopyOnWriteArrayList() /** 页面可用元素回调 **/ - internal var elementsCallback: (() -> List) = { CopyOnWriteArrayList() } + protected var elementsCallback: (() -> List) = { CopyOnWriteArrayList() } /** 页面可用元素缓存 **/ - internal var elementsCache = emptyList() + protected var elementsCache = emptyList() /** 点击事件回调 **/ - 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 /** * 是否锁定所有位置 @@ -119,11 +119,11 @@ open class Linked(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) } } } @@ -140,11 +140,11 @@ open class Linked(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) } } } @@ -193,13 +193,13 @@ open class Linked(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) } diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Stored.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Stored.kt index 8028a46a7..13b1f9575 100644 --- a/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Stored.kt +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/type/Stored.kt @@ -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?) { diff --git a/module/module-ui/src/main/kotlin/taboolib/module/ui/virtual/InventoryHandler.kt b/module/module-ui/src/main/kotlin/taboolib/module/ui/virtual/InventoryHandler.kt index bbb1f4dff..bbef2406b 100644 --- a/module/module-ui/src/main/kotlin/taboolib/module/ui/virtual/InventoryHandler.kt +++ b/module/module-ui/src/main/kotlin/taboolib/module/ui/virtual/InventoryHandler.kt @@ -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 /** @@ -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) } } }