2

I want to make an ambient music that plays when hostile mobs are nearby. I already have the appropriate sound event ready in the custom sounds.json of the resourcepack I'm using and now want to repeatedly play it to all players within a 10-block radius of every creeper, skeleton, zombie etc. (using /execute), but only if neither an enemy ambient nor a boss music is playing to that player. While for the boss music I could repeatedly stop the ambient sound event for everyone in the boss area, the ambient sound itself is more of an issue, because I'm pretty sure it would overlap, and if I stopped it every time I relaunch it the music would loop quite oddly. I also want the music to start playing as immediately as possible when a player gets within the radius, and not only at the beginning of the next loop. So is there a way to achieve what I need, and if yes, how does it work?

Oh, almost forgot to mention, I'm still using 1.8 because that's my Forge version, so please take this into account. (No, that doesn't mean I'd be OK with using mods. I have Forge in primarily because of WorldEdit, and want the map to be playable in Vanilla.)

  • Rough idea, I'll think about it more later: Couldn't you add a hearing zombie, hearing_skeleton etc. score to every player, set it to 0 under the same conditions as the sound plays, increment it once per tick and not play another sound (and not reset the score) again until the score is above the length of the sound (in ticks). As another idea, you could actually use the ambient sounds of zombies, skeletons, etc, except if that's different from what you need. – Fabian Röling Aug 16 '17 at 14:29
  • About your question itself: No, you can't detect sounds. – Fabian Röling Aug 16 '17 at 14:30
  • @Fabian If I used the "normal" ambient sounds, it would be hard to reasonably combine them into a constant ambient music. The first possibility sounds interesting, though. – Egor Hans Aug 16 '17 at 14:52
  • Monsters make music? That sounds great. Can you link the map in a comment to my to-be-written answer when you're done? – Fabian Röling Aug 16 '17 at 14:58
  • Oh, I misread the question: It's one piece of music across all monsters, not the monster sounds mixing to make music. – Fabian Röling Aug 16 '17 at 17:33
2

Solution: Saving the current music progress in a scoreboard.
This example assumes a loop length of 42 seconds, just to have a value. A score value of 0 makes the music start, everything from 1 to 839 prevents the sound from being played and 840 means that the player is ready to hear the music again.

First, do this:

/scoreboard objectives add music dummy

Then put these commands in your game loop:

/execute as @e[type=Zombie] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/execute as @e[type=Skeleton] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/execute as @e[type=Creeper] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/playsound someTunes hostile @a[scores={music=0}]
/scoreboard players add @a[scores={music=..839}] music 1

First you reset the ticking "music" score of every player who is near a mob, but only if it has already ticked up to 42 seconds for him.
Then you play the music to every player who just got their score reset.
Then you tick the score up.

Here (archive) is a list of all entity IDs, to help you not forget some of them.

  • For 1.13, replace execute @e[type=zombie] ~ ~ ~ with execute at @e[type=zombie]. I think my commands should all work the same in 1.8, but I'm not sure. They definitely do not work in 1.7 because of /execute. – Fabian Röling Aug 16 '17 at 18:55
  • It should work the same in 1.8, except entity IDs are uppercase. I'll try it out, thanks! – Egor Hans Aug 17 '17 at 15:09
  • Correct. And since capitalizing them doesn't make them incompatible to 1.12 or 1.13, I'll change it in my answer. – Fabian Röling Aug 17 '17 at 15:22
  • 1
    Also, this seems to be quite easy to extend to cover boss music as well. Another score set to 1 for players in the boss area, that is checked to be 0 when playing the normal battle music to players, and there's no more extra work needed. And multiple music tracks for multiple entity groups seem to go well too, even including grouping by custom name and other properties that can be checked for. Very nice solution, thanks! I should seriously get more in touch with scoreboards... – Egor Hans Aug 17 '17 at 15:25
  • Actually wanted to try it out first, but you're right, I think I'm at no risk here. – Egor Hans Aug 17 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.