Flandre923
1036 words
5 minutes
06 土豆雷

06 土豆雷#

/**
 * PotatoTNTPrepareBlock类继承自FlowerBlock,用于实现特定效果的土豆TNT准备块
 * 该类通过延迟操作来转换方块状态,以达到预期的游戏效果
 */
public class PotatoTNTPrepareBlock extends FlowerBlock {
    /**
     * 构造函数,初始化PotatoTNTPrepareBlock实例
     * 
     * @param effect     该方块赋予生物的效果
     * @param seconds    效果持续的时间(秒)
     * @param properties 方块的属性
     */
    public PotatoTNTPrepareBlock(Holder<MobEffect> effect, float seconds, Properties properties) {
        super(effect, seconds, properties);
    }

    /**
     * 当方块被放置时,除了调用父类方法外,还启动一个延迟操作
     * 
     * @param level      世界级别
     * @param pos        方块的位置
     * @param state      方块的状态
     * @param placer     放置方块的生物,可能为null
     * @param stack      放置时使用的物品堆
     */
    @Override
    public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
        super.setPlacedBy(level, pos, state, placer, stack);
        startDelayedOperation(level,pos,state,placer,stack);
    }

    /**
     * 启动一个延迟操作,用于在一定时间后转换方块状态
     * 
     * @param level      世界级别
     * @param pos        方块的位置
     * @param state      方块的当前状态
     * @param placer     放置方块的生物,可能为null
     * @param stack      放置时使用的物品堆
     */
    public void startDelayedOperation(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 移除当前方块
                level.removeBlock(pos,false);
                // 在原位置设置新的方块状态,更新邻近方块的状态
                level.setBlock(pos,(ModBlock.POTATO_TNT.get().defaultBlockState()),3);
            }
        }, 5000); // 延迟5000毫秒执行
    }
}

/**
 * PotatoTNTBlock类继承自SlabBlock,用于实现土豆TNT块的特性
 * 主要功能是在玩家或其他生物踩踏时触发TNT爆炸
 */
public class PotatoTNTBlock extends SlabBlock {
  
    /**
     * 构造函数,用于初始化PotatoTNTBlock对象
     * @param properties 块的属性,用于控制硬度、材质等
     */
    public PotatoTNTBlock(Properties properties) {
        super(properties);
    }

    /**
     * 当玩家或其他生物踩踏到土豆TNT块时触发的方法
     * 该方法会实例化一个TNTProjectileEntity,并向指定方向发射,模拟玩家或生物投掷TNT球的效果
     * @param level 游戏世界等级
     * @param pos 块的位置
     * @param state 块的状态
     * @param entity 踩踏到块的实体
     */
    @Override
    public void stepOn(Level level, BlockPos pos, BlockState state, Entity entity) {
        super.stepOn(level, pos, state, entity);
        if (entity instanceof LivingEntity){
            TNTProjectileEntity tntProjectileEntity = new TNTProjectileEntity(((LivingEntity) entity), level);
            tntProjectileEntity.setItem(ModItems.TNT_BALL.get().getDefaultInstance());
            tntProjectileEntity.shootFromRotation(entity,90,entity.getYRot(), 0.0f, 5f, 0f);
            level.addFreshEntity(tntProjectileEntity);
        }
    }

    /**
     * 当土豆TNT块受到爆炸影响时触发的方法
     * 目前该方法仅调用父类同名方法,未实现额外逻辑
     * @param state 块的当前状态
     * @param level 游戏世界等级
     * @param pos 块的位置
     * @param explosion 爆炸对象,包含爆炸源、爆炸威力等信息
     * @param dropConsumer 用于处理爆炸后物品掉落的消费者
     */
    @Override
    protected void onExplosionHit(BlockState state, Level level, BlockPos pos, Explosion explosion, BiConsumer<ItemStack, BlockPos> dropConsumer) {
        super.onExplosionHit(state, level, pos, explosion, dropConsumer);
    }
}



public class ModBlock {

    public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(NeoMafishMod.MODID);

    public static final DeferredBlock<Block> POTATO_TNT = registerBlock("potato_tnt",
            ()-> new PotatoTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_BROWN)
                    .instrument(NoteBlockInstrument.BASS).strength(5f,1f).sound(SoundType.STONE)));

    public static final DeferredBlock<Block> POTATO_TNT_PREPARE = registerBlock("potato_tnt_prepare",
            ()-> new PotatoTNTPrepareBlock(MobEffects.FIRE_RESISTANCE,10,BlockBehaviour.Properties.ofFullCopy(Blocks.ALLIUM).noOcclusion().noCollission()));


    private static DeferredBlock<Block> registerBlock(String name, Supplier<Block> blockSupplier) {
        DeferredBlock<Block> register = BLOCKS.register(name, blockSupplier);
        ModItems.ITEMS.register(name,()-> new BlockItem(register.get(),new Item.Properties()));
        return register;
    }

    public static DeferredBlock<Block> registerSimpleBlock(String name, BlockBehaviour.Properties props) {
        DeferredBlock<Block> deferredBlock =  BLOCKS.registerSimpleBlock(name,props);
        ModItems.ITEMS.register(name,()-> new BlockItem(deferredBlock.get(),new Item.Properties()));
        return  deferredBlock;
    }

    public static DeferredBlock<Block> registerSimpleBlock(String name, BlockBehaviour.Properties props, Item.Properties properties) {
        DeferredBlock<Block> deferredBlock =  BLOCKS.registerSimpleBlock(name,props);
        ModItems.ITEMS.register(name,()-> new BlockItem(deferredBlock.get(),properties));
        return  deferredBlock;
    }

    public static void register(IEventBus eventBus){
        BLOCKS.register(eventBus);
    }
}

// The value here should match an entry in the META-INF/neoforge.mods.toml file
@Mod(NeoMafishMod.MODID)
public class NeoMafishMod
{
    // Define mod id in a common place for everything to reference
    public static final String MODID = "neomafishmod";
    private static final Logger LOGGER = LogUtils.getLogger();
    public NeoMafishMod(IEventBus modEventBus, ModContainer modContainer)
    {

        ModBlock.register(modEventBus);


        modContainer.registerConfig(ModConfig.Type.COMMON,Config.SPEC);
    }
}
// resources/assets/modid/blockstates/potato_tnt.json 
{
  "variants": {
    "type=bottom": {
      "model": "neomafishmod:block/potato_tnt"
    },
    "type=double": {
      "model": "neomafishmod:block/potato_tnt"
    },
    "type=top": {
      "model": "neomafishmod:block/potato_tnt"
    }
  }
}
// resources/assets/modid/blockstates/potato_tnt_prepare.json 

{
  "variants": {
    "": {
      "model": "neomafishmod:block/potato_tnt_prepare"
    }
  }
}
// resources/assets/modid/model/block/potato_tnt.json 
{
  "parent": "block/slab",
  "textures": {
    "bottom": "neomafishmod:block/potato_tnt",
    "side": "neomafishmod:block/potato_tnt",
    "top": "neomafishmod:block/potato_tnt"
  }
}
// resources/assets/modid/model/block/potato_tnt_prepare.json
{
  "parent": "block/cross",
  "render_type": "cutout",
  "textures": {
    "cross": "neomafishmod:block/potato_tnt_prepare"
  }
}

纹理图片

potato_tnt

potato_tnt_prepare

物品注册


public class ModItems {
    public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(NeoMafishMod.MODID);

    public static DeferredItem<Item> registerItem(String name, Supplier<Item> itemSupplier){
        return ITEMS.register(name,itemSupplier);
    }

}


public class ModTabs {
    public static final DeferredRegister<CreativeModeTab> CREATIVE_TABS =
            DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);

    public static final DeferredHolder<CreativeModeTab,CreativeModeTab> NEO_MAFISHMOD =
            CREATIVE_TABS.register("neo_mafishmod", ()-> CreativeModeTab.builder()
                    .title(Component.translatable("itemGroup.neomafishmod"))
                    .withTabsBefore(CreativeModeTabs.COMBAT)
                    .icon(()-> ModItems.MAFISH.get().getDefaultInstance())
                    .displayItems((itemDisplayParameters, output) -> {

// 添加对应的物品到创造模式物品栏

                        output.accept(ModBlock.POTATO_TNT.get());



                    }).build());
}

注册Item和物品栏

// The value here should match an entry in the META-INF/neoforge.mods.toml file
@Mod(NeoMafishMod.MODID)
public class NeoMafishMod
{
    // Define mod id in a common place for everything to reference
    public static final String MODID = "neomafishmod";
    private static final Logger LOGGER = LogUtils.getLogger();
    public NeoMafishMod(IEventBus modEventBus, ModContainer modContainer)
    {
        ModItems.ITEMS.register(modEventBus);
        ModTabs.CREATIVE_TABS.register(modEventBus);
        ModBlock.register(modEventBus);

    }
}

物品模型

// resources/assets/neomafishmod/models/item/potato_tnt.json
{
  "parent": "item/generated",
  "textures": {
    "layer0": "neomafishmod:item/potato_tnt"
  }
}
//resources/assets/neomafishmod/models/item/potato_tnt_prepare.json
{
  "parent": "item/generated",
  "textures": {
    "layer0": "neomafishmod:item/potato_tnt"
  }
}
06 土豆雷
https://fuwari.vercel.app/posts/minecraft1_21_0/06_土豆雷/
Author
Flandre923
Published at
2024-08-24