Trouble running to next if statement.

Discussion in 'Plugin Development' started by VinexAx789, Jul 4, 2019.

  1. Online

    VinexAx789

    Hello everyone,

    I am trying to stop an if statement from running if a map called "alphaUsed" already contains the player. The player will be the damager in this case. If the "alphaUsed" map does not equal true or if "attackersAlpha" is equal to damager, then it will run the first statement. If there is another damager it will goto the next statement which will add them to the map called "bravoUsed".

    Code:
    Code:
        @EventHandler
        public void onPlayerDamaged(EntityDamageByEntityEvent e) {
            if (!(e.getEntity() instanceof Player) || (!(e.getDamager() instanceof Player)))
                return;
            Player victim = (Player) e.getEntity();
            Player damager = (Player) e.getDamager();
     
            // RUNS TWICE NOT LOCKING AFTER FIRST HIT, SHOULDN'T BE LOCKED FOR THE ORIGINAL            // DAMAGER
            if (alphaUsed.get(damager.getUniqueId()) != true
                    || attackersAlpha.get(victim.getUniqueId()).equals(damager.getUniqueId())) {
                alpha.put(victim.getUniqueId(), alpha.get(victim.getUniqueId()) - e.getFinalDamage());
                victimHealth.put(victim.getUniqueId(), victim.getHealth());
                attackersAlpha.put(victim.getUniqueId(), damager.getUniqueId());
                alphaUsed.put(damager.getUniqueId(), true);
                Bukkit.broadcastMessage("RAN - " + Bukkit.getPlayer(attackersAlpha.get(victim.getUniqueId())).getName());
            } else if (bravoUsed.get(damager.getUniqueId()) != true
                    || attackersBravo.get(victim.getUniqueId()).equals(damager.getUniqueId())) {
                Bukkit.broadcastMessage("RAN BRAVO");
                bravo.put(victim.getUniqueId(), bravo.get(victim.getUniqueId()) - e.getFinalDamage());
                victimHealth.put(victim.getUniqueId(), victim.getHealth());
                attackersBravo.put(victim.getUniqueId(), damager.getUniqueId());
                bravoUsed.put(damager.getUniqueId(), true);
            }
        }
    The current issue is: When one damager hits the victim they're added to the map alphaUsed, which is good. But when the second damager hits the victim they should be added to the map bravoUsed (Not working). However, it's actually running the first if statement again instead of moving down to the Bravo section. Therefore it takes two hits from the same damager for the next damager to be added in the Bravo section.

    Any thoughts?
     
    Last edited: Jul 4, 2019
  2. Offline

    Kars

    You can use Map.containsKey(key) to check if a map contains said key.

    Anyway. Your code is hard for me to understand.
    You want to execute some code when a second attacker hits a victim? Keep a Map<Player, Player> in which the key is the victim and the value is the attacker. On a player hits player event, check the content of the map. If it contains the key for victim, you'll have what you are looking for.
     
  3. Online

    VinexAx789

    Yeah, the code and the concept itself is confusing AF!

    So pretty much what I want to achieve is when the first damager hits the victim they'll get locked to the alpha maps.
    Second, I want the next damager that hits the same victim to be placed in the bravo maps.

    What it is currently doing is that when the first damager hits the player they're added to the alpha maps but the second damager is also running alpha instead of going to the else if bravo.

    Hope this clears it up.
     
  4. Offline

    Kars

    @VinexAx789 So everybody that attacks after the first attacker goes to bravo?
    Keep each victim that gets hit in a list. In the event, check if victim is in the list.
     
  5. Online

    VinexAx789

    So I am only showing alpha and bravo. I have a max of 3 enemies per player. What I want to achieve is basically the first damager to attack the victim will be placed into the alpha section, then the second damager will goto bravo if they do not equal alpha, same exact theory with charlie.
     
  6. Offline

    Kars

    PHP:
    if (alpha.containsKey(attacker)) {
        if (
    bravo.containsKey(attacker)) {
            
    charlie.put(attacker);
        } else {
            
    bravo.put(attacker);
        }
    } else {
        
    alpha.put(attacker);
    }
        
     
  7. Online

    VinexAx789

    @Kars

    This is exactly what I have currently. Gross looking but:
    PHP:
    if (alphaUsed.get(damager.getUniqueId()) != true
                    
    || attackersAlpha.get(victim.getUniqueId()).equals(damager.getUniqueId())) {
                
    alpha.put(victim.getUniqueId(), alpha.get(victim.getUniqueId()) - e.getFinalDamage());
                
    victimHealth.put(victim.getUniqueId(), victim.getHealth());
                
    attackersAlpha.put(victim.getUniqueId(), damager.getUniqueId());
                
    alphaUsed.put(damager.getUniqueId(), true);
                
    Bukkit.broadcastMessage("RAN - " Bukkit.getPlayer(attackersAlpha.get(victim.getUniqueId())).getName());
            } else if (
    alphaUsed.get(damager.getUniqueId()) == true && !attackersAlpha.get(victim.getUniqueId()).equals(damager.getUniqueId()) && bravoUsed.get(damager.getUniqueId()) != true
                    
    || attackersBravo.get(victim.getUniqueId()).equals(damager.getUniqueId())) {
                
    Bukkit.broadcastMessage("RAN BRAVO");
                
    bravo.put(victim.getUniqueId(), bravo.get(victim.getUniqueId()) - e.getFinalDamage());
                
    victimHealth.put(victim.getUniqueId(), victim.getHealth());
                
    attackersBravo.put(victim.getUniqueId(), damager.getUniqueId());
                
    bravoUsed.put(damager.getUniqueId(), true);
            } else if (
    charlieUsed.get(damager.getUniqueId()) != true
                    
    || attackersCharlie.get(victim.getUniqueId()).equals(damager.getUniqueId())) {
                
    charlie.put(victim.getUniqueId(), charlie.get(victim.getUniqueId()) - e.getFinalDamage());
                
    victimHealth.put(victim.getUniqueId(), victim.getHealth());
                
    attackersCharlie.put(victim.getUniqueId(), damager.getUniqueId());
                
    charlieUsed.put(damager.getUniqueId(), true);
    }
    EDIT: Current code runs alpha twice for two different damagers, but if the second damager hits the victim for a third time they'll be placed into bravo.
     
  8. Offline

    Kars

    @VinexAx789 This is not what you have. Your conditionals don't nest. Also you don't use containsKey anywhere. Gross looking, yes. And you should never use get on a map to assert that it contains a value.

    Your first if statement says if damager is not in the list OR victim is in the list. This is always true because (likely) the list is empty at first and if it isn't, you add victim to it.
    The else statements never get executed.

    Use my code.
     

Share This Page