2301_76275164 2025-08-10 06:24 采纳率: 83.3%
浏览 9
已结题

Minecraft模型出错

img

为什么物品fg_machine的物品状态正常,放置后却变成了透明的?
BlockInit.java:


package com.GaoHY.GaoHY_mod.Init;

import com.GaoHY.GaoHY_mod.blockentities.FGMachineBlockEntity;
import com.GaoHY.GaoHY_mod.g_mod;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DropExperienceBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import net.minecraft.world.level.block.BaseEntityBlock;

import javax.annotation.Nullable;
import java.util.function.Supplier;

public class BlockInit {
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, g_mod.MODID);
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, g_mod.MODID);

    public static final RegistryObject<Block> FU_BLOCK = registerBlock("fu_block",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.STONE)
                    .strength(3.0f, 6.0f)
                    .requiresCorrectToolForDrops()),
            g_mod.TUTORIAL_TAB
    );

    public static final RegistryObject<Block> FU_GRASS = registerBlock("fu_grass",
            () -> new Block(BlockBehaviour.Properties.copy(Blocks.GRASS_BLOCK)
                    .strength(0.7f, 10.0f)),
            g_mod.TUTORIAL_TAB
    );

    public static final RegistryObject<Block> FU_ORE = registerBlock("fu_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.STONE)
                    .strength(5f)
                    .requiresCorrectToolForDrops()
                    .lightLevel(state -> 15),
                    UniformInt.of(3, 10)),
            g_mod.TUTORIAL_TAB
    );

    public static final RegistryObject<Block> DEEP_FU_ORE = registerBlock("deep_fu_ore",
            () -> new DropExperienceBlock(BlockBehaviour.Properties.copy(Blocks.STONE)
                    .strength(7f)
                    .requiresCorrectToolForDrops()
                    .lightLevel(state -> 15),
                    UniformInt.of(5, 15)),
            g_mod.TUTORIAL_TAB
    );

    public static final RegistryObject<Block> FG_MACHINE = registerBlock("fg_machine",
            () -> new BaseEntityBlock(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK)
                    .strength(3.5f, 10.0f)) {

                @Override
                public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
                    if (!level.isClientSide && player instanceof ServerPlayer) {
                        BlockEntity blockEntity = level.getBlockEntity(pos);
                        if (blockEntity instanceof MenuProvider) {
                            NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) blockEntity, pos);
                        }
                    }
                    return InteractionResult.sidedSuccess(level.isClientSide);
                }

                @Override
                public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
                    if (state.getBlock() != newState.getBlock()) {
                        BlockEntity blockEntity = level.getBlockEntity(pos);
                        if (blockEntity instanceof FGMachineBlockEntity) {
                            ((FGMachineBlockEntity) blockEntity).drops();
                        }
                    }
                    super.onRemove(state, level, pos, newState, isMoving);
                }

                @Nullable
                @Override
                public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
                    return new FGMachineBlockEntity(pos, state);
                }

                @Nullable
                @Override
                public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
                    return createTickerHelper(type, BlockEntityInit.FG_MACHINE_BLOCK_ENTITY.get(), FGMachineBlockEntity::tick);
                }
            },
            g_mod.TUTORIAL_TAB
    );


    private static <T extends Block> RegistryObject<T> registerBlock(String name, Supplier<T> block, CreativeModeTab tab) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerBlockItem(name, toReturn, tab);
        return toReturn;
    }

    private static <T extends Block> void registerBlockItem(String name, RegistryObject<T> block, CreativeModeTab tab) {
        ITEMS.register(name, () -> new BlockItem(block.get(),
                new Item.Properties().tab(tab))); // 添加.tab(tab)
    }

}

BlockEntityInit.java:

// BlockEntityInit.java
package com.GaoHY.GaoHY_mod.Init;

import com.GaoHY.GaoHY_mod.g_mod;
import com.GaoHY.GaoHY_mod.blockentities.FGMachineBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class BlockEntityInit {
    public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES =
            DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, g_mod.MODID);

    public static final RegistryObject<BlockEntityType<FGMachineBlockEntity>> FG_MACHINE_BLOCK_ENTITY =
            BLOCK_ENTITIES.register("fg_machine_block_entity",
                    () -> BlockEntityType.Builder.of(FGMachineBlockEntity::new, BlockInit.FG_MACHINE.get()).build(null));
}

  • 写回答

