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

How can i make it so when i interact with an item it runs a command?

Discussion in 'Development' started by Artemio Velasquez, Nov 26, 2017.

  1. Artemio Velasquez

    Artemio Velasquez Creeper

    Messages:
    5
    <?php

    namespace MagicalShards;

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

    class Main extends PluginBase implements Listener {

    public function onEnable() {
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    $this->getLogger()->info(TextFormat::GREEN . "Mshards Enabled");
    }

    public function onDisable() {
    $this->getLogger()->info(TextFormat::RED . "Mshards Disabled");
    }

    public function onCommand(CommandSender $sender, Command $cmd, string $label,array $args) : bool {

    switch($cmd->getName()){

    case "magicalshard":

    $name = $this->getServer()->getPlayerExact($args[0]);

    if($name instanceof Player) {
    $item = Item::get(409, 0, 1);
    $item = $item->setCustomName(TextFormat::GREEN . TextFormat::BOLD . "MagicalShard" . TextFormat::RESET . TextFormat::GRAY . "(Tap Anywhere)" . "\n" . "\n" . TextFormat::RESET . TextFormat::DARK_PURPLE . "Rewards:" . "\n" . TextFormat::RESET . TextFormat::GRAY . "-MoneyPouches");
    $name->getInventory()->addItem($item);
    }
    }
    return true;
    }

    public function onInteract(PlayerInteractEvent $event){

    $player = $event->getPlayer();
    $item = $player->getInventory()->getItemInHand();

    if($item->getId() == 409){
    $event->setCancelled();
    $player->addTitle(TextFormat::AQUA.TextFormat::BOLD."You Claimed your Price!");
    }
    }
    }
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    $item $event->getItem();
    To run a command you use Server::dispatchCommand(). In your Main class used by your plugin, you can use $this->getServer()->dispatchCommand($commandSender, "command without slash here");
     
  3. Artemio Velasquez

    Artemio Velasquez Creeper

    Messages:
    5
    thx but could you like put it into my code??
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    figure it out ;)
     
  5. Artemio Velasquez

    Artemio Velasquez Creeper

    Messages:
    5
    one more question how can i also make it so when the player taps the shard it removes it from their inventory
     
  6. Artemio Velasquez

    Artemio Velasquez Creeper

    Messages:
    5
  7. NickTehUnicorn

    NickTehUnicorn Zombie

    Messages:
    200
    GitHub:
    unickorn
    Get the hotbar slot of the item and set it to air (id 0).
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    $item->pop();
    What if the Item is in a stack?
     
  9. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Az928 likes this.
  10. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Just do
    PHP:
    $player->getInventory()->removeItem ({id here}, {damage here}, {amount you want to remove here});
     
  11. Artemio Velasquez

    Artemio Velasquez Creeper

    Messages:
    5
  12. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    The argument for removeItem() should be an instance of Item.

    Proper Method
    PHP:
    $player->getInventory()->removeItem(Item::get(Item::CARROT01));
     
    Kyd, XCodeMCPE and Az928 like this.
  13. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Kindly learn right thing before replying, the thing you sent will basically send an error
     
  14. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Never did for me :p
     
  15. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Bet?
     
  16. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    xD I may have said the line wrong xD
     
  17. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    did you solve this? if not:
    PHP:
    <?php
    /**
    * Created by PhpStorm.
    * User: arjxy
    * Date: 11/28/2017
    * Time: 3:22 PM
    */

    namespace MagicalShards;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as TF;

    class 
    Main extends PluginBase implements Listener
    {

        public function 
    onLoad()
        {
            
    $this->getLogger()->info(" is now loading");
        }

        public function 
    onEnable()
        {
            
    $this->getLogger()->info(" is now enabled");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onDisable()
        {
            
    $this->getLogger()->critical(" is now disabling");
        }

        public function 
    onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool
        
    {
            if (
    $command->getName() == "magicalshard") {
                if (
    count($args) > 1) {
                    
    $player $this->getServer()->getPlayer($args[0]);
                    if (
    $player instanceof Player) {
                        
    $count $args[1];
                        if (
    is_numeric($count)) {
                            
    $item Item::get(4090$count);
                            
    $item->setCustomName(TF::GREEN "Majical Shard");
                            
    $player->getInventory()->addItem($item);
                        } else {
                            
    $sender->sendMessage("Please enter a valid number.");
                        }
                    } else {
                        
    $sender->sendMessage(TF::RED "Player not found!");
                    }
                } else {
                    
    $sender->sendMessage(TF::YELLOW "Usage: /magicalshard <player> <count>");
                }
            }
            return 
    true;
        }

        public function 
    onTap(PlayerInteractEvent $event)
        {
            
    $player $event->getPlayer();
            
    $itemId $player->getInventory()->getItemInHand()->getId();
            
    $itemName $player->getInventory()->getItemInHand()->getName();
            if (
    $itemId == 409 and $itemName == TF::GREEN "Majical Shard") {
                
    // do stuffs
            
    }
        }
    }
     
    Last edited: Nov 29, 2017
  18. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You know what happens on the console when you do this?
    upload_2017-11-29_10-0-19.png
    Why so many garbage lines?
     
  19. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    lol, I created this while in call with a newbie friend who I tried to teach things I know so I add those "garbage lines"
    You can always delete lines or not use the code in the first place
     
    Last edited: Nov 30, 2017
  20. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Yes sorry I forgot to add the Item:: part lmao
     
  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.