Skip to content

Commit

Permalink
[publish] 6.0.12 Update VirtualInventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jan 9, 2024
1 parent 8bf6303 commit 7379dbc
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ inline fun <reified T : Menu> HumanEntity.openMenu(title: String = "chest", buil
/**
* 打开一个构建后的菜单
*/
fun HumanEntity.openMenu(buildMenu: Inventory) {
fun HumanEntity.openMenu(buildMenu: Inventory, changeId: Boolean = true) {
try {
if (buildMenu is VirtualInventory) {
val remoteInventory = openVirtualInventory(buildMenu)
val remoteInventory = openVirtualInventory(buildMenu, changeId)
val basic = MenuHolder.fromInventory(buildMenu)
if (basic != null) {
remoteInventory.inject(basic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ open class Basic(title: String = "chest") : Menu(title) {
// 重新构建页面
build()
// 重新打开页面
viewers.forEach { it.openMenu(lastInventory) }
viewers.forEach { it.openMenu(lastInventory, changeId = false) }
} catch (ex: Throwable) {
ex.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class Stored(title: String) : Basic(title) {

/** 被改写 Rule 的 Stored 不支持虚拟页面 */
override fun virtualize(storageContents: List<ItemStack>?) {
if (rule == EmptyRule) throw UnsupportedOperationException("Stored does not support virtual pages.")
if (rule != EmptyRule) throw UnsupportedOperationException("Stored does not support virtual pages.")
else super.virtualize(storageContents)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class InventoryHandler {

abstract fun parseToCraftChatMessage(source: String): Any

abstract fun openInventory(player: Player, inventory: VirtualInventory, cursorItem: ItemStack = player.itemOnCursor): RemoteInventory
abstract fun openInventory(player: Player, inventory: VirtualInventory, cursorItem: ItemStack = player.itemOnCursor, updateId: Boolean = true): RemoteInventory

@PlatformSide([Platform.BUKKIT])
companion object {
Expand All @@ -40,11 +40,13 @@ abstract class InventoryHandler {

val playerRemoteInventoryMap = ConcurrentHashMap<String, RemoteInventory>()

fun getContainerCounter(player: Player): Int {
fun getContainerCounter(player: Player, updateId: Boolean = true): Int {
val id = playerContainerCounterMap.computeIfAbsent(player.name) { 0 }
val newId = id % 100 + 1
playerContainerCounterMap[player.name] = newId
return newId
return if (updateId) {
val newId = id % 100 + 1
playerContainerCounterMap[player.name] = newId
newId
} else id
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class InventoryHandlerImpl : InventoryHandler() {
}
}

override fun openInventory(player: Player, inventory: VirtualInventory, cursorItem: ItemStack): RemoteInventory {
val id = getContainerCounter(player)
override fun openInventory(player: Player, inventory: VirtualInventory, cursorItem: ItemStack, updateId: Boolean): RemoteInventory {
val id = getContainerCounter(player, updateId)
when (major) {
// 1.8 1.9, 1.10, 1.11, 1.12
// public static String getNotchInventoryType(InventoryType type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fun Inventory.virtualize(storageContents: List<ItemStack>? = null): VirtualInven
/**
* 使玩家打开虚拟页面
*/
fun HumanEntity.openVirtualInventory(inventory: VirtualInventory): RemoteInventory {
val remoteInventory = InventoryHandler.instance.openInventory(this as Player, inventory, ItemStack(Material.AIR))
fun HumanEntity.openVirtualInventory(inventory: VirtualInventory, updateId: Boolean = true): RemoteInventory {
val remoteInventory = InventoryHandler.instance.openInventory(this as Player, inventory, ItemStack(Material.AIR), updateId)
inventory.remoteInventory = remoteInventory
InventoryHandler.playerRemoteInventoryMap[name] = remoteInventory
// 唤起事件
Expand Down

0 comments on commit 7379dbc

Please sign in to comment.