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

Item Vanish

Discussion in 'Development' started by JblusMC, Aug 21, 2018.

  1. JblusMC

    JblusMC Spider

    Messages:
    14
    I would like that when we execute genre / admin that we delete our inventory and give us a renamed item that executes a command. how to do ?
     
  2. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    I think what you mean is "Clear the inventory of a player upon a command, followed by sending the player an item that calls a function upon interaction with that item." Correct me if I am wrong.
    Catch the command:
    PHP:
    /* ON COMMAND */
    $sender->getInventory()->clearAll(); // Clears the inventory.
    $sender->getInventory()->addItem(Item::get(Item::BED)->setCustomName("69 rounds of hulk smash.")); // Adds item with a custom name.
    In your event listener:
    PHP:
    public function catchInteraction(PlayerInteractEvent $event) { // Whenever a player interacts.
        
    if (($item $event->getPlayer()->getInventory()->getItemInHand()) instanceof Bed && $item->hasCustomName()) { // Checks if the item in hand is a bed and has a custom name.
            /* DO STUFF */
        
    }
    }
    IMPORTANT NOTES:
    • If players create a bed with a custom name, any custom name, your block of code under the if condition will be called. In order to fix this, use NBT Tags.
    • Make sure that you have imported the Item class namespace (in this case, Bed) otherwise your code wouldn't work and you won't get an error because PHP doesn't detect errors after the instanceof function if the target is an object.
     
    Last edited: Aug 21, 2018
  3. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    For Listensr why didnt u use if item is bed, but your is good too lol.
     
  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.