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

Disabling Enchantments

Discussion in 'Development' started by minijaham, Dec 23, 2020.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Is there a way to completely disable an enchantment's ability?

    You can still enchant it, but it does nothing.
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    In case of Weapon and Protection enchantments, you can override the registered enchantments with your own objects.

    Create new class DisabledProtectionEnchantment extending ProtectionEnchantment. Override isApplicable and simply return false.
    Having the class done, create multiple instances with same parameters as it's predecessor and register it to finish disabling it (see Enchantment::registerEnchantment()).
    Repeat similar steps for next enchantments, however there are some that can't be disabled this way because their implementations are scattered throughout the source.

    One common thing these all implementations have, is to check for enchantment existence on the item, by calling Item::hasEnchantment() method, and then carrying out the modifications accordingly.
    If you could somehow override the method and return false instead, it would disable rest of enchantments.
    However it can't be done as Reflections does not allow to do that, and any other options I've considered disables enchantments on a client side as well.

    (see Enchantment::init())
     
    Last edited: Dec 24, 2020
  3. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Oh, I see. I'll try the methods you've provided :D Thanks!
     
  4. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    You can make this with events too:

    PHP:
    <?php

    namespace test;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\Player;

    class 
    test extends PluginBase implements Listener {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    noEnchant(Player $player) {
            foreach(
    $player->getInventory()->getContents() as $item) {
                if(
    count($item->getEnchantments()) != 0) {
                    foreach(
    $item->getEnchantments() as $enchant) {
                        
    $item->removeEnchantment($enchant->getId());
                    }
                }
            }
        }
        public function 
    itemHeld(PlayerItemHeldEvent $event) {
            
    $this->noEnchant($event->getPlayer());
        }
        public function 
    itemUse(PlayerInteractEvent $event) {
            
    $this->noEnchant($event->getPlayer());
        }
    }
     
  5. Primus

    Primus Zombie Pigman

    Messages:
    749
    OP wants to keep the enchantments, but disable their abilities.
     
  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.