Projectiles and explosions affecting scheduler bug

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

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

    shohouku

    So I have a anti combat log but however projectiles and explosions make the scheduler buggy. Why is that?

    The timer gets decremented wrong. For example sometimes it counts down too fast or the numbers are out of order counting from 10 only when the player gets attacked by a projectile or an explosion.

    Code:
    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;
            }
            }
        }
     
    Last edited: Jan 29, 2020
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    shohouku

    Edit:

    The timer gets decremented wrong. For example sometimes it counts down too fast or the numbers are out of order counting from 10 only when the player gets attacked by a projectile or an explosion.

    You can test the code if you want.
     
  4. Online

    timtower Administrator Administrator Moderator

    I can't test as I don't have the right machines here.
    You are using a global timer that all players use.
    Make it player specific.
     
  5. Offline

    shohouku

    How do I make it player specific?
     
  6. Online

    timtower Administrator Administrator Moderator

    Hashmap
     
  7. Offline

    shohouku

    How would I add the timer into the hashmap?

    For example decrementing the int inside the hashmap for the timer.
     
  8. Online

    timtower Administrator Administrator Moderator

    @shohouku You get the value, lower it, set it again?
     
  9. Offline

    shohouku

    How do you decrement integers inside hashmaps?
     
  10. Online

    timtower Administrator Administrator Moderator

     
  11. Offline

    shohouku

    @timtower
    I think the issue has to do something with the projectiles and explosions. I've tried adding the player into a hashmap but the same thing happens.

    Hitting a mob and getting hit by a mob works but for some reason projectiles and explosions don't seem to work properly with the timer.
     
    Last edited: Jan 29, 2020
Thread Status:
Not open for further replies.

Share This Page