How to cancel villager trading window?

Discussion in 'Plugin Development' started by Pr3castDragon, Jul 28, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Pr3castDragon

    I'm making a villager menu plugin, and to get the menus to open, the villager trading window must be cancelled. However, I don't know how exactly to cancel them. I tried this, but it didn't work:

    Code:
    @EventHandler
        public void villDisableTrade(InventoryOpenEvent trade){
    
            if(trade.getInventory().getType() == InventoryType.MERCHANT) {
                trade.setCancelled(true);
            }
        }
    Here's the whole class, although most of it is irrelevant
    Show Spoiler

    Code:
    package pw.pr3cast.VillagerGUI;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.metadata.FixedMetadataValue;
    
    public class Commands implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            //Checks for 'vgui' tag
            if (label.equalsIgnoreCase("vgui")) {
    
                //Base command w/o arguments
                if (args.length == 0) {
    
                    sender.sendMessage(prefix() + "This is VillagerGUI (for DeluxeMenus) version "+ version() + ", by Pr3cast.");
                } else {
    
                    //General commands
                    if (args[0].equalsIgnoreCase("help")) {
                        help();
                    }
    
                    //Checks if sender is player to initialize player variable & access player only commands
                    if (sender instanceof Player) {
    
                        Player player = (Player) sender;
    
                        //CMD for creating new villager
                        if (args[0].equalsIgnoreCase("new")) {
    
                            //checks if all argument are entered
                            if (args[2].length() <= 0) {
    
                                // Not all arguments fufilled, player is sent message detailing how to use the command
                                player.sendMessage(prefix() + "The correct usage is " + ChatColor.YELLOW + "\"/vgui new <name of villager> <DeluxeMenus menu name>\"");
                                return true;
                            } else {
    
                                newVillager(player, args[1], args[2]);
    
                                player.sendMessage(prefix() + "The TEST villager has been placed.");
                            }
                        }
                    } else sender.sendMessage(prefix() + "You aint a player, nigga");
                }
            }
    
            //Fufill boolean that is a boolean for no reason
            return true;
        }
    
        private void newVillager(Player player, String villName, String villMenu) {
    
            Location playerLoc = player.getLocation();
            Villager newVill = (Villager) player.getWorld().spawnEntity(playerLoc, EntityType.VILLAGER);
    
    
            newVill.setCustomName(villName);
    
            Bukkit.getLogger().info(prefix() + villMenu);
            newVill.setMetadata("menu", new FixedMetadataValue(Bukkit.getPluginManager().getPlugin("VillagerGUI"), villMenu));
        }
    
        @EventHandler
        public void villDisableTrade(InventoryOpenEvent trade){
    
            if(trade.getInventory().getType() == InventoryType.MERCHANT) {
                trade.setCancelled(true);
            }
        }
    
    //    @EventHandler
    //    public void villClick(PlayerInteractEntityEvent villEvent) {
    //
    //        if (villEvent.getRightClicked() instanceof Villager) {
    //
    //            Player clickAgent = (Player) villEvent.getRightClicked();
    //            Villager villClicked = (Villager) villEvent.getRightClicked();
    //
    //            clickAgent.sendMessage(prefix() + "The villager's metadata is " + villClicked.getMetadata("menu"));
    //            clickAgent.sendMessage(prefix() + "The villager's metadata toString is " + villClicked.getMetadata("menu").toString());
    //
    //            if (!villClicked.getMetadata("menu").equals(null)) {
    //
    //                clickAgent.sendMessage(prefix() + "This villager is not a vGUi villager.");
    //            } else {
    //
    //                clickAgent.sendMessage(prefix() + "This villager IS INDEED a vGUi villager.");
    //            }
    //        }
    //    }
    
        //Text
        public String prefix() {
            return (ChatColor.YELLOW + "[" + ChatColor.GREEN + "vGUI" + ChatColor.YELLOW + "] " + ChatColor.WHITE);
        }
    
        public String version() {
            return ("A0.1.3");
        }
    
        public String help() {
            return "in progress!";
        }
    }
    
     
  2. Offline

    Machine Maker

    @Pr3castDragon
    You can probably check the InventoryOpenEvent and check the inventory type to see if the type is a villager inventory view.
     
  3. Offline

    Pr3castDragon

    "trade.getInventory().getType() == InventoryType.MERCHANT"
    This is what I wrote. Is it not the same as what you just said?
     
  4. Offline

    Machine Maker

    @Pr3castDragon
    Wow duh, I didn't see that. Try, before canceling the event, closing the inventory of the player.
     
  5. Offline

    Pr3castDragon

    didn't work :p
     
  6. Offline

    Machine Maker

    @Pr3castDragon
    How are you opening up the other inventory? EntityInteractEvent? If so, cancel that event. That should prevent the inventory from opening up in the first place.
     
  7. Offline

    Pr3castDragon

    I haven't even written that event yet, so I have no idea why it doesn't work :( I pasted the entire class in the OP, if you want to read it.
     
  8. Offline

    Machine Maker

    @Pr3castDragon
    Forgot what I said about checking InventoryOpenEvent. A much better way (which I just tested myself) is to check PlayerInteractEntityEvent and check if the entity was a villager and if so, cancel the event. Then, after you cancel it, that's where you'll put the code to open up your own inventory.

    Code:
    @EventHandler
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        if (event.getRightClicked().getType() == EntityType.VILLAGER) {
            event.setCancelled(true);
        }
    }
     
  9. Offline

    Pr3castDragon

    Odd, this only works on green villagers and no other ones, even if they are newly spawned.
     
  10. Offline

    Pr3castDragon

    Nevermind :p in 1.12 all the villagers have different 'types' and the code does work on the "villager" type of villager. thank you!
     
  11. Offline

    Machine Maker

    @Pr3castDragon
    Glad I could help. Please mark this thread as Solved if your issue has been resolved.
     
  12. Offline

    RcExtract

    Try to listen to playerinteractevent

    Sent from my LG-H860 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page