Solved Projectile and explosions affecting scheduler

Discussion in 'Spigot Plugin Development' started by kinukin, Jan 29, 2020.

  1. So I have a anti combat log but however projectiles and explosions make the scheduler seem to make the timer not work properly. Why is that?
    You can test the code and you'll see what I mean.
    Code (Text):
      int time = 10;
    public ArrayList<String> combatlogged = new ArrayList<>();
        @EventHandler
        public void onDamage (EntityDamageByEntityEvent e) {
            Entity attacked = e.getEntity();
            Entity attacker = e.getDamager();
            if (e.getEntity() instanceof Entity && e.getDamager() instanceof Entity) {
                if(!combatlogged.contains(attacked.getName()) || (!combatlogged.contains(attacker.getName()))) {
                combatlogged.add(attacked.getName());
                combatlogged.add(attacker.getName());
                attacker.sendMessage(ChatColor.GOLD + "You're now in Combat!");
                attacked.sendMessage(ChatColor.GOLD + "You're now in Combat!");
           
                new BukkitRunnable() {
                     @Override
                        public void run()
                        {
                         if ((combatlogged.contains(attacker.getName())) && (combatlogged.contains(attacked.getName()))) {
                            if (time == 0)
                            {
                                   combatlogged.remove(attacker.getName());
                                   combatlogged.remove(attacked.getName());
                                   attacker.sendMessage(ChatColor.GREEN + "You can now log out safely.");
                                   attacked.sendMessage(ChatColor.GREEN + "You can now log out safely.");
                                   time = 10;
                                   this.cancel();
                                   return;
                            }
                            attacked.sendMessage(time + " second(s) remains!");
                            attacker.sendMessage(time + " second(s) remains!");          
                        time--;                            
                        }
                        }          
                }.runTaskTimer(Main.getInstance(), 20L, 20L);    
            } else {
                attacker.sendMessage("added 10 seconds to combat time");
                attacked.sendMessage("added 10 seconds to combat time");
                time = 10;
            }
            }
        }
     
    #1 kinukin, Jan 29, 2020
    Last edited: Jan 30, 2020
  2. I think you should have a look at the entity types you are receiving from getEntity() and getDamager()

    Looks like your treating the damager as a player, however arrows aren't players - You'll need to do some checks and retrieve the player from the arrow.