Skip to content

Commit 2a24982

Browse files
committed
ParticleCommand: standardise usage, don't use legacy block/item IDs
1 parent f2dc918 commit 2a24982

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

src/command/defaults/ParticleCommand.php

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
namespace pocketmine\command\defaults;
2525

26-
use pocketmine\block\BlockFactory;
26+
use pocketmine\block\BlockLegacyIds;
2727
use pocketmine\color\Color;
2828
use pocketmine\command\CommandSender;
2929
use pocketmine\command\utils\InvalidCommandSyntaxException;
30-
use pocketmine\item\ItemFactory;
30+
use pocketmine\item\StringToItemParser;
3131
use pocketmine\item\VanillaItems;
3232
use pocketmine\lang\KnownTranslationFactory;
3333
use pocketmine\math\Vector3;
@@ -70,7 +70,6 @@
7070
use function max;
7171
use function microtime;
7272
use function mt_rand;
73-
use function strpos;
7473
use function strtolower;
7574

7675
class ParticleCommand extends VanillaCommand{
@@ -114,7 +113,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
114113

115114
$count = isset($args[7]) ? max(1, (int) $args[7]) : 1;
116115

117-
$data = isset($args[8]) ? (int) $args[8] : null;
116+
$data = $args[8] ?? null;
118117

119118
$particle = $this->getParticle($name, $data);
120119

@@ -138,7 +137,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
138137
return true;
139138
}
140139

141-
private function getParticle(string $name, ?int $data = null) : ?Particle{
140+
private function getParticle(string $name, ?string $data = null) : ?Particle{
142141
switch($name){
143142
case "explode":
144143
return new ExplodeParticle();
@@ -156,7 +155,7 @@ private function getParticle(string $name, ?int $data = null) : ?Particle{
156155
case "crit":
157156
return new CriticalParticle();
158157
case "smoke":
159-
return new SmokeParticle($data ?? 0);
158+
return new SmokeParticle((int) ($data ?? 0));
160159
case "spell":
161160
return new EnchantParticle(new Color(0, 0, 0, 255)); //TODO: colour support
162161
case "instantspell":
@@ -175,25 +174,31 @@ private function getParticle(string $name, ?int $data = null) : ?Particle{
175174
case "lava":
176175
return new LavaParticle();
177176
case "reddust":
178-
return new RedstoneParticle($data ?? 1);
177+
return new RedstoneParticle((int) ($data ?? 1));
179178
case "snowballpoof":
180179
return new ItemBreakParticle(VanillaItems::SNOWBALL());
181180
case "slime":
182181
return new ItemBreakParticle(VanillaItems::SLIMEBALL());
183182
case "itembreak":
184-
if($data !== null && $data !== 0){
185-
return new ItemBreakParticle(ItemFactory::getInstance()->get($data));
183+
if($data !== null){
184+
$item = StringToItemParser::getInstance()->parse($data);
185+
if($item !== null && !$item->isNull()){
186+
return new ItemBreakParticle($item);
187+
}
186188
}
187189
break;
188190
case "terrain":
189-
if($data !== null && $data !== 0){
190-
return new TerrainParticle(BlockFactory::getInstance()->get($data, 0));
191+
if($data !== null){
192+
$block = StringToItemParser::getInstance()->parse($data)?->getBlock();
193+
if($block !== null && $block->getId() !== BlockLegacyIds::AIR){
194+
return new TerrainParticle($block);
195+
}
191196
}
192197
break;
193198
case "heart":
194-
return new HeartParticle($data ?? 0);
199+
return new HeartParticle((int) ($data ?? 0));
195200
case "ink":
196-
return new InkParticle($data ?? 0);
201+
return new InkParticle((int) ($data ?? 0));
197202
case "droplet":
198203
return new RainSplashParticle();
199204
case "enchantmenttable":
@@ -203,26 +208,32 @@ private function getParticle(string $name, ?int $data = null) : ?Particle{
203208
case "angryvillager":
204209
return new AngryVillagerParticle();
205210
case "forcefield":
206-
return new BlockForceFieldParticle($data ?? 0);
211+
return new BlockForceFieldParticle((int) ($data ?? 0));
207212
case "mobflame":
208213
return new EntityFlameParticle();
209-
}
210-
211-
if(strpos($name, "iconcrack_") === 0){
212-
$d = explode("_", $name);
213-
if(count($d) === 3){
214-
return new ItemBreakParticle(ItemFactory::getInstance()->get((int) $d[1], (int) $d[2]));
215-
}
216-
}elseif(strpos($name, "blockcrack_") === 0){
217-
$d = explode("_", $name);
218-
if(count($d) === 2){
219-
return new TerrainParticle(BlockFactory::getInstance()->get(((int) $d[1]) & 0xff, ((int) $d[1]) >> 12));
220-
}
221-
}elseif(strpos($name, "blockdust_") === 0){
222-
$d = explode("_", $name);
223-
if(count($d) >= 4){
224-
return new DustParticle(new Color(((int) $d[1]) & 0xff, ((int) $d[2]) & 0xff, ((int) $d[3]) & 0xff, isset($d[4]) ? ((int) $d[4]) & 0xff : 255));
225-
}
214+
case "iconcrack":
215+
if($data !== null && ($item = StringToItemParser::getInstance()->parse($data)) !== null && !$item->isNull()){
216+
return new ItemBreakParticle($item);
217+
}
218+
break;
219+
case "blockcrack":
220+
if($data !== null && ($block = StringToItemParser::getInstance()->parse($data)?->getBlock()) !== null && $block->getId() !== BlockLegacyIds::AIR){
221+
return new TerrainParticle($block);
222+
}
223+
break;
224+
case "blockdust":
225+
if($data !== null){
226+
$d = explode("_", $data);
227+
if(count($d) >= 3){
228+
return new DustParticle(new Color(
229+
((int) $d[0]) & 0xff,
230+
((int) $d[1]) & 0xff,
231+
((int) $d[2]) & 0xff,
232+
((int) ($d[3] ?? 255)) & 0xff
233+
));
234+
}
235+
}
236+
break;
226237
}
227238

228239
return null;

0 commit comments

Comments
 (0)