PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
Loading...
Searching...
No Matches
EffectCommand.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
34use function count;
35use function strtolower;
36
38
39 public function __construct(string $namespace, string $name){
40 parent::__construct(
41 $namespace,
42 $name,
43 KnownTranslationFactory::pocketmine_command_effect_description(),
44 KnownTranslationFactory::commands_effect_usage()
45 );
46 $this->setPermissions([
47 DefaultPermissionNames::COMMAND_EFFECT_SELF,
48 DefaultPermissionNames::COMMAND_EFFECT_OTHER
49 ]);
50 }
51
52 public function execute(CommandSender $sender, string $commandLabel, array $args){
53 if(count($args) < 2){
55 }
56
57 $player = $this->fetchPermittedPlayerTarget($commandLabel, $sender, $args[0], DefaultPermissionNames::COMMAND_EFFECT_SELF, DefaultPermissionNames::COMMAND_EFFECT_OTHER);
58 if($player === null){
59 return true;
60 }
61 $effectManager = $player->getEffects();
62
63 if(strtolower($args[1]) === "clear"){
64 $effectManager->clear();
65
66 $sender->sendMessage(KnownTranslationFactory::commands_effect_success_removed_all($player->getDisplayName()));
67 return true;
68 }
69
70 $effect = StringToEffectParser::getInstance()->parse($args[1]);
71 if($effect === null){
72 $sender->sendMessage(KnownTranslationFactory::commands_effect_notFound($args[1])->prefix(TextFormat::RED));
73 return true;
74 }
75
76 $amplification = 0;
77
78 if(count($args) >= 3){
79 if(($d = $this->getBoundedInt($sender, $args[2], 0, (int) (Limits::INT32_MAX / 20))) === null){
80 return false;
81 }
82 $duration = $d * 20; //ticks
83 }else{
84 $duration = null;
85 }
86
87 if(count($args) >= 4){
88 $amplification = $this->getBoundedInt($sender, $args[3], 0, 255);
89 if($amplification === null){
90 return false;
91 }
92 }
93
94 $visible = true;
95 if(count($args) >= 5){
96 $v = strtolower($args[4]);
97 if($v === "on" || $v === "true" || $v === "t" || $v === "1"){
98 $visible = false;
99 }
100 }
101
102 if($duration === 0){
103 if(!$effectManager->has($effect)){
104 if(count($effectManager->all()) === 0){
105 $sender->sendMessage(KnownTranslationFactory::commands_effect_failure_notActive_all($player->getDisplayName()));
106 }else{
107 $sender->sendMessage(KnownTranslationFactory::commands_effect_failure_notActive($effect->getName(), $player->getDisplayName()));
108 }
109 return true;
110 }
111
112 $effectManager->remove($effect);
113 $sender->sendMessage(KnownTranslationFactory::commands_effect_success_removed($effect->getName(), $player->getDisplayName()));
114 }else{
115 $instance = new EffectInstance($effect, $duration, $amplification, $visible);
116 $effectManager->add($instance);
117 self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_effect_success($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName(), (string) ($instance->getDuration() / 20)));
118 }
119
120 return true;
121 }
122}
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)