Solved How to set enchant on item in hand

Discussion in 'Plugin Help' started by AsponPL, Aug 5, 2019.

  1. AsponPL

    AsponPL Spider Jockey

    Messages:
    27
    Hello, i have a problem with my plugin this is for adding a enchant to an item in hand but it do nothing that's my code


    PHP:
    $player->getInventory()->getItemInHand()->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(5), 3));
     
  2. Deniel

    Deniel Spider Jockey

    Messages:
    30
    GitHub:
    DenielWorld
    $player = $event->getPlayer();
    $inv = $player->getInventory();
    $item = $inv->getItemInHand();
    $item->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(5), 3));
    $inv->setItemInHand($item);

    I don't know how to put the code in the format you have, but here is an example for how to fix your problem.
    When you add an enchantment to an $item, you basically make a clone of it in code, and in no way affect the player's inventory. This is why you need to setItemInHand to that specific item after adding an enchantment to it. I hope I helped you.
     
    wolfdale and HimbeersaftLP like this.
  3. AsponPL

    AsponPL Spider Jockey

    Messages:
    27
    it work thanks
     

Share This Page

  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.