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

Enchantments With NBT

Discussion in 'Development' started by Junkdude, Mar 7, 2017.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    How would I go about adding enchantments via nbt? Heres my code(P.S. ITS WRONG!):
    PHP:
    if($args[0] === "cetest")
            {
                
    $tag $item->getNamedTag();
                
    $count 0;
                foreach(
    $tag->ench as $key => $value){
                    if(
    is_numeric($key)){
                        
    $count++;
                    }
                }
                if(isset(
    $tag->ench)){
                
    $tag $tag->ench;
                if(
    $tag instanceof ListTag){
                    foreach(
    $item->getEnchantments() as $en)
                    {
                    
    $player->sendMessage("lit");
                    
    $item->setCustomName("sick");
                    
    $tag = new CompoundTag('', []);
                    
    $tag->ench->{$count 1} = new CompoundTag("", [
                    
    "id" => new ShortTag("id"$en->getId()),
                    
    "lvl" => new ShortTag("lvl"$en->getLevel())
                    ]);
                    
    $item->setNamedTag($tag);
                    
    $inv->setItemInHand($item);
                    return 
    true;

                }
            }
        }
                    
                }
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    @Muqsit could you have any help in this?
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Muqsit likes this.
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Set the custom name before adding the enchantment. This was a bug but was fixed long back.
     
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Did but it repeatedly drops out of ur inventory,
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's a problem with MCPE itself. For some weird reason, an item with 3+ NBT tags just drop it off the inventory.
     
  9. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    yeah but i have an item with 1 enchantment and a custom name and it does that
     
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    @Muqsit how do you do item nametags then for Cosmic?
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
  12. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  13. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  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.