5条回答 默认 最新

  • 檀越@新空间 2025-08-10 06:25
    关注

    上午好☀️☀️☀️️
    本答案参考通义千问

    在Minecraft模组开发中,如果物品状态正常(即在物品栏中显示正常),但放置后变成透明,通常是因为以下原因:


    1. Block类没有正确实现渲染逻辑

    如果你的FG_MACHINE块没有正确设置模型或纹理,那么它可能会在客户端上显示为透明。这可能是因为:

    • 没有注册正确的模型文件。
    • 模型文件路径错误。
    • 未使用BlockEntity时,缺少了RenderType的定义。

    ✅ 解决方案:

    • 确保你已经为fg_machine创建了正确的模型文件(如models/block/fg_machine.json)。
    • 在资源包中确保模型和纹理文件存在且路径正确。
    • 如果使用BlockEntity,确保你在ClientModInitializer中注册了渲染器。

    2. Block类没有继承BaseEntityBlock或没有正确处理BlockEntity

    你的FG_MACHINE块继承了BaseEntityBlock,这是正确的做法。但需要确保你已经正确注册了BlockEntityType并实现了getBlockEntity方法。

    ✅ 解决方案:

    • 确保在BlockInit.java中注册了BlockEntityType
    • FG_MACHINE块中重写getTicker方法以支持客户端上的BlockEntity更新。
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
        return (level.isClientSide() ? (BlockEntityTicker<T>) (world, pos, state, blockEntity) -> {
            if (blockEntity instanceof FGMachineBlockEntity) {
                ((FGMachineBlockEntity) blockEntity).tick();
            }
        } : null);
    }
    

    3. BlockItem没有正确绑定到Block

    虽然你的BlockItem可能被正确注册,但有时候会因为没有正确关联导致显示问题。

    ✅ 解决方案:

    • 确保ITEMS中注册了fg_machine对应的BlockItem
    public static final RegistryObject<Item> FG_MACHINE_ITEM = ITEMS.register("fg_machine",
            () -> new BlockItem(FG_MACHINE.get(), new Item.Properties().tab(g_mod.TUTORIAL_TAB)));
    

    4. 客户端与服务端不一致

    如果FG_MACHINE块在服务端和客户端之间没有同步,可能导致客户端无法正确渲染。

    ✅ 解决方案:

    • 确保在ClientModInitializer中注册了所有客户端相关的资源,如模型、纹理等。
    • 如果使用BlockEntity,确保在客户端加载时也初始化了该实体。

    5. Block的材质设置错误

    如果你的块材质设置为“无”或错误的材质,也会导致透明效果。

    ✅ 解决方案:

    • 检查BlockBehaviour.Properties中的材质设置是否正确。
    new BaseEntityBlock(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK)
            .strength(3.5f, 10.0f)
            .requiresCorrectToolForDrops()
            .lightLevel(state -> 15))
    

    ✅ 总结:关键检查点

    1. 模型文件是否正确models/block/fg_machine.json
    2. 纹理文件是否存在textures/block/fg_machine.png
    3. BlockItem是否正确绑定BlockItem应指向FG_MACHINE
    4. BlockEntity是否注册BlockEntityType必须与FG_MACHINE匹配)
    5. Block的getTicker方法是否正确实现
    6. 客户端是否正确加载模型和纹理

    🧩 示例代码修改(BlockInit.java)

    public static final RegistryObject<Block> FG_MACHINE = registerBlock("fg_machine",
            () -> new BaseEntityBlock(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK)
                    .strength(3.5f, 10.0f)
                    .requiresCorrectToolForDrops()
                    .lightLevel(state -> 15)) {
    
                @Override
                public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
                    if (!level.isClientSide && player instanceof ServerPlayer) {
                        BlockEntity blockEntity = level.getBlockEntity(pos);
                        if (blockEntity instanceof MenuProvider) {
                            NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) blockEntity, pos);
                        }
                    }
                    return InteractionResult.sidedSuccess(level.isClientSide);
                }
    
                @Override
                public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
                    return (level.isClientSide() ? (BlockEntityTicker<T>) (world, pos, state, blockEntity) -> {
                        if (blockEntity instanceof FGMachineBlockEntity) {
                            ((FGMachineBlockEntity) blockEntity).tick();
                        }
                    } : null);
                }
            },
            g_mod.TUTORIAL_TAB
    );
    

    🔍 建议调试步骤:

    1. 打开游戏,进入创造模式,尝试放置fg_machine
    2. 查看控制台是否有错误信息(如找不到模型或纹理)。
    3. 使用F3+H查看块的渲染状态,确认是否加载了正确的模型。
    4. 检查logs/latest.log是否有相关异常。

    如有更多代码片段(如FGMachineBlockEntity.java或模型文件内容),我可以进一步帮助你定位问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 8月20日
  • 已采纳回答 8月12日
  • 创建了问题 8月10日