Solved Why this code doenst work?

Discussion in 'Plugin Development' started by Gonmarte, Aug 29, 2015.

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

    Gonmarte

    Hi guys
    What is the error? When i test the plugin and i click in a villager runs an error :(
    Code :
    Code:
    @EventHandler
    public void onPlayerInteractVillager(PlayerInteractEntityEvent event) {
         
            Player player = event.getPlayer();
         
            if(event.getRightClicked() instanceof Villager) {
             
                Inventory inv = Bukkit.getServer().createInventory(null, 27, "Drugs");
    
                 inv.setItem(0, new ItemStack(Material.APPLE));
            
                   player.openInventory(inv);              
    
    
    
     
    Last edited: Aug 29, 2015
  2. Offline

    finalblade1234

    @Gonmarte
    Could you show us the error? You also should change inv.setItem, to inv.addItem
     
  3. Offline

    Gonmarte

    @finalblade1234 There isnt any error, when i right click in a villager doent happens anything.I
    With inv.addItem i cant get the slot, with inv.setItem i can get the slot.

    @finalblade1234 That plugin was a test so i didnt specify a slot, but ill change thank you.
    EDIT: I Changed it! Still doesnt work!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    WPM

    Code:
     inv.setItem(new ItemStack(Material.APPLE));
    You didnt specify where you wanted to put that item.
     
  5. Offline

    Gonmarte

    @finalblade1234 When i right click a villager opens the villagers inventory not the inventory that i created!

    I have already fix that!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  6. Offline

    WPM

    Sorry I didnt see the post before me. I am going to get a closer look into it now.
     
  7. Offline

    finalblade1234

    @Gonmarte Before you use openInventory, use event.setCanceled(true);
     
  8. Offline

    WPM

    And I figured out why it doesnt open the GUI, it is because when you right click a villager it already opens the trading GUI. So with that, here you go:


    Code:
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onPlayerInteractVillager(PlayerInteractEntityEvent e) {
           
            Player player = e.getPlayer();
        
            if(e.getRightClicked() instanceof Villager) {
                e.setCancelled(true);
                Inventory inv = Bukkit.getServer().createInventory(null, 27, "Drugs");
                inv.setItem(0, new ItemStack(Material.APPLE));
                player.openInventory(inv);  
            }
        }
     
  9. Offline

    Gonmarte

    @finalblade1234 What? It i use that ill cancel that event and i wont be able to right click on a villager and open a inventory.
     
  10. Offline

    WPM

    Canceling the event will disallow the player to open the default villager GUI which is the trading thing so you can open your custom inventory.
     
  11. Offline

    Gonmarte

  12. Offline

    WPM

    Great! Please mark as solved so people dont read over the whole thread and then find out it is already solved! :D
     
  13. Offline

    Gonmarte

    @WPM just more 1 thing, if i right click in all the villagers they will all open a inventory, there is anyway that we can check the villager that we want to open a inventory?
     
Thread Status:
Not open for further replies.

Share This Page