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
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 } } } }}
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. }
Why not hasenchantment()? PHP: /** @var Item $item */ if($item->hasEnchantment(15)){ //Item has the enchantment. } else{ //Item does not have the enchantment. }
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?
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..
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)
But how did you create and add the custom enchant to the item? Also, please use [PHP] and [/PHP] tags around your code.
PHP: $breaker->sendMessage(c::YELLOW."Haste I".c::WHITE." was activated for 3 minutes."); $breaker->addEffect($effect);