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

Is it possible to add an enchantment to an item in a chest?

Discussion in 'Plugin Help' started by KingRainbow44, Sep 17, 2019.

  1. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    I'm making a kits plugin using InvMenu and I wanted to make it so the player grabs the item in the chest. I tried adding an enchantment to an item in the chest.


    $menu->getInventory()->setContents([
    Item::get(Item::IRON_HELMET)->addEnchantment(EEnchantment::getEnchantment(1), 1);
    ]);

    I tried other methods too. This is the latest code. It this possible?
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    $i = Item::get(Item::IRON_HELMET);
    $i->addEnchantment(EEnchantment::getEnchantment(1);
    $menu->getInventory()->setItem($slot, $i);
     
  3. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    Didn't know you could do that. Thanks!
     
  4. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    Oof. It didn't work. Is it another way to do this?
     
  5. Rim

    Rim Spider Jockey

    Messages:
    28
    GitHub:
    boomyourbang
    Please show your code.
     
  6. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    public function Starter(Player $player) {
    $menu = InvMenu::create(InvMenu::TYPE_DOUBLE_CHEST)->setName(TextFormat::GREEN . "Kit Shop");
    $inventory = $menu->getInventory();

    $helmet = Item::IRON_HELMET;
    $enchInstance = new EnchantmentInstance(Enchantment::getEnchantment(17), 1);

    $i = Item::get(Item::IRON_HELMET);
    $i->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(17), 1));
    $menu->getInventory()->addItem(Item::get($i));

    /** @var Player $player */
    $menu->send($player);
    }
     
  7. Rim

    Rim Spider Jockey

    Messages:
    28
    GitHub:
    boomyourbang
    No need for $helmet, also why not just do $i->addEnchantment($enchInstance)? Seems cleaner to me.

    Your error is likely in your InvMenu class. Is it Musqit's InvMenu virion?
     
    Last edited: Sep 22, 2019
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    it should be
    PHP:
    $i Item::get(Item::IRON_HELMET);
    $i->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(Enchantment::UNBREAKING), 1));
    $menu->getInventory()->addItem($i);
     
  9. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    Yeah, I'm using Musqit's InvMenu virion.
     
  10. KingRainbow44

    KingRainbow44 Spider

    Messages:
    7
    GitHub:
    kingrainbow44
    Now it doesn't display errors or anything.


    public function Starter(Player $player) {
    $menu = InvMenu::create(InvMenu::TYPE_DOUBLE_CHEST)->setName(TextFormat::GREEN . "Kit Shop");
    $inv = $menu->getInventory();

    $i = Item::get(Item::IRON_HELMET);
    $i->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(Enchantment::UNBREAKING), 1));
    $inv->addItem($i);

    /** @var Player $player */
    $menu->send($player);
    }
     
  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.