PlayerItemConsumeEvent

Discussion in 'Requests' started by CylekPOG, Jan 14, 2021.

  1. CylekPOG

    CylekPOG Spider

    Messages:
    6
    How to add command with PlayerItemConsumeEvent that execute when a player eat or drink into a specific item?
     
  2. minijaham

    minijaham Zombie

    Messages:
    222
    GitHub:
    minijaham
    PHP:
    public function onConsume(PlayerItemConsumeEvent $event) {
           
            
    $player $event->getPlayer();
            if(
    $event->getItem()->getId() === 373) { // water bottle
                       
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "Command without slash"); // Execute from console
                       
    $this->getServer()->dispatchCommand($player"Command without slash"); // Execute from player
            
    }
        }
    Might want to research before asking :l
     
    CylekPOG likes this.
  3. CylekPOG

    CylekPOG Spider

    Messages:
    6
    How about giving a player with that 373 with meta and custom name and executing the command after consuming it
     
  4. minijaham

    minijaham Zombie

    Messages:
    222
    GitHub:
    minijaham
    This is a code I used to use

    PHP:
    public function onConsume(PlayerItemConsumeEvent $event) {
            
    $player $event->getPlayer();
            if(
    $event->getItem()->getId() === 373) {
                
    $damage $event->getItem()->getDamage();
                switch(
    $damage) {
                    case 
    100// You can add multiple according to their meta(damage)
                    // Code
                    
    return true;
                    break;
                    case 
    101:
                    
    // Code
                    
    return true;
                    break;
                }
            }
        }
    You can have extra checks like
    PHP:
    if ($event->getItem()->getCustomName() === "Biggest Water"){

    So in combination, it could be something like this:

    PHP:
    public function onConsume(PlayerItemConsumeEvent $event) {
            
    $player $event->getPlayer();
            if(
    $event->getItem()->getId() === 373) {
                if (
    $event->getItem()->getCustomName() === "Biggest Water"){
                    
    $damage $event->getItem()->getDamage();
                    switch(
    $damage) {
                        case 
    100// You can add multiple according to their meta(damage)
                       // Code
                       
    return true;
                        break;
                        case 
    101:
                        
    // Code
                       
    return true;
                       break;
                    }
                }
            }
        }
     

Share This Page

  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.