Solved when exit trade give items from slots back to player?

Discussion in 'Spigot Plugin Development' started by Swiftlicious, Sep 14, 2019 at 12:05 AM.

  1. Code (Text):
        @EventHandler
        public void onVillagerTrade(InventoryClickEvent e) {
            if (e.getCurrentItem() == null || e.getCurrentItem().getType() == null) return;
            Player p = (Player) e.getWhoClicked();
            if (e.getInventory().getType() != InventoryType.MERCHANT) return;
                if (GameManager.isState(GameManager.GAME)) {
                    if (e.getInventory().getType() == InventoryType.MERCHANT) {
                        if (e.getSlotType() == SlotType.RESULT && e.getCurrentItem().getType() != Material.AIR) {
                            p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_YES, 1.0F, 1.0F);
                        } else if (e.getSlotType() == SlotType.CRAFTING && e.getCurrentItem().getType() != Material.AIR) {
                            p.updateInventory();
                    }
                }
            }
        }
    This is what I have (the last two lines was my attempt at my question but currently it doesn't work).
    I am trying to make it so when a player leaves a villager trade menu with items in the first two slots it won't drop them on the ground and instead give them back to the player's inventory. I'm not sure how to go about this I know the two slots are referred to as CRAFTING in a merchant inventory. (I use a custom villager so whatever may by default happen doesn't for me like the sound as you can see above that.
     
  2. You have a redundant if-statement (if (e.getInventory().getType() == InventoryType.MERCHANT)). Also, everything below your 2nd if statement is unnecessarily indented.

    What I would do is listen for InventoryCloseEvent (which isn't realistically cancellable), and utilize the fact that the inventory slots are numbered to retrieve the item(s) and add them back into the player's inventory yourself, then remove the items from the Merchant inventory so they don't duplicate. To find the InventoryType's specific slot arrangement, I'd recommend debugging/broadcasting the slot numbers when you click on them.
     
  3. Alright I seem to have got it down.
    EDIT: Had to redo it to check for null errors but this one DOES work
    Code (Text):
        @EventHandler
        public void onVillagerInventoryClose(InventoryCloseEvent e) {
            Player p = (Player) e.getPlayer();
            InventoryView i = p.getOpenInventory();
            if (!(i.getItem(0) != null || i.getItem(0).getType() != null || i.getItem(0).getType() != Material.AIR || i.getItem(1) != null || i.getItem(1).getType() != null || i.getItem(1).getType() != Material.AIR)) return;
            ItemStack currency = i.getItem(0);
            ItemStack currencyv2 = i.getItem(1);
            i.setItem(0, null);
            i.setItem(1, null);
           
            if (e.getInventory().getType() != InventoryType.MERCHANT) return;
                if (GameManager.isState(GameManager.GAME)) {
                    if (p.getOpenInventory().getTopInventory().getType() == InventoryType.MERCHANT) {
                        if (i.getBottomInventory().firstEmpty() != -1) {
                            if (currency != null && currency.getType() != Material.AIR) { p.getInventory().addItem(currency); }
                            if (currencyv2 != null && currencyv2.getType() != Material.AIR) { p.getInventory().addItem(currencyv2); }
                        } else {
                            Location l = i.getBottomInventory().getLocation();
                            if (currency != null && currency.getType() != Material.AIR) { l.getWorld().dropItemNaturally(l, currency); }
                            if (currencyv2 != null && currencyv2.getType() != Material.AIR) { l.getWorld().dropItemNaturally(l, currencyv2); }
                            p.updateInventory();  
                    }
                }
            }
        }
    I know some checks might be unnecessary but it can't hurt to see. Thanks for the help.
     
    #3 Swiftlicious, Sep 14, 2019 at 6:54 AM
    Last edited: Sep 14, 2019 at 9:18 AM