Solved Closing inventory & adding items

Discussion in 'Spigot Plugin Development' started by HockeyFreak062, Feb 18, 2020.

  1. Hey, I'm trying to make a new enchanting system. Let me explain a bit. You can use the command /Merchant Enchanting to open the enchanting inventory. When you click an item it gets transferred to Slot 13 of the opened inventory. Then you can click some books to enchant the item. This is working perfectly, but when you use E or ESC to close the inventory without taking the Item of enchanting back it gets deleted with the inventory. How would I add the item back to the players inventory when the inventory is closed with E or ESC?
    There are two options that would work for me. One, Close the inventory and add the item or seccondly cancel the event when they try to close it with E or ESC. I have no clue how to do either. Can someone help?
     
  2. You can stop them closing the inventory or add the item when they're closing it by using the event InventoryCloseEvent
     
    • Useful Useful x 1
  3. Thanks! I'll try something out.

    I have used this
    Code (Java):
     
    @EventHandler
    public void onClose(InventoryCloseEvent event){

        Player p = (Player) event.getPlayer();

        ItemStack item = event.getInventory().getItem(13);

        if(event.getInventory().getTitle().equalsIgnoreCase("       Enchanting Merchant")) {
            p.getInventory().addItem(item);
            p.updateInventory();
        }
    }
    And it worked thanks!
     
    #3 HockeyFreak062, Feb 18, 2020
    Last edited: Feb 18, 2020