Solved ShootBowEvent

Discussion in 'Spigot Plugin Development' started by Hortexel, Mar 3, 2020.

  1. Hi, i have a problem:
    I would like to block the use of the bow in the 'spawn' region,
    Unfortunately I don't know how to do it, i wrote this event but it doesn't work




    Code (Java):
        @EventHandler
        public void onShoot(EntityShootBowEvent e){
            Player player = (Player) e.getEntity();

            Location loc = BukkitAdapter.adapt(player.getLocation());
            RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
            RegionQuery query = container.createQuery();
            ApplicableRegionSet set = query.getApplicableRegions(loc);

                for (ProtectedRegion region : set) {
                    player.sendMessage(region.getId());
                    if(region.getId().equalsIgnoreCase("spawn")){
                        e.setCancelled(true);
                        player.sendMessage(changeColor(plugin.getConfig().getString("message.disable-damage")));
                    }
                }



        }
     
  2. Have you tried ProjectileLaunchEvent? That may work
    And then just check if the player is trying to shoot an arrow with a bow
     
  3. 1- Is the event registered?
    2- Also, do not cast to Player before checking if it is really a player.
    And try the event that Aspiracy mentioned :)
     
    #3 Ckblck, Mar 4, 2020
    Last edited: Mar 4, 2020
    • Agree Agree x 1
  4. Any errors/warnings/etc. ?
     
    • Agree Agree x 1
  5. Also please note that e.getEntity() returns the arrow entity, not the one who shoot the arrow, use e.getShooter() instead
     
    • Agree Agree x 1
    • Agree Agree x 1
  6. Thanks :D

    Code (Java):
        @EventHandler
        public void onShoot(ProjectileLaunchEvent e){
            if(e.getEntity().getShooter() instanceof Player){
                Player player = (Player) e.getEntity().getShooter();
                if(!(player.hasPermission("bowfix.bypass")) || !(player.isOp())){
                    Location loc = player.getLocation();
                    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
                    RegionQuery query = container.createQuery();
                    ApplicableRegionSet set = query.getApplicableRegions(BukkitAdapter.adapt(loc));

                    for(String region : file.getStringList("regions")){
                        for (ProtectedRegion regions : set) {
                            if(regions.getId().equalsIgnoreCase(region)){
                                e.setCancelled(true);
                                player.sendMessage(changeColor(plugin.getConfig().getString("message.disable-damage")));
                            }
                        }
                    }
                }



            }
        }
     
    • Like Like x 1
    • Optimistic Optimistic x 1
  7. You're welcome!