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

Code before:

        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!");
    }

    @Override
    public LocationInventoryViewBuilder<V> copy() {
        final CraftBlockEntityInventoryViewBuilder<V> copy = new CraftBlockEntityInventoryViewBuilder<>(super.handle, this.block, this.builder, this.forceTile);
        copy.world = this.world;
        copy.position = this.position;
        copy.checkReachable = super.checkReachable;
        copy.title = title;
        return copy;
    }

    public interface CraftTileInventoryBuilder {
        MenuProvider build(BlockPos blockPosition, BlockState blockData);
    }
}


Review comment (suggestion):
Early return instead.
`!(obj instanceOf final Type type)` works.

Code after:
        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());
        }

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

    @Override
    public LocationInventoryViewBuilder<V> copy() {
        final CraftBlockEntityInventoryViewBuilder<V> copy = new CraftBlockEntityInventoryViewBuilder<>(super.handle, this.block, this.builder, this.useFakeBlockEntity);
        copy.world = this.world;
        copy.position = this.position;
        copy.checkReachable = super.checkReachable;
        copy.title = title;
        return copy;
    }

    public interface CraftBlockInventoryBuilder {
        MenuProvider build(BlockPos blockPosition, BlockState blockData);
    }
}