NichtStudioCode / InvUI

A spigot library for creating custom inventory-based GUIs.
MIT License
242 stars 19 forks source link

It is possible to take Item from the GUI #38

Closed BlackBaroness closed 1 year ago

BlackBaroness commented 1 year ago

Hello. When using shift+click at ScrollGui, it is possible to take item to player inventory.

Paper 1.16.5. Kotlin.

    private fun openFactoryGui(player: Player, factory: PlacedFactory) {
        val productionGui = ScrollGui.items()
            .setStructure(
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x"
            )
            .addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
            .setContent(factory.config.items.map { ProductionItem(factory, it) })
            .build()

        val inputInventoryGui = Gui.normal()
            .setStructure(
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x"
            )
            .addIngredient('x', factory.inputInventory)
            .build()

        val outputInventory = factory.outputInventory
        outputInventory.setPreUpdateHandler { if (it.isAdd) it.isCancelled = true }

        val outputInventoryGui = Gui.normal()
            .setStructure(
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x"
            )
            .addIngredient('x', factory.outputInventory)
            .build()

        val tabGui = TabGui.normal()
            .setStructure(
                "# # 0 1 # 2 3 # #",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x",
                "x x x x x x x x x"
            )
            .setTabs(listOf(productionGui, inputInventoryGui, outputInventoryGui))
            .addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
            .addIngredient('0', TabButton(Material.IRON_PICKAXE, Component.text("Производство"), 0))
            .addIngredient('1', TabButton(Material.CHEST, Component.text("Инвентарь станка"), 1))
            .addIngredient('2', TabButton(Material.ENDER_CHEST, Component.text("Готовые вещи"), 2))
            .addIngredient('3', InfoButton(factory))
            .addIngredient('#', ItemBuilder(Material.GRAY_STAINED_GLASS_PANE).setDisplayName(Component.empty()))
            .build()

        server.scheduler.runTask(plugin, Runnable {
            Window.single()
                .setGui(tabGui)
                .setTitle("Станок")
                .build(player)
                .open()
        })
    }

I use tab gui with 3 tabs - ScrollGui and 2 Virtual Inventories (Gui). Player can extract item from the ScrollGui, what is completely unexpected.

NichtStudioCode commented 1 year ago

Is this issue also present on 1.20?

BlackBaroness commented 1 year ago

No

        server.pluginManager.registerEvents(object : Listener {
            @EventHandler
            fun onLoad(event: PlayerJoinEvent) {
                val productionGui = ScrollGui.items()
                    .setStructure(
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x"
                    )
                    .addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
                    .addContent(SimpleItem(ItemWrapper(ItemStack(Material.STONE))))
                    .build()

                val inputInventoryGui = Gui.normal()
                    .setStructure(
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x"
                    )
                    .addIngredient('x', VirtualInventoryManager.getInstance().createNew(UUID.randomUUID(), 18))
                    .build()

                val outputInventoryGui = Gui.normal()
                    .setStructure(
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x"
                    )
                    .addIngredient('x', VirtualInventoryManager.getInstance().createNew(UUID.randomUUID(), 18))
                    .build()

                val tabGui = TabGui.normal()
                    .setStructure(
                        "# # 0 1 # 2 3 # #",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x",
                        "x x x x x x x x x"
                    )
                    .setTabs(listOf(productionGui, inputInventoryGui, outputInventoryGui))
                    .addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
                    .addIngredient('0', object : TabItem(0) {
                        override fun getItemProvider(gui: TabGui?): ItemProvider =
                            ItemWrapper(ItemStack(Material.CHEST))
                    })
                    .addIngredient('1', object : TabItem(1) {
                        override fun getItemProvider(gui: TabGui?): ItemProvider =
                            ItemWrapper(ItemStack(Material.STONE))
                    })
                    .addIngredient('2', object : TabItem(2) {
                        override fun getItemProvider(gui: TabGui?): ItemProvider =
                            ItemWrapper(ItemStack(Material.STONECUTTER))
                    })
                    .addIngredient('#', ItemBuilder(Material.GRAY_STAINED_GLASS_PANE).setDisplayName(Component.empty()))
                    .build()

                server.scheduler.runTask(plugin, Runnable {
                    Window.single()
                        .setGui(tabGui)
                        .setTitle("Станок")
                        .build(event.player)
                        .open()
                })
            }
        }, plugin)
BlackBaroness commented 1 year ago

This issue was because of me. I have used

override fun handleClick(clickType: ClickType, player: Player, event: InventoryClickEvent) {
            player.closeInventory()
}

instead of

override fun handleClick(clickType: ClickType, player: Player, event: InventoryClickEvent) {
            server.scheduler.runTask(plugin, Runnable { player.closeInventory() })
}