不存在类型变量 T 的实例,因此 Block 符合 Supplier<T>

no instance(s) of type variable(s) T exist so that Block conforms to Supplier<T>

我刚从一门课程中编写代码,就出现了这个错误:

no instance(s) of type variable(s) T exist so that Block conforms to Supplier<T>

我不知道它是什么,但这是我的代码:

package com.berriz44.breloaded.block;

import com.berriz44.breloaded.util.Registration;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Supplier;

public class ModBlocks {

    public static RegistryObject<Block> SMOOTH_BRICK = register("smooth_brick", new Block(BlockBehaviour.Properties.of(Material.STONE).sound(SoundType.STONE).strength(2f,6f)));

    private static <T extends Block>RegistryObject<T> register(String name, Supplier<T> block) {
        RegistryObject<T> toReturn = Registration.BLOCKS.register(name, block);
        Registration.ITEMS.register(name, () -> new BlockItem(toReturn.get(), new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS)));
        return toReturn;
    }

}

发送帮助。

方法 register 采用 Supplier<Block>,而不是 Block

您试图在此处传递 Block 而不是 Supplier<Block>:

public static RegistryObject<Block> SMOOTH_BRICK = register("smooth_brick",
    new Block(BlockBehaviour.Properties.of(Material.STONE).sound(SoundType.STONE).strength(2f,6f)));

您可以使用 lambda 表达式实现 Supplier<Block>

public static RegistryObject<Block> SMOOTH_BRICK = register("smooth_brick",
    () -> new Block(BlockBehaviour.Properties.of(Material.STONE).sound(SoundType.STONE).strength(2f,6f)));