kotlinpluginsminecraftspigot

Include Armor in openInventory() Kotlin


I am trying to write a, minecraft, program where I can type /invsee "target" and I will be able to see the players inventory in real time. With the following code its easy to see the players inventory

val targetInventory: Inventory = target.inventory
player.openInventory(targetInventory)

but I really want to be able to see their armor as well which this wont show. I have been trying to add the players armor to be added into my own inventory in slots 9-12, to avoid creating a whole new inventory with expanded dimensions(not sure if this will be necessary). The issue I am running into is getting the pieces to be dynamic and disappear/change when the target player takes off/changes their armor pieces.

private fun addArmorToPlayerInventory(player: Player, target: Player) {
        val armorItems = arrayOf(
            target.inventory.helmet,
            target.inventory.chestplate,
            target.inventory.leggings,
            target.inventory.boots
        )
        for (i in armorItems.indices) {
            player.inventory.setItem(9 + i, armorItems[i] ?: ItemStack(Material.AIR))
        }
    }

What is the best way to get dynamic values and display them with the rest of the players inventory?


Solution

  • I think the best way to do this is by keeping track (e.g., a Map<UUID, UUID>) of who is watching whom and listening to the following two events:

    These two events will allow you to detect changes in the target's armor slots and update the items in the watchers' inventories accordingly.