Skip to content

Commit

Permalink
Add a volume modifier argument to Midi command
Browse files Browse the repository at this point in the history
May not do exactly as you expect it to...
  • Loading branch information
Morphan1 committed Aug 19, 2015
1 parent 57e0a8d commit 1fcdbe9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Expand Up @@ -1730,7 +1730,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Midi
// @Syntax midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>)
// @Syntax midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>) (volume:<#.#>)
// @Required 1
// @Stable stable
// @Short Plays a midi file at a given location or to a list of players using note block sounds.
Expand Down Expand Up @@ -1763,7 +1763,7 @@ public void registerCoreMembers() {

// -->
registerCoreMember(MidiCommand.class,
"MIDI", "midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>)", 1);
"MIDI", "midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>) (volume:<#.#>)", 1);


// <--[command]
Expand Down
Expand Up @@ -40,6 +40,12 @@ else if (!scriptEntry.hasObject("entities") &&

scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));

else if (!scriptEntry.hasObject("volume") &&
arg.matchesPrimitive(aH.PrimitiveType.Double) &&
arg.matchesPrefix("volume", "vol", "v"))

scriptEntry.addObject("volume", arg.asElement());

else if (!scriptEntry.hasObject("tempo") &&
arg.matchesPrimitive(aH.PrimitiveType.Double))

Expand Down Expand Up @@ -71,7 +77,7 @@ else if (!scriptEntry.hasObject("file")) {
(((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity()) : null));
}

scriptEntry.defaultObject("tempo", new Element(1));
scriptEntry.defaultObject("tempo", new Element(1)).defaultObject("volume", new Element(10));
}

@SuppressWarnings("unchecked")
Expand All @@ -88,22 +94,24 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dLocation location = (dLocation) scriptEntry.getObject("location");
float tempo = (float) scriptEntry.getElement("tempo").asDouble();
float tempo = scriptEntry.getElement("tempo").asFloat();
float volume = scriptEntry.getElement("volume").asFloat();

// Report to dB
dB.report(scriptEntry, getName(), (cancel ? aH.debugObj("cancel", cancel) : "") +
(file != null ? aH.debugObj("file", file.getPath()) : "") +
(entities != null ? aH.debugObj("entities", entities.toString()) : "") +
(location != null ? location.debug() : "") +
aH.debugObj("tempo", tempo));
aH.debugObj("tempo", tempo) +
aH.debugObj("volume", volume));

// Play the midi
if (!cancel) {
if (location != null) {
MidiUtil.playMidi(file, tempo, location);
MidiUtil.playMidi(file, tempo, volume, location);
}
else {
MidiUtil.playMidi(file, tempo, entities);
MidiUtil.playMidi(file, tempo, volume, entities);
}
}
else {
Expand Down
Expand Up @@ -36,9 +36,10 @@ public static void startSequencer(File file, float tempo, NoteBlockReceiver rece
sequencer.start();
}

public static void playMidi(File file, float tempo, List<dEntity> entities) {
public static void playMidi(File file, float tempo, float volume, List<dEntity> entities) {
try {
NoteBlockReceiver receiver = new NoteBlockReceiver(entities, entities.get(0).getUUID().toString());
receiver.VOLUME_RANGE = volume;
// If there is already a midi file being played for one of the entities,
// stop playing it
for (dEntity entity : entities) {
Expand All @@ -53,9 +54,10 @@ public static void playMidi(File file, float tempo, List<dEntity> entities) {
}
}

public static void playMidi(File file, float tempo, dLocation location) {
public static void playMidi(File file, float tempo, float volume, dLocation location) {
try {
NoteBlockReceiver receiver = new NoteBlockReceiver(location, location.identify());
receiver.VOLUME_RANGE = volume;
// If there is already a midi file being played for this location,
// stop playing it
stopMidi(location.identify());
Expand Down
Expand Up @@ -16,7 +16,7 @@
* @author authorblues
*/
public class NoteBlockReceiver implements Receiver {
private static final float VOLUME_RANGE = 10.0f;
public float VOLUME_RANGE = 10.0f;

private List<dEntity> entities;
private dLocation location;
Expand Down

0 comments on commit 1fcdbe9

Please sign in to comment.