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

Solved How to run a command when player interacts with an item?

Discussion in 'Development' started by Brant, Aug 16, 2020.

  1. Brant

    Brant Baby Zombie

    Messages:
    104
    GitHub:
    tetroyt
    I am new to PHP and im trying to setup a ServerMenu for the main HUB server where players can select the server they want to connect to but idk how to make it so the user opens a UI when they interact with the item

    PHP:
    <?php

    namespace Tetro\NetworkMenu;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\player\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\Commandsender;
    use 
    pocketmine\utils\TextFormat;

    use 
    jojoe77777\FormAPI\CustomForm;

    class 
    Main extends PluginBase {

      public function 
    onEnable() {
        
    $this->getServer()->getPluginManager()->registerEvent($this$this);
      }

      public function 
    onCommand($CommandSender $senderCommand $cmdstring $label, array $args) {
        switch(
    $cmd->getName()) {
          case 
    "servers":
            
    $this->openServerMenu($player);
          break;

          case 
    "info":
            
    $this->openInfoMenu($player);
          break;
        }
        
      }

      public function 
    openServerMenu($player) {
        
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
        
    $form $api->createSimpleForm(function (Player $senderint $datanull) {
          
    $result $data;
          if(
    $result === null) {
            
          }
        });
        
    $form->setTitle(TextFormat::PURPLE "Server Selector");
        
    $form->addButton(TextFormat::AQUA "Factions");
        
    $form->addButton(TextFormat::GREEN "Prison");
        
    $form->addButton(TextFormat::RED "Exit");
        
    $form->sendToPlayer($player);
        return 
    $form;
      }

      public function 
    openInfoMenu($player) {
        
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
        
    $form $api->createSimpleForm(function (Player $senderint $datanull) {
          
    $result $data;
          if(
    $result === null) {
            
          }
        });
        
    $form->setTitle(TextFormat::PURPLE "Psycho Network");
        
    $form->setContent(TextFormat::GREEN "Psycho is a network brought to you by Tetro \nand the Psycho staff team.\n\n" TextFormat::YELLOW "Social Medias:\n" TextFormat::AQUA "\nDiscord: \n\n" TextFormat::PURPLE "Store: psychope.tebex.io");
        
    $form->addButton(TextFormat::RED "Exit")
        
    $form->sendToPlayer($player);
        return 
    $form;
      }
    }
     
  2. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    PHP:
    public function onInteract(PlayerInteractEvent $event) {
      
    $item Item::get(200); //2 is grass block
       
    if ($event->getPlayer()->getItemInHand()->getId() === $item) {
       
    $this->getServer()->dispatchcommand($event->getPlayer(), "command here");
     }
       }
    set prefix to SPLVED ;)
     
  3. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    or if you can when player hit block to floor, use:
    PHP:
    public function onInteract(PlayerInteractEvent $event) {
      
    $item Item::get(200); //2 is grass block
       
    if ($event->getPlayer()->getItemInHand()->getId() === $item) {
       
    $this->openServerMenu($player);
     }
       }
    solved.
     
  4. Brant

    Brant Baby Zombie

    Messages:
    104
    GitHub:
    tetroyt
    thank you :D
     
  5. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    no problem ;)
     
  6. Besher

    Besher Witch

    Messages:
    51
    Hey both of these dont work they dont give errors or anything just nothing happens
     
  7. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    :|
    register events, its rude in chat
     
    Last edited: Aug 22, 2020
  8. Besher

    Besher Witch

    Messages:
    51
    Oh sorry but i dont understand what you mean by register events
     
  9. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    PHP:
    public function onEnable()
    {
        
    $this->getServer()->getPluginManager()->registerEvents($this$this); //now you registered events
    }
    use this code in this function in your Enable function (i reakky can't explain better, give me your code if it has error) :D
     
  10. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    You don’t have implemented Listener, maybe that can cause the problem too.

    Use:
    PHP:
    class Main extends PluginBase implements Listener
     
    GodWeedZao likes this.
  11. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    oh my god, i see the code full right now, there is a lot of wrongs in your code.
    wait im writing code for you.
     
  12. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    so in first you need to use these statments (after your namespace) :
    PHP:
    use jojoe77777\FormAPI;
    use 
    pocketmine\event\Listener
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;
    now you should paste this class with all functions instead your class:
    PHP:
    class Main extends PluginBase implements Listener
    {

        public function 
    onEnable()
        {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        
    /**
         * @param $player
         * @return mixed
         */
        
    public function openServerMenu($player)
        {
            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $api->createSimpleForm(function (Player $playerint $data null) {
                
    $result $data;
                if (
    $result === null) {
                    return;
                }
                switch (
    $result) {
                    case 
    "0":
                        
    $this->getServer()->dispatchcommand($player"transfer IP PORT");
                        break;
                    case 
    "1":
                        
    $this->getServer()->dispatchcommand($player"transfer IP2 PORT2");
                        break;
                }
            });
            
    $form->setTitle(TextFormat::DARK_PURPLE "Server Selector");
            
    $form->addButton(TextFormat::AQUA "Factions");
            
    $form->addButton(TextFormat::GREEN "Prison");
            
    $form->addButton(TextFormat::RED "Exit");
            
    $form->sendToPlayer($player);
            return 
    $form;
        }

        
    /**
         * @param PlayerInteractEvent $event
         */
        
    public function onInteract(PlayerInteractEvent $event)
        {
            
    $player $event->getPlayer();
            
    $item Item::get(200); //2 is grass block, you can change it to what ever you want
            
    if ($player->getInventory()->getItemInHand()->getId() === $item) {
                
    $this->openServerMenu($player);
                
    $player->sendMessage("Opening Server UI...");
            }
        }

    }
    now run your server and enjoy. ;)
    if it has errors, just give me the error.
     
  13. Besher

    Besher Witch

    Messages:
    51
    Thank you your the best :)
     
  14. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    no problem :)
     
  15. Brant

    Brant Baby Zombie

    Messages:
    104
    GitHub:
    tetroyt
    Hey im new to coding as you can tell i just have a question about one thing

    what is this for?

    ive seen it a few times now is it just notes to the coder or?
     
  16. Besher

    Besher Witch

    Messages:
    51
    its notes, it think
     
  17. Besher

    Besher Witch

    Messages:
    51
    i just tried this and it still dosent work nothing happens i have a grass block and i click and nothing happens, help!
     
  18. Brant

    Brant Baby Zombie

    Messages:
    104
    GitHub:
    tetroyt
    its because this code originally came with a config ive just grabbed a part of the code from the main so you'll need to add the ip and port yourself in the code and also give permission to the players to use 'transferserver'
     
  19. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    Send your code
     
  20. Besher

    Besher Witch

    Messages:
    51
    <?php

    namespace besher\test;

    use pocketmine\Server;
    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\utils\TextFormat;
    use jojoe77777\FormAPI;
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerInteractEvent;
    use pocketmine\item\Item;

    class main extends PluginBase implements Listener {

    public function onEnable()
    {
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    }

    public function onCommand(CommandSender $sender, Command $cmd, String $label, Array $args) : bool{
    switch($cmd->getName()){
    case "core":
    if($sender instanceof Player){
    $this->cosform($sender);
    } else {
    $sender->sendMessage("You cant open a ui when you are console");
    }
    }
    return true;
    }

    public function cosForm($player){
    $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form = $api->createSimpleForm(function (Player $player, int $data = null){
    $result = $data;
    if($result === null){
    return true;
    }
    switch($result){
    case 0:
    $this->openSizeForm($player);
    break;

    case 1:
    $this->openFlyForm($player);
    break;
    }
    });
    $form->setTitle("Cosmetics");
    $form->setContent("Cosmetics");
    $form->addButton("Size");
    $form->addButton("Fly");
    $form->sendToPlayer($player);
    return $form;
    }

    public function openFlyForm($player){
    $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form = $api->createSimpleForm(function (Player $player, int $data = null){
    $result = $data;
    if($result === null){
    return true;
    }
    switch($result){
    case 0:
    $player->setAllowFlight("true");
    break;

    case 1:
    $player->setAllowFlight("false");
    break;

    case 2:
    $this->cosForm($player);
    break;
    }
    });
    $form->setTitle("Fly");
    $form->setContent("Enable or Disable Flight");
    $form->addButton("Enable Flight");
    $form->addButton("Disable Flight");
    $form->sendToPlayer($player);
    return $form;
    }

    public function openSizeForm($player){
    $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form = $api->createSimpleForm(function (Player $player, int $data = null){
    $result = $data;
    if($result === null){
    return true;
    }
    switch($result){
    case 0:
    $player->setScale("5");
    break;

    case 1:
    $player->setScale("3");
    break;

    case 2:
    $player->setScale("1");
    break;

    case 3:
    $this->cosForm($player);
    break;
    }
    });
    $form->setTitle("Size");
    $form->setContent("Choose a SIZE");
    $form->addButton("Large");
    $form->addButton("Medium");
    $form->addButton("Normal");
    $form->addButton("Back");
    $form->sendToPlayer($player);
    return $form;
    }

    public function NaviForm($player){
    $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form = $api->createSimpleForm(function (Player $player, int $data = null){
    $result = $data;
    if($result === null){
    return true;
    }
    switch($result){
    case 0:
    $command = "sw random";
    $this->getServer()->getCommandMap()->dispatch($player, $command);
    break;

    case 1:
    $command = "is go";
    $this->getServer()->getCommandMap()->dispatch($player, $command);
    break;

    case 2:
    $player->sendMessage("BedWars is currently under construction");
    break;

    case 3:
    $player->sendMessage("TheBridge is currently under construction");
    break;
    }
    });
    $form->setTitle("§4§6NAVIGATOR");
    $form->setContent("§lChoose a MINIGAME you would like to PLAY");
    $form->addButton("§6§lSkyWars");
    $form->addButton("§l§7SKY§l§aBLOCK");
    $form->addButton("§l§4BED§f§lWars");
    $form->addButton("§l§cTHEB§l§9RIDGE");
    $form->sendToPlayer($player);
    return $form;
    }

    public function onInteract(PlayerInteractEvent $event)
    {
    $player = $event->getPlayer();
    $item = Item::get(2, 0, 0); //2 is grass block, you can change it to what ever you want
    if ($player->getInventory()->getItemInHand()->getId() === $item) {
    $this->cosForm($player);
    $player->sendMessage("Opening Server UI...");
    }
    }

    }
     
    Last edited: Aug 23, 2020
  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.