PocketMine-MP 5.37.2 git-e507eb5e50da3ead3ae88ed2324df21e75820019
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 $infinite = false;
78
79 if(count($args) >= 3){
80 if(strtolower($args[2]) === "infinite"){
81 $duration = null;
82 $infinite = true;
83 }else{
84 if(($d = $this->getBoundedInt($sender, $args[2], 0, (int) (Limits::INT32_MAX / 20))) === null){
85 return false;
86 }
87 $duration = $d * 20; // ticks
88 }
89 }else{
90 $duration = null;
91 }
92
93 if(count($args) >= 4){
94 $amplification = $this->getBoundedInt($sender, $args[3], 0, 255);
95 if($amplification === null){
96 return false;
97 }
98 }
99
100 $visible = true;
101 if(count($args) >= 5){
102 $v = strtolower($args[4]);
103 if($v === "on" || $v === "true" || $v === "t" || $v === "1"){
104 $visible = false;
105 }
106 }
107
108 if($duration === 0){
109 if(!$effectManager->has($effect)){
110 if(count($effectManager->all()) === 0){
111 $sender->sendMessage(KnownTranslationFactory::commands_effect_failure_notActive_all($player->getDisplayName()));
112 }else{
113 $sender->sendMessage(KnownTranslationFactory::commands_effect_failure_notActive($effect->getName(), $player->getDisplayName()));
114 }
115 return true;
116 }
117
118 $effectManager->remove($effect);
119 $sender->sendMessage(KnownTranslationFactory::commands_effect_success_removed($effect->getName(), $player->getDisplayName()));
120 }else{
121 $instance = new EffectInstance($effect, $duration, $amplification, $visible, infinite: $infinite);
122 $effectManager->add($instance);
123
124 if($infinite){
125 self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_effect_success_infinite($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName()));
126 }else{
127 self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_effect_success($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName(), (string) ($instance->getDuration() / 20)));
128 }
129 }
130
131 return true;
132 }
133}
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)