注册ItemBlock的方式比我在这里更好吗?

时间:2017-11-15 03:17:09

标签: java minecraft minecraft-forge

所以,我试图让一个ItemBlock在我的Block注册时注册。到目前为止,我已经想出了这个。有没有更好的方法来做到这一点?我对itemBlock.setRegistryName内的registerBlock()部分并不满意!

public class ModBlocks {

    public static Block tinBlock;

    public static void init(){
        tinBlock = new BlockTinBlock("tin_block", "tin_block");
    }

    public static void register(){
        registerBlock(tinBlock);
    }

    public static void registerRenders(){
        registerRender(tinBlock);
    }

    public static void registerBlock(Block block){
        GameRegistry.register(block);
        ItemBlock itemBlock = new ItemBlock(block);
        itemBlock.setRegistryName(Reference.MODID, block.getUnlocalizedName().substring(5));
        GameRegistry.register(itemBlock);
    }

    public static void registerRender(Block block){
        ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(new ResourceLocation(Reference.MODID, block.getUnlocalizedName().substring(5)), "inventory"));
    }
}

2 个答案:

答案 0 :(得分:1)

我偶然发现了2天前在MinecraftForge论坛上发布给我的链接。这种注册项目和块的方法是最新的,并使用RegistryEvents!看看,考虑转换!

[MinecraftForge] 1.11.2 - Is using RegistryEvent this way ok?

答案 1 :(得分:0)

你正在做的事情就是它,但是对于块,物品和模型等的注册,你应该使用相应的事件:
为此,您需要一个事件监听器来监听事件 事件侦听器是一个以Event为参数的函数,并且注释为@SubscribeEvent,它们可以位于@Mod类中 或在注释@Mod.EventBusSubscriber(modid = "yourmodid")

的类中

要在单独的类中注册Blocks,Items和Models,您可以编写如下内容:

@Mod.EventBusSubscriber(modid = "yourmodid")
public class Registration{
    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
        IForgeRegistry registry = event.getRegistry();
        //register blocks here using registry
    }

    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
        //register items here like we did with the blocks
    }

    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public static void initModels(ModelRegistryEvent event) {
        //register models here
    }
}

有关此主题的更多信息,请参阅可以找到的here官方Minecraft Forge文档。