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

Get enchantments

Discussion in 'Development' started by DiamondGamer30, Feb 21, 2019.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    What’s the most efficient way to get all the enchantments of a player as like the all armor enchantments and enchantments on the item the player is currently holding.
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Fair warning. This is example code. I don't recommend copying it.

    through the Player class, use getArmorInventory() to get the armor slots, then use the getAll() or whatever the equivalent function is to get an array output of the items in the slots. next loop through each of their enchantments and add them to an array variable.
    To get the item in the player's hand, go through the Player class to getInventory() to the the actual inventory. Then, get use the getItemInHand() function to get the item in hand. This description is shown in the following code. $list is the output enchantment list
    PHP:
    foreach($player->getArmorInventory()->getAll() as $item) {
        foreach(
    $item->getEnchantments() as $Enchantment) {
            
    $list[] = $enchantment;
        }
    }
    foreach(
    $player->getInventory()->getItemInHand()->getEnchantments() as $enchantment) {
        
    $list[] = $enchantment;
    }
     
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    That's what I do right now but I'm asking if there's any other way. A way that I can cache the enchantments.
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    save the enchantment instance to a private array in the class you are doing your comparison? then update on inventory transaction event (good luck with that though)
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Thanks.
     
  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.