1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Getting all enchant levels

Discussion in 'Development' started by DiamondGamer30, Jul 22, 2018.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    What's the best way to get all enchant levels of certain enchants (for instance: enchants on armor pieces) without to many steps? I'm unsure if theres a better way, this is currently what I have:

    PHP:
    $entity $event->getEntity();
    $enchants = [];
    foreach(
    $entity->getArmorInventory()->getContents() as $armor) {
        foreach(
    $armor->getEnchantments() as $enchant) {
            if(!
    $enchant->getType() instanceof CustomEnchantment) {
                break;
            }
            if(isset(
    $enchants[$enchant->getId()])) {
                
    $enchants[$enchant->getId()] += $enchant->getLevel();
                break;
            }
            
    $enchants[$enchant->getId()] = $enchant->getLevel();
        }
    }
     
  2. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    I forgot to mention, this is called on EntityDamageByEntityEvent.
     
  3. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Why break? That would stop the foreach, you should use continue.
     
  4. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Oof.smh I should've read more into the php manual xD.
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    that's more of a common sense the names gives a lot of hint
    Code:
    break
    Code:
    continute
    
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.