下面列出了怎么用net.minecraft.util.SoundEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onBlockDestroyedByPlayer(World world, BlockPos pos,
IBlockState state) {
super.onBlockDestroyedByPlayer(world, pos, state);
if(world.isRemote) {
FxSystemElectricArc.spawnArc(world, pos.getX() + 0.5f, pos.getY() + 0.5f, pos.getZ() + 0.5f, .3, 7);
world.playSound(pos.getX(), pos.getY(), pos.getZ(), new SoundEvent( new ResourceLocation("advancedrocketry:ElectricShockSmall")), SoundCategory.BLOCKS, .7f, 0.975f + world.rand.nextFloat()*0.05f, false);
}
}
@Override
public SoundEvent getBreakSound() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "dig." + sound.name : sound.getID())
.map(soundID -> SoundEvent.REGISTRY.getObject(new ResourceLocation(soundID)))
.orElseGet(super::getBreakSound);
}
public static SoundEvent registerSound(IForgeRegistry<SoundEvent> reg, ResourceLocation name) {
SoundEvent event = new SoundEvent(name);
event.setRegistryName(name);
reg.register(event);
return event;
}
public BlockSound(String name, SoundEvent sound) {
super(Material.ROCK);
this.setCreativeTab(CommunityGlobals.TAB);
this.setSoundType(new SoundType(2.0F, 1.0F, sound, sound, sound, sound, sound));
this.setRegistryName(name);
this.setTranslationKey(getRegistryName().toString());
}
@SubscribeEvent
public static void registerSounds(RegistryEvent.Register<SoundEvent> event)
{
IForgeRegistry<SoundEvent> registry = event.getRegistry();
GNOME_SPEAK = registerSoundEvent(registry, "mob.gnome.say");
GNOME_DEATH = registerSoundEvent(registry, "mob.gnome.death");
}
private static SoundEvent registerSoundEvent(IForgeRegistry<SoundEvent> registry, String location)
{
ResourceLocation rl = new ResourceLocation(CommunityGlobals.MOD_ID, location);
SoundEvent sound = new SoundEvent(rl);
sound.setRegistryName(rl);
registry.register(sound);
return sound;
}
public ItemPLRecord(String name, SoundEvent soundEvent) {
super(name, soundEvent);
this.name = name;
this.setCreativeTab(ProductionLine.creativeTabPL);
this.setUnlocalizedName(MOD_ID + "." + name);
GameRegistry.<Item>register(this, new ResourceLocation(MOD_ID, name));
}
@Override
public void playSoundToAllNearExcept(
EntityPlayer player,
SoundEvent soundIn,
SoundCategory category,
double x,
double y,
double z,
float volume,
float pitch) {
}
@Override
public SoundEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
ResourceLocation key = context.deserialize(json, ResourceLocation.class);
SoundEvent event = SoundEvent.REGISTRY.getObject(key);
return event;
}
@Test
public void fromString()
{
Map<String, SoundEvent> map = gson.fromJson("{ \"sound\": \"item.armor.equip_gold\" }", new TypeToken<Map<String, SoundEvent>>() {}.getType());
SoundEvent sound = map.get("sound");
assertNotNull(sound);
assertSame(SoundEvents.ITEM_ARMOR_EQUIP_GOLD, sound);
}
@Override
public SoundEvent getPlaceSound() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE)
.map(sound -> sound.domain.isEmpty() ? "step." + sound.name : sound.getID())
.map(soundID -> SoundEvent.REGISTRY.getObject(new ResourceLocation(soundID)))
.orElseGet(super::getPlaceSound);
}
@Override
public SoundEvent getStepSound() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "step." + sound.name : sound.getID())
.map(soundID -> SoundEvent.REGISTRY.getObject(new ResourceLocation(soundID)))
.orElseGet(super::getBreakSound);
}
@SubscribeEvent
public void registerSounds(RegistryEvent.Register<SoundEvent> event) {
event.getRegistry().registerAll(
new SoundEvent(new ResourceLocation(MODID, "treasure")),
new SoundEvent(new ResourceLocation(MODID, "magician")),
new SoundEvent(new ResourceLocation(MODID, "reaper"))
);
}
private static SoundEvent createEvent(String name) {
SoundEvent sound = new SoundEvent(new ResourceLocation(TofuMain.MODID, name));
sound.setRegistryName(new ResourceLocation(TofuMain.MODID, name));
return sound;
}
@SubscribeEvent
public static void registerSounds(RegistryEvent.Register<SoundEvent> evt) {
evt.getRegistry().register(TOFUNIAN_YES);
evt.getRegistry().register(TOFUNIAN_AMBIENT);
evt.getRegistry().register(TOFUBUGLE);
}
@Override
public SoundEvent getSound() {
return AudioRegistry.lathe;
}
protected SoundEvent getSpellPrepareSound() {
return SoundEvents.ENTITY_ZOMBIE_VILLAGER_CONVERTED;
}
protected SoundEvent getSpellPrepareSound() {
return SoundEvents.EVOCATION_ILLAGER_PREPARE_SUMMON;
}
@Nullable
protected abstract SoundEvent getSpellPrepareSound();
@Override
protected void playMachineSound(SoundEvent event) {
world.playSound(getPos().getX(), getPos().getY() + 7, getPos().getZ(), event, SoundCategory.BLOCKS, Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.BLOCKS), 0.975f + world.rand.nextFloat()*0.05f, false);
}
@Override
protected SoundEvent getAmbientSound() {
return TofuSounds.TOFUNIAN_AMBIENT;
}
@Override
public SoundEvent getSound() {
return AudioRegistry.rollingMachine;
}
@Override
protected SoundEvent getHurtSound(DamageSource damageSource) {
return null;
}
@Override
public SoundEvent getSound() {
return AudioRegistry.gravityOhhh;
}
protected SoundEvent getDeathSound() {
return SoundEvents.VINDICATION_ILLAGER_DEATH;
}
@SubscribeEvent
public static void onSoundEvenrRegistration(RegistryEvent.Register<SoundEvent> event) {
event.getRegistry().register(TAIKO.setRegistryName(new ResourceLocation(SakuraMain.MODID, "taiko")));
}
@SubscribeEvent
public static void onRegisterSounds(RegistryEvent.Register<SoundEvent> event) {
GNU_LINUX = RegUtil.registerSound(event.getRegistry(), new ResourceLocation(CommunityGlobals.MOD_ID, "gnu_linux"));
}
@SubscribeEvent
public static void registerSoundEvents(RegistryEvent.Register<SoundEvent> event) {
event.getRegistry().registerAll(E_SOUND, BRUH_2_SOUND);
}
protected SoundEvent getAmbientSound()
{
return SoundEvents.ENTITY_CHICKEN_AMBIENT;
}
protected SoundEvent getHurtSound(DamageSource damageSourceIn)
{
return SoundEvents.ENTITY_CHICKEN_HURT;
}