PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
Loading...
Searching...
No Matches
ClearCommand.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
37use function count;
38use function min;
39
41
42 public function __construct(string $namespace, string $name){
43 parent::__construct(
44 $namespace,
45 $name,
46 KnownTranslationFactory::pocketmine_command_clear_description(),
47 KnownTranslationFactory::pocketmine_command_clear_usage()
48 );
49 $this->setPermissions([DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER]);
50 }
51
52 public function execute(CommandSender $sender, string $commandLabel, array $args){
53 if(count($args) > 3){
55 }
56
57 $target = $this->fetchPermittedPlayerTarget($commandLabel, $sender, $args[0] ?? null, DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER);
58 if($target === null){
59 return true;
60 }
61
62 $targetItem = null;
63 $maxCount = -1;
64 if(isset($args[1])){
65 try{
66 $targetItem = StringToItemParser::getInstance()->parse($args[1]) ?? LegacyStringToItemParser::getInstance()->parse($args[1]);
67
68 if(isset($args[2])){
69 $targetItem->setCount($maxCount = $this->getInteger($sender, $args[2], -1));
70 }
72 //vanilla checks this at argument parsing layer, can't come up with a better alternative
73 $sender->sendMessage(KnownTranslationFactory::commands_give_item_notFound($args[1])->prefix(TextFormat::RED));
74 return true;
75 }
76 }
77
81 $inventories = [
82 $target->getInventory(),
83 $target->getCursorInventory(),
84 $target->getArmorInventory(),
85 $target->getOffHandInventory()
86 ];
87
88 // Checking player's inventory for all the items matching the criteria
89 if($targetItem !== null && $maxCount === 0){
90 $count = $this->countItems($inventories, $targetItem);
91 if($count > 0){
92 $sender->sendMessage(KnownTranslationFactory::commands_clear_testing($target->getName(), (string) $count));
93 }else{
94 $sender->sendMessage(KnownTranslationFactory::commands_clear_failure_no_items($target->getName())->prefix(TextFormat::RED));
95 }
96
97 return true;
98 }
99
100 $clearedCount = 0;
101 if($targetItem === null){
102 // Clear all items from the inventories
103 $clearedCount += $this->countItems($inventories, null);
104 foreach($inventories as $inventory){
105 $inventory->clearAll();
106 }
107 }else{
108 // Clear the item from target's inventory irrelevant of the count
109 if($maxCount === -1){
110 $clearedCount += $this->countItems($inventories, $targetItem);
111 foreach($inventories as $inventory){
112 $inventory->remove($targetItem);
113 }
114 }else{
115 // Clear the item from target's inventory up to maxCount
116 foreach($inventories as $inventory){
117 foreach($inventory->all($targetItem) as $index => $item){
118 // The count to reduce from the item and max count
119 $reductionCount = min($item->getCount(), $maxCount);
120 $item->pop($reductionCount);
121 $clearedCount += $reductionCount;
122 $inventory->setItem($index, $item);
123
124 $maxCount -= $reductionCount;
125 if($maxCount <= 0){
126 break 2;
127 }
128 }
129 }
130 }
131 }
132
133 if($clearedCount > 0){
134 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_clear_success($target->getName(), (string) $clearedCount));
135 }else{
136 $sender->sendMessage(KnownTranslationFactory::commands_clear_failure_no_items($target->getName())->prefix(TextFormat::RED));
137 }
138
139 return true;
140 }
141
145 protected function countItems(array $inventories, ?Item $target) : int{
146 $count = 0;
147 foreach($inventories as $inventory){
148 $contents = $target !== null ? $inventory->all($target) : $inventory->getContents();
149 foreach($contents as $item){
150 $count += $item->getCount();
151 }
152 }
153 return $count;
154 }
155}
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)
countItems(array $inventories, ?Item $target)