Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is Enchanting Items Possible?

Comments in 'Plugin Development' started by Heroata, Apr 9, 2016.

  1. Heroata
    Offline

    Heroata New Member

    Joined:
    Dec 15, 2013
    Posts:
    31
    Minecraft User:
    Heroata
    I got an Item with this code:


    Code:
      $sender->getInventory()->addItem(new Item(276, 0, 1));
    
    But how can I ench this item?
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Use Item::get() instead of new Item().
    PHP:
    $item Item::get($id$meta$amount);
    $item->addEnchantment($enchantment);
  3. Heroata
    Offline

    Heroata New Member

    Joined:
    Dec 15, 2013
    Posts:
    31
    Minecraft User:
    Heroata
    PHP:
            $item Item::get(27602);
            
    $item->addEnchantment(10);
            
    $sender->getInventory()->addItem($item);
    Scaring to post this but doesnt work :rolleyes:
    hoyinm14mc and Skullex like this.
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $enchantment Enchantment::getEnchantment(10);
    $enchantment->setLevel(/*The level you want*/);
    $item->addEnchantment($enchantment);
    If you would read the error in your console you would know that it wants an Enchantment, not an integer.
  5. Heroata
    Offline

    Heroata New Member

    Joined:
    Dec 15, 2013
    Posts:
    31
    Minecraft User:
    Heroata
    Error with this code:
    PHP:
            $item Item::get(27602);
            
    $item->addEnchantment(10);
            
    $sender->getInventory()->addItem($item);
    Code:
    [21:17:47] [Server thread/CRITICAL]: Unhandled exception executing command 'olybe' in olybe: Argument 1 passed to pocketmine\item\Item::addEnchantment() must be an instance of pocketmine\item\enchantment\Enchantment, integer given, called in /home/ataberk/server/server/plugins/PhpProject1/src/AndrOyuN/test/test.php on line 32
    [21:17:47] [Server thread/CRITICAL]: TypeError: "Argument 1 passed to pocketmine\item\Item::addEnchantment() must be an instance of pocketmine\item\enchantment\Enchantment, integer given, called in /home/ataberk/server/server/plugins/PhpProject1/src/AndrOyuN/test/test.php on line 32" (EXCEPTION) in "/src/pocketmine/item/Item" at line 870
    
    Okay but how can I add this enchant to item with 276 id?

    PHP:
            $enchantment Enchantment::getEnchantment(10);
            
    $enchantment->setLevel(2);
            
    $item->addEnchantment($enchantment);   
            
    $sender->getInventory()->addItem($item);
    I want to give somebody enchanted 276.
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Learn. PHP.
    hoyinm14mc likes this.
  7. Heroata
    Offline

    Heroata New Member

    Joined:
    Dec 15, 2013
    Posts:
    31
    Minecraft User:
    Heroata
    Dude really? I just dunno pocket mine api. Php doesnt teach me how to give an enchanted item.
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Actually it says that you must pass Enchantment class as first argument - thats pure PHP
    Survingo likes this.
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  10. Heroata
    Offline

    Heroata New Member

    Joined:
    Dec 15, 2013
    Posts:
    31
    Minecraft User:
    Heroata
    I thought PocketMine-MP-master includes this class.
  11. Xx__ItzTyler__xX
    Offline

    Xx__ItzTyler__xX New Member

    Joined:
    Oct 28, 2015
    Posts:
    24
    Minecraft User:
    SuperDude36
    Can somebody please put a line of code :)?
  12. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
  13. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Can somebody please read every post and understand them before asking for a line of code :)?
    If you don't understand the code (for example, how do you know it is called addEnchantment), it is pointless even if you put a line of code.

    P.S. We have many lines of code above!
    MCPEPIG, CraftYourBukkit and Survingo like this.
  14. Xx__ItzTyler__xX
    Offline

    Xx__ItzTyler__xX New Member

    Joined:
    Oct 28, 2015
    Posts:
    24
    Minecraft User:
    SuperDude36
    Sorry, it says addEnchantment. But how does it know which item to put the enchantment on?
    My modified crate key plugin has this:
    Code:
    case 7:
              $inventory->addItem(Item::get(310,0,1));
              $inventory->addItem(Item::get(311,0,1));
              $inventory->addItem(Item::get(312,0,1));
              $inventory->addItem(Item::get(313,0,1));
              $player->sendMessage("§r§l§1[SC]§r§3 You won a set of diamond gear!");
            break;
    
    How would I add enchantments to the diamond gear?
  15. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    PHP:
    $enchantment Enchantment::getEnchantment(10);
    $enchantment->setLevel(1);
    $helmet Item::get(31001);
    $chestplate Item::get(31101);
    $leggings Item::get(31201);
    $boots Item::get(31301);
    $inv player->getInventory();
    $helmet->addEnchantment($enchantment);
    $chestplate->addEnchantment($enchantment);
    $leggings->addEnchantment($enchantment);
    $boots->addEnchantment($enchantment);
    $inv->addItem($helmet);
    $inv->addItem($chestplate);
    $inv->addItem($leggings);
    $inv->addItem($boots);
    Primus, SOFe and Survingo like this.

Share This Page

Advertisement