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

Checking For Enchants

Discussion in 'Plugin Help' started by ObeyBeastJj23, May 29, 2017.

  1. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    Does Anyone Know The Way To Check If An Enchantment Like 15 For Efficiency Is On An Item?
    If So Please Comment How To Do So As It Would Be Great Help To Me :D
     
  2. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    PHP:
    public function onItem(PlayerItemHeldEvent $heldEvent) {
        
    $player $heldEvent->getPlayer();
        
    $item $player->getInventory()->getItemInHand();
        
    $max 15//max level an enchants can have
        
    if ($item instanceof Item) {
            if (
    $item->hasEnchantments()) {
                foreach (
    $item->getEnchantments() as $enchantment) {
                    if (
    $enchantment->getLevel() >= $max) {
                        
    //Do something you want if the level is higher than 15
                    
    }
                }
            }
        }
    }
     
  3. Hipster

    Hipster Zombie

    Messages:
    214
    He's not asking about enchantment level, hes asking if the item has efficiency enchant on it -_-
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Item->getEnchantment(int $id) returns either an Enchantment object or null. I believe this would work:
    PHP:
     /** @var Item $item */
     
    if($item->getEnchantment(15) != null){
      
    //Item has the enchantment.
      
    } else{
      
    //Item does not have the enchantment.
     
     
  5. Hipster

    Hipster Zombie

    Messages:
    214
    Why not hasenchantment()?
    PHP:
    /** @var Item $item */
     
    if($item->hasEnchantment(15)){
      
    //Item has the enchantment.
      
    } else{
      
    //Item does not have the enchantment.
     
     
  6. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    @corytortoise Could i also check the level of that specified enchantment?
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Yes. Since it returns an Enchantment or null, you could save the return value in a variable, and after making sure it is an Enchantment, you could use returnVariable->getLevel() to see the level.
    I was originally going to post that, but I can't find it in Item.php. Can you link to it?
     
  8. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    Ok this isnt working for me as it isnt returning anything to state a yes i think so it cant run a randomiser
    by the way i am making a Custom Enchants plugin for my Prison server and it works if i use getCustomName() but if i try getEnchantment(15) != null it wont work..
     
  9. MyNET

    MyNET Silverfish

    Messages:
    24
    Show us ur code
     
  10. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    How are the Custom Enchants handled? How do you add them to items?
     
  11. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    public function onBlockBreak(BlockBreakEvent $break) {
    $breaker = $break->getPlayer();
    $thing = $break->getItem();
    if($thing->getEnchantment(21)) {
    $breaker = $break->getPlayer();
    $effect = Effect::getEffect(3);
    $effect->setVisible(true);
    $effect->setDuration(3528);
    $effect->setAmplifier(1);
    switch (mt_rand(1,10)) {


    (By the way if i seem a bit Noob like its because im just a starter at php and writing plugins but am a kinda fast learner)
     
  12. MyNET

    MyNET Silverfish

    Messages:
    24
    Did u forgot to add this
    use pocketmine\item\enchantment\Enchantment;
     
  13. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    Nope its added

    By the way ii also tried to remove the != null off of the end as you can see
     
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    But how did you create and add the custom enchant to the item? Also, please use [PHP] and [/PHP] tags around your code.
     
  15. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    PHP:
    $breaker->sendMessage(c::YELLOW."Haste I".c::WHITE." was activated for 3 minutes.");
                
    $breaker->addEffect($effect);
     
  16. MyNET

    MyNET Silverfish

    Messages:
    24
    PHP:
    $thing Enchantment::getEnchantment(21);
     
  17. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    its more of a custom effect instead of enchantment
     
  18. MyNET

    MyNET Silverfish

    Messages:
    24
    Show us your whole code lel.
    Plus please use
    PHP:
     and 
    around ur code
     
  19. MyNET

    MyNET Silverfish

    Messages:
    24
    Tf! Oh PLAIN...
     
  20. ObeyBeastJj23

    ObeyBeastJj23 Spider

    Messages:
    13
    -
     
    Last edited: May 29, 2017
  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.