PocketMine-MP 5.23.2 git-45917d495c14f3ce37bbf0848262ad8a470030f3
Loading...
Searching...
No Matches
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
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
38use function array_slice;
39use function count;
40use function implode;
41
43
44 public function __construct(){
45 parent::__construct(
46 "give",
47 KnownTranslationFactory::pocketmine_command_give_description(),
48 KnownTranslationFactory::pocketmine_command_give_usage()
49 );
50 $this->setPermissions([
51 DefaultPermissionNames::COMMAND_GIVE_SELF,
52 DefaultPermissionNames::COMMAND_GIVE_OTHER
53 ]);
54 }
55
56 public function execute(CommandSender $sender, string $commandLabel, array $args){
57 if(count($args) < 2){
59 }
60
61 $player = $this->fetchPermittedPlayerTarget($sender, $args[0], DefaultPermissionNames::COMMAND_GIVE_SELF, DefaultPermissionNames::COMMAND_GIVE_OTHER);
62 if($player === null){
63 return true;
64 }
65
66 try{
67 $item = StringToItemParser::getInstance()->parse($args[1]) ?? LegacyStringToItemParser::getInstance()->parse($args[1]);
69 $sender->sendMessage(KnownTranslationFactory::commands_give_item_notFound($args[1])->prefix(TextFormat::RED));
70 return true;
71 }
72
73 if(!isset($args[2])){
74 $item->setCount($item->getMaxStackSize());
75 }else{
76 $count = $this->getBoundedInt($sender, $args[2], 1, 32767);
77 if($count === null){
78 return true;
79 }
80 $item->setCount($count);
81 }
82
83 if(isset($args[3])){
84 $data = implode(" ", array_slice($args, 3));
85 try{
86 $tags = JsonNbtParser::parseJson($data);
87 }catch(NbtDataException $e){
88 $sender->sendMessage(KnownTranslationFactory::commands_give_tagError($e->getMessage()));
89 return true;
90 }
91
92 try{
93 $item->setNamedTag($tags);
94 }catch(NbtException $e){
95 $sender->sendMessage(KnownTranslationFactory::commands_give_tagError($e->getMessage()));
96 return true;
97 }
98 }
99
100 //TODO: overflow
101 $player->getInventory()->addItem($item);
102
103 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_give_success(
104 $item->getName() . " (" . $args[1] . ")",
105 (string) $item->getCount(),
106 $player->getName()
107 ));
108 return true;
109 }
110}
setPermissions(array $permissions)
Definition Command.php:103
execute(CommandSender $sender, string $commandLabel, array $args)
static parseJson(string $data)