Canceling Events

Discussion in 'Spigot Plugin Development' started by KingDotCom, Jun 9, 2016.

  1. Ok, so my plugin doesn't cancel the event, can anyone help?

    Code (Text):
        @EventHandler
        public void onClickE(PlayerInteractEvent e) {
            if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && (e.getPlayer().getInventory().getItemInHand().getType() == (Material.ENCHANTMENT_TABLE))) {
                if (e.getPlayer().hasPermission("addons.enchanting")) {
                    e.getPlayer().openEnchanting(e.getPlayer().getLocation(), true);
                    e.setCancelled(true);

                }else{
                    e.setCancelled(true);
                }
            }

        }
     
  2. Have you registered the class as a listener in your onEnable?
     
  3. As a listener? Dont know what you mean, I made everything in the main class
     
  4. You have to make your main class implement org.bukkit.event.Listener and register it in your onEnable with getServer().getPluginManager.registerEvents
     
  5. Yeah I did that.
     
  6. I copied your code and ran it and it works fine for me. Try putting debug messages in.
    (As in, put Bukkit.broadcastMessage("1"); in the first line of the eventhandler, then put 2 inside an if statement, etc. to narrow down where it's messing up)
     
  7. This is my main lol it doesnt work
     
  8. Code (Text):
    package me.s.s;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class M extends JavaPlugin implements Listener {

        public void onEnable() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(this, this);
        }
       
        @EventHandler
        public void onClickE(PlayerInteractEvent e) {
            if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && (e.getPlayer().getInventory().getItemInHand().getType() == (Material.ENCHANTMENT_TABLE))) {
                if (e.getPlayer().hasPermission("addons.enchanting")) {
                    e.getPlayer().openEnchanting(e.getPlayer().getLocation(), true);
                    e.setCancelled(true);

                }else{
                    e.setCancelled(true);
                }
            }

        }
       
    }
     
     
  9. Hi, do you have an error in your console ? you're plugin.yml is correct ?
    If his correct, set this in you're onEnable():
    Code (Text):
    System.out.println("The Plugin is detected");
     
    and this in you're event:
    Code (Text):

        @EventHandler
        public void onClickE(PlayerInteractEvent e) {
            if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && (e.getPlayer().getInventory().getItemInHand().getType() == (Material.ENCHANTMENT_TABLE))) {
                if (e.getPlayer().hasPermission("addons.enchanting")) {
                    e.getPlayer().openEnchanting(e.getPlayer().getLocation(), true);
                    e.setCancelled(true);

                }else{
                    e.setCancelled(true);
                }
            }else{
                e.getPlayer().sendMessage("Incorrect Action");
            }

        }
     
    now, reload you're plugin and restart you're server, check if in the console, there is "The Plugin is detected" and when you execute the action ( right click ) you do not have the message "Incorrect Action" so now you can locate your error
     
  10. there is no errors ... there has never been errors, the other guy said it worked, and it doesnt work. no sence lol the other guy has to be lying, the code doesn't work.
     
  11. Why would I be 'lying'? I copied it and pasted it into one of my plugins and ran it and it worked. Check your plugin.yml

    and your privilege ( ͡° ͜ʖ ͡°)
     
    #11 Trophonix, Jun 10, 2016
    Last edited: Jun 11, 2016
  12. I highly doubt he is lying. It is most likely something to do with your plugin. What exactly are you trying to cancel?
     
  13. Have it log a message in the onEnable so that we know for sure your plugin is running. Then have a broadcast message line of code in your event so we know the event is getting fired. If one of those don't work, then we can work from there.
     
  14. Code (Text):
    @EventHandler
        public void onClickE(PlayerInteractEvent e) {
            if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && (e.getPlayer().getInventory().getItemInHand().getType() == (Material.ENCHANTMENT_TABLE))) {
                if (e.getPlayer().hasPermission("addons.enchanting")) {
                    e.getPlayer().openEnchanting(e.getPlayer().getLocation(), true);
                 ---> e.setCancelled(true);

                }else{
                 ---> e.setCancelled(true);
                }
            }else{
                e.getPlayer().sendMessage("Incorrect Action");
            }

        }

    try to change true to false
     
  15. Why would he want to uncancel an uncancelled event? That makes no sense?!
     
    • Like Like x 1
  16. cancel only one so that the player can place the table
     
  17. my tips


    change : e.getPlayer().openEnchanting(e.getPlayer().getLocation(), true); (CHANGE THE true TO FALSE)

    CHECK: if all your } { are in the right place.

    Check your plugin.yml
     
  18. It's really rude to accuse somebody of lying. They don't have to help you.