PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
GiveCommand.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\command\defaults;
23 
29 
31 
32  public function __construct($name){
33  parent::__construct(
34  $name,
35  "Gives the specified player a certain amount of items",
36  "/give <player> <item[:damage]> [amount]"
37  );
38  $this->setPermission("pocketmine.command.give");
39  }
40 
41  public function execute(CommandSender $sender, $currentAlias, array $args){
42  if(!$this->testPermission($sender)){
43  return true;
44  }
45 
46  if(count($args) < 2){
47  $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
48 
49  return false;
50  }
51 
52  $player = $sender->getServer()->getPlayer($args[0]);
53  $item = Item::fromString($args[1]);
54 
55  if(!isset($args[2])){
56  $item->setCount($item->getMaxStackSize());
57  }else{
58  $item->setCount((int) $args[2]);
59  }
60 
61  if($player instanceof Player){
62  if(($player->getGamemode() & 0x01) === 0x01){
63  $sender->sendMessage(TextFormat::RED . "Player is in creative mode");
64 
65  return true;
66  }
67  if($item->getId() == 0){
68  $sender->sendMessage(TextFormat::RED . "There is no item called " . $args[1] . ".");
69 
70  return true;
71  }
72 
73  //TODO: overflow
74  $player->getInventory()->addItem(clone $item);
75  }else{
76  $sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
77 
78  return true;
79  }
80 
81  Command::broadcastCommandMessage($sender, "Gave " . $player->getName() . " " . $item->getCount() . " of " . $item->getName() . " (" . $item->getId() . ":" . $item->getDamage() . ")");
82 
83  return true;
84  }
85 }
static broadcastCommandMessage(CommandSender $source, $message, $sendToSource=true)
Definition: Command.php:287
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112