e.setCancelled(true); not working, not getting any errors.

Discussion in 'Spigot Plugin Development' started by Julianbos02, Jun 17, 2019.

  1. I'm making a cosmetics menu, but when I'm putting e.setcancelled(true); in... It won't work. I'm not getting an error either.
    Code (Text):
        @EventHandler
        public void onClickHoofdmenu(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            ItemStack item = e.getCurrentItem();
           
            if(item != null && item.getItemMeta() != null && item.getItemMeta().getDisplayName() != null) {
                if(e.getInventory().getName().equals("§3§l» §bLobbymenu §7- §bHoofdmenu")) {
                   
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bGlow-effecten")) {
                        p.sendMessage("§3§lSOLAR§b§lMC §7Deze functie is momenteel nog niet beschikbaar!");
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bChatkleur")) {
                        openChatColormenu.chatColorMenu(p);
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setResult(Result.DENY);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bBanners")) {
                        openBannermenu.bannerMenu(p);
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bInstellingen")) {
                        openSettingsmenu.settingsMenu(p);
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bPersoonlijke informatie")) {
                        openProfielmenu.profielMenu(p);
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bParticles")) {
                        p.sendMessage("§3§lSOLAR§b§lMC §7Deze functie is momenteel nog niet beschikbaar!");
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bTrails")) {
                        openTrailsmenu.trailsMenu(p);
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bHats")) {
                        p.sendMessage("§3§lSOLAR§b§lMC §7Deze functie is momenteel nog niet beschikbaar!");
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bGadgets")) {
                        p.sendMessage("§3§lSOLAR§b§lMC §7Deze functie is momenteel nog niet beschikbaar!");
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                    if(item.getItemMeta().getDisplayName().equals("§3§l» §bPets")) {
                        p.sendMessage("§3§lSOLAR§b§lMC §7Deze functie is momenteel nog niet beschikbaar!");
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, (float) 0.5, (float) 0.5);
                        e.setCancelled(true);
                    }
                }
            }
        }
    }
     
    #1 Julianbos02, Jun 17, 2019
    Last edited: Jun 17, 2019
  2. Please use a code block... do your conditions actually return true? Have you done any debugging?
     
  3. I edited the code block, Is it even possible to return a event?
     
  4. They mean your if conditions. Make sure the e.setCancelled line is actually getting executed and not negated due to the ifs.
     
  5. Hello,

    Are you even sure that your event is triggered? Have you correctly registered the event?
    If it's the case, then, as bwfcwalshy said, try to debug your event.
     
  6. everything above it, in the same box to say it, just works. except the e.setcancelled
     
  7. Uhh... Can you confirm that you successfully receive message (with p.sendMessage(...), but your event is not cancelled? Without any console error message?
     
  8. yeah sure, here you are
     

    Attached Files:

  9. The code is correct, this should work.. You should put only once e.setCancelled(true); at the first inventory name condition, that will include all items, but it's just a detail..
    What behavior do you expect from a e.setCancelled(true); ?
     
  10. I wan't to cancel that players can take the itemstack in the inventory. That player can only click on these. If you wan't I can make you a short video of how its (not) working?
     
  11. If you want to cancel it for every item in the inventory just put it under the inventory title check

    also use this for checking display names

    Code (Text):
    if(e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().hasDisplayName() && e.getCurrentItem().getItemMeta().getDisplayName().equals("Whatever")) {
    }
    //Edit
    you can replace e.getCurrentItem() with 'item' as you have a variable for it

    also I'm suggesting you are using an older api version, as now it's

    Code (Text):
    e.getView().getTitle()
     
  12. not working, its showing a error now
     
  13. whats in line 26?

    Code (Text):
    at com.julian.solarlobby.eventmanager.hoofdmenuClickEvent.onClickHoofdmenu(hoofdmenuClickEvent.java:26)
     
  14. your itemmeta check ect
    Code (Text):
    if(item.hasItemMeta() && e.getCurrentItem().getItemMeta().hasDisplayName() && e.getCurrentItem().getItemMeta().getDisplayName().equals("§3§l» §bLobbymenu §7- §bHoofdmenu")) {
     
  15. It has to do with your check. I'd recommend you to check first the current inventory name and create the further checks
     
  16. That's weird, this code always worked perfectly for me
     
  17. ik zie dat je Nederlands bent, dus dat praat wat makkelijker voor mij. Ik heb alles haarfijn nagekeken, niks werkte.
     
  18. Translation: I see that you are Dutch, so that makes it easier for me to talk. I checked everything in detail, nothing worked.

    Please use Google in all sections of the forums (aside from PMs). You could post both your native language and English via a translator if desired.

     
  19. I made a gui with banners, with exactly the same way.. and it is working fine..