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