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

PlayerInteractEvent not working

Discussion in 'Development' started by ZenixPlayzMC, Dec 8, 2018.

  1. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    Hey guys. I have coded a something for my server and PlayerInteractEvent seems not to be working. It does not send any errors to console. Here is the code:
    PHP:
        public function execute(CommandSender $playerstring $label, array $args) {
            if(!(
    $player instanceof Player)) {
                
    $player->sendMessage("Please use this command in-game");
                return 
    true;
            }
            
    $gem Item::get(3991011);
          
            
    $gem->setCustomName(TF::RESET TF::AQUA "Ancient" TF::GRAY " Gem");
          
            
    $player->getInventory()->addItem($gem);
        }
      
        public function 
    onInteract(PlayerInteractEvent $event){
            
    $player $event->getPlayer();
            
    $item $event->getItem();
            
    $damage $item->getDamage();
            
    $ls CustomEnchants::getEnchantment(100);
            switch (
    $damage) {
                case 
    101:
                
    $gem Item::get(3991011);
                
    $sword Item::get(27601);
                
    $sword->addEnchantment(new CustomEnchants($ls1));
                
    $things = [$sword];
                
    $rand mt_rand(01);
                
    $player->getInventory()->addItem($things[$rand]);
                
    $player->getInventory()->removeItem($gem);
                break;
            }
            return 
    true;
        }
    }
    When i hold the gem and tap the ground, it doesnt work. If anyone can help me, thank you.
     
  2. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    Are you listening events in onEnable?
     
  3. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    I'm going to take great pleasure breaking this code in the future, because you hardcoded all the item IDs. :)
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    I don't get this.
     
    xXNiceAssassinlo YT likes this.
  5. Primus

    Primus Zombie Pigman

    Messages:
    749
    Why did someone put like on this post ^ ? :D

    This code breaks, unwanted behaviour occurs, to be exact, if Mojang decides to change item ids or you're adding the string ids to pocketmine, which I didn't consider as possibility when writing that post. If not, then method can't deny user input as it has no way to tell(as of now) if that input value came from constant.
     
  6. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    This is exactly my point. I've been warning people for months there's going to be armageddon because I'm gunning for item magic numbers, and everyone is hardcoding them.
     
    Muqsit likes this.
  7. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    I have considered some ways to sabotage this kind of code (yes, there are ways), but it would be nice if people would just ... not hardcode them.
     
    OnTheVerge and Muqsit like 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.