PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ParticleCommand.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
68use function count;
69use function explode;
70use function max;
71use function microtime;
72use function mt_rand;
73use function strtolower;
74
76
77 public function __construct(){
78 parent::__construct(
79 "particle",
80 KnownTranslationFactory::pocketmine_command_particle_description(),
81 KnownTranslationFactory::pocketmine_command_particle_usage()
82 );
83 $this->setPermission(DefaultPermissionNames::COMMAND_PARTICLE);
84 }
85
86 public function execute(CommandSender $sender, string $commandLabel, array $args){
87 if(count($args) < 7){
89 }
90
91 if($sender instanceof Player){
92 $senderPos = $sender->getPosition();
93 $world = $senderPos->getWorld();
94 $pos = new Vector3(
95 $this->getRelativeDouble($senderPos->getX(), $sender, $args[1]),
96 $this->getRelativeDouble($senderPos->getY(), $sender, $args[2], World::Y_MIN, World::Y_MAX),
97 $this->getRelativeDouble($senderPos->getZ(), $sender, $args[3])
98 );
99 }else{
100 $world = $sender->getServer()->getWorldManager()->getDefaultWorld();
101 $pos = new Vector3((float) $args[1], (float) $args[2], (float) $args[3]);
102 }
103
104 $name = strtolower($args[0]);
105
106 $xd = (float) $args[4];
107 $yd = (float) $args[5];
108 $zd = (float) $args[6];
109
110 $count = isset($args[7]) ? max(1, (int) $args[7]) : 1;
111
112 $data = $args[8] ?? null;
113
114 $particle = $this->getParticle($name, $data);
115
116 if($particle === null){
117 $sender->sendMessage(KnownTranslationFactory::commands_particle_notFound($name)->prefix(TextFormat::RED));
118 return true;
119 }
120
121 $sender->sendMessage(KnownTranslationFactory::commands_particle_success($name, (string) $count));
122
123 $random = new Random((int) (microtime(true) * 1000) + mt_rand());
124
125 for($i = 0; $i < $count; ++$i){
126 $world->addParticle($pos->add(
127 $random->nextSignedFloat() * $xd,
128 $random->nextSignedFloat() * $yd,
129 $random->nextSignedFloat() * $zd
130 ), $particle);
131 }
132
133 return true;
134 }
135
136 private function getParticle(string $name, ?string $data = null) : ?Particle{
137 switch($name){
138 case "explode":
139 return new ExplodeParticle();
140 case "hugeexplosion":
141 return new HugeExplodeParticle();
142 case "hugeexplosionseed":
143 return new HugeExplodeSeedParticle();
144 case "bubble":
145 return new BubbleParticle();
146 case "splash":
147 return new SplashParticle();
148 case "wake":
149 case "water":
150 return new WaterParticle();
151 case "crit":
152 return new CriticalParticle();
153 case "smoke":
154 return new SmokeParticle((int) ($data ?? 0));
155 case "spell":
156 return new EnchantParticle(new Color(0, 0, 0, 255)); //TODO: colour support
157 case "instantspell":
158 return new InstantEnchantParticle(new Color(0, 0, 0, 255)); //TODO: colour support
159 case "dripwater":
160 return new WaterDripParticle();
161 case "driplava":
162 return new LavaDripParticle();
163 case "townaura":
164 case "spore":
165 return new SporeParticle();
166 case "portal":
167 return new PortalParticle();
168 case "flame":
169 return new FlameParticle();
170 case "lava":
171 return new LavaParticle();
172 case "reddust":
173 return new RedstoneParticle((int) ($data ?? 1));
174 case "snowballpoof":
175 return new ItemBreakParticle(VanillaItems::SNOWBALL());
176 case "slime":
177 return new ItemBreakParticle(VanillaItems::SLIMEBALL());
178 case "itembreak":
179 if($data !== null){
180 $item = StringToItemParser::getInstance()->parse($data);
181 if($item !== null && !$item->isNull()){
182 return new ItemBreakParticle($item);
183 }
184 }
185 break;
186 case "terrain":
187 if($data !== null){
188 $block = StringToItemParser::getInstance()->parse($data)?->getBlock();
189 if($block !== null && $block->getTypeId() !== BlockTypeIds::AIR){
190 return new TerrainParticle($block);
191 }
192 }
193 break;
194 case "heart":
195 return new HeartParticle((int) ($data ?? 0));
196 case "ink":
197 return new InkParticle((int) ($data ?? 0));
198 case "droplet":
199 return new RainSplashParticle();
200 case "enchantmenttable":
201 return new EnchantmentTableParticle();
202 case "happyvillager":
203 return new HappyVillagerParticle();
204 case "angryvillager":
205 return new AngryVillagerParticle();
206 case "forcefield":
207 return new BlockForceFieldParticle((int) ($data ?? 0));
208 case "mobflame":
209 return new EntityFlameParticle();
210 case "iconcrack":
211 if($data !== null && ($item = StringToItemParser::getInstance()->parse($data)) !== null && !$item->isNull()){
212 return new ItemBreakParticle($item);
213 }
214 break;
215 case "blockcrack":
216 if($data !== null && ($block = StringToItemParser::getInstance()->parse($data)?->getBlock()) !== null && $block->getTypeId() !== BlockTypeIds::AIR){
217 return new TerrainParticle($block);
218 }
219 break;
220 case "blockdust":
221 if($data !== null){
222 $d = explode("_", $data);
223 if(count($d) >= 3){
224 return new DustParticle(new Color(
225 ((int) $d[0]) & 0xff,
226 ((int) $d[1]) & 0xff,
227 ((int) $d[2]) & 0xff,
228 ((int) ($d[3] ?? 255)) & 0xff
229 ));
230 }
231 }
232 break;
233 }
234
235 return null;
236 }
237}
execute(CommandSender $sender, string $commandLabel, array $args)