1.15.2 How do I cancel a player event?

Discussion in 'Spigot Plugin Development' started by colonialsoldier, Jan 20, 2020.

  1. I'm new to programming in general. I figured out how to make a custom leave and join message but beyond that I am lost. I want to cancel players from being able to place blocks unless they have the permission node.
    How do I achieve this?
  2. There is a BlockPlaceEvent that you can use. You can use Player#hasPermission(String permissionNode) to test if a player has the permission. If they don't, you can use BlockPlaceEvent#setCancelled(true) to cancel the event.
    • Like Like x 2
    • Agree Agree x 1
  3. FrostedSnowman

    Resource Staff

    • Like Like x 3
  4. Qhucy is prettymuch spot on, without giving you copy/paste-able code. I assume that you're learning via YouTube? (That's how I learned at least.) If so, you should continue through the series (Or however you were learning,) as it will most likely cover this later on. A good youtuber I used is {TheSourceCode}.

    As FrostedSnowman said, not all events are cancellable. An example of this is InventoryClose. If you wish to 'cancel' this you have to reopen the inventory a tick (or so) later.
    • Like Like x 1
    • Friendly Friendly x 1
  5. Thank you. I've been watching YouTube videos and just kind of copying their code, but I think its better for me to just test things out and use different resources such as this rather than just copy code.
  6. You're right. I've been watching a lot of YouTube videos about it. I have watched the {TheSourceCode} (I'm subscribed in fact!), and also Kody Simpson. Mostly I've been copying their code, which is ok for a little bit, but I want to move a little faster and I feel like trying to do a little more advanced stuff, and I feel like figuring things out myself and using resources like this will help me better understand what I'm coding instead of just copying code.
  7. Also, you need to register your event using Bukkit.getPluginManager().registerEvents(<your class implementing Listener>, this); in your onEnable() method.

    Also, make sure that your method for the event is below a @EventHandler line and that the class of your event is implement the class Listener from Bukkit.
  8. Code (Java):
    if(!event.isCancelled()) {

  9. FrostedSnowman

    Resource Staff

    In this case you’d just use the ignoreCancelled field in your EventHandler annotation
  10. I don't tested it!

    Code (Java):
        public void onBlockPlace(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            if(!player.hasPermission("build.bypass") || !player.hasPermission("*")) {
                if(!event.isCancelled()) {
                    player.sendMessage(ChatColor.RED+"You can't build without permission!");
    • Like Like x 1
  11. Strahan


    You're way better off to take the time to learn and understand the code you copy so you can fix things. You aren't learning shit just grabbing someone else's code and dumping it into your project.

    You don't need to test for permission wildcards, that's the job of the permissions manager.
    • Like Like x 1
    • Agree Agree x 1