Prevent Trade

Discussion in 'Spigot Plugin Development' started by Rusketh, Apr 16, 2017.

Thread Status:
Not open for further replies.
  1. Hi all,

    just a simple question real quick. How do i prevent a villager trade, i have a custom item that uses the emerald material and I do not want villagers to accept trades made with these items.
     
  2. Create a villager trade event, check input itemstack, check if material is emerald, check if it has an itemmeta, check if the itemmeta's displayname/lore is the one of your custom item, cancel the event if so.
     
  3. The problem is I cant find any reference to an existing villager trade event.
     
  4. Pulled from another thread on the old bukkit forums:

    "Not to pick a side, but yes InventoryClickEvent is what I needed in this instance, and then checked for type of MERCHANT on the click event's RawSlot.

    The goal here was to disable trading of written books if a special character was in the title (brought on by another part of my plug-in that in any other case would allow duping of emeralds) I've got it sorted out with these tips and some help from others, thanks to you both."
     
  5. Thank you for the replies. I have tried messing with the inventory interact event but so far no luck.

    Code (Text):

    @EventHandler
            public void AntiMerchant(InventoryClickEvent e) {
                ItemStack origonal = e.getCurrentItem();
               
                if (origonal != null) {
                    CustomItemStack item = new CustomItemStack(origonal);
                   
                    CustomItem obj = item.getCustomItem();
                   
                    if (obj != null) {
                            Inventory inv = e.getInventory();
                           
                            if (inv instanceof MerchantInventory) {
                                int slot = e.getRawSlot();
                               
                                if (slot == 0 || slot == 1) {
                                    InventoryAction act = e.getAction();
                                   
                                    if (act == InventoryAction.PLACE_ALL || act == InventoryAction.PLACE_SOME || act == InventoryAction.PLACE_ONE) {
                                       
                                        if (obj.getID() == this.getID())
                                            e.setCancelled(true);
                                    }
                                }
                            }
                    }
                }
     
    Anybody able to help me here?
     
  6. Solved thank you all.

    Here is my solution, alter it to your own needs for those who need a solution.

    Code (Text):


        public boolean placeIntoInventory(HumanEntity whoClicked, CustomItemStack item, Inventory inventory, int rawSlot, int slot) {
            return !((inventory instanceof CraftInventoryMerchant) && (slot == 0 || slot == 1));
        }

        @EventHandler
        public void InventoryClickEvent(InventoryClickEvent e) {
            InventoryAction act = e.getAction();
           
            if (act == InventoryAction.PLACE_ONE || act == InventoryAction.PLACE_SOME || act == InventoryAction.PLACE_ALL) {
                ItemStack item = e.getCursor();
               
                if (item != null && item.getType() != Material.AIR) {
                    if (!placeIntoInventory(e.getWhoClicked(), item, e.getInventory(), e.getRawSlot(), e.getSlot())) {
                        e.setCancelled(true);
                        return;
                    }
                }
            }
        }
     
     
Thread Status:
Not open for further replies.