Bow Logic - Help Please

Discussion in 'Plugin Development' started by OhYes, Mar 9, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    OhYes

    In my plugin, I have made it to where all players are PvP protected until they have a kit via the /kit command. This works PERFECTLY. Apart from one issue which practically has made me disable any kits with bows in, in my server. Even if a player doesn't have a kit, (pvp protected) they can still take damage from players with bows. Idk why either. Secondly, easy one I assume, how do you check if an arrow shot from a player, has hit another player, and if so, how do you return the shot-player so you can mess around with em?

    Code:
     //Arrow ability.
      
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onEnderBow(ProjectileHitEvent e){
            if(!(e.getEntity() instanceof Arrow)) return;
            Arrow a = (Arrow) e.getEntity();
            if(!(a.getShooter()instanceof Player)) return;
            final Player p = (Player) a.getShooter();
          
          
            if(!(cooldowns.contains(p))){
                p.teleport(a.getLocation());
                p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 3, 3);
                cooldowns.add(p);
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
                    public void run(){
                        cooldowns.remove(p);
                    }
                },100);
            }
        }
      
        //Pvp protection.
      
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e){
            if(e.getEntity() instanceof Player){
                Player p = (Player) e.getEntity();
                if(e.getDamager() instanceof Player){
                    Player damager = (Player) e.getDamager();
                    if((!(pvp.containsKey(p)))||(!(pvp.containsKey(damager)))){
                      
                        damager.sendMessage(ChatColor.RED + "You cannot attack this person.");
                        e.setCancelled(true);
                        if(e.getCause()==DamageCause.PROJECTILE){
                            e.setCancelled(true);
                            return;
                        }
                        return;
                    }
                }
            }
        }
    I tried using "
    if(e.getCause()==DamageCause.PROJECTILE){
    e.setCancelled(true);
    return;" but it does not work.
     
  2. Offline

    Zombie_Striker

    @OhYes
    That is because if a player is damaged by an arrow, the EntityDamageByEntityEvent will be called, and the damager will be an instanceof Arrow.

    Check if the damager is Arrow in the EntityDamageByEntityEvent, and cancel the event if the shooter (use Arrow.getShooter() to return the shooter) is not in the pvp collection.
     
    OhYes likes this.
  3. Offline

    OhYes

    Thank you! :D
     
  4. Offline

    Gonmarte

    Mark this as solved :)
     
  5. Offline

    OhYes

    @Gonmarte @Zombie_Striker Ah, got one more problem before that though. I made it so the ender tp effect will activate when you have a bow in your off-hand too! but now it won't work in main hand :$
    Code:
     @SuppressWarnings("deprecation")
        @EventHandler
        public void onarrowHit(EntityDamageByEntityEvent e){
            if(!(e.getDamager() instanceof Arrow)) return;
            Arrow a = (Arrow) e.getDamager();
            if(!(a.getShooter() instanceof Player)) return;
            final Player shooter = (Player) a.getShooter();
            if(!(e.getEntity() instanceof Player)) return;
            Player hit = (Player) e.getEntity();
            if((!(pvp.containsKey(shooter))) || (!(pvp.containsKey(hit)))){
             shooter.sendMessage(ChatColor.RED + "You cannot attack this person.");
             e.setCancelled(true);
             return;
             }
            if(shooter.getInventory().getItemInOffHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_PURPLE + "Ender Bow")
                    || shooter.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_PURPLE + "Ender Bow")){
                if(!(cooldowns.contains(shooter))){
                    shooter.teleport(hit.getLocation());
                    shooter.getWorld().playSound(shooter.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 3, 3);
                    cooldowns.add(shooter);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
                        public void run(){
                            cooldowns.remove(shooter);
                        }
                    },100);
                }
            }
        }
        
     
  6. Offline

    Gonmarte

    Sorry, i was not paying attention to this thread and im pretty busy right now :/
     
  7. Offline

    Zombie_Striker

    @OhYes
    Have you debugged? What is the display name of "item in main hand"? What is the displayname of "item in off hand"
     
Thread Status:
Not open for further replies.

Share This Page