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