Skip to content

Instantly share code, notes, and snippets.

@Arkia
Last active August 25, 2016 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arkia/9fbdb69ce64321f344bbd1f2b6982867 to your computer and use it in GitHub Desktop.
Save Arkia/9fbdb69ce64321f344bbd1f2b6982867 to your computer and use it in GitHub Desktop.
// MusicPlayer.js
// Type: Script Block
// Desc: Makes it easier to play looping music tracks
//
// This script should be used by a Script Block that is triggered every tick
// by a Command Block in Repeat mode. It should also be placed in a spawn chunk
// so that it is always loaded.
//
// This script requires 4 new scoreboard objectives to work correctly
// called mNext, mTrack, mTimer, and mLength all with dummy criteria.
// These must be added to the scoreboard before use.
//
// To play a track, simply set the mNext score for the player to an index in the TRACK_LIST array
// To stop playing music, set mNext to -1
var TRACK_LIST = ["dotc.boss", "dotc.dungeon1", "dotc.dungeon2", "dotc.town1"]; // These are the tracks the music player can play. Change these to the sound events for your music tracks
var TRACK_LEN = [720, 4440, 2100, 3820]; // These are the lengths of each track in ticks
var scoreboard = world.getScoreboard();
var players = world.getEntities("@a");
for (var i=0; i<players.size(); i++) {
p = players.get(i);
var score_next = scoreboard.getScore(p.getName(), "mNext");
var score_track = scoreboard.getScore(p.getName(), "mTrack");
var score_timer = scoreboard.getScore(p.getName(), "mTimer");
var score_length = scoreboard.getScore(p.getName(), "mLength");
// Switch tracks
if (score_next.getScore() != score_track.getScore()) {
score_timer.setScore(0);
score_track.setScore(score_next.getScore());
if (score_next.getScore() >= 0) score_length.setScore(TRACK_LEN[score_next.getScore()]);
world.command("stopsound " + p.getName() + " music");
}
// Loop current track
if (score_track.getScore() >= 0) {
score_timer.setScore(score_timer.getScore() - 1);
if (score_timer.getScore() <= 0) {
score_timer.setScore(score_length.getScore());
world.command("playsound " + TRACK_LIST[score_track.getScore()] + " music " + p.getName() + " ~ ~ ~ 10000");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment