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