Repository: PaperMC/Paper
File: paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/builder/CraftBlockEntityInventoryViewBuilder.java (Java)

Code before:
        this.forceTile = forceTile;
        this.block = block;
        this.builder = builder;
    }

    @Override
    protected AbstractContainerMenu buildContainer(final ServerPlayer player) {
        if (this.world == null) {
            this.world = player.level();
        }

        if (this.position == null) {
            this.position = player.blockPosition();
        }

        final BlockEntity entity = this.world.getBlockEntity(position);
        if (!(entity instanceof final MenuConstructor container)) {
            return buildFakeTile(player);
        }

        final AbstractContainerMenu atBlock = container.createMenu(player.nextContainerCounter(), player.getInventory(), player);
        if (atBlock.getType() != super.handle) {
            return buildFakeTile(player);
        }

        trySetDefaultTitle(entity);
        return atBlock;
    }

    private AbstractContainerMenu buildFakeTile(final ServerPlayer player) {
        final MenuProvider inventory = this.builder.build(this.position, this.block.defaultBlockState());
        if (inventory instanceof final BlockEntity tile) {
            tile.setLevel(this.world);
            trySetDefaultTitle(tile);
        }

        if (!this.forceTile) { // gets around open noise for chest
            return handle.create(player.nextContainerCounter(), player.getInventory());
        }

        return inventory.createMenu(player.nextContainerCounter(), player.getInventory(), player);
    }

    private void trySetDefaultTitle(BlockEntity blockEntity) {
        if (blockEntity instanceof Nameable nameable) {
            super.defaultTitle = nameable.getDisplayName();
            return;
        }

        throw new IllegalStateException("Provided blockEntity during MenuType creation can not find a default title! This is a bug!");
    }

Review comment (refactor):
Less of a `try`, this will error if it doesn't succeed and that error isn't an expected state unless we fucked up.
So just, `setDefaultTitle`, or honestly `super.defaultTitle = extractDefaultTitle(entity)` is better here. 

Code after:

    public CraftBlockEntityInventoryViewBuilder(
        final MenuType<?> handle,
        final Block block,
        final @Nullable CraftBlockInventoryBuilder builder,
        final boolean useFakeBlockEntity
    ) {
        super(handle);
        this.useFakeBlockEntity = useFakeBlockEntity;
        this.block = block;
        this.builder = builder;
    }

    @Override
    protected AbstractContainerMenu buildContainer(final ServerPlayer player) {
        if (this.world == null) {
            this.world = player.level();
        }

        if (this.position == null) {
            this.position = player.blockPosition();
            return buildFakeBlockEntity(player);
        }

        final BlockEntity entity = this.world.getBlockEntity(position);
        if (!(entity instanceof final MenuConstructor container)) {
            return buildFakeBlockEntity(player);
        }

        final AbstractContainerMenu atBlock = container.createMenu(player.nextContainerCounter(), player.getInventory(), player);
        if (atBlock.getType() != super.handle) {
            return buildFakeBlockEntity(player);
        }

        if (!(entity instanceof final MenuProvider provider)) {
            throw new IllegalStateException("Provided blockEntity during MenuType creation can not find a default title! This is a bug!");
        }

        super.defaultTitle = provider.getDisplayName();
        return atBlock;
    }

    private AbstractContainerMenu buildFakeBlockEntity(final ServerPlayer player) {
        final MenuProvider inventory = this.builder.build(this.position, this.block.defaultBlockState());
        if (inventory instanceof final BlockEntity blockEntity) {
            blockEntity.setLevel(this.world);
            super.defaultTitle = inventory.getDisplayName();
        }

        if (!this.useFakeBlockEntity) { // gets around open noise for chest
            return handle.create(player.nextContainerCounter(), player.getInventory());