is it possible to remove a certain enchant from an item? like if someone has knockback 5 it removes that enchant but not the other enchants
PHP: public function onEnchant(Player $player){if($player->getInventory()->getItemInHand()->getEnchantment(12)){$player->getInventory()->getItemInHand()->getEnchantment(12)->setLevel(0);}}
Hm, I don't think there is a simple method for that. You can try unsetting the NBT tag 'ench' that holds the enchantment data. I can't give a working example, but you can see more about NBT here. This is probably a bit hackier, but you might be able to get all enchantments of an item, create a new item, then add any enchantments the former item had to the new item, excluding the enchantment you want to remove.
PHP: public function onEnchant(Player $player){ $enchants = $player->getInventory()->getItemInHand()->getEnchants(); // array or enchantment objects $item = clone $player->getInventory()->getItemInHand(); // cloned hand item $toRemove = 12; // enchant ID foreach($enchants as $ench) { // ench is an Enchantment object if($ench->getId() == $toRemove) { // if the enchantment is the set Id, do not add to the new item continue; } $item->addEnchantment(Enchantment::getEnchantment($ench->getId())); // if the enchantment is not the set Id, add to the new item } $player->getInventory()->setItemInHand($item); // set item in hand to be the same with new enchants}
From the api3/blocks branch PHP: public function removeEnchantment(int $id, int $level = -1){ if(!$this->hasEnchantments()){ return; } $tag = $this->getNamedTag(); foreach($tag->ench as $k => $entry){ if($entry["id"] === $id){ if($level === -1 or $entry["lvl"] === $level){ unset($tag->ench[$k]); break; } } } $this->setNamedTag($tag); }
people run bleeding edge builds on production servers time to time, just so they gets an edge over others