Tile: Be explicit about not calling Tile::createNBT() (#2388)

A common pitfall developers fall into with this function is that it has to be called from the scope of the tile class you're creating NBT for, but people commonly do Tile::createNBT() directly, which then results in cryptic "Tile is not registered" errors. This now throws a BadMethodCallException instead to be fully clear about this.

In the future this will be removed completely once NBT is no longer required to create a tile, but for now this is a confusing issue that should be dealt with.
This commit is contained in:
Dylan K. Taylor 2018-08-19 19:54:22 +01:00 committed by GitHub
parent c496480d2b
commit ddcb2f002a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -209,6 +209,9 @@ abstract class Tile extends Position{
* @return CompoundTag
*/
public static function createNBT(Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : CompoundTag{
if(static::class === self::class){
throw new \BadMethodCallException(__METHOD__ . " must be called from the scope of a child class");
}
$nbt = new CompoundTag("", [
new StringTag(self::TAG_ID, static::getSaveId()),
new IntTag(self::TAG_X, (int) $pos->x),