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

Solved enchants?

Discussion in 'Development' started by DogTheBlock, May 25, 2017.

  1. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    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
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Yes it is. please show proof of a previous attempt before any more assistance is given.
     
  3. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    PHP:
    public function onEnchant(Player $player){
    if(
    $player->getInventory()->getItemInHand()->getEnchantment(12)){
    $player->getInventory()->getItemInHand()->getEnchantment(12)->setLevel(0);
    }
    }
     
    Last edited: May 27, 2017
  4. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
    Last edited: May 28, 2017
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    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
    }
     
    corytortoise and Muqsit like this.
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    From the api3/blocks branch

    PHP:
    public function removeEnchantment(int $idint $level = -1){
            if(!
    $this->hasEnchantments()){
                return;
            }
            
    $tag $this->getNamedTag();
            foreach(
    $tag->ench as $k => $entry){
                if(
    $entry["id"] === $id){
                    if(
    $level === -or $entry["lvl"] === $level){
                        unset(
    $tag->ench[$k]);
                        break;
                    }
                }
            }
            
    $this->setNamedTag($tag);
        }
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    That isn't on the master branch though, so it won't really be usable on production servers
     
  9. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    thank you!
     
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    people run bleeding edge builds on production servers time to time, just so they gets an edge over others
     
    jasonwynn10 likes this.
  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.