onInventoryClick Help

Discussion in 'Plugin Development' started by Aceix8, Sep 26, 2016.

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

    Aceix8

    So Im Making A plugin and i have this code...

    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Inventory Name"))
                return;
           
            Player player = (Player) event.getWhoClicked();
            event.setCancelled(true);
           
            if (event.getCurrentItem() == null
                    || event.getCurrentItem().getType() == Material.AIR
                    || !event.getCurrentItem().hasItemMeta()) {
                player.closeInventory();
                return;
               
            }
           
            switch (event.getCurrentItem().getType()){
            case ENDER_PEARL:
                player.chat("/wild");
                player.closeInventory();
                break;
           
            case ANVIL:
                player.chat("/fix");
                player.closeInventory();
                break;
               
            case REDSTONE:
                player.chat("/i redstone");
                player.closeInventory();
                break;
           
            default:
                player.closeInventory();
            }
        }
    Whenever You Click It Doesnt Do A Thing To Anything

    Can i have some help ive been stuck for a hour already on this xD
     
  2. Offline

    Zombie_Striker

    @Aceix8
    Are you sure you are registering the class? Have you tried debugging? If so, does your event ever get called?
    Are you sure this is true? Are you sure this is not returning too soon?
     
  3. Offline

    Ste4lthPr0xy

    So thats what I do most of the time when i work with the InventoryClickEvent... dont really see where your problem is... but maybe this could help you :D

    Code:
    
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Inventory inv = event.getInventory();
        if(!(event.getWhoClicked() instanceof Player)) {
            return;
        }
     
        if(!(inv.getTitle().equals("Menu"))) {
            return;
        }
     
        Player player = (Player) event.getWhoClicked();
        ItemStack item = event.getCurrentItem();
        if(item.getType() == Material.COMPASS) {
            player.teleport(player.getWorld().getSpawnLocation());
            player.sendMessage("Teleported to SpawnLocation");
            player.getWorld().playEffect(player.getLocation(), Effect.CLOUD, 1);
        }
        event.setCancelled(true);
        player.closeInventory();
    }
    
    
     
  4. Offline

    Lordloss

    @Ste4lthPr0xy Hello spoonfeeder, please stop it. You wont help anybody with this. Also, people will copy & paste it, and come back after 5 minutes because the type check is throwing an NPE.
    Never post code to beginners. They have to figure out how it works to really understand whats going on, this way they will not. What is even worse is posting broken code like yours, or code with bad habits.

    Feel free to also read the 1 trillion other posts about spoonfeeding, thank you.
     
    Zombie_Striker and bwfcwalshy like this.
  5. Offline

    Ste4lthPr0xy

    @Lordloss well, ofc it does work, because u dont have the rest of the code, but i was just trying to give him some orientation how it could be done on some other ways, i was not trying to give him some code he should just copy & paste... anyways i think he should be able to decide it on his own whether he wants to just copy it, or if he wants to learn something. Also I dont really understand why you are calling me a spoonfeeder.... i was not intended to give him all he need, without doing anything to understand it... i was trying to give him like mentioned before "to give him some orientation" maybe to compare his code with the code of others...

    I would love to hear why u think my code is broken? It work perfectly if u implement it the right way, i would love to send you a pm, with my full source code, so you would be able to see the "broken" code on its beauty...

    Anyways... my opinion is, that Forums like this are there to help each other... I dont want to argue at any point with anyone, but thats just how Fourms work... I'm Sorry for you, because you have obviously made bad experience with like you call "spoonfeeders"... but i really could not know, on what stand of knowledge he is.... Anyways... if you wish, i will delete my first post on this thread?

    Greetings Ste4lthPr0xy

    Excuse me for my bad English | im from Germany :D
     
  6. Offline

    Lordloss

    But this is what most of the new guys are doing. And your code is broken in that way, that it will spam NPEs if clicked any slot which is empty, or clicked outside of the window.
     
  7. Offline

    Zombie_Striker

    @Ste4lthPr0xy
    This is what he was referring to. The current item can be null if he clicks on anything that is not an item. You have to null check before getting the item's type.

    Again, do not spoonfeed new players. If a person needs code in order to understand what you're saying, either you are not explaining your point correctly or they should not be programming.
     
  8. Offline

    Ste4lthPr0xy

    Yea, well i didn't see that, thank you :)... well maybe i should explain myself more, just because i am programming for years now, i might expect others to be at the same level... well... thats stupid, thanks anyways :D
     
Thread Status:
Not open for further replies.

Share This Page