Solved e.setCancelled(true); Not working!

Discussion in 'Spigot Plugin Development' started by goats123123456, May 21, 2016.

  1. Im currently working on a GUI plugin and on my wool GUI the "e.setCancelled(true);" will not work. Any player can move or take the items. Can someone help?

    [EDIT] Here is some of my code:

    Code (Text):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            e.setCancelled(true);
            if (!e.getInventory().getName().equals(ChatColor.translateAlternateColorCodes((char)'&', (String)"&b&lWool Menu"))) {
                return;
            }
            Player p = (Player)e.getWhoClicked();
            e.setCancelled(true);
            if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || !e.getCurrentItem().hasItemMeta()) {
                return;
            }
            if (e.getCurrentItem().equals((Object)Wool.wool0)) {
                if (e.getWhoClicked().hasPermission("Hats.WhiteWoolHead")) {
                    e.setCancelled(true);
                    p.playSound(p.getLocation(), Sound.ORB_PICKUP, 10.0f, 10.0f);
                    e.getWhoClicked().getInventory().setHelmet(Wool.wool0);
                    Title.sendActionBar(p, String.valueOf(ChatColor.translateAlternateColorCodes((char)'&', (String)this.plugin.getConfig().getString("Prefix"))) + ChatColor.translateAlternateColorCodes((char)'&', (String)this.plugin.getConfig().getString("SelectMessage")) + (Object)ChatColor.WHITE + "Wool Head");
                    e.getWhoClicked().closeInventory();
     
  2. -
     
    #2 AdamJLE, May 21, 2016
    Last edited: Mar 16, 2022
  3. Im so confused.
     
  4. Show your main class- and your plugin.yml

    Are you registering your events?
     
  5. Here you go.


    Main:
    Code (Text):
    package hat;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import Click.HatsMenuClick;
    import Click.ItemInteract;
    import Click.WindowMenuClick;
    import Events.ArmorSlot;
    import Items.Hats;
    import Items.Window;
    import Items.Wool;
    import Select.Selector;

    public class Main extends JavaPlugin implements Listener {
        public static FileConfiguration config;

        public void onEnable() {
            Hats.setupHats();
            Window.setupwindowHats();
            Wool.setupWoolHats();
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents((Listener)new ArmorSlot(), (Plugin)this);
            pm.registerEvents((Listener)new HatsMenuClick(this), (Plugin)this);
            pm.registerEvents((Listener)new WindowMenuClick(this), (Plugin)this);
            pm.registerEvents((Listener)new ItemInteract(this), (Plugin)this);
            pm.registerEvents((Listener)new Selector(this), (Plugin)this);
            pm.registerEvents((Listener)this, (Plugin)this);
            config = this.getConfig();
            config.addDefault("InventoryTitle", (Object)"&b&lHats");
            config.addDefault("Prefix", (Object)"&b&lHats &7&l>> ");
            config.addDefault("SelectMessage", (Object)"&bYour New Hat &7> ");
            config.addDefault("PermissionMessage", (Object)"&4You can't do this command!");
            config.addDefault("RemoveMessage", (Object)"&4You removed your hat!");
            config.options().copyDefaults(true);
            this.saveConfig();
        }

        public void onDisable() {
        }

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if (label.equalsIgnoreCase("hats") && sender.hasPermission("Hats.use")) {
                if (args.length == 0) {
                    Selector.openSelectMenu(p);
                    return true;
                }
                if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("Hats.reload")) {
                    if (args.length == 1) {
                        Title.sendActionBar(p, ChatColor.translateAlternateColorCodes((char)'&', (String)(String.valueOf(this.getConfig().getString("Prefix")) + (Object)ChatColor.GREEN + " Config Reloaded!")));
                        this.reloadConfig();
                    }
                }
            }
            return false;
        }
    }

     
    Plugin.yml:
    Code (Text):
    name: HatsGUIPro
    version: 1.0
    main: hat.Main
    commands:
    hats:
      description: Main Command!
    hats reload:
      description: Reload Command!
     
     
  6. @goats123123456 Why are you casting the classes to Listener? If they already implement Listener, it is useless. If they don't implement Listener, make them and remove the cast.

    What class is that event method in?
     
    • Agree Agree x 1
  7. One GUI works perfectly but the Wool GUI lets players take out the items and move them around...
     
  8. Thanks for the help guys i fixed the problem!
     
  9. 4chan

    Supporter

    lmfao this code
    so many casts
     
    • Agree Agree x 1