<?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:ARK_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!"); } } }
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");
one more question how can i also make it so when the player taps the shard it removes it from their inventory
you could also try to use a pre-made, configurable plugin: https://github.com/TheAz928/ItemCommands/pull/5
Just do PHP: $player->getInventory()->removeItem ({id here}, {damage here}, {amount you want to remove here});
The argument for removeItem() should be an instance of Item. Proper Method PHP: $player->getInventory()->removeItem(Item::get(Item::CARROT, 0, 1));
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 $sender, Command $command, string $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(409, 0, $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 } }}
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" Spoiler You can always delete lines or not use the code in the first place