From 2512fb2e0a74a2b30e4b8c2b9c8a4be85fcb1c56 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Mon, 22 Apr 2024 17:50:04 -0500 Subject: [PATCH] Clear pollution to register an entity on EntityFactory Is still a mess to register a spawn egg :( --- src/IvanCraft623/MobPlugin/MobPlugin.php | 85 ++++++++-------------- src/IvanCraft623/MobPlugin/utils/Utils.php | 4 + 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/IvanCraft623/MobPlugin/MobPlugin.php b/src/IvanCraft623/MobPlugin/MobPlugin.php index 15672a2..60c21d2 100644 --- a/src/IvanCraft623/MobPlugin/MobPlugin.php +++ b/src/IvanCraft623/MobPlugin/MobPlugin.php @@ -39,8 +39,10 @@ use IvanCraft623\MobPlugin\entity\monster\Slime; use IvanCraft623\MobPlugin\entity\monster\Spider; use IvanCraft623\MobPlugin\item\ExtraItemRegisterHelper; +use IvanCraft623\MobPlugin\utils\Utils; use pocketmine\entity\AttributeFactory; +use pocketmine\entity\Entity; use pocketmine\entity\EntityDataHelper as Helper; use pocketmine\entity\EntityFactory; use pocketmine\nbt\tag\CompoundTag; @@ -53,6 +55,23 @@ class MobPlugin extends PluginBase { use SingletonTrait; + public const ALL_ENTITIES = [ + Bat::class, + Chicken::class, + Cow::class, + MooshroomCow::class, + Pig::class, + Sheep::class, + IronGolem::class, + SnowGolem::class, + CaveSpider::class, + Creeper::class, + Enderman::class, + Endermite::class, + Slime::class, + Spider::class + ]; + private ?Random $random = null; public function onLoad() : void { @@ -86,60 +105,20 @@ private function registerAttributes() : void{ private function registerEntities() : void{ $factory = EntityFactory::getInstance(); - $factory->register(Endermite::class, function(World $world, CompoundTag $nbt) : Endermite{ - return new Endermite(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:endermite', 'Endermite']); - - $factory->register(Cow::class, function(World $world, CompoundTag $nbt) : Cow{ - return new Cow(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:cow', 'Cow']); - - $factory->register(MooshroomCow::class, function(World $world, CompoundTag $nbt) : MooshroomCow{ - return new MooshroomCow(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:mooshroom', 'Mooshroom']); - - $factory->register(Sheep::class, function(World $world, CompoundTag $nbt) : Sheep{ - return new Sheep(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:sheep', 'Sheep']); - - $factory->register(Creeper::class, function(World $world, CompoundTag $nbt) : Creeper{ - return new Creeper(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:creeper', 'Creeper']); - - $factory->register(Chicken::class, function(World $world, CompoundTag $nbt) : Chicken{ - return new Chicken(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:chicken', 'Chicken']); - - $factory->register(Pig::class, function(World $world, CompoundTag $nbt) : Pig{ - return new Pig(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:pig', 'Pig']); - - $factory->register(Bat::class, function(World $world, CompoundTag $nbt) : Bat{ - return new Bat(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:bat', 'Bat']); - - $factory->register(Slime::class, function(World $world, CompoundTag $nbt) : Slime{ - return new Slime(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:slime', 'Slime']); - - $factory->register(Enderman::class, function(World $world, CompoundTag $nbt) : Enderman{ - return new Enderman(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:enderman', 'Enderman']); - - $factory->register(Spider::class, function(World $world, CompoundTag $nbt) : Spider{ - return new Spider(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:spider', 'Spider']); - - $factory->register(CaveSpider::class, function(World $world, CompoundTag $nbt) : CaveSpider{ - return new CaveSpider(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:cave_spider', 'Cave Spider']); + foreach (self::ALL_ENTITIES as $entityClass) { + $this->registerEntity($factory, $entityClass); + } + } - $factory->register(IronGolem::class, function(World $world, CompoundTag $nbt) : IronGolem{ - return new IronGolem(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:iron_golem', 'Iron Golem']); + /** + * @phpstan-param class-string $entityClass + */ + private function registerEntity(EntityFactory $factory, string $entityClass) : void{ + //Did you know that bedrock entity's save ids are the same as network ids? + $entityId = $entityClass::getNetworkTypeId(); - $factory->register(SnowGolem::class, function(World $world, CompoundTag $nbt) : SnowGolem{ - return new SnowGolem(Helper::parseLocation($nbt, $world), $nbt); - }, ['minecraft:snow_golem', 'Snow Golem']); + $factory->register($entityClass, function(World $world, CompoundTag $nbt) use ($entityClass) : Entity{ + return new $entityClass(Helper::parseLocation($nbt, $world), $nbt); + }, [$entityId, Utils::getEntityNameFromId($entityId)]); } } diff --git a/src/IvanCraft623/MobPlugin/utils/Utils.php b/src/IvanCraft623/MobPlugin/utils/Utils.php index c5c3ddb..9ee482b 100644 --- a/src/IvanCraft623/MobPlugin/utils/Utils.php +++ b/src/IvanCraft623/MobPlugin/utils/Utils.php @@ -313,4 +313,8 @@ public static function signum(int|float $i) : int{ public static function lerp(int|float $start, int|float $end, int|float $t) : int|float { return $end + $start * ($t - $end); } + + public static function getEntityNameFromId(string $id) : string{ + return ucwords(strtolower(str_replace(["_", "minecraft:"], [" ", ""], trim($id)))); + } }