提供: Minecraft Modding Wiki
移動先: 案内検索
1行目: 1行目:
 
{{前提MOD|reqmod="Fabric API 0.3.2 build 230~"}}
 
{{前提MOD|reqmod="Fabric API 0.3.2 build 230~"}}
 
{{チュートリアル難易度|difficulty=0|clear=none}}
 
{{チュートリアル難易度|difficulty=0|clear=none}}
{{チュートリアルカテゴリー|difficulty=0|type=Block}}
+
<p>再生できるサウンドの追加方法</p>
<p>ワールド上に設置できる簡単な無機能ブロックの追加方法</p>
 
 
==サウンドの追加==
 
==サウンドの追加==
 
+
===外部サイト===
 +
[https://minecraft-ja.gamepedia.com/Sounds.json Sounds.jsonの書き方(Minecraft公式Wiki)]
 
===ソースコード===
 
===ソースコード===
  
 
*SampleMod.java
 
*SampleMod.java
 +
<source lang = "java">
 +
package com.example.sound;
 +
 +
import net.fabricmc.api.ModInitializer;
 +
import net.minecraft.sound.SoundEvent;
 +
import net.minecraft.util.Identifier;
 +
import net.minecraft.util.registry.Registry;
  
public class SampleMod {
+
public class SampleMod implements ModInitializer {
 
     public static final Identifier SAMPLE_SOUND_ID = new Identifier(MOD_ID":sample_sound")
 
     public static final Identifier SAMPLE_SOUND_ID = new Identifier(MOD_ID":sample_sound")
 
     public static SoundEvent SAMPLE_SOUND_EVENT = new SoundEvent(SAMPLE_SOUND_ID);
 
     public static SoundEvent SAMPLE_SOUND_EVENT = new SoundEvent(SAMPLE_SOUND_ID);
18行目: 25行目:
 
     }  
 
     }  
 
}
 
}
 +
</source>
 +
==サウンドの再生==
 +
*SampleBlock.java
 +
<source lang = "java">
 +
//省略
 +
    @Override
 +
    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {
 +
        if (!world.isClient) {
 +
            world.playSound(
 +
                    null, // Player (purpose unknown, edit if you know)
 +
                    blockPos, // The position of where the sound will come from
 +
                    SampleMod.SAMPLE_SOUND_EVENT, // The sound that will play
 +
                    SoundCategory.BLOCKS, // This determines which of the volume sliders affect this sound
 +
                    1f, //Volume multiplier, 1 is normal, 0.5 is half volume, etc
 +
                    1f // Pitch multiplier, 1 is normal, 0.5 is half pitch, etc
 +
            );
 +
        }
 +
        return false;
 +
    }
 +
//省略
 +
</source>
 +
*SampleItem.java
 +
<source lang = "java">
 +
package com.example.sound;
 +
 +
import net.minecraft.entity.player.PlayerEntity;
 +
import net.minecraft.item.Item;
 +
import net.minecraft.item.ItemStack;
 +
import net.minecraft.util.ActionResult;
 +
import net.minecraft.util.Hand;
 +
import net.minecraft.util.TypedActionResult;
 +
import net.minecraft.world.World;
 +
 +
public class SampleItem extends Item
 +
{
 +
    public SampleItem(Settings item$Settings_SampleItem) {
 +
        super(item$Settings_SampleItem);
 +
    }
 +
    @Override
 +
    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand)
 +
    {
 +
        playerEntity.playSound(SampleMod.SAMPLE_SOUND_EVENT, 1.0F, 1.0F);
 +
        return new TypedActionResult<>(ActionResult.SUCCESS, playerEntity.getStackInHand(hand));
 +
    }
 +
}
 +
</source>
 +
==解説==
 +
<source lang = "java">
 +
.playSound(SampleMod.SAMPLE_SOUND_EVENT, 1.0F, 1.0F)
 +
</source>
 +
再生することができる。

2019年9月14日 (土) 22:54時点における版

この記事は"Fabric API 0.3.2 build 230~"を前提MODとしています。

Wood pickaxe.png
初心者向けのチュートリアルです。

再生できるサウンドの追加方法

サウンドの追加

外部サイト

Sounds.jsonの書き方(Minecraft公式Wiki)

ソースコード

  • SampleMod.java
package com.example.sound;

import net.fabricmc.api.ModInitializer;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class SampleMod implements ModInitializer {
    public static final Identifier SAMPLE_SOUND_ID = new Identifier(MOD_ID":sample_sound")
    public static SoundEvent SAMPLE_SOUND_EVENT = new SoundEvent(SAMPLE_SOUND_ID);

    @Override
    public void onInitialize(){
         Registry.register(Registry.SOUND_EVENT, SampleMod.SAMPLE_SOUND_ID, SAMPLE_SOUND_EVENT);
    } 
}

サウンドの再生

  • SampleBlock.java
//省略
    @Override
    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) {
        if (!world.isClient) {
            world.playSound(
                    null, // Player (purpose unknown, edit if you know)
                    blockPos, // The position of where the sound will come from
                    SampleMod.SAMPLE_SOUND_EVENT, // The sound that will play
                    SoundCategory.BLOCKS, // This determines which of the volume sliders affect this sound
                    1f, //Volume multiplier, 1 is normal, 0.5 is half volume, etc
                    1f // Pitch multiplier, 1 is normal, 0.5 is half pitch, etc
            );
        }
        return false;
    }
//省略
  • SampleItem.java
package com.example.sound;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class SampleItem extends Item 
{
    public SampleItem(Settings item$Settings_SampleItem) {
        super(item$Settings_SampleItem);
    }
    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand)
    {
        playerEntity.playSound(SampleMod.SAMPLE_SOUND_EVENT, 1.0F, 1.0F);
        return new TypedActionResult<>(ActionResult.SUCCESS, playerEntity.getStackInHand(hand));
    }
}

解説

.playSound(SampleMod.SAMPLE_SOUND_EVENT, 1.0F, 1.0F)

再生することができる。