PocketMine-MP icon indicating copy to clipboard operation
PocketMine-MP copied to clipboard

Implement a proposed EntityRegistrar for PM4

Open CortexPE opened this issue 3 years ago • 3 comments

Introduction

PM4 currently lacks a way to overwrite and/or "register" entities, this is due to the new improved design principles that avoids type-unsafe variadic argument passing to entity constructors and for the better separation of the network layer from the plugin API

Changes

Build

Implemented Closure signature generation for build/generate-registry-annotations.php

API changes

Added a VanillaEntities class that works similarly to other Vanilla<type> classes

Backwards compatibility

Since there is no actual entity registration in PM4 and developers needed to construct the entities themselves, backwards-compatibility is not an issue.

Follow-up

This pull request is not done, this is just the first stage. However I am asking for comments for further development as the next stages require more changes in the internal PocketMine codebase to migrate from direct entity construction, to the usage of the VanillaEntities class

EntityFactory should be renamed appropriately to EntityDataParser (or a similar name) as its actual purpose is to construct the entities from the NBT data read from disk.

Tests

// allowed:
VanillaEntities::register("human", function(Location $location, Skin $skin, ?CompoundTag $nbt = null):CustomHuman{
	return new CustomHuman($location, $skin, $nbt);
});

// not allowed:
VanillaEntities::register("human", function(Location $location, Skin $skin, Player $linked, ?CompoundTag $nbt = null):EnderPearl{
	return new EnderPearl($location, null, $nbt);
});

CortexPE avatar Sep 05 '21 09:09 CortexPE

EntityFactory should be renamed appropriately to EntityDataParser (or a similar name) as its actual purpose is to construct the entities from the NBT data read from disk.

It should also be moved to the pocketmine/data package.

dktapps avatar Sep 05 '21 22:09 dktapps

Any updates on that, since it's quite a pain to overwrite existing entities.

ColinHDev avatar Dec 14 '21 11:12 ColinHDev

I'm currently leaning towards an EntityCreationEvent. I don't like this solution; it's too much of a hack and it's too inflexible. If we're going to do a hack for a poor API, we can at least do the hack well.

dktapps avatar Jan 10 '22 16:01 dktapps

Closing due to lack of activity.

dktapps avatar May 29 '23 20:05 dktapps