Java开发Minecraft带孔洞自定义方块纹理加载失败求助
Minecraft模组开发纹理错误修复方案
问题背景
正在学习Java并开发Minecraft模组,尝试制作带孔洞和延伸结构的自定义方块,启动游戏时出现纹理相关问题,报错如下:
终端报错
Cowardly refusing to send event net.minecraftforge.client.event.TextureStitchEvent$Post to a broken mod state java
Minecraft报错
mods.toml missing metadata for modid forge
相关代码文件如下:
Cosmicages.java
package quintoniusvlint.cosmicages; import com.mojang.logging.LogUtils; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent; import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import org.slf4j.Logger; import quintoniusvlint.cosmicages.core.ModBlocks; import quintoniusvlint.cosmicages.core.ModItems; import java.util.stream.Collectors; import static net.minecraftforge.versions.forge.ForgeVersion.MOD_ID; // The value here should match an entry in the META-INF/mods.toml file @Mod(MOD_ID) public class Cosmicages { // Directly reference a slf4j logger public static final String MOD_ID = "cosmicages"; private static final Logger LOGGER = LogUtils.getLogger(); public Cosmicages() { IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); bus.addListener(this::setup); bus.addListener(this::enqueueIMC); bus.addListener(this::processIMC); ModBlocks.BLOCKS.register(bus); ModItems.ITEMS.register(bus); MinecraftForge.EVENT_BUS.register(this); } private void setup(final FMLCommonSetupEvent event) { // Some preinit code LOGGER.info("HELLO FROM PREINIT"); LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); } private void enqueueIMC(final InterModEnqueueEvent event) { // Some example code to dispatch IMC to another mod InterModComms.sendTo("cosmicages", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world"; }); } private void processIMC(final InterModProcessEvent event) { // Some example code to receive and process InterModComms from other mods LOGGER.info("Got IMC {}", event.getIMCStream(). map(m -> m.messageSupplier().get()). collect(Collectors.toList())); } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { // Do something when the server starts LOGGER.info("HELLO from server starting"); } // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD // Event bus for receiving Registry Events) @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public static class RegistryEvents { @SubscribeEvent public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) { // Register a new block here LOGGER.info("HELLO from Register Block"); } } }
ModBlocks.java
package quintoniusvlint.cosmicages.core; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.material.Material; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; import quintoniusvlint.cosmicages.Cosmicages; public class ModBlocks { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Cosmicages.MOD_ID); public static final RegistryObject<Block> ARTIFACT_BLOCK = BLOCKS.register("artifact_block", () -> new Block(BlockBehaviour.Properties.of(Material.STONE))); }
ModItems.java
package quintoniusvlint.cosmicages.core; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.Item; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; import quintoniusvlint.cosmicages.Cosmicages; public class ModItems { public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, Cosmicages.MOD_ID); public static final RegistryObject<Item> ARTIFACT_BLOCK = ITEMS.register("ARTIFACT_BLOCK", () -> new BlockItem(ModBlocks.ARTIFACT_BLOCK.get(), new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS))); }
blockstates/artifact_block.json
{ "variants": { "": {"model": "cosmicages:block/artifact_block"} }, "credit": "Made with Blockbench", "parent": "ArtifactActivationBlock", "texture_size": [128, 128], "textures": { "0": "block/ texture" }, "elements": [ { { "variants": { "": {"model": "cosmicages:block/artifact_block"} }, "credit": "Made with Blockbench", "parent": "ArtifactActivationBlock", "texture_size": [128, 128], "textures": { "0": "block/ texture" }, "elements": [ { //and then we have the position of the block etc
models/block/artifact_block.json
{ "credit": "Made with Blockbench", "parent": "block/cube_all", "texture_size": [128, 128], "textures": { "all": "cosmicages:block/artifact_block" }, "elements": [ { //and then we have the position of the block etc
models/item/artifact_block.json
{ "credit": "Made with Blockbench", "parent": "cosmicages:block/artifact_block", "texture_size": [128, 128], "textures": { "0": "block/texture" }, "elements": [ { //and then we have the position of the block etc
错误修复步骤
1. 修复主类@Mod注解参数错误
报错mods.toml missing metadata for modid forge的根源是主类注解用了Forge的模组ID而非自己的模组ID:
- 删除静态导入
import static net.minecraftforge.versions.forge.ForgeVersion.MOD_ID; - 将
@Mod(MOD_ID)改为@Mod(Cosmicages.MOD_ID)
2. 统一方块与物品注册ID大小写
ModItems.java中物品注册ID用了大写ARTIFACT_BLOCK,和方块的小写artifact_block不匹配,修改为:
public static final RegistryObject<Item> ARTIFACT_BLOCK = ITEMS.register("artifact_block", () -> new BlockItem(ModBlocks.ARTIFACT_BLOCK.get(), new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS)));
3. 修复Blockstates文件语法错误
Blockstates文件无需自定义元素,仅需映射变体到模型,替换为正确内容:
{ "variants": { "": { "model": "cosmicages:block/artifact_block" } } }
4. 修正模型文件问题
方块模型(models/block/artifact_block.json)
- 自定义带孔洞的方块不能用
block/cube_all父模型,改用block/cube或完全自定义元素 - 确保JSON语法完整(闭合所有括号)
- 纹理路径保持与文件位置一致(需存在
resources/assets/cosmicages/textures/block/artifact_block.png)
示例修正后内容:
{ "credit": "Made with Blockbench", "parent": "block/cube", "texture_size": [128, 128], "textures": { "down": "cosmicages:block/artifact_block", "up": "cosmicages:block/artifact_block", "north": "cosmicages:block/artifact_block", "south": "cosmicages:block/artifact_block", "west": "cosmicages:block/artifact_block", "east": "cosmicages:block/artifact_block" }, "elements": [ // 填入Blockbench导出的完整元素数据 ] }
物品模型(models/item/artifact_block.json)
- 直接继承方块模型即可,无需重复定义元素,修正为:
{ "parent": "cosmicages:block/artifact_block" }
5. 纹理文件检查
- 确保
artifact_block.png存在于resources/assets/cosmicages/textures/block/目录 - 所有纹理路径不能包含空格(如
block/ texture需改为正确路径)
内容的提问来源于stack exchange,提问作者Q Zijlstra
相关产品推荐
相关产品推荐